Skip to content

Commit 60eff58

Browse files
committed
MP-33 member tracks in settings
1 parent 3b35bae commit 60eff58

File tree

11 files changed

+78
-16
lines changed

11 files changed

+78
-16
lines changed

src/apps/accounts/src/settings/tabs/config/account-settings-tabs-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function getHashFromTabId(tabId: string): string {
4444

4545
export function getTabIdFromHash(hash: string): string {
4646
switch (hash) {
47-
case '#tcandyu': return AccountSettingsTabViews.tcandyou
47+
case '#tcandyou': return AccountSettingsTabViews.tcandyou
4848
case '#tools': return AccountSettingsTabViews.tools
4949
case '#account': return AccountSettingsTabViews.account
5050
case '#preferences': return AccountSettingsTabViews.preferences

src/apps/accounts/src/settings/tabs/tcandyou/TCandYouTab.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import styles from './TCandYouTab.module.scss'
77

88
interface TCandYouTabProps {
99
profile: UserProfile
10+
// eslint-disable-next-line react/no-unused-prop-types
1011
memberTraits: UserTraits[] | undefined
1112
}
1213

src/apps/accounts/src/settings/tabs/tcandyou/tracks/Tracks.tsx

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { Dispatch, FC, useState } from 'react'
1+
import { Dispatch, FC, useContext, useEffect, useState } from 'react'
22
import { bind } from 'lodash'
3+
import { toast } from 'react-toastify'
34

4-
import { TC_TRACKS, UserProfile } from '~/libs/core'
5+
import { profileContext, ProfileContextData, TC_TRACKS, updateMemberProfileAsync, UserProfile } from '~/libs/core'
56
import { Collapsible, FormToggleSwitch } from '~/libs/ui'
67
import { DataScienceTrackIcon, DesignTrackIcon, DevelopmentTrackIcon, SettingSection } from '~/apps/accounts/src/lib'
78

@@ -12,20 +13,46 @@ interface TracksProps {
1213
}
1314

1415
const Tracks: FC<TracksProps> = (props: TracksProps) => {
15-
const [devTrack, setDevTrack]: [boolean, Dispatch<boolean>]
16-
= useState<boolean>(!!props.profile.tracks?.includes('DEVELOP'))
16+
const [memberTracks, setMemberTracks]: [TC_TRACKS[], Dispatch<TC_TRACKS[]>]
17+
= useState<TC_TRACKS[]>(props.profile.tracks || [])
1718

18-
const [designTrack, setDesignTrack]: [boolean, Dispatch<boolean>]
19-
= useState<boolean>(!!props.profile.tracks?.includes('DESIGN'))
19+
const memberProfileContext: ProfileContextData = useContext(profileContext)
2020

21-
const [dsTrack, setDSTrack]: [boolean, Dispatch<boolean>]
22-
= useState<boolean>(!!props.profile.tracks?.includes('DATA_SCIENCE'))
21+
useEffect(() => {
22+
setMemberTracks(props.profile.tracks || [])
23+
}, [props.profile])
2324

2425
function handleTracksChange(type: TC_TRACKS): void {
25-
26-
}
26+
const hasTrack: boolean = memberTracks.includes(type)
27+
let updatedTracks: TC_TRACKS[]
28+
29+
if (hasTrack) {
30+
// remove track
31+
updatedTracks = memberTracks.filter((track: TC_TRACKS) => track !== type)
32+
} else {
33+
// add track
34+
updatedTracks = memberTracks.concat(type)
35+
}
2736

28-
console.log('devTrack', props)
37+
updateMemberProfileAsync(
38+
props.profile.handle,
39+
{ tracks: updatedTracks },
40+
)
41+
.then(() => {
42+
setMemberTracks(updatedTracks)
43+
memberProfileContext.updateProfileContext({
44+
...memberProfileContext,
45+
profile: {
46+
...memberProfileContext.profile,
47+
tracks: updatedTracks,
48+
} as any,
49+
})
50+
toast.success('Your profile has been updated.')
51+
})
52+
.catch(() => {
53+
toast.error('Failed to update your profile.')
54+
})
55+
}
2956

3057
return (
3158
<Collapsible
@@ -48,7 +75,7 @@ const Tracks: FC<TracksProps> = (props: TracksProps) => {
4875
<FormToggleSwitch
4976
name='designTrack'
5077
onChange={bind(handleTracksChange, this, 'DESIGN')}
51-
value={designTrack}
78+
value={!!memberTracks.includes('DESIGN')}
5279
/>
5380
)}
5481
/>
@@ -63,7 +90,7 @@ const Tracks: FC<TracksProps> = (props: TracksProps) => {
6390
<FormToggleSwitch
6491
name='devTrack'
6592
onChange={bind(handleTracksChange, this, 'DEVELOP')}
66-
value={devTrack}
93+
value={!!memberTracks.includes('DEVELOP')}
6794
/>
6895
)}
6996
/>
@@ -78,7 +105,7 @@ const Tracks: FC<TracksProps> = (props: TracksProps) => {
78105
<FormToggleSwitch
79106
name='dsTrack'
80107
onChange={bind(handleTracksChange, this, 'DATA_SCIENCE')}
81-
value={dsTrack}
108+
value={!!memberTracks.includes('DATA_SCIENCE')}
82109
/>
83110
)}
84111
/>

src/libs/core/lib/profile/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ export * from './modify-user-email-preferences.model'
1414
export * from './modify-user-mfa.model'
1515
export * from './user-traits.model'
1616
export * from './modify-tracks.request'
17+
export * from './modify-user-profile.model'
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { TC_TRACKS } from './user-profile.model'
2+
3+
export interface UpdateProfileRequest {
4+
firstName?: string
5+
lastName?: string
6+
tracks?: TC_TRACKS[]
7+
}

src/libs/core/lib/profile/profile-context/profile-context-data.model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ export interface ProfileContextData {
77
isLoggedIn: boolean
88
profile?: UserProfile
99
updateProfile: (updatedProfileContext: ProfileContextData) => Promise<void>
10+
updateProfileContext: (updatedProfileContext: ProfileContextData) => void
1011
}

src/libs/core/lib/profile/profile-context/profile.context-provider.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const ProfileProvider: FC<ProfileProviderProps> = (props: ProfileProvider
3131
isLoggedIn: !!profile,
3232
profile,
3333
updateProfile,
34+
updateProfileContext,
3435
}
3536
setProfileContextData(contextData)
3637
}
@@ -52,6 +53,17 @@ export const ProfileProvider: FC<ProfileProviderProps> = (props: ProfileProvider
5253
.then(() => setProfileContextData(updatedContext))
5354
}
5455

