-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Subscribe change to subscribev2 (#1524)
* Subscribe change to subscribev2 * Changed the code from for loop to array.reduce * Removed Comments
- Loading branch information
1 parent
5dfc3a6
commit 75c87a5
Showing
4 changed files
with
107 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,90 @@ | ||
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; | ||
}; | ||
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 numberOfSettings = 0; | ||
|
||
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') { | ||
acc = acc + SLIDER_TYPE + SETTING_DELIMITER + enabled + SETTING_DELIMITER + ele.value; | ||
} else { | ||
acc = | ||
acc + | ||
RANGE_TYPE + | ||
SETTING_DELIMITER + | ||
enabled + | ||
SETTING_DELIMITER + | ||
ele.value?.lower + | ||
SETTING_DELIMITER + | ||
ele.value?.upper; | ||
} | ||
} else { | ||
// boolean type | ||
acc = acc + BOOLEAN_TYPE + SETTING_DELIMITER + enabled; | ||
} | ||
|
||
if (i !== settings.length - 1) acc = acc + SETTING_SEPARATOR; | ||
|
||
return acc; | ||
}, ''); | ||
|
||
return numberOfSettings + SETTING_SEPARATOR + userSetting; | ||
}; |