Fix state of articleId preventing labels from saving on reader view

This commit is contained in:
Jackson Harper
2023-06-29 15:03:38 -07:00
parent f10e1f667b
commit 2009ce1b72

View File

@ -13,7 +13,7 @@ export type LabelsDispatcher = (action: {
export const useSetPageLabels = (
articleId?: string
): [{ labels: Label[] }, LabelsDispatcher] => {
const saveLabels = (labels: Label[]) => {
const saveLabels = (labels: Label[], articleId: string) => {
;(async () => {
const labelIds = labels.map((l) => l.id)
if (articleId) {
@ -30,7 +30,8 @@ export const useSetPageLabels = (
const labelsReducer = (
state: {
labels: Label[]
throttledSave: (labels: Label[]) => void
articleId: string | undefined
throttledSave: (labels: Label[], articleId: string) => void
},
action: {
type: string
@ -51,8 +52,8 @@ export const useSetPageLabels = (
}
}
case 'SAVE': {
if (articleId) {
state.throttledSave(action.labels)
if (state.articleId) {
state.throttledSave(action.labels, state.articleId)
} else {
showErrorToast('Unable to update labels', {
position: 'bottom-right',
@ -69,11 +70,15 @@ export const useSetPageLabels = (
}
const debouncedSave = useCallback(
throttle((labels: Label[]) => saveLabels(labels), 2000),
throttle(
(labels: Label[], articleId: string) => saveLabels(labels, articleId),
2000
),
[]
)
const [labels, dispatchLabels] = useReducer(labelsReducer, {
labels: [],
articleId: articleId,
throttledSave: debouncedSave,
})