56+
function updateProfileContext(updatedContext: ProfileContextData): void {
57+
58+
const { profile }: ProfileContextData = updatedContext
59+
60+
if (!profile) {
61+
throw new Error('Cannot update an undefined profile')
62+
}
63+
64+
setProfileContextData(updatedContext)
65+
}
66+
5567
useEffect(() => {
5668

5769
// if our profile is already initialized, no need to continue

src/libs/core/lib/profile/profile-context/profile.context.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const defaultProfileContextData: ProfileContextData = {
77
initialized: false,
88
isLoggedIn: false,
99
updateProfile: () => Promise.resolve(undefined),
10+
updateProfileContext: () => undefined,
1011
}
1112

1213
const profileContext: Context<ProfileContextData> = createContext(defaultProfileContextData)

src/libs/core/lib/profile/profile-functions/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export {
1313
updateMemberTraitsAsync,
1414
createMemberTraitsAsync,
1515
modifyTracksAsync,
16+
updateMemberProfileAsync,
1617
} from './profile.functions'
1718
export * from './profile-store'
1819
export * from './rating.functions'

src/libs/core/lib/profile/profile-functions/profile-store/profile-xhr.store.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { EditNameRequest } from '../../edit-name-request.model'
44
import { ModifyTracksRequest } from '../../modify-tracks.request'
55
import { ModifyMemberEmailPreferencesRequest } from '../../modify-user-email-preferences.model'
66
import { ModifyUserMFARequest, ModifyUserMFAResponse } from '../../modify-user-mfa.model'
7+
import { UpdateProfileRequest } from '../../modify-user-profile.model'
78
import { ModifyUserPropertyRequest, ModifyUserPropertyResponse } from '../../modify-user-role.model'
89
import { UserEmailPreferences } from '../../user-email-preference.model'
910
import { UserProfile } from '../../user-profile.model'
@@ -94,6 +95,10 @@ export async function createMemberTraits(
9495
return xhrPostAsync<UserTraits[], UserTraits[]>(`${profileUrl(handle)}/traits`, traits)
9596
}
9697

97-
export function modifyTracks(handle: string, request: ModifyTracksRequest): Promise<UserProfile> {
98+
export async function modifyTracks(handle: string, request: ModifyTracksRequest): Promise<UserProfile> {
9899
return xhrPutAsync<ModifyTracksRequest, UserProfile>(profileUrl(handle), request)
99100
}
101+
102+
export async function updateMemberProfile(handle: string, profile: UpdateProfileRequest): Promise<UserProfile> {
103+
return xhrPutAsync<UpdateProfileRequest, UserProfile>(profileUrl(handle), profile)
104+
}

0 commit comments

Comments
 (0)