diff --git a/src/account/accountSlice.ts b/src/account/accountSlice.ts index 45d60723..28710eab 100644 --- a/src/account/accountSlice.ts +++ b/src/account/accountSlice.ts @@ -15,7 +15,7 @@ * along with this program. If not, see https://www.gnu.org/licenses/. */ -import { createSlice } from '@reduxjs/toolkit'; +import { createSlice, Draft, PayloadAction } from '@reduxjs/toolkit'; import _ from 'lodash/fp'; import * as accountService from 'terraso-client-shared/account/accountService'; import { getToken, removeToken } from 'terraso-client-shared/account/auth'; @@ -50,6 +50,8 @@ export const initialState = { users: {} as Record, }; +type AccountState = typeof initialState; + export type User = { id: string; email: string; @@ -97,32 +99,39 @@ export const unsubscribeFromNotifications = createAsyncThunk( false, ); +export const setUsers = ( + state: Draft, + users: Record, +) => { + state.users = users; +}; +export const updateUsers = ( + state: Draft, + users: Record, +) => { + Object.assign(state.users, users); +}; + +export const addUser = (state: Draft, user: User) => { + state.users[user.id] = user; +}; + export const userSlice = createSlice({ name: 'user', initialState, reducers: { - setCurrentUser: (state, action) => ({ + setCurrentUser: (state, action: PayloadAction) => ({ ...state, currentUser: { data: action.payload, fetching: false, }, }), - setHasToken: (state, action) => ({ + setHasToken: (state, action: PayloadAction) => ({ ...state, hasToken: action.payload, }), - setUsers: (state, { payload: users }) => { - state.users = users; - }, - updateUsers: (state, { payload: users }) => { - Object.assign(state.users, users); - }, - - addUser: (state, { payload: user }) => { - state.users[user.id] = user; - }, }, extraReducers: builder => { @@ -274,8 +283,7 @@ export const userSlice = createSlice({ }, }); -export const { setCurrentUser, setUsers, updateUsers, setHasToken, addUser } = - userSlice.actions; +export const { setCurrentUser, setHasToken } = userSlice.actions; export default userSlice.reducer; diff --git a/src/project/projectService.ts b/src/project/projectService.ts index 94795eec..c9d18af5 100644 --- a/src/project/projectService.ts +++ b/src/project/projectService.ts @@ -213,7 +213,9 @@ export const addUserToProject = (input: ProjectAddUserMutationInput) => { `); return terrasoApi .requestGraphQL(command, { input }) - .then(output => output.addUserToProject); + .then(output => + collapseProjectMembership(output.addUserToProject.membership), + ); }; export const updateUserRole = (input: ProjectUpdateUserRoleMutationInput) => { diff --git a/src/site/siteService.ts b/src/site/siteService.ts index b6f232e2..0f681909 100644 --- a/src/site/siteService.ts +++ b/src/site/siteService.ts @@ -235,7 +235,7 @@ export const addSiteNote = (siteNote: SiteNoteAddMutationInput) => { return terrasoApi .requestGraphQL(query, { input: siteNote }) - .then(resp => resp.addSiteNote.siteNote!); + .then(resp => collapseSiteNote(resp.addSiteNote.siteNote)); }; export const deleteSiteNote = (siteNote: SiteNote) => { @@ -266,5 +266,5 @@ export const updateSiteNote = (siteNote: SiteNoteUpdateMutationInput) => { return terrasoApi .requestGraphQL(query, { input: siteNote }) - .then(resp => resp.updateSiteNote.siteNote!); + .then(resp => collapseSiteNote(resp.updateSiteNote.siteNote)); }; diff --git a/src/store/utils.ts b/src/store/utils.ts index 531abd6d..e2efc2a6 100644 --- a/src/store/utils.ts +++ b/src/store/utils.ts @@ -17,10 +17,8 @@ import { useEffect } from 'react'; import { - AnyAction, AsyncThunkAction, createAsyncThunk as createAsyncThunkBase, - ThunkAction, } from '@reduxjs/toolkit'; import type { BaseThunkAPI } from '@reduxjs/toolkit/dist/createAsyncThunk'; import _ from 'lodash/fp'; @@ -36,7 +34,6 @@ import type { SharedDispatch, SharedState, } from 'terraso-client-shared/store/store'; -import { entries, fromEntries } from 'terraso-client-shared/utils'; export type SerializableSet = Record; @@ -171,30 +168,3 @@ export const useFetchData = ( return () => req.abort(); }, [dispatch, dataFetchCallback]); }; - -type DispatchMap = { - [Property in keyof Result]: ( - arg: Result[Property], - ) => - | AnyAction - | ThunkAction; -}; - -export const dispatchByKeys = ( - fetcher: CreateAsyncThunkParams, - dispatchMapFn: (_: T) => DispatchMap, -): CreateAsyncThunkParams => { - return async (arg, currentUser, thunkAPI) => { - const result = await fetcher(arg, currentUser, thunkAPI); - const dispatchMap = dispatchMapFn(result); - - return fromEntries( - await Promise.all( - entries(result).map(async ([key, result]) => { - await thunkAPI.dispatch(dispatchMap[key](result)); - return [key, result]; - }), - ), - ) as T; - }; -};