"use client"; import Card from "@mui/material/Card"; import CardContent from "@mui/material/CardContent"; import Grid from "@mui/material/Grid"; import Typography from "@mui/material/Typography"; import { useTranslation } from "react-i18next"; import TransferList from "../TransferList"; import Button from "@mui/material/Button"; import React, { useCallback, useMemo, useState } from "react"; import CardActions from "@mui/material/CardActions"; import RestartAlt from "@mui/icons-material/RestartAlt"; import FormControl from "@mui/material/FormControl"; import Select, { SelectChangeEvent } from "@mui/material/Select"; import MenuItem from "@mui/material/MenuItem"; import InputLabel from "@mui/material/InputLabel"; import { Task, TaskTemplate } from "@/app/api/tasks"; import { useFormContext } from "react-hook-form"; import { CreateProjectInputs } from "@/app/api/projects/actions"; import isNumber from "lodash/isNumber"; import intersectionWith from "lodash/intersectionWith"; interface Props { allTasks: Task[]; taskTemplates: TaskTemplate[]; isActive: boolean; } const TaskSetup: React.FC = ({ allTasks: tasks, taskTemplates, isActive, }) => { const { t } = useTranslation(); const { setValue, watch } = useFormContext(); const currentTaskGroups = watch("taskGroups"); const currentTaskIds = Object.values(currentTaskGroups).reduce( (acc, group) => { return [...acc, ...group.taskIds]; }, [], ); const onReset = useCallback(() => { setValue("taskGroups", {}); }, [setValue]); const [selectedTaskTemplateId, setSelectedTaskTemplateId] = useState< "All" | number >("All"); const onSelectTaskTemplate = useCallback( (e: SelectChangeEvent) => { if (e.target.value === "All" || isNumber(e.target.value)) { setSelectedTaskTemplateId(e.target.value); // onReset(); } }, [onReset], ); const items = useMemo(() => { const taskList = selectedTaskTemplateId === "All" ? tasks : taskTemplates.find( (template) => template.id === selectedTaskTemplateId, )?.tasks || tasks; return taskList.map((t) => ({ id: t.id, label: t.name, group: t.taskGroup, })); }, [tasks, selectedTaskTemplateId, taskTemplates]); const selectedItems = useMemo(() => { return intersectionWith( tasks, currentTaskIds, (task, taskId) => task.id === taskId, ).map((t) => ({ id: t.id, label: t.name, group: t.taskGroup })); }, [currentTaskIds, tasks]); return ( {t("Task List Setup")} {t("Task List Source")} label={t("Task List Source")} value={selectedTaskTemplateId} onChange={onSelectTaskTemplate} > {t("All tasks")} {taskTemplates.map((template, index) => ( {template.name} ))} { const newTaskGroups = selectedTasks.reduce< CreateProjectInputs["taskGroups"] >((acc, item) => { if (!item.group) { // TODO: this should not happen (all tasks are part of a group) return acc; } if (!acc[item.group.id]) { return { ...acc, [item.group.id]: { taskIds: [item.id], percentAllocation: currentTaskGroups[item.group.id]?.percentAllocation || 0, }, }; } return { ...acc, [item.group.id]: { ...acc[item.group.id], taskIds: [...acc[item.group.id].taskIds, item.id], }, }; }, {}); setValue("taskGroups", newTaskGroups); }} allItemsLabel={t("Task Pool")} selectedItemsLabel={t("Project Task List")} /> ); }; export default TaskSetup;