Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add site notes slice/service #154

Merged
merged 17 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.3",
"terraso-backend": "github:techmatters/terraso-backend#f671497",
"terraso-backend": "github:techmatters/terraso-backend#2ffcc08",
"uuid": "^9.0.1"
},
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions src/project/projectFragments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ export const projectData = /* GraphQL */ `
name
privacy
description
siteInstructions
updatedAt
archived
measurementUnits
membershipList {
...projectMembershipList
}
Expand Down
1 change: 1 addition & 0 deletions src/project/projectService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export const collapseProject = ({
return {
project: {
...project,
siteInstructions: project.siteInstructions || undefined,
sites: collapseToSet(Object.keys(sites)),
memberships,
},
Expand Down
3 changes: 3 additions & 0 deletions src/project/projectSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
updateUsers,
} from 'terraso-client-shared/account/accountSlice';
import {
MeasurementUnits,
ProjectAddUserMutationInput,
UserRole,
} from 'terraso-client-shared/graphqlSchema/graphql';
Expand All @@ -44,10 +45,12 @@ export type Project = {
name: string;
privacy: 'PRIVATE' | 'PUBLIC';
description: string;
siteInstructions?: string;
updatedAt: string; // this should be Date.toLocaleDateString; redux can't serialize Dates
memberships: Record<string, ProjectMembership>;
sites: SerializableSet;
archived: boolean;
measurementUnits: MeasurementUnits;
};

interface MembershipKey {
Expand Down
3 changes: 3 additions & 0 deletions src/selectors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ const generateProject = (
sites: siteSet,
archived: false,
memberships: keyBy(memberships, 'id'),
measurementUnits: 'METRIC',
siteInstructions: '',
};
};

Expand All @@ -67,6 +69,7 @@ const generateSite = (project?: Project): Site => {
privacy: 'PRIVATE',
archived: false,
updatedAt: '2023-10-24',
notes: {},
};
if (project !== undefined) {
project.sites[site.id] = true;
Expand Down
24 changes: 24 additions & 0 deletions src/site/siteFragments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,29 @@ export const siteData = /* GraphQL */ `
project {
id
}
notes {
edges {
node {
...siteNoteData
}
}
}
}
`;

export const siteNoteData = /* GraphQL */ `
fragment siteNoteData on SiteNoteNode {
id
content
createdAt
updatedAt
author {
id
firstName
lastName
}
site {
id
}
}
`;
78 changes: 76 additions & 2 deletions src/site/siteService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,55 @@ import { graphql } from 'terraso-client-shared/graphqlSchema';
import type {
SiteAddMutationInput,
SiteDataFragment,
SiteNoteAddMutationInput,
SiteNoteDataFragment,
SiteNoteUpdateMutationInput,
SiteTransferMutationInput,
SiteUpdateMutationInput,
} from 'terraso-client-shared/graphqlSchema/graphql';
import type { Site } from 'terraso-client-shared/site/siteSlice';
import type { Site, SiteNote } from 'terraso-client-shared/site/siteSlice';
import * as terrasoApi from 'terraso-client-shared/terrasoApi/api';
import {
collapseEdges,
Connection,
} from 'terraso-client-shared/terrasoApi/utils';

export const collapseSite = (site: SiteDataFragment): Site => {
const { project, owner, ...rest } = site;
const { project, owner, notes, ...rest } = site;
return {
...rest,
projectId: project?.id,
ownerId: owner?.id,
notes: collapseSiteNotes(notes),
};
};

export const collapseSites = (sites: Connection<SiteDataFragment>) =>
Object.fromEntries(
collapseEdges(sites).map(site => [site.id, collapseSite(site)]),
);

export const collapseSiteNotes = (
siteNotes: Connection<SiteNoteDataFragment>,
) =>
Object.fromEntries(
collapseEdges(siteNotes).map(siteNote => [
siteNote.id,
collapseSiteNote(siteNote),
]),
);

export const collapseSiteNote = (siteNote: SiteNoteDataFragment): SiteNote => {
const { author, site, ...rest } = siteNote;
return {
...rest,
authorId: author.id,
authorFirstName: author.firstName,
authorLastName: author.lastName,
siteId: site.id,
};
};

export const fetchSite = (id: string) => {
const query = graphql(`
query site($id: ID!) {
Expand Down Expand Up @@ -194,3 +220,51 @@ export const transferSitesToProject = (input: SiteTransferMutationInput) => {
},
);
};

