Fix state of articleId preventing labels from saving on reader view
This commit is contained in:
@ -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,
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user