From 493bab9d5c9931f28edf0211c563c3aa7e19833b Mon Sep 17 00:00:00 2001 From: David Code Howard Date: Tue, 17 Oct 2023 11:37:27 -0400 Subject: [PATCH] feat: Add thunk for updating user role in 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 261a209d..3f6cca83 100644 --- a/src/project/projectService.ts +++ b/src/project/projectService.ts @@ -26,6 +26,7 @@ import type { ProjectDeleteMutationInput, ProjectMembershipFieldsFragment, ProjectUpdateMutationInput, + ProjectUpdateUserRoleMutationInput, } from 'terraso-client-shared/graphqlSchema/graphql'; import { collapseSites } from 'terraso-client-shared/site/siteService'; import * as terrasoApi from 'terraso-client-shared/terrasoApi/api'; @@ -207,3 +208,32 @@ export const addUserToProject = (input: ProjectAddUserMutationInput) => { .requestGraphQL(command, { input }) .then(output => output.addUserToProject); }; + +export const updateUserRole = (input: ProjectUpdateUserRoleMutationInput) => { + const command = graphql(` + mutation updateRole($input: ProjectUpdateUserRoleMutationInput!) { + updateUserRoleInProject(input: $input) { + project { + id + } + membership { + id + userRole + } + } + } + `); + + return terrasoApi.requestGraphQL(command, { input }).then( + ({ + updateUserRoleInProject: { + project: { id: projectId }, + membership: { id: membershipId, userRole }, + }, + }) => ({ + projectId, + membershipId, + userRole, + }), + ); +}; diff --git a/src/project/projectSlice.ts b/src/project/projectSlice.ts index ab34a07a..3d46a616 100644 --- a/src/project/projectSlice.ts +++ b/src/project/projectSlice.ts @@ -126,6 +126,13 @@ const projectSlice = createSlice({ }; }, ); + + builder.addCase( + updateUserRole.fulfilled, + (state, { payload: { projectId, membershipId, userRole } }) => { + state.projects[projectId].memberships[membershipId].userRole = userRole; + }, + ); }, }); @@ -209,4 +216,9 @@ export const addUserToProject = createAsyncThunk< }; }); +export const updateUserRole = createAsyncThunk( + 'project/updateUserRole', + projectService.updateUserRole, +); + export default projectSlice.reducer;