Skip to content

Commit

Permalink
refactor: When checkUserInProject returns a user, add them to account…
Browse files Browse the repository at this point in the history
…Slice's cached users (#1031)
  • Loading branch information
knipec authored Nov 13, 2024
1 parent 4ec5bca commit cdf2161
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
11 changes: 7 additions & 4 deletions src/account/accountService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,13 @@ export const signOut = async () => {

export type UserInProjectError = 'NoUser' | 'InProject';

export const checkUserInProject = async (
projectId: string,
userEmail: string,
) => {
export const checkUserInProject = async ({
projectId,
userEmail,
}: {
projectId: string;
userEmail: string;
}) => {
const existQuery = graphql(`
query userExistsInProject($email: String!, $project: String!) {
userExists: users(email_Iexact: $email) {
Expand Down
22 changes: 16 additions & 6 deletions src/account/accountSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ export const unsubscribeFromNotifications = createAsyncThunk(
false,
);

export const checkUserInProject = createAsyncThunk(
'account/checkUserInProject',
accountService.checkUserInProject,
);

export const setUsers = (
state: Draft<AccountState>,
users: Record<string, User>,
Expand Down Expand Up @@ -132,10 +137,6 @@ export const userSlice = createSlice({
...state,
hasToken: action.payload,
}),
addUserToCache: (state, action: PayloadAction<User>) => {
addUser(state, action.payload);
return state;
},
},

extraReducers: builder => {
Expand Down Expand Up @@ -284,11 +285,20 @@ export const userSlice = createSlice({
unsubscribeFromNotifications.fulfilled,
_.set('unsubscribe', { processing: false, success: true, error: null }),
);

builder.addCase(checkUserInProject.fulfilled, (state, action) => {
// TODO: Client should not be able to access other users' preferences
// https://github.com/techmatters/terraso-client-shared/issues/1030 and
// https://github.com/techmatters/terraso-backend/issues/1548
if (!('type' in action.payload)) {
const user = { ...action.payload, preferences: {} } as User;
addUser(state, user);
}
});
},
});

export const { setCurrentUser, setHasToken, addUserToCache } =
userSlice.actions;
export const { setCurrentUser, setHasToken } = userSlice.actions;

export default userSlice.reducer;

Expand Down

0 comments on commit cdf2161

Please sign in to comment.