Skip to content

Commit

Permalink
feat: Add GraphQL mutation to add user to project
Browse files Browse the repository at this point in the history
  • Loading branch information
David Code Howard committed Oct 2, 2023
1 parent 656ce90 commit 36ee3cb
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"lodash": "^4.17.21",
"react": "^18.2.0",
"react-redux": "^8.1.2",
"terraso-backend": "github:techmatters/terraso-backend#4170565",
"terraso-backend": "github:techmatters/terraso-backend#9e7b2eb",
"uuid": "^9.0.0"
},
"scripts": {
Expand Down
6 changes: 5 additions & 1 deletion src/account/accountSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ export const userSlice = createSlice({
setUsers: (state, { payload: users }) => {
Object.assign(state.users, users);
},

addUser: (state, { payload: user }) => {
state.users[user.id] = user;
},
},

extraReducers: builder => {
Expand Down Expand Up @@ -265,7 +269,7 @@ export const userSlice = createSlice({
},
});

export const { setUser, setHasToken } = userSlice.actions;
export const { setUser, setHasToken, addUser } = userSlice.actions;

export default userSlice.reducer;

Expand Down
20 changes: 20 additions & 0 deletions src/project/projectService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type { User } from 'terraso-client-shared/account/accountSlice';
import { graphql } from 'terraso-client-shared/graphqlSchema';
import type {
ProjectAddMutationInput,
ProjectAddUserMutationInput,
ProjectArchiveMutationInput,
ProjectDataFragment,
ProjectDeleteMutationInput,
Expand Down Expand Up @@ -201,3 +202,22 @@ export const archiveProject = (project: ProjectArchiveMutationInput) => {
})
.then(_ => project.archived);
};

export const addUserToProject = (input: ProjectAddUserMutationInput) => {
const command = graphql(`
mutation addUserToProject($input: ProjectAddUserMutationInput!) {
addUserToProject(input: $input) {
errors
project {
id
}
membership {
...projectMembershipFields
}
}
}
`);
return terrasoApi
.requestGraphQL(command, { input })
.then(output => output.addUserToProject);
};
40 changes: 38 additions & 2 deletions src/project/projectSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@
*/

import { createAction, createSlice } from '@reduxjs/toolkit';
import { setUsers, User } from 'terraso-client-shared/account/accountSlice';
import { UserRole } from 'terraso-client-shared/graphqlSchema/graphql';
import {
addUser,
setUsers,
User,
} from 'terraso-client-shared/account/accountSlice';
import {
ProjectAddUserMutationInput,
UserRole,
} from 'terraso-client-shared/graphqlSchema/graphql';
import * as projectService from 'terraso-client-shared/project/projectService';
import { setSites, Site } from 'terraso-client-shared/site/siteSlice';
import {
Expand Down Expand Up @@ -122,6 +129,24 @@ export const archiveProject = createAsyncThunk(
projectService.archiveProject,
);

export const addUserToProject = createAsyncThunk<
ProjectMembership,
ProjectAddUserMutationInput
>('project/addUser', async (input, _, { dispatch }) => {
const res = await projectService.addUserToProject(input);
// TODO: Should make user required in future!
// https://github.com/techmatters/terraso-backend/issues/859
if (res.membership.user === undefined || res.membership.user === null) {
throw Error(`Membership ${res.membership.id} created without user!`);
}
dispatch(addUser(res.membership.user));
return {
userId: res.membership.user.id,
userRole: res.membership.userRole,
id: res.membership.id,
};
});

const projectSlice = createSlice({
name: 'project',
initialState,
Expand Down Expand Up @@ -189,6 +214,17 @@ const projectSlice = createSlice({
state.projects[meta.arg.id].archived = archived;
},
);

builder.addCase(
addUserToProject.fulfilled,
(state, { meta, payload: { id: membershipId, userRole, userId } }) => {
state.projects[meta.arg.projectId].memberships[membershipId] = {
id: membershipId,
userRole,
userId,
};
},
);
},
});

Expand Down

0 comments on commit 36ee3cb

Please sign in to comment.