168 lines
4.6 KiB
TypeScript
168 lines
4.6 KiB
TypeScript
|
|
import { useApi } from './useApi';
|
||
|
|
import { useLoader } from '../context/loaders/useLoader';
|
||
|
|
import { useToast } from '../context/toasts/useToast';
|
||
|
|
import { CheckItem } from '../types/kanban';
|
||
|
|
|
||
|
|
export function useChecklistMutations(cardId: number, onUpdate: () => void) {
|
||
|
|
const { createChecklist, deleteChecklist, createCheckItem, updateCheckItem, deleteCheckItem } =
|
||
|
|
useApi();
|
||
|
|
const { withLoader } = useLoader();
|
||
|
|
const { addNotification } = useToast();
|
||
|
|
|
||
|
|
const addChecklist = async (name: string, pos: number) => {
|
||
|
|
try {
|
||
|
|
await withLoader(() => createChecklist(cardId, { name, pos }), 'Creating checklist...');
|
||
|
|
onUpdate();
|
||
|
|
addNotification({
|
||
|
|
type: 'success',
|
||
|
|
title: 'Checklist Created',
|
||
|
|
message: 'Checklist has been created successfully.',
|
||
|
|
duration: 3000,
|
||
|
|
});
|
||
|
|
return true;
|
||
|
|
} catch (err) {
|
||
|
|
const errorMessage = err instanceof Error ? err.message : 'Failed to create checklist';
|
||
|
|
addNotification({
|
||
|
|
type: 'error',
|
||
|
|
title: 'Error',
|
||
|
|
message: errorMessage,
|
||
|
|
duration: 5000,
|
||
|
|
});
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
const removeChecklist = async (checklistId: number) => {
|
||
|
|
try {
|
||
|
|
await withLoader(() => deleteChecklist(checklistId), 'Deleting checklist...');
|
||
|
|
onUpdate();
|
||
|
|
addNotification({
|
||
|
|
type: 'success',
|
||
|
|
title: 'Checklist Deleted',
|
||
|
|
message: 'Checklist has been deleted successfully.',
|
||
|
|
duration: 3000,
|
||
|
|
});
|
||
|
|
return true;
|
||
|
|
} catch (err) {
|
||
|
|
const errorMessage = err instanceof Error ? err.message : 'Failed to delete checklist';
|
||
|
|
addNotification({
|
||
|
|
type: 'error',
|
||
|
|
title: 'Error',
|
||
|
|
message: errorMessage,
|
||
|
|
duration: 5000,
|
||
|
|
});
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
const addCheckItem = async (
|
||
|
|
checklistId: number,
|
||
|
|
name: string,
|
||
|
|
pos: number,
|
||
|
|
state: 'incomplete' | 'complete' = 'incomplete'
|
||
|
|
) => {
|
||
|
|
try {
|
||
|
|
await withLoader(() => createCheckItem(checklistId, { name, pos, state }), 'Adding item...');
|
||
|
|
onUpdate();
|
||
|
|
addNotification({
|
||
|
|
type: 'success',
|
||
|
|
title: 'Item Added',
|
||
|
|
message: 'Check item has been added successfully.',
|
||
|
|
duration: 3000,
|
||
|
|
});
|
||
|
|
return true;
|
||
|
|
} catch (err) {
|
||
|
|
const errorMessage = err instanceof Error ? err.message : 'Failed to add item';
|
||
|
|
addNotification({
|
||
|
|
type: 'error',
|
||
|
|
title: 'Error',
|
||
|
|
message: errorMessage,
|
||
|
|
duration: 5000,
|
||
|
|
});
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
const toggleCheckItem = async (item: CheckItem, currentState: 'incomplete' | 'complete') => {
|
||
|
|
console.log('item', item);
|
||
|
|
try {
|
||
|
|
const newState = currentState === 'incomplete' ? 'complete' : 'incomplete';
|
||
|
|
await withLoader(
|
||
|
|
() => updateCheckItem(item.id, { name: item.name, pos: 0, state: newState }),
|
||
|
|
'Updating item...'
|
||
|
|
);
|
||
|
|
onUpdate();
|
||
|
|
return true;
|
||
|
|
} catch (err) {
|
||
|
|
const errorMessage = err instanceof Error ? err.message : 'Failed to update item';
|
||
|
|
addNotification({
|
||
|
|
type: 'error',
|
||
|
|
title: 'Error',
|
||
|
|
message: errorMessage,
|
||
|
|
duration: 5000,
|
||
|
|
});
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
const editCheckItem = async (
|
||
|
|
itemId: number,
|
||
|
|
name: string,
|
||
|
|
pos: number,
|
||
|
|
state: 'incomplete' | 'complete'
|
||
|
|
) => {
|
||
|
|
try {
|
||
|
|
await withLoader(() => updateCheckItem(itemId, { name, pos, state }), 'Updating item...');
|
||
|
|
onUpdate();
|
||
|
|
addNotification({
|
||
|
|
type: 'success',
|
||
|
|
title: 'Item Updated',
|
||
|
|
message: 'Check item has been updated successfully.',
|
||
|
|
duration: 3000,
|
||
|
|
});
|
||
|
|
return true;
|
||
|
|
} catch (err) {
|
||
|
|
const errorMessage = err instanceof Error ? err.message : 'Failed to update item';
|
||
|
|
addNotification({
|
||
|
|
type: 'error',
|
||
|
|
title: 'Error',
|
||
|
|
message: errorMessage,
|
||
|
|
duration: 5000,
|
||
|
|
});
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
const removeCheckItem = async (itemId: number) => {
|
||
|
|
try {
|
||
|
|
await withLoader(() => deleteCheckItem(itemId), 'Deleting item...');
|
||
|
|
onUpdate();
|
||
|
|
addNotification({
|
||
|
|
type: 'success',
|
||
|
|
title: 'Item Deleted',
|
||
|
|
message: 'Check item has been deleted successfully.',
|
||
|
|
duration: 3000,
|
||
|
|
});
|
||
|
|
return true;
|
||
|
|
} catch (err) {
|
||
|
|
const errorMessage = err instanceof Error ? err.message : 'Failed to delete item';
|
||
|
|
addNotification({
|
||
|
|
type: 'error',
|
||
|
|
title: 'Error',
|
||
|
|
message: errorMessage,
|
||
|
|
duration: 5000,
|
||
|
|
});
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
return {
|
||
|
|
addChecklist,
|
||
|
|
removeChecklist,
|
||
|
|
addCheckItem,
|
||
|
|
toggleCheckItem,
|
||
|
|
editCheckItem,
|
||
|
|
removeCheckItem,
|
||
|
|
};
|
||
|
|
}
|