Update bookmark mutation generator function
This commit is contained in:
parent
40de116ad4
commit
8afc7d2229
|
|
@ -38,6 +38,7 @@ describe('Bookmarks', () => {
|
|||
ModuleEditModeQuery: {},
|
||||
InstrumentQuery: {
|
||||
instrument: {
|
||||
bookmarks: [],
|
||||
contents: [
|
||||
{
|
||||
type: 'text_block',
|
||||
|
|
|
|||
|
|
@ -198,25 +198,21 @@ const editNote = () => {
|
|||
store.dispatch('editNote', note);
|
||||
};
|
||||
|
||||
const { mutation, variables, update, optimisticResponse } = constructContentComponentBookmarkMutation(
|
||||
const { mutation, updateCurry, getVariables, optimisticResponse } = constructContentComponentBookmarkMutation(
|
||||
props.component.id,
|
||||
bookmarked.value,
|
||||
props.parent,
|
||||
props.root
|
||||
);
|
||||
const { mutate: mutateBookmarkContent } = useMutation(mutation, {
|
||||
update,
|
||||
optimisticResponse,
|
||||
});
|
||||
|
||||
const bookmarkContent = (bookmarked: boolean) => {
|
||||
const newVars = {
|
||||
input: {
|
||||
...variables.input,
|
||||
bookmarked,
|
||||
},
|
||||
};
|
||||
mutateBookmarkContent(newVars);
|
||||
const variables = getVariables(bookmarked);
|
||||
const update = updateCurry(bookmarked);
|
||||
mutateBookmarkContent(variables, {
|
||||
update,
|
||||
});
|
||||
};
|
||||
|
||||
const markHighlights = () => {
|
||||
|
|
|
|||
|
|
@ -4,39 +4,28 @@ import { pushToArray, removeAtIndex } from '@/graphql/immutable-operations';
|
|||
import { graphql } from '@/__generated__';
|
||||
|
||||
const compareUuid = (uuid: string) => (element) => element.uuid === uuid;
|
||||
type UpdateCurry = (newBookmarkedValue: boolean) => (store) => void;
|
||||
type GetVariables = (newBookmarkedValue: boolean) => any;
|
||||
|
||||
interface Mutation {
|
||||
mutation: any;
|
||||
variables: any;
|
||||
update: (store: any) => void;
|
||||
optimisticResponse: any;
|
||||
updateCurry: UpdateCurry;
|
||||
getVariables: GetVariables;
|
||||
}
|
||||
|
||||
export const constructContentComponentBookmarkMutation: (
|
||||
uuid: string,
|
||||
bookmarked: boolean,
|
||||
parent: any,
|
||||
root: any
|
||||
) => Mutation = (uuid, bookmarked, parent, root) => {
|
||||
let mutation: Mutation;
|
||||
export const constructContentComponentBookmarkMutation: (uuid: string, parent: any, root: any) => Mutation = (
|
||||
uuid,
|
||||
parent,
|
||||
root
|
||||
) => {
|
||||
let mutation: any;
|
||||
let updateCurry: UpdateCurry;
|
||||
let getVariables: GetVariables;
|
||||
let optimisticResponse: any;
|
||||
|
||||
if (parent.__typename === 'InstrumentNode') {
|
||||
mutation = {
|
||||
mutation: graphql(`
|
||||
mutation UpdateInstrumentBookmark($input: UpdateInstrumentBookmarkInput!) {
|
||||
updateInstrumentBookmark(input: $input) {
|
||||
success
|
||||
}
|
||||
}
|
||||
`),
|
||||
variables: {
|
||||
input: {
|
||||
uuid,
|
||||
instrument: root,
|
||||
bookmarked,
|
||||
},
|
||||
},
|
||||
update: (store) => {
|
||||
updateCurry = (newBookmarkedValue: boolean) => (store) => {
|
||||
const fragment = INSTRUMENT_FRAGMENT;
|
||||
const id = store.identify({
|
||||
__typename: 'InstrumentNode',
|
||||
|
|
@ -48,8 +37,9 @@ export const constructContentComponentBookmarkMutation: (
|
|||
});
|
||||
|
||||
let bookmarks: any[];
|
||||
console.log(newBookmarkedValue);
|
||||
|
||||
if (bookmarked) {
|
||||
if (newBookmarkedValue) {
|
||||
const element = {
|
||||
note: null,
|
||||
uuid,
|
||||
|
|
@ -69,38 +59,39 @@ export const constructContentComponentBookmarkMutation: (
|
|||
...instrument,
|
||||
bookmarks,
|
||||
};
|
||||
console.log(data);
|
||||
|
||||
store.writeFragment({
|
||||
data,
|
||||
fragment,
|
||||
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',
|
||||
updateInstrumentBookmark: {
|
||||
__typename: 'UpdateInstrumentBookmarkPayload',
|
||||
success: true,
|
||||
},
|
||||
},
|
||||
};
|
||||
} else {
|
||||
mutation = {
|
||||
mutation: graphql(`
|
||||
mutation UpdateContentBookmark($input: UpdateContentBookmarkInput!) {
|
||||
updateContentBookmark(input: $input) {
|
||||
success
|
||||
}
|
||||
}
|
||||
`),
|
||||
variables: {
|
||||
input: {
|
||||
uuid,
|
||||
contentBlock: root,
|
||||
bookmarked,
|
||||
},
|
||||
},
|
||||
update: (store) => {
|
||||
updateCurry = (newBookmarkedValue: boolean) => (store) => {
|
||||
const query = CONTENT_BLOCK_QUERY;
|
||||
const variables = { id: root };
|
||||
const { contentBlock } = store.readQuery({
|
||||
|
|
@ -111,7 +102,7 @@ export const constructContentComponentBookmarkMutation: (
|
|||
// const bookmarks = data.contentBlock.bookmarks;
|
||||
let bookmarks: any[];
|
||||
|
||||
if (bookmarked) {
|
||||
if (newBookmarkedValue) {
|
||||
const element = {
|
||||
note: null,
|
||||
uuid,
|
||||
|
|
@ -139,15 +130,30 @@ export const constructContentComponentBookmarkMutation: (
|
|||
query,
|
||||
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',
|
||||
updateContentBookmark: {
|
||||
__typename: 'UpdateContentBookmarkPayload',
|
||||
success: true,
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
return mutation;
|
||||
return { mutation, updateCurry, getVariables, optimisticResponse };
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue