Update bookmark mutation generator function

This commit is contained in:
Ramon Wenger 2024-02-28 13:20:52 +01:00
parent 40de116ad4
commit 8afc7d2229
4 changed files with 137 additions and 134 deletions

View File

@ -38,6 +38,7 @@ describe('Bookmarks', () => {
ModuleEditModeQuery: {}, ModuleEditModeQuery: {},
InstrumentQuery: { InstrumentQuery: {
instrument: { instrument: {
bookmarks: [],
contents: [ contents: [
{ {
type: 'text_block', type: 'text_block',

View File

@ -198,25 +198,21 @@ const editNote = () => {
store.dispatch('editNote', note); store.dispatch('editNote', note);
}; };
const { mutation, variables, update, optimisticResponse } = constructContentComponentBookmarkMutation( const { mutation, updateCurry, getVariables, optimisticResponse } = constructContentComponentBookmarkMutation(
props.component.id, props.component.id,
bookmarked.value,
props.parent, props.parent,
props.root props.root
); );
const { mutate: mutateBookmarkContent } = useMutation(mutation, { const { mutate: mutateBookmarkContent } = useMutation(mutation, {
update,
optimisticResponse, optimisticResponse,
}); });
const bookmarkContent = (bookmarked: boolean) => { const bookmarkContent = (bookmarked: boolean) => {
const newVars = { const variables = getVariables(bookmarked);
input: { const update = updateCurry(bookmarked);
...variables.input, mutateBookmarkContent(variables, {
bookmarked, update,
}, });
};
mutateBookmarkContent(newVars);
}; };
const markHighlights = () => { const markHighlights = () => {

View File

@ -4,39 +4,28 @@ import { pushToArray, removeAtIndex } from '@/graphql/immutable-operations';
import { graphql } from '@/__generated__'; import { graphql } from '@/__generated__';
const compareUuid = (uuid: string) => (element) => element.uuid === uuid; const compareUuid = (uuid: string) => (element) => element.uuid === uuid;
type UpdateCurry = (newBookmarkedValue: boolean) => (store) => void;
type GetVariables = (newBookmarkedValue: boolean) => any;
interface Mutation { interface Mutation {
mutation: any; mutation: any;
variables: any;
update: (store: any) => void;
optimisticResponse: any; optimisticResponse: any;
updateCurry: UpdateCurry;
getVariables: GetVariables;
} }
export const constructContentComponentBookmarkMutation: ( export const constructContentComponentBookmarkMutation: (uuid: string, parent: any, root: any) => Mutation = (
uuid: string, uuid,
bookmarked: boolean, parent,
parent: any, root
root: any ) => {
) => Mutation = (uuid, bookmarked, parent, root) => { let mutation: any;
let mutation: Mutation; let updateCurry: UpdateCurry;
let getVariables: GetVariables;
let optimisticResponse: any;
if (parent.__typename === 'InstrumentNode') { if (parent.__typename === 'InstrumentNode') {
mutation = { updateCurry = (newBookmarkedValue: boolean) => (store) => {
mutation: graphql(`
mutation UpdateInstrumentBookmark($input: UpdateInstrumentBookmarkInput!) {
updateInstrumentBookmark(input: $input) {
success
}
}
`),
variables: {
input: {
uuid,
instrument: root,
bookmarked,
},
},
update: (store) => {
const fragment = INSTRUMENT_FRAGMENT; const fragment = INSTRUMENT_FRAGMENT;
const id = store.identify({ const id = store.identify({
__typename: 'InstrumentNode', __typename: 'InstrumentNode',
@ -48,8 +37,9 @@ export const constructContentComponentBookmarkMutation: (
}); });
let bookmarks: any[]; let bookmarks: any[];
console.log(newBookmarkedValue);
if (bookmarked) { if (newBookmarkedValue) {
const element = { const element = {
note: null, note: null,
uuid, uuid,
@ -69,38 +59,39 @@ export const constructContentComponentBookmarkMutation: (
...instrument, ...instrument,
bookmarks, bookmarks,
}; };
console.log(data);
store.writeFragment({ store.writeFragment({
data, data,
fragment, fragment,
id, id,
}); });
};
getVariables = (newBookmarkedValue: boolean) => {
return {
input: {
uuid,
instrument: root,
bookmarked: newBookmarkedValue,
}, },
optimisticResponse: { };
};
mutation = graphql(`
mutation UpdateInstrumentBookmark($input: UpdateInstrumentBookmarkInput!) {
updateInstrumentBookmark(input: $input) {
success
}
}
`);
optimisticResponse = {
__typename: 'Mutation', __typename: 'Mutation',
updateInstrumentBookmark: { updateInstrumentBookmark: {
__typename: 'UpdateInstrumentBookmarkPayload', __typename: 'UpdateInstrumentBookmarkPayload',
success: true, success: true,
}, },
},
}; };
} else { } else {
mutation = { updateCurry = (newBookmarkedValue: boolean) => (store) => {
mutation: graphql(`
mutation UpdateContentBookmark($input: UpdateContentBookmarkInput!) {
updateContentBookmark(input: $input) {
success
}
}
`),
variables: {
input: {
uuid,
contentBlock: root,
bookmarked,
},
},
update: (store) => {
const query = CONTENT_BLOCK_QUERY; const query = CONTENT_BLOCK_QUERY;
const variables = { id: root }; const variables = { id: root };
const { contentBlock } = store.readQuery({ const { contentBlock } = store.readQuery({
@ -111,7 +102,7 @@ export const constructContentComponentBookmarkMutation: (
// const bookmarks = data.contentBlock.bookmarks; // const bookmarks = data.contentBlock.bookmarks;
let bookmarks: any[]; let bookmarks: any[];
if (bookmarked) { if (newBookmarkedValue) {
const element = { const element = {
note: null, note: null,
uuid, uuid,
@ -139,15 +130,30 @@ export const constructContentComponentBookmarkMutation: (
query, query,
variables, variables,
}); });
};
getVariables = (newBookmarkedValue: boolean) => {
return {
input: {
uuid,
contentBlock: root,
bookmarked: newBookmarkedValue,
}, },
optimisticResponse: { };
};
mutation = graphql(`
mutation UpdateContentBookmark($input: UpdateContentBookmarkInput!) {
updateContentBookmark(input: $input) {
success
}
}
`);
optimisticResponse = {
__typename: 'Mutation', __typename: 'Mutation',
updateContentBookmark: { updateContentBookmark: {
__typename: 'UpdateContentBookmarkPayload', __typename: 'UpdateContentBookmarkPayload',
success: true, success: true,
}, },
},
}; };
} }
return mutation; return { mutation, updateCurry, getVariables, optimisticResponse };
}; };