Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: companion PR for mobile client global redux actions PR #962

Merged
merged 4 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions src/account/accountSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -50,6 +50,8 @@ export const initialState = {
users: {} as Record<string, User>,
};

type AccountState = typeof initialState;

export type User = {
id: string;
email: string;
Expand Down Expand Up @@ -97,32 +99,39 @@ export const unsubscribeFromNotifications = createAsyncThunk(
false,
);

export const setUsers = (
state: Draft<AccountState>,
users: Record<string, User>,
) => {
state.users = users;
};
export const updateUsers = (
state: Draft<AccountState>,
users: Record<string, User>,
) => {
Object.assign(state.users, users);
};

export const addUser = (state: Draft<AccountState>, user: User) => {
state.users[user.id] = user;
};

export const userSlice = createSlice({
name: 'user',
initialState,

reducers: {
setCurrentUser: (state, action) => ({
setCurrentUser: (state, action: PayloadAction<User | null>) => ({
...state,
currentUser: {
data: action.payload,
fetching: false,
},
}),
setHasToken: (state, action) => ({
setHasToken: (state, action: PayloadAction<boolean>) => ({
...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 => {
Expand Down Expand Up @@ -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;

Expand Down
4 changes: 3 additions & 1 deletion src/project/projectService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
4 changes: 2 additions & 2 deletions src/site/siteService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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));
};
30 changes: 0 additions & 30 deletions src/store/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<string, boolean>;

Expand Down Expand Up @@ -171,30 +168,3 @@ export const useFetchData = (
return () => req.abort();
}, [dispatch, dataFetchCallback]);
};

type DispatchMap<Result> = {
[Property in keyof Result]: (
arg: Result[Property],
) =>
| AnyAction
| ThunkAction<Result[Property], SharedState, undefined, AnyAction>;
};

export const dispatchByKeys = <T extends object, ThunkArg>(
fetcher: CreateAsyncThunkParams<T, ThunkArg>,
dispatchMapFn: (_: T) => DispatchMap<T>,
): CreateAsyncThunkParams<T, ThunkArg> => {
return async (arg, currentUser, thunkAPI) => {
const result = await fetcher(arg, currentUser, thunkAPI);
const dispatchMap = dispatchMapFn(result);

return fromEntries(
await Promise.all(
entries<keyof T, T[keyof T]>(result).map(async ([key, result]) => {
await thunkAPI.dispatch(dispatchMap[key](result));
return [key, result];
}),
),
) as T;
};
};
Loading