|
|
@@ -16,9 +16,10 @@ 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 { CreateProjectInputs, ManhourAllocation } from "@/app/api/projects/actions"; |
|
|
|
import isNumber from "lodash/isNumber"; |
|
|
|
import intersectionWith from "lodash/intersectionWith"; |
|
|
|
import { difference } from "lodash"; |
|
|
|
|
|
|
|
interface Props { |
|
|
|
allTasks: Task[]; |
|
|
@@ -32,11 +33,11 @@ const TaskSetup: React.FC<Props> = ({ |
|
|
|
isActive, |
|
|
|
}) => { |
|
|
|
const { t } = useTranslation(); |
|
|
|
const { setValue, watch } = useFormContext<CreateProjectInputs>(); |
|
|
|
const { setValue, watch, clearErrors, setError } = useFormContext<CreateProjectInputs>(); |
|
|
|
const currentTaskGroups = watch("taskGroups"); |
|
|
|
const currentTaskIds = Object.values(currentTaskGroups).reduce<Task["id"][]>( |
|
|
|
(acc, group) => { |
|
|
|
return [...acc, ...group.taskIds]; |
|
|
|
return group.taskIds && group.taskIds.length > 0 ? [...acc, ...group.taskIds] : acc; |
|
|
|
}, |
|
|
|
[], |
|
|
|
); |
|
|
@@ -59,12 +60,46 @@ const TaskSetup: React.FC<Props> = ({ |
|
|
|
); |
|
|
|
|
|
|
|
const items = useMemo(() => { |
|
|
|
const selectedTaskTemplate = taskTemplates.find( |
|
|
|
(template) => template.id === selectedTaskTemplateId, |
|
|
|
) |
|
|
|
|
|
|
|
if (selectedTaskTemplateId !== "All") { |
|
|
|
// update the "manhour allocation by grade" by task template |
|
|
|
const updatedManhourPercentageByGrade: ManhourAllocation = watch("manhourPercentageByGrade") |
|
|
|
selectedTaskTemplate?.gradeAllocations.forEach((gradeAllocation) => { |
|
|
|
updatedManhourPercentageByGrade[gradeAllocation.grade.id] = gradeAllocation?.percentage |
|
|
|
}) |
|
|
|
|
|
|
|
setValue("manhourPercentageByGrade", updatedManhourPercentageByGrade) |
|
|
|
if (Object.values(updatedManhourPercentageByGrade).reduce((acc, value) => acc + value, 0) === 100) clearErrors("manhourPercentageByGrade") |
|
|
|
else setError("manhourPercentageByGrade", {message: "manhourPercentageByGrade value is not valid", type: "invalid"}) |
|
|
|
|
|
|
|
// update the "manhour allocation by grade by stage" by task template |
|
|
|
const updatedTaskGroups = watch("taskGroups") |
|
|
|
const taskGroupsKeys = Object.keys(updatedTaskGroups) |
|
|
|
selectedTaskTemplate?.groupAllocations.forEach((groupAllocation) => { |
|
|
|
const taskGroupId = groupAllocation.taskGroup.id |
|
|
|
if(taskGroupsKeys.includes(taskGroupId.toString())) { |
|
|
|
updatedTaskGroups[taskGroupId] = {...updatedTaskGroups[taskGroupId], percentAllocation: groupAllocation?.percentage} |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
|
const percentageToZeroGroupIds = difference(taskGroupsKeys.map(key => parseFloat(key)), selectedTaskTemplate?.groupAllocations.map(groupAllocation => groupAllocation.taskGroup.id)!!) |
|
|
|
percentageToZeroGroupIds.forEach((percentageToZeroGroupId) => { |
|
|
|
updatedTaskGroups[percentageToZeroGroupId] = {...updatedTaskGroups[percentageToZeroGroupId], percentAllocation: 0} |
|
|
|
}) |
|
|
|
|
|
|
|
setValue("taskGroups", updatedTaskGroups) |
|
|
|
if (Object.values(updatedTaskGroups).reduce((acc, value) => acc + value.percentAllocation, 0) === 100) clearErrors("taskGroups") |
|
|
|
else setError("taskGroups", {message: "Task Groups value is not invalid", type: "invalid"}) |
|
|
|
} |
|
|
|
|
|
|
|
const taskList = |
|
|
|
selectedTaskTemplateId === "All" |
|
|
|
? tasks |
|
|
|
: taskTemplates.find( |
|
|
|
(template) => template.id === selectedTaskTemplateId, |
|
|
|
)?.tasks || tasks; |
|
|
|
: selectedTaskTemplate?.tasks || tasks; |
|
|
|
|
|
|
|
|
|
|
|
return taskList.map((t) => ({ |
|
|
|
id: t.id, |
|
|
|