From bc7bc21552a01caa04ed90d36d2efba0bba03f5c Mon Sep 17 00:00:00 2001 From: abhishek-01k Date: Tue, 14 May 2024 16:19:16 +0530 Subject: [PATCH 1/3] Subscribe change to subscribev2 --- .../dropdowns/OptinNotifSettingDropdown.tsx | 13 ++- src/helpers/channel/notifSetting.ts | 101 ++++++++++++++---- 2 files changed, 88 insertions(+), 26 deletions(-) diff --git a/src/components/dropdowns/OptinNotifSettingDropdown.tsx b/src/components/dropdowns/OptinNotifSettingDropdown.tsx index 39e36ce649..8582e33fa2 100644 --- a/src/components/dropdowns/OptinNotifSettingDropdown.tsx +++ b/src/components/dropdowns/OptinNotifSettingDropdown.tsx @@ -20,7 +20,7 @@ import { convertAddressToAddrCaip } from 'helpers/CaipHelper'; import useToast from 'hooks/useToast'; import { MdCheckCircle, MdError } from 'react-icons/md'; import { ChannelSetting } from 'helpers/channel/types'; -import { notifChannelSettingFormatString, userSettingsFromDefaultChannelSetting } from 'helpers/channel/notifSetting'; +import { UserSettingType, getMinimalUserSetting, notifChannelSettingFormatString, userSettingsFromDefaultChannelSetting } from 'helpers/channel/notifSetting'; import { AppContext } from 'contexts/AppContext'; import LoaderSpinner, { LOADER_TYPE } from 'components/reusables/loaders/LoaderSpinner'; import { updateSubscriptionStatus, updateUserSetting } from 'redux/slices/channelSlice'; @@ -206,10 +206,10 @@ const OptinNotifSettingDropdown: React.FC = (opt channelSettings, setLoading, }: { - channelSettings?: ChannelSetting[]; + channelSettings: ChannelSetting[]; setLoading?: React.Dispatch>; }) => { - const setLoadingFunc = setLoading || (options && options.setLoading) || (() => {}); + const setLoadingFunc = setLoading || (options && options.setLoading) || (() => { }); setLoadingFunc(true); let userPushInstance = userPushSDKInstance; @@ -240,10 +240,15 @@ const OptinNotifSettingDropdown: React.FC = (opt const _signer = await web3Provider?.getSigner(walletAddress); - await PushAPI.channels.subscribe({ + const notifSettings: UserSettingType[] = notifChannelSettingFormatString({ settings: channelSettings }); + + const settingsToSubscribe = getMinimalUserSetting(notifSettings); + + await PushAPI.channels.subscribeV2({ signer: _signer, channelAddress: convertAddressToAddrCaip(channelAddress, chainId), // channel address in CAIP userAddress: convertAddressToAddrCaip(walletAddress, chainId), // user address in CAIP + settings: settingsToSubscribe, onSuccess: () => { dispatch(updateSubscriptionStatus({ channelAddress, status: true })); dispatch( diff --git a/src/helpers/channel/notifSetting.ts b/src/helpers/channel/notifSetting.ts index 2683800193..5e251816a5 100644 --- a/src/helpers/channel/notifSetting.ts +++ b/src/helpers/channel/notifSetting.ts @@ -1,30 +1,87 @@ -import { ChannelSetting, UserSetting } from "./types"; +import { ChannelSetting, UserSetting } from './types'; const isSettingType1 = (setting: ChannelSetting) => setting.type === 1; +export type UserSettingType = { + enabled: boolean; + value?: number | { lower: number; upper: number }; +}; + export const notifChannelSettingFormatString = ({ settings }: { settings: ChannelSetting[] }) => { - let _notifSettings = []; - settings && settings.forEach((setting) => - isSettingType1(setting) - ? _notifSettings.push({ enabled: (setting as ChannelSetting & { type: 1 }).default }) - : _notifSettings.push({ value: (setting as ChannelSetting & { type: 2 }).default , enabled: (setting as ChannelSetting & { type: 2 }).enabled })); - return _notifSettings; -} + let _notifSettings: UserSettingType[] = []; + settings && + settings.forEach((setting) => + isSettingType1(setting) + ? _notifSettings.push({ enabled: (setting as ChannelSetting & { type: 1 }).default }) + : _notifSettings.push({ + value: (setting as ChannelSetting & { type: 2 }).default, + enabled: (setting as ChannelSetting & { type: 2 }).enabled, + }) + ); + + return _notifSettings; +}; export const notifUserSettingFormatString = ({ settings }: { settings: UserSetting[] }) => { - let _notifSettings = []; - settings && settings.forEach((setting) => - isSettingType1(setting) - ? _notifSettings.push({ enabled: setting.user }) - : _notifSettings.push({ value: setting.user, enabled: (setting as ChannelSetting & { type: 2 }).enabled })); - return _notifSettings; -} + let _notifSettings = []; + settings && + settings.forEach((setting) => + isSettingType1(setting) + ? _notifSettings.push({ enabled: setting.user }) + : _notifSettings.push({ value: setting.user, enabled: (setting as ChannelSetting & { type: 2 }).enabled }) + ); + return _notifSettings; +}; export const userSettingsFromDefaultChannelSetting = ({ channelSetting }: { channelSetting: ChannelSetting[] }) => { - let _userSettings = []; - channelSetting && channelSetting.forEach((setting) => - isSettingType1(setting) - ? _userSettings.push({ ...setting, user: setting.default }) - : _userSettings.push({ ...setting, user: setting.default })); - return _userSettings; -}; \ No newline at end of file + let _userSettings = []; + channelSetting && + channelSetting.forEach((setting) => + isSettingType1(setting) + ? _userSettings.push({ ...setting, user: setting.default }) + : _userSettings.push({ ...setting, user: setting.default }) + ); + return _userSettings; +}; + +const SETTING_DELIMITER = '-'; +const SETTING_SEPARATOR = '+'; +const RANGE_TYPE = 3; +const SLIDER_TYPE = 2; +const BOOLEAN_TYPE = 1; + +export const getMinimalUserSetting = (settings: UserSettingType[]) => { + if (!settings) { + return null; + } + + let userSetting = ''; + let numberOfSettings = 0; + for (let i = 0; i < settings.length; i++) { + const ele = settings[i]; + const enabled = ele.enabled ? 1 : 0; + if (ele.enabled) numberOfSettings++; + + if (Object.keys(ele).includes('value')) { + // slider type + if (typeof ele.value == 'number') + userSetting = userSetting + SLIDER_TYPE + SETTING_DELIMITER + enabled + SETTING_DELIMITER + ele.value; + else { + userSetting = + userSetting + + RANGE_TYPE + + SETTING_DELIMITER + + enabled + + SETTING_DELIMITER + + ele.value?.lower + + SETTING_DELIMITER + + ele.value?.upper; + } + } else { + // boolean type + userSetting = userSetting + BOOLEAN_TYPE + SETTING_DELIMITER + enabled; + } + if (i != settings.length - 1) userSetting = userSetting + SETTING_SEPARATOR; + } + return numberOfSettings + SETTING_SEPARATOR + userSetting; +}; From b8dee012a002a29e4111e12d363df3bcd7625ee3 Mon Sep 17 00:00:00 2001 From: abhishek-01k Date: Fri, 24 May 2024 16:02:41 +0530 Subject: [PATCH 2/3] Changed the code from for loop to array.reduce --- src/helpers/channel/notifSetting.ts | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/helpers/channel/notifSetting.ts b/src/helpers/channel/notifSetting.ts index 5e251816a5..da87d1a9f5 100644 --- a/src/helpers/channel/notifSetting.ts +++ b/src/helpers/channel/notifSetting.ts @@ -15,7 +15,7 @@ export const notifChannelSettingFormatString = ({ settings }: { settings: Channe ? _notifSettings.push({ enabled: (setting as ChannelSetting & { type: 1 }).default }) : _notifSettings.push({ value: (setting as ChannelSetting & { type: 2 }).default, - enabled: (setting as ChannelSetting & { type: 2 }).enabled, + enabled: (setting as ChannelSetting & { type: 2 }).enabled }) ); @@ -55,20 +55,21 @@ export const getMinimalUserSetting = (settings: UserSettingType[]) => { return null; } - let userSetting = ''; + // let userSetting = ''; let numberOfSettings = 0; - for (let i = 0; i < settings.length; i++) { - const ele = settings[i]; + + //TODO: Use array.reduce( preffered) or array.forEach (less preffered) instead of for loop + const userSetting = settings.reduce((acc, ele, i) => { const enabled = ele.enabled ? 1 : 0; if (ele.enabled) numberOfSettings++; if (Object.keys(ele).includes('value')) { // slider type - if (typeof ele.value == 'number') - userSetting = userSetting + SLIDER_TYPE + SETTING_DELIMITER + enabled + SETTING_DELIMITER + ele.value; - else { - userSetting = - userSetting + + if (typeof ele.value === 'number') { + acc = acc + SLIDER_TYPE + SETTING_DELIMITER + enabled + SETTING_DELIMITER + ele.value; + } else { + acc = + acc + RANGE_TYPE + SETTING_DELIMITER + enabled + @@ -79,9 +80,13 @@ export const getMinimalUserSetting = (settings: UserSettingType[]) => { } } else { // boolean type - userSetting = userSetting + BOOLEAN_TYPE + SETTING_DELIMITER + enabled; + acc = acc + BOOLEAN_TYPE + SETTING_DELIMITER + enabled; } - if (i != settings.length - 1) userSetting = userSetting + SETTING_SEPARATOR; - } + + if (i !== settings.length - 1) acc = acc + SETTING_SEPARATOR; + + return acc; + }, ''); + return numberOfSettings + SETTING_SEPARATOR + userSetting; }; From 1f947b74116ea7e370b07ecdab69bd551c9f6392 Mon Sep 17 00:00:00 2001 From: abhishek-01k Date: Mon, 27 May 2024 16:24:12 +0530 Subject: [PATCH 3/3] Removed Comments --- src/helpers/channel/notifSetting.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/helpers/channel/notifSetting.ts b/src/helpers/channel/notifSetting.ts index da87d1a9f5..f688d9c866 100644 --- a/src/helpers/channel/notifSetting.ts +++ b/src/helpers/channel/notifSetting.ts @@ -55,10 +55,8 @@ export const getMinimalUserSetting = (settings: UserSettingType[]) => { return null; } - // let userSetting = ''; let numberOfSettings = 0; - //TODO: Use array.reduce( preffered) or array.forEach (less preffered) instead of for loop const userSetting = settings.reduce((acc, ele, i) => { const enabled = ele.enabled ? 1 : 0; if (ele.enabled) numberOfSettings++;