-
Notifications
You must be signed in to change notification settings - Fork 10
Linxiaoli/vsl 236 and vsl 263: integrate with API and show error message and implement subscribe all community flow #698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: notifications
Are you sure you want to change the base?
Changes from all commits
9aab295
2574dcf
10781e3
80c11b4
d672de4
4790c39
c92c3af
41e4bd1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,32 +5,50 @@ import { | |
| LEANPLUM_EXPORT_KEY, | ||
| LEANPLUM_PROD_KEY, | ||
| } from 'api/constants'; | ||
| import { subscribeNotificationIntentions } from 'const'; | ||
| import Leanplum from 'leanplum-sdk'; | ||
|
|
||
| const COMMUNITY_UPDATES_CATEGORY_ID = 1; | ||
| const options = { | ||
| method: 'GET', | ||
| headers: { accept: 'application/json' }, | ||
| }; | ||
|
|
||
| export const startLeanplum = () => { | ||
| /* @param: communitySubIntentions : [{communityId:"1", subscribeIntention:"subscribe"},{communityId:"2",subscribeIntention:"unsubscribe"}] | ||
| * @return: {communityId1:'True', communityId2:'False'} | ||
| */ | ||
| const getDesiredAttributes = (communitySubIntentions) => { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is to convert the array in line 18 into a format of {community1:'True', community2:'False'} |
||
| return communitySubIntentions | ||
| .map(({ communityId, subscribeIntention }) => ({ | ||
| key: `community${communityId}`, | ||
| value: | ||
| subscribeIntention === subscribeNotificationIntentions.subscribe | ||
| ? 'True' | ||
| : 'False', | ||
| })) | ||
| .reduce((acc, curr) => { | ||
| const { key, value } = curr; | ||
| acc[key] = value; | ||
| return acc; | ||
| }, {}); | ||
| }; | ||
| export const startLeanplumForUser = (walletId) => { | ||
| const IS_LOCAL_DEV = process.env.REACT_APP_APP_ENV === 'development'; | ||
|
|
||
| if (IS_LOCAL_DEV) { | ||
| Leanplum.setAppIdForDevelopmentMode(LEANPLUM_APP_ID, LEANPLUM_DEV_KEY); | ||
| } else { | ||
| Leanplum.setAppIdForProductionMode(LEANPLUM_APP_ID, LEANPLUM_PROD_KEY); | ||
| } | ||
|
|
||
| Leanplum.start(); | ||
| }; | ||
|
|
||
| export const setUserId = async (walletId) => { | ||
| try { | ||
| Leanplum.setUserId(walletId); | ||
| } catch (e) { | ||
| throw new Error(e); | ||
| } | ||
| return new Promise((resolve, reject) => { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Returning a promise is necessary for ensuring the sequence of execution, we want start leanplum first then get user settings |
||
| Leanplum.start((success) => { | ||
| Leanplum.setUserId(walletId); | ||
| if (success) { | ||
| resolve('leanplum user set'); | ||
| } else { | ||
| reject('leanplum user not set'); | ||
| } | ||
| }); | ||
| }); | ||
| }; | ||
|
|
||
| export const getUserSettings = async (walletId) => { | ||
|
|
@@ -47,61 +65,49 @@ export const getUserSettings = async (walletId) => { | |
| if (!data.userAttributes) { | ||
| throw new Error('User Not Found'); | ||
| } | ||
|
|
||
| const communitySubscriptions = []; | ||
| const res = { | ||
| email: data.userAttributes.email, | ||
| }; | ||
| let isSubscribedToCommunityUpdates = true; | ||
|
|
||
| let isSubscribedFromCommunityUpdates = true; | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming correction to align the names with the notion document |
||
| for (const property in data.userAttributes) { | ||
| if (property.includes('community')) { | ||
| if ( | ||
| property.includes('community') && | ||
| typeof data.userAttributes[property] == 'string' | ||
| ) { | ||
| const communityId = property.split('community')[1]; | ||
| communitySubscriptions.push({ | ||
| communityId, | ||
| subscribed: data.userAttributes[property] === 'True', | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| res.communitySubscription = communitySubscriptions; | ||
|
|
||
| if (data.unsubscribeCategories) { | ||
| data.unsubscribeCategories.forEach((category) => { | ||
| if (parseInt(category.id) === COMMUNITY_UPDATES_CATEGORY_ID) { | ||
| isSubscribedToCommunityUpdates = false; | ||
| isSubscribedFromCommunityUpdates = false; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| res.isSubscribedToCommunityUpdates = isSubscribedToCommunityUpdates; | ||
| res.isSubscribedFromCommunityUpdates = isSubscribedFromCommunityUpdates; | ||
| return res; | ||
| } catch (e) { | ||
| throw new Error(e); | ||
| } | ||
| // setTimeout(() => { | ||
| // throw new Error('get user setting error'); | ||
| // }, 500); | ||
| }; | ||
|
|
||
| export const setUserEmail = async (email) => { | ||
| try { | ||
| await Leanplum.setUserAttributes({ email }); | ||
| return true; | ||
| } catch (e) { | ||
| throw new Error(e); | ||
| } | ||
| }; | ||
|
|
||
| export const unsubscribeCommunity = async (communityId) => { | ||
| try { | ||
| Leanplum.setUserAttributes({ [`community${communityId}`]: 'False' }); | ||
| return true; | ||
| } catch (e) { | ||
| throw new Error(e); | ||
| } | ||
| Leanplum.setUserAttributes({ email }); | ||
| }; | ||
|
|
||
| export const subscribeCommunity = async (communityId) => { | ||
| export const updateCommunitySubscription = async (communitySubIntentions) => { | ||
| const desiredAttributes = getDesiredAttributes(communitySubIntentions); | ||
| try { | ||
| Leanplum.setUserAttributes({ [`community${communityId}`]: 'True' }); | ||
| Leanplum.setUserAttributes(desiredAttributes); | ||
| return true; | ||
| } catch (e) { | ||
| throw new Error(e); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