diff options
Diffstat (limited to 'src/store')
-rw-r--r-- | src/store/actions/user.ts | 23 | ||||
-rw-r--r-- | src/store/initialStates.ts | 4 | ||||
-rw-r--r-- | src/store/reducers/userReducer.ts | 8 |
3 files changed, 32 insertions, 3 deletions
diff --git a/src/store/actions/user.ts b/src/store/actions/user.ts index 941101df..b1cb8719 100644 --- a/src/store/actions/user.ts +++ b/src/store/actions/user.ts @@ -4,12 +4,14 @@ import {Action, ThunkAction} from '@reduxjs/toolkit'; import { getProfilePic, loadProfileInfo, + removeBadgesService, sendSuggestedPeopleLinked, } from '../../services'; import {UniversityBadge, UserType} from '../../types/types'; import {getTokenOrLogout} from '../../utils'; import { clearHeaderAndProfileImages, + profileBadgeRemoved, profileBadgesUpdated, profileCompletionStageUpdated, setIsOnboardedUser, @@ -107,6 +109,27 @@ export const updateUserBadges = } }; +/** + * Removes a single badge from logged-in user by badge name. + * @param badgeName name of badge to be removed + * @param loggedInUserId userId of loggedInUser + */ +export const removeUserBadge = + ( + badgeName: string, + loggedInUserId: string, + ): ThunkAction<Promise<void>, RootState, unknown, Action<string>> => + async (dispatch) => { + try { + const success = await removeBadgesService([badgeName], loggedInUserId); + if (success) { + dispatch({type: profileBadgeRemoved.type, payload: {badge: badgeName}}); + } + } catch (error) { + console.log(error); + } + }; + export const updateProfileCompletionStage = ( stage: number, diff --git a/src/store/initialStates.ts b/src/store/initialStates.ts index e0f9d776..e2902a2d 100644 --- a/src/store/initialStates.ts +++ b/src/store/initialStates.ts @@ -1,4 +1,4 @@ -import {CommentThreadType} from './../types/types'; +import {CommentThreadType, UniversityType} from './../types/types'; import { MomentType, NotificationType, @@ -17,7 +17,7 @@ export const NO_PROFILE: ProfileInfoType = { gender: '', birthday: undefined, university_class: 2021, - university: undefined, + university: UniversityType.Empty, badges: [], //Default to an invalid value and ignore it gracefully while showing tutorials / popups. profile_completion_stage: -1, diff --git a/src/store/reducers/userReducer.ts b/src/store/reducers/userReducer.ts index 97bf845c..4692c5d3 100644 --- a/src/store/reducers/userReducer.ts +++ b/src/store/reducers/userReducer.ts @@ -46,6 +46,12 @@ const userDataSlice = createSlice({ state.profile.badges = action.payload.badges; }, + profileBadgeRemoved: (state, action) => { + state.profile.badges = state.profile.badges.filter( + (badge) => badge.name !== action.payload.badge, + ); + }, + profileCompletionStageUpdated: (state, action) => { state.profile.profile_completion_stage = action.payload.stage; }, @@ -95,6 +101,6 @@ export const { setSuggestedPeopleImage, clearHeaderAndProfileImages, profileBadgesUpdated, - // setChatClientReady, + profileBadgeRemoved, } = userDataSlice.actions; export const userDataReducer = userDataSlice.reducer; |