2026-02-26 14:12:19 +00:00
|
|
|
import { useApi } from './useApi';
|
|
|
|
|
import { useLoader } from '../context/loaders/useLoader';
|
|
|
|
|
import { useToast } from '../context/toasts/useToast';
|
|
|
|
|
|
2026-02-27 07:53:36 +00:00
|
|
|
export function useListMutations(boardId: number, onUpdate: () => void) {
|
2026-02-26 14:12:19 +00:00
|
|
|
const { createList, updateList, deleteList } = useApi();
|
|
|
|
|
const { withLoader } = useLoader();
|
|
|
|
|
const { addNotification } = useToast();
|
|
|
|
|
|
|
|
|
|
const createNewList = async (name: string, pos: number) => {
|
|
|
|
|
try {
|
|
|
|
|
const data = await withLoader(() => createList(boardId, { name, pos }), 'Creating list...');
|
2026-02-27 07:53:36 +00:00
|
|
|
onUpdate();
|
2026-02-26 14:12:19 +00:00
|
|
|
addNotification({
|
|
|
|
|
type: 'success',
|
|
|
|
|
title: 'List Created',
|
|
|
|
|
message: `List "${data.name}" created successfully.`,
|
|
|
|
|
duration: 3000,
|
|
|
|
|
});
|
|
|
|
|
return data;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
const errorMessage = err instanceof Error ? err.message : 'Failed to create list';
|
|
|
|
|
addNotification({
|
|
|
|
|
type: 'error',
|
|
|
|
|
title: 'Error Creating List',
|
|
|
|
|
message: errorMessage,
|
|
|
|
|
duration: 5000,
|
|
|
|
|
});
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const updateExistingList = async (listId: number, data: { name: string; pos: number }) => {
|
|
|
|
|
try {
|
|
|
|
|
const updated = await withLoader(
|
|
|
|
|
() => updateList(listId, { ...data, closed: false }),
|
|
|
|
|
'Updating list...'
|
|
|
|
|
);
|
2026-02-27 19:38:38 +00:00
|
|
|
onUpdate(); // Refresh board data after update
|
2026-02-26 14:12:19 +00:00
|
|
|
addNotification({
|
|
|
|
|
type: 'success',
|
|
|
|
|
title: 'List Updated',
|
|
|
|
|
message: `List "${updated.name}" updated successfully.`,
|
|
|
|
|
duration: 3000,
|
|
|
|
|
});
|
|
|
|
|
return updated;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
const errorMessage = err instanceof Error ? err.message : 'Failed to update list';
|
|
|
|
|
addNotification({
|
|
|
|
|
type: 'error',
|
|
|
|
|
title: 'Error Updating List',
|
|
|
|
|
message: errorMessage,
|
|
|
|
|
duration: 5000,
|
|
|
|
|
});
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const removeList = async (listId: number) => {
|
|
|
|
|
try {
|
|
|
|
|
await withLoader(() => deleteList(listId), 'Deleting list...');
|
|
|
|
|
addNotification({
|
|
|
|
|
type: 'success',
|
|
|
|
|
title: 'List Deleted',
|
|
|
|
|
message: 'List deleted successfully.',
|
|
|
|
|
duration: 3000,
|
|
|
|
|
});
|
2026-02-27 19:38:38 +00:00
|
|
|
onUpdate(); // Refresh board data after delete
|
2026-02-26 14:12:19 +00:00
|
|
|
} catch (err) {
|
|
|
|
|
const errorMessage = err instanceof Error ? err.message : 'Failed to delete list';
|
|
|
|
|
addNotification({
|
|
|
|
|
type: 'error',
|
|
|
|
|
title: 'Error Deleting List',
|
|
|
|
|
message: errorMessage,
|
|
|
|
|
duration: 5000,
|
|
|
|
|
});
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
createList: createNewList,
|
|
|
|
|
updateList: updateExistingList,
|
|
|
|
|
deleteList: removeList,
|
|
|
|
|
};
|
|
|
|
|
}
|