Fix the issue where the chore reappears after completion then becomes hidden again.
This commit is contained in:
@@ -154,6 +154,18 @@ export const useChoreActions = ({
|
|||||||
async (action, chore, extraData = {}) => {
|
async (action, chore, extraData = {}) => {
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case 'complete':
|
case 'complete':
|
||||||
|
// 1. Instantly hide the chore from the UI and Cache
|
||||||
|
setChores(prev => prev.filter(c => c.id !== chore.id))
|
||||||
|
setFilteredChores(prev => prev.filter(c => c.id !== chore.id))
|
||||||
|
|
||||||
|
queryClient.setQueriesData({ queryKey: ['chores'] }, oldData => {
|
||||||
|
if (!oldData || !oldData.res) return oldData;
|
||||||
|
return {
|
||||||
|
...oldData,
|
||||||
|
res: oldData.res.filter(c => c.id !== chore.id),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await MarkChoreComplete(
|
const response = await MarkChoreComplete(
|
||||||
chore.id,
|
chore.id,
|
||||||
@@ -162,10 +174,36 @@ export const useChoreActions = ({
|
|||||||
null,
|
null,
|
||||||
)
|
)
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
const data = await response.json()
|
// 2. Show the success notification with Undo
|
||||||
updateChoreInState(data.res, 'completed')
|
showSuccess({
|
||||||
|
message: 'Task completed',
|
||||||
|
undoAction: async () => {
|
||||||
|
try {
|
||||||
|
const undoResponse = await UndoChoreAction(chore.id)
|
||||||
|
if (undoResponse.ok) {
|
||||||
|
refetchChores()
|
||||||
|
showUndo({
|
||||||
|
title: 'Undo Successful',
|
||||||
|
message: 'Task completion has been undone.',
|
||||||
|
})
|
||||||
|
} else throw new Error('Failed to undo')
|
||||||
|
} catch (error) {
|
||||||
|
showError({
|
||||||
|
title: 'Undo Failed',
|
||||||
|
message: 'Unable to undo the action. Please try again.',
|
||||||
|
})
|
||||||
|
}
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
// 3. Fetch the fresh active list from the server silently
|
||||||
|
// (This brings in the next occurrence if recurring, without showing the completed one)
|
||||||
|
queryClient.invalidateQueries({ queryKey: ['chores'] })
|
||||||
|
} else {
|
||||||
|
refetchChores() // Network failed, revert to truth
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
refetchChores() // Network failed, revert to truth
|
||||||
if (error?.queued) {
|
if (error?.queued) {
|
||||||
showError({
|
showError({
|
||||||
title: 'Update Failed',
|
title: 'Update Failed',
|
||||||
|
|||||||
Reference in New Issue
Block a user