From 5c6bedb572556586d34b99384dac1cad3a153402 Mon Sep 17 00:00:00 2001 From: Ivan Chen Date: Tue, 1 Jun 2021 16:41:18 -0400 Subject: Refactor to use action instead of util function --- src/store/actions/user.ts | 23 +++++++++++++++++++++++ src/store/reducers/userReducer.ts | 10 +++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) (limited to 'src/store') 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, RootState, unknown, Action> => + 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/reducers/userReducer.ts b/src/store/reducers/userReducer.ts index 97bf845c..03fee112 100644 --- a/src/store/reducers/userReducer.ts +++ b/src/store/reducers/userReducer.ts @@ -1,4 +1,6 @@ import {createSlice} from '@reduxjs/toolkit'; +import {ActionSheetIOS} from 'react-native'; +import {Badge} from 'react-native-elements'; import {NO_USER_DATA} from '../initialStates'; /** @@ -46,6 +48,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 +103,6 @@ export const { setSuggestedPeopleImage, clearHeaderAndProfileImages, profileBadgesUpdated, - // setChatClientReady, + profileBadgeRemoved, } = userDataSlice.actions; export const userDataReducer = userDataSlice.reducer; -- cgit v1.2.3-70-g09d2 From ba010611f8768ff523390141010899acf2d77b8b Mon Sep 17 00:00:00 2001 From: Ivan Chen Date: Tue, 1 Jun 2021 19:01:09 -0400 Subject: Lint --- src/store/reducers/userReducer.ts | 2 -- src/utils/common.ts | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) (limited to 'src/store') diff --git a/src/store/reducers/userReducer.ts b/src/store/reducers/userReducer.ts index 03fee112..4692c5d3 100644 --- a/src/store/reducers/userReducer.ts +++ b/src/store/reducers/userReducer.ts @@ -1,6 +1,4 @@ import {createSlice} from '@reduxjs/toolkit'; -import {ActionSheetIOS} from 'react-native'; -import {Badge} from 'react-native-elements'; import {NO_USER_DATA} from '../initialStates'; /** diff --git a/src/utils/common.ts b/src/utils/common.ts index 6804558f..7e54eeaf 100644 --- a/src/utils/common.ts +++ b/src/utils/common.ts @@ -1,5 +1,4 @@ import AsyncStorage from '@react-native-community/async-storage'; -import {HeaderTitle} from '@react-navigation/stack'; import moment from 'moment'; import {Linking} from 'react-native'; import {getAll} from 'react-native-contacts'; @@ -13,6 +12,7 @@ import { NotificationType, UniversityBadge, UniversityBadgeDisplayType, + UniversityBadgeType, UniversityType, } from './../types/types'; -- cgit v1.2.3-70-g09d2 From c18b2436897cd92e7a33c33c75e13dba1fec8ffe Mon Sep 17 00:00:00 2001 From: Ivan Chen Date: Mon, 7 Jun 2021 17:07:09 -0400 Subject: Fix default value for university type --- src/store/initialStates.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/store') 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, -- cgit v1.2.3-70-g09d2