diff options
Diffstat (limited to 'src/store/actions')
| -rw-r--r-- | src/store/actions/index.ts | 1 | ||||
| -rw-r--r-- | src/store/actions/momentCategories.tsx | 63 | ||||
| -rw-r--r-- | src/store/actions/userX.ts | 10 |
3 files changed, 73 insertions, 1 deletions
diff --git a/src/store/actions/index.ts b/src/store/actions/index.ts index 04fa9767..f9fd5e9c 100644 --- a/src/store/actions/index.ts +++ b/src/store/actions/index.ts @@ -1,6 +1,7 @@ export * from './user'; export * from './userFollow'; export * from './userMoments'; +export * from './momentCategories'; export * from './socials'; export * from './taggUsers'; export * from './userBlock'; diff --git a/src/store/actions/momentCategories.tsx b/src/store/actions/momentCategories.tsx new file mode 100644 index 00000000..a522c3e0 --- /dev/null +++ b/src/store/actions/momentCategories.tsx @@ -0,0 +1,63 @@ +import {RootState} from '../rootReducer'; +import { + deleteMomentCategories, + loadMomentCategories, + postMomentCategories, +} from '../../services'; +import {Action, ThunkAction} from '@reduxjs/toolkit'; +import {momentCategoriesFetched} from '../reducers'; +import {getTokenOrLogout} from '../../utils'; +import {MomentCategoryType} from '../../types'; + +/** + * Load all categories for user + * @param userId id of the user for whom categories should be loaded + */ +export const loadUserMomentCategories = ( + userId: string, +): ThunkAction<Promise<void>, RootState, unknown, Action<string>> => async ( + dispatch, +) => { + try { + const token = await getTokenOrLogout(dispatch); + const categories = await loadMomentCategories(userId, token); + dispatch({ + type: momentCategoriesFetched.type, + payload: {categories, add: true}, + }); + } catch (error) { + console.log(error); + } +}; + +/** + * Handle addition / deletion of categories for a user + * @param categories List of categories + * @param add boolean, if true, we add new categories, else we delete + * @param userId id of the user for whom categories should be updated + */ +export const updateMomentCategories = ( + categories: Array<MomentCategoryType>, + add: boolean, + userId: string, +): ThunkAction<Promise<void>, RootState, unknown, Action<string>> => async ( + dispatch, +) => { + try { + const token = await getTokenOrLogout(dispatch); + let success = false; + if (add) { + success = await postMomentCategories(categories, token); + } else { + success = await deleteMomentCategories(categories, userId, token); + } + if (success) { + dispatch({ + type: momentCategoriesFetched.type, + payload: {categories, add}, + }); + } + } catch (error) { + console.log(error); + } +}; diff --git a/src/store/actions/userX.ts b/src/store/actions/userX.ts index 5468f762..87162eb1 100644 --- a/src/store/actions/userX.ts +++ b/src/store/actions/userX.ts @@ -1,6 +1,7 @@ +import {loadMomentCategories} from './../../services/MomentCategoryService'; import {userXInStore} from './../../utils/'; import {getTokenOrLogout, loadAllSocialsForUser} from './../../utils'; -import {UserType, ScreenType, ProfilePreviewType} from '../../types/types'; +import {UserType, ScreenType} from '../../types/types'; import {RootState} from '../rootReducer'; import {Action, ThunkAction} from '@reduxjs/toolkit'; import { @@ -13,6 +14,7 @@ import { userXProfileFetched, userXSocialsFetched, userXUserFetched, + userXMomentCategoriesFetched, resetScreen, } from '../reducers'; import { @@ -80,6 +82,12 @@ export const loadUserX = ( payload: {screenType, userId, data}, }), ); + loadMomentCategories(userId, token).then((data) => { + dispatch({ + type: userXMomentCategoriesFetched.type, + payload: {screenType, userId, data}, + }); + }); } catch (error) { console.log(error); } |
