import { CreateProjectInputs } from "@/app/api/projects/actions"; import { TaskGroup } from "@/app/api/tasks"; import { moneyFormatter } from "@/app/utils/formatUtil"; import { Divider, Stack, Typography } from "@mui/material"; import React, { useCallback, useEffect, useMemo } from "react"; import { useFormContext } from "react-hook-form"; import { useTranslation } from "react-i18next"; interface Props { taskGroups: TaskGroup[]; } const ProjectTotalFee: React.FC = ({ taskGroups }) => { const { t } = useTranslation(); const { watch, setError, clearErrors } = useFormContext(); const milestones = watch("milestones"); const expectedTotalFee = watch("expectedProjectFee"); let projectTotal = 0; useEffect(() => { console.log(Object.keys(milestones).reduce((acc, key) => acc + milestones[parseInt(key)].payments.reduce((acc2, value) => acc2 + value.amount, 0), 0)) if (Object.keys(milestones).reduce((acc, key) => acc + milestones[parseInt(key)].payments.reduce((acc2, value) => acc2 + value.amount, 0), 0) !== expectedTotalFee) { setError("milestones", {message: "project total is not valid", type: "invalid"}) } else { clearErrors("milestones") } }, [milestones]) return ( {taskGroups.map((group, index) => { const payments = milestones[group.id]?.payments || []; const paymentTotal = payments.reduce((acc, p) => acc + p.amount, 0); projectTotal += paymentTotal; return ( {group.name} {moneyFormatter.format(paymentTotal)} ); })} {t("Project Total Fee")} {moneyFormatter.format(projectTotal)} {projectTotal !== expectedTotalFee && ( {t("Project total fee should be same as the expected total fee!")} )} ); }; export default ProjectTotalFee;