From 67a5d10da956de739c6ea5dc1533bbf5a12a31df Mon Sep 17 00:00:00 2001 From: David Code Howard Date: Wed, 18 Oct 2023 13:58:46 -0400 Subject: [PATCH] feat: Add thunk to delete user from project --- src/project/projectService.ts | 30 ++++++++++++++++++++++++++++++ src/project/projectSlice.ts | 12 ++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/project/projectService.ts b/src/project/projectService.ts index 3f6cca83..73e34ea7 100644 --- a/src/project/projectService.ts +++ b/src/project/projectService.ts @@ -24,6 +24,7 @@ import type { ProjectArchiveMutationInput, ProjectDataFragment, ProjectDeleteMutationInput, + ProjectDeleteUserMutationInput, ProjectMembershipFieldsFragment, ProjectUpdateMutationInput, ProjectUpdateUserRoleMutationInput, @@ -237,3 +238,32 @@ export const updateUserRole = (input: ProjectUpdateUserRoleMutationInput) => { }), ); }; + +export const deleteUserFromProject = ( + input: ProjectDeleteUserMutationInput, +) => { + const command = graphql(` + mutation removeUser($input: ProjectDeleteUserMutationInput!) { + deleteUserFromProject(input: $input) { + membership { + id + } + project { + id + } + } + } + `); + + return terrasoApi.requestGraphQL(command, { input }).then( + ({ + deleteUserFromProject: { + project: { id: projectId }, + membership: { id: membershipId }, + }, + }) => ({ + projectId, + membershipId, + }), + ); +}; diff --git a/src/project/projectSlice.ts b/src/project/projectSlice.ts index 3d46a616..6b67fc35 100644 --- a/src/project/projectSlice.ts +++ b/src/project/projectSlice.ts @@ -133,6 +133,13 @@ const projectSlice = createSlice({ state.projects[projectId].memberships[membershipId].userRole = userRole; }, ); + + builder.addCase( + deleteUserFromProject.fulfilled, + (state, { payload: { projectId, membershipId } }) => { + delete state.projects[projectId].memberships[membershipId]; + }, + ); }, }); @@ -221,4 +228,9 @@ export const updateUserRole = createAsyncThunk( projectService.updateUserRole, ); +export const deleteUserFromProject = createAsyncThunk( + 'project/deleteUserFromProject', + projectService.deleteUserFromProject, +); + export default projectSlice.reducer;