Fix not setting labels due to the undefined labels in search API response

This commit is contained in:
Hongbo Wu
2022-11-28 10:29:27 +08:00
parent e2dbb82b91
commit b1ef8b8e4b
2 changed files with 23 additions and 18 deletions

View File

@ -14,7 +14,7 @@ interface Edge {
interface Page {
id: string
labels: Label[]
labels?: Label[] // labels is optional in the API response
isArchived: boolean
readingProgressPercent: number
}

View File

@ -93,38 +93,43 @@ export const triggerActions = async (
rule.actions.forEach((action) => {
switch (action.type) {
case RuleActionType.AddLabel: {
const existingLabelIds = filteredPage.labels.map((label) => label.id)
const newLabelIds = action.params
if (newLabelIds.every((id) => existingLabelIds.includes(id))) {
// All labels are already set
return
}
const existingLabelIds =
filteredPage.labels?.map((label) => label.id) || []
const labelIdsToSet = [...existingLabelIds]
// combine existing labels with new labels in a set to avoid duplicates
const labelIds = new Set([...existingLabelIds, ...newLabelIds])
// combine existing labels with new labels to avoid duplicates
action.params.forEach((newLabelId) => {
if (!labelIdsToSet.includes(newLabelId)) {
labelIdsToSet.push(newLabelId)
}
})
actionPromises.push(
setLabels(apiEndpoint, authToken, data.id, Array.from(labelIds))
// call the api if it has new labels to set
return (
labelIdsToSet.length > existingLabelIds.length &&
actionPromises.push(
setLabels(apiEndpoint, authToken, data.id, labelIdsToSet)
)
)
break
}
case RuleActionType.Archive:
!filteredPage.isArchived &&
return (
!filteredPage.isArchived &&
actionPromises.push(archivePage(apiEndpoint, authToken, data.id))
break
)
case RuleActionType.MarkAsRead:
filteredPage.readingProgressPercent < 100 &&
return (
filteredPage.readingProgressPercent < 100 &&
actionPromises.push(markPageAsRead(apiEndpoint, authToken, data.id))
break
)
case RuleActionType.SendNotification:
actionPromises.push(
return actionPromises.push(
sendNotification(
apiEndpoint,
authToken,
'New page added to your feed'
)
)
break
}
})
}