-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add confirmation modal before deleting a task status (#3309)
* add confirmation modal before deleteing a task status if used * update the status to 'open' after deleting a used status
- Loading branch information
Showing
15 changed files
with
159 additions
and
3 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
apps/web/lib/features/task-status/delete-status-confirmation-modal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { ITaskStatusItemList, TaskStatusEnum } from '@/app/interfaces'; | ||
import { useTaskStatus, useTeamTasks } from '@app/hooks'; | ||
import { Button, Card, Modal, Text } from 'lib/components'; | ||
import { useTranslations } from 'next-intl'; | ||
import { useCallback, useMemo } from 'react'; | ||
|
||
interface DeleteTaskStatusModalProps { | ||
open: boolean; | ||
closeModal: () => void; | ||
status: ITaskStatusItemList; | ||
onCancel: () => void; | ||
} | ||
|
||
/** | ||
* A confirmation modal before deleting a status that is used by at least one task in the team. | ||
* | ||
* @param {Object} props - The props Object | ||
* @param {boolean} props.open - If true open the modal otherwise close the modal | ||
* @param {() => void} props.closeModal - A function to close the modal | ||
* @param {ITaskStatusItemList} props.status - The status object to be deleted | ||
* @param {() => void} props.onCancel - Callback function when deletion is cancelled | ||
* | ||
* @returns {JSX.Element} The modal element | ||
*/ | ||
export function DeleteTaskStatusConfirmationModal(props: DeleteTaskStatusModalProps) { | ||
const { closeModal, open, status, onCancel } = props; | ||
const { deleteTaskStatus, deleteTaskStatusLoading } = useTaskStatus(); | ||
const t = useTranslations(); | ||
const { tasks, updateTask } = useTeamTasks(); | ||
|
||
// Filter tasks that are using the current status | ||
const tasksUsingStatus = useMemo(() => tasks.filter((task) => task.status === status.name), [tasks, status.name]); | ||
|
||
const handleCloseModal = useCallback(() => { | ||
onCancel(); | ||
closeModal(); | ||
}, [closeModal, onCancel]); | ||
|
||
// Function to handle task status deletion | ||
const handleDeleteTaskStatus = useCallback(async () => { | ||
if (!status.id) { | ||
console.error('Status ID is not provided.'); | ||
return; | ||
} | ||
|
||
try { | ||
// Update each task that uses the current status | ||
const updatePromises = tasksUsingStatus.map((task) => updateTask({ ...task, status: TaskStatusEnum.OPEN })); | ||
|
||
await Promise.all(updatePromises); | ||
|
||
// Delete the task status after updating related tasks | ||
await deleteTaskStatus(status.id); | ||
} catch (error) { | ||
console.error('Error while deleting task status:', error); | ||
} finally { | ||
handleCloseModal(); | ||
} | ||
}, [deleteTaskStatus, handleCloseModal, status, tasksUsingStatus, updateTask]); | ||
|
||
return ( | ||
<Modal | ||
isOpen={open} | ||
closeModal={closeModal} | ||
className="w-[98%] md:w-[530px] relative" | ||
showCloseIcon={false} | ||
aria-labelledby="delete-status-modal-title" | ||
aria-describedby="delete-status-modal-description" | ||
> | ||
<Card className="w-full" shadow="custom"> | ||
<div className="w-full flex flex-col justify-between gap-6"> | ||
<Text.Heading as="h5" id="delete-status-modal-title" className="mb-3 text-center"> | ||
{t('pages.taskStatus.DELETE_STATUS_CONFIRMATION', { statusName: status.name })} | ||
</Text.Heading> | ||
<div className="w-full flex items-center justify-evenly"> | ||
<Button | ||
disabled={deleteTaskStatusLoading} | ||
variant="outline" | ||
type="button" | ||
onClick={handleCloseModal} | ||
className="rounded-md font-light text-md dark:text-white dark:bg-slate-700 dark:border-slate-600" | ||
> | ||
{t('common.NO')} | ||
</Button> | ||
<Button | ||
loading={deleteTaskStatusLoading} | ||
disabled={deleteTaskStatusLoading} | ||
onClick={handleDeleteTaskStatus} | ||
variant="primary" | ||
type="submit" | ||
className=" rounded-md font-light text-md dark:text-white" | ||
> | ||
{t('common.YES')} | ||
</Button> | ||
</div> | ||
</div> | ||
</Card> | ||
</Modal> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters