From cee77043ab7356113a99e212fd6c904d4790bbd5 Mon Sep 17 00:00:00 2001 From: shrouxm Date: Mon, 14 Oct 2024 13:19:58 -0700 Subject: [PATCH 1/4] refactor: remove dispatchByKeys previous helper for multi-slice mutations --- src/store/utils.ts | 30 ------------------------------ 1 file changed, 30 deletions(-) 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; - }; -}; From f9583380c2a32cbbb80736e62b54943c66db5d24 Mon Sep 17 00:00:00 2001 From: shrouxm Date: Mon, 14 Oct 2024 13:20:35 -0700 Subject: [PATCH 2/4] cleanup: add missing calls to `collapseX` was creating inconsistency/extra effort downstream --- src/project/projectService.ts | 4 +++- src/site/siteService.ts | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) 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)); }; From a2ca1fb60880846d9a229abb9fe0de14470071f0 Mon Sep 17 00:00:00 2001 From: shrouxm Date: Mon, 14 Oct 2024 13:22:06 -0700 Subject: [PATCH 3/4] refactor: rewrite actions as state mutations --- src/account/accountSlice.ts | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/account/accountSlice.ts b/src/account/accountSlice.ts index 45d60723..4a291825 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,6 +99,23 @@ 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, @@ -113,16 +132,6 @@ export const userSlice = createSlice({ ...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; From 001f1d5fa90076f4a887a9cefe1b131a97bd76cc Mon Sep 17 00:00:00 2001 From: shrouxm Date: Mon, 14 Oct 2024 13:23:01 -0700 Subject: [PATCH 4/4] types: add missing argument types --- src/account/accountSlice.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/account/accountSlice.ts b/src/account/accountSlice.ts index 4a291825..28710eab 100644 --- a/src/account/accountSlice.ts +++ b/src/account/accountSlice.ts @@ -121,14 +121,14 @@ export const userSlice = createSlice({ 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, }),