Skip to content

Commit

Permalink
Merge pull request #49 from fedellen/unstrike-all-fix
Browse files Browse the repository at this point in the history
fix: unstrike current list id instead of first
  • Loading branch information
fedellen authored Jan 4, 2024
2 parents 73f87a0 + 368c1da commit 6154a8e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
4 changes: 2 additions & 2 deletions web/src/components/sideMenu/SideMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type SideMenuProps = {
};

export default function SideMenu({ strikedItems }: SideMenuProps) {
const [{ sideMenuState }, dispatch] = useStateValue();
const [{ sideMenuState, currentListId }, dispatch] = useStateValue();

const handleAddItemClick = () => {
dispatch({
Expand All @@ -34,7 +34,7 @@ export default function SideMenu({ strikedItems }: SideMenuProps) {
const [strikeItems, strikeItemsSubmitting] = useStrikeItems();
const handleUnStrikeAllClick = async () => {
if (strikeItemsSubmitting) return;
await strikeItems(strikedItems);
await strikeItems(strikedItems, currentListId);
handleReturnClick();
};

Expand Down
57 changes: 30 additions & 27 deletions web/src/hooks/mutations/item/useStrikeItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,44 @@ import { sendNotification } from 'src/utils/dispatchActions';

export default function useStrikeItems() {
const [mutationSubmitting, setMutationSubmitting] = useState(false);
const [{ currentListId: listId }, dispatch] = useStateValue();
const [, dispatch] = useStateValue();
const [strikeItems] = useStrikeItemsMutation();
const mutationCooldown = useDelayedFunction(() =>
setMutationSubmitting(false)
);
const sendMutation = useCallback(async (itemNameArray: string[]) => {
if (mutationSubmitting) return;
setMutationSubmitting(true);
const sendMutation = useCallback(
async (itemNameArray: string[], listId: string) => {
if (mutationSubmitting) return;
setMutationSubmitting(true);

try {
const { data } = await strikeItems({
variables: {
data: {
itemNameArray,
listId
try {
const { data } = await strikeItems({
variables: {
data: {
itemNameArray,
listId
}
}
}
});
if (data?.strikeItems.errors) {
errorNotification(data.strikeItems.errors, dispatch);
mutationCooldown();
} else {
dispatch({
type: 'ADD_TO_UNDO',
payload: ['strikeItems', { itemNameArray, listId }]
});
if (data?.strikeItems.errors) {
errorNotification(data.strikeItems.errors, dispatch);
mutationCooldown();
} else {
dispatch({
type: 'ADD_TO_UNDO',
payload: ['strikeItems', { itemNameArray, listId }]
});
}
dispatch({ type: 'CLEAR_STATE' });
} catch (err) {
sendNotification(dispatch, [
'Connection to the server could not be established. Interacting with the list will not function offline.'
]);
dispatch({ type: 'CLEAR_STATE' });
}
dispatch({ type: 'CLEAR_STATE' });
} catch (err) {
sendNotification(dispatch, [
'Connection to the server could not be established. Interacting with the list will not function offline.'
]);
dispatch({ type: 'CLEAR_STATE' });
}
}, []);
},
[]
);

return [sendMutation, mutationSubmitting] as const;
}

0 comments on commit 6154a8e

Please sign in to comment.