Files
omnivore/packages/web/lib/networking/mutations/setLabelsForHighlight.ts
Jackson Harper f13fc9c35a Only save labels from the modal when the user finishes selection
This prevents creating issues by calling the setlabels mutation
too frequently if a user is rapidly changing labels, if that
happens a failure in the API could cause the user's changes
to be overwritten.
2022-12-21 12:29:34 +08:00

48 lines
1.2 KiB
TypeScript

import { gql } from 'graphql-request'
import { Label, labelFragment } from '../fragments/labelFragment'
import { gqlFetcher } from '../networkHelpers'
export type SetLabelsForHighlightResult = {
setLabelsForHighlight: SetLabelsForHighlight
}
type SetLabelsForHighlight = {
labels: Label[]
errorCodes?: unknown[]
}
export async function setLabelsForHighlight(
highlightId: string,
labelIds: string[]
): Promise<Label[] | undefined> {
const mutation = gql`
mutation SetLabelsForHighlight($input: SetLabelsForHighlightInput!) {
setLabelsForHighlight(input: $input) {
... on SetLabelsSuccess {
labels {
...LabelFields
}
}
... on SetLabelsError {
errorCodes
}
}
}
${labelFragment}
`
try {
const data = (await gqlFetcher(mutation, {
input: { highlightId, labelIds },
})) as SetLabelsForHighlightResult
console.log(' -- errorCodes', data.setLabelsForHighlight.errorCodes)
return data.setLabelsForHighlight.errorCodes
? undefined
: data.setLabelsForHighlight.labels
} catch (error) {
console.log('setLabelsForHighlightInput error', error)
return undefined
}
}