export const addSiteNote = (siteNote: SiteNoteAddMutationInput) => {
const query = graphql(`
mutation addSiteNote($input: SiteNoteAddMutationInput!) {
addSiteNote(input: $input) {
siteNote {
...siteNoteData
}
errors
}
}
`);

return terrasoApi
.requestGraphQL(query, { input: siteNote })
.then(resp => resp.addSiteNote.siteNote!);
};

export const deleteSiteNote = (siteNote: SiteNote) => {
const query = graphql(`
mutation deleteSiteNote($input: SiteNoteDeleteMutationInput!) {
deleteSiteNote(input: $input) {
errors
}
}
`);

return terrasoApi
.requestGraphQL(query, { input: { id: siteNote.id } })
.then(_ => siteNote);
};

export const updateSiteNote = (siteNote: SiteNoteUpdateMutationInput) => {
const query = graphql(`
mutation updateSiteNote($input: SiteNoteUpdateMutationInput!) {
updateSiteNote(input: $input) {
siteNote {
...siteNoteData
}
errors
}
}
`);

return terrasoApi
.requestGraphQL(query, { input: siteNote })
.then(resp => resp.updateSiteNote.siteNote!);
};
56 changes: 56 additions & 0 deletions src/site/siteSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import {
SiteAddMutationInput,
SiteNoteAddMutationInput,
SiteNoteUpdateMutationInput,
SiteTransferMutationInput,
SiteUpdateMutationInput,
} from 'terraso-client-shared/graphqlSchema/graphql';
Expand All @@ -41,6 +43,18 @@ export type Site = {
privacy: SitePrivacy;
archived: boolean;
updatedAt: string;
notes: Record<string, SiteNote>;
};

export type SiteNote = {
id: string;
siteId: string;
content: string;
createdAt: string;
updatedAt: string;
authorId: string;
authorFirstName: string;
authorLastName: string;
};

const initialState = {
Expand Down Expand Up @@ -110,6 +124,30 @@ export const transferSites = createAsyncThunk<
return result;
});

export const addSiteNote = createAsyncThunk<SiteNote, SiteNoteAddMutationInput>(
'site/addSiteNote',
async (siteNote, _) => {
let result = await siteService.addSiteNote(siteNote);
return siteService.collapseSiteNote(result);
},
);

export const deleteSiteNote = createAsyncThunk<SiteNote, SiteNote>(
'site/deleteSiteNote',
async siteNote => {
let result = await siteService.deleteSiteNote(siteNote);
return result;
},
);

export const updateSiteNote = createAsyncThunk<
SiteNote,
SiteNoteUpdateMutationInput
>('site/updateSiteNote', async (siteNote, _) => {
let result = await siteService.updateSiteNote(siteNote);
return siteService.collapseSiteNote(result);
});

const siteSlice = createSlice({
name: 'site',
initialState,
Expand Down Expand Up @@ -170,6 +208,24 @@ const siteSlice = createSlice({
}
},
);

builder.addCase(addSiteNote.fulfilled, (state, { payload: siteNote }) => {
state.sites[siteNote.siteId].notes[siteNote.id] = siteNote;
});

builder.addCase(
deleteSiteNote.fulfilled,
(state, { payload: siteNote }) => {
delete state.sites[siteNote.siteId].notes[siteNote.id];
},
);

builder.addCase(
updateSiteNote.fulfilled,
(state, { payload: siteNote }) => {
state.sites[siteNote.siteId].notes[siteNote.id] = siteNote;
},
);
},
});

Expand Down