From 656ce90c6ae820e5ba664beeaa6e68a13acdc45b Mon Sep 17 00:00:00 2001 From: David Code Howard Date: Wed, 27 Sep 2023 17:38:40 -0400 Subject: [PATCH] feat: Use Membership List in project (#104) * fix: Update backend and fragments * fix: Update project service datatypes * fix: Remove unneeded memberships functions * fix: Add id to ProjectMembership type * chore: update backend --- package-lock.json | 4 +-- package.json | 2 +- src/project/projectFragments.ts | 28 +++++++++++++-- src/project/projectService.ts | 63 ++++++++++++++++++--------------- src/project/projectSlice.ts | 18 +++++----- 5 files changed, 72 insertions(+), 43 deletions(-) diff --git a/package-lock.json b/package-lock.json index a1950ae0..8f20a564 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,7 @@ "lodash": "^4.17.21", "react": "^18.2.0", "react-redux": "^8.1.2", - "terraso-backend": "github:techmatters/terraso-backend#d87539e", + "terraso-backend": "github:techmatters/terraso-backend#4170565", "uuid": "^9.0.0" }, "devDependencies": { @@ -12995,7 +12995,7 @@ }, "node_modules/terraso-backend": { "version": "0.1.0", - "resolved": "git+ssh://git@github.com/techmatters/terraso-backend.git#d87539e82c9ae59ace3948e1350f14ec25fedce4" + "resolved": "git+ssh://git@github.com/techmatters/terraso-backend.git#4170565525a1b85462685d4e2c861a3cfeec7fba" }, "node_modules/test-exclude": { "version": "6.0.0", diff --git a/package.json b/package.json index 24302bcb..735ff04c 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "lodash": "^4.17.21", "react": "^18.2.0", "react-redux": "^8.1.2", - "terraso-backend": "github:techmatters/terraso-backend#d87539e", + "terraso-backend": "github:techmatters/terraso-backend#4170565", "uuid": "^9.0.0" }, "scripts": { diff --git a/src/project/projectFragments.ts b/src/project/projectFragments.ts index 63a71be3..9330bc61 100644 --- a/src/project/projectFragments.ts +++ b/src/project/projectFragments.ts @@ -15,6 +15,29 @@ * along with this program. If not, see https://www.gnu.org/licenses/. */ +export const projectMembershipFields = /* GraphQL */ ` + fragment projectMembershipFields on ProjectMembershipNode { + id + userRole + user { + ...userFields + } + } +`; + +export const projectMembershipList = /* GraphQL */ ` + fragment projectMembershipList on ProjectMembershipListNode { + memberships { + totalCount + edges { + node { + ...projectMembershipFields + } + } + } + } +`; + export const projectData = /* GraphQL */ ` fragment projectData on ProjectNode { id @@ -23,9 +46,8 @@ export const projectData = /* GraphQL */ ` description updatedAt archived - group { - id - ...groupMembers + membershipList { + ...projectMembershipList } siteSet { edges { diff --git a/src/project/projectService.ts b/src/project/projectService.ts index d7ace0f2..e59bc794 100644 --- a/src/project/projectService.ts +++ b/src/project/projectService.ts @@ -24,10 +24,10 @@ import type { ProjectDeleteMutationInput, ProjectUpdateMutationInput, } from 'terraso-client-shared/graphqlSchema/graphql'; -import { Membership } from 'terraso-client-shared/memberships/membershipsSlice'; import type { HydratedProject, Project, + ProjectMembership, SerializableSet, } from 'terraso-client-shared/project/projectSlice'; import { collapseSiteFields } from 'terraso-client-shared/site/siteService'; @@ -47,20 +47,25 @@ const collapseProjectFields = collapseFields< const siteIds = inp.siteSet.edges .map(edge => edge.node.id) .reduce((x, y) => ({ ...x, [y]: true }), {} as SerializableSet); - const membershipIds = inp.group.memberships.edges - .map(edge => edge.node) - .reduce( - (x, { id, user }) => ({ ...x, [id]: { user: user.id } }), - {} as Record, - ); - - const { siteSet: _x, group: _y, updatedAt, ...rest } = inp; + const memberships = + inp.membershipList.memberships?.edges + .map(edge => edge.node) + .reduce( + (x, { id, user, userRole }) => { + if (user === null || user === undefined) { + return x; + } + return { ...x, [id]: { userId: user.id, userRole, id } }; + }, + {} as Record, + ) || {}; + + const { siteSet: _x, membershipList: _y, updatedAt, ...rest } = inp; const output: Project = { ...rest, updatedAt: new Date(updatedAt).toLocaleString(), siteIds, - membershipIds, - groupId: inp.group.id, + memberships, }; return output; }, @@ -71,24 +76,26 @@ const collapseProjectFields = collapseFields< (x, y) => ({ ...x, [y.id]: collapseSiteFields(y) }), {} as Record, ), - memberships: inp => - inp.group.memberships.edges - .map(({ node: { id, userRole, membershipStatus } }) => ({ - membershipId: id, - userRole, - membershipStatus, - })) - .reduce( - (x, y) => ({ ...x, [y.membershipId]: y }), - {} as Record, - ), users: inp => - inp.group.memberships.edges - .map(({ node: { user } }) => ({ - ...user, - preferences: {}, - })) - .reduce((x, y) => ({ ...x, [y.id]: y }), {} as Record), + inp.membershipList.memberships?.edges + .map(({ node: { user } }) => { + if (user === undefined || user === null) { + return undefined; + } + return { + ...user, + preferences: {}, + }; + }) + .reduce( + (x, y) => { + if (y !== undefined) { + return { ...x, [y.id]: y }; + } + return x; + }, + {} as Record, + ) || {}, }, true, ); diff --git a/src/project/projectSlice.ts b/src/project/projectSlice.ts index 521529ab..48c73cfa 100644 --- a/src/project/projectSlice.ts +++ b/src/project/projectSlice.ts @@ -17,10 +17,7 @@ import { createAction, createSlice } from '@reduxjs/toolkit'; import { setUsers, User } from 'terraso-client-shared/account/accountSlice'; -import { - Membership, - setMembers, -} from 'terraso-client-shared/memberships/membershipsSlice'; +import { 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 { @@ -32,28 +29,31 @@ const { plural: dehydrateProjects, sing: dehydrateProject } = dehydrated< Project, HydratedProject >({ - memberships: setMembers, users: setUsers, sites: setSites, }); export type SerializableSet = Record; +export type ProjectMembership = { + userId: string; + userRole: UserRole; + id: string; +}; + export type Project = { id: string; name: string; privacy: 'PRIVATE' | 'PUBLIC'; description: string; updatedAt: string; // this should be Date.toLocaleDateString; redux can't serialize Dates - membershipIds: Record; // TODO: Why doesn't the membership have user info? have to store user id here as well + memberships: Record; siteIds: SerializableSet; - groupId: string; archived: boolean; }; export type HydratedProject = { dehydrated: Project; - memberships: Record; users: Record; sites: Record; }; @@ -137,7 +137,7 @@ const projectSlice = createSlice({ builder.addCase( removeMembershipFromProject, (state, { payload: { membershipId, projectId } }) => { - delete state.projects[projectId].membershipIds[membershipId]; + delete state.projects[projectId].memberships[membershipId]; }, );