Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 

64 linhas
2.3 KiB

  1. import { CreateProjectInputs } from "@/app/api/projects/actions";
  2. import { TaskGroup } from "@/app/api/tasks";
  3. import { moneyFormatter } from "@/app/utils/formatUtil";
  4. import { Divider, Stack, Typography } from "@mui/material";
  5. import React, { useCallback, useEffect, useMemo } from "react";
  6. import { useFormContext } from "react-hook-form";
  7. import { useTranslation } from "react-i18next";
  8. interface Props {
  9. taskGroups: TaskGroup[];
  10. }
  11. const ProjectTotalFee: React.FC<Props> = ({ taskGroups }) => {
  12. const { t } = useTranslation();
  13. const { watch, setError, clearErrors } = useFormContext<CreateProjectInputs>();
  14. const milestones = watch("milestones");
  15. const expectedTotalFee = watch("expectedProjectFee");
  16. let projectTotal = 0;
  17. useEffect(() => {
  18. console.log(Object.keys(milestones).reduce((acc, key) => acc + milestones[parseInt(key)].payments.reduce((acc2, value) => acc2 + value.amount, 0), 0))
  19. if (Object.keys(milestones).reduce((acc, key) => acc + milestones[parseInt(key)].payments.reduce((acc2, value) => acc2 + value.amount, 0), 0) !== expectedTotalFee) {
  20. setError("milestones", {message: "project total is not valid", type: "invalid"})
  21. } else {
  22. clearErrors("milestones")
  23. }
  24. }, [milestones])
  25. return (
  26. <Stack spacing={1}>
  27. {taskGroups.map((group, index) => {
  28. const payments = milestones[group.id]?.payments || [];
  29. const paymentTotal = payments.reduce((acc, p) => acc + p.amount, 0);
  30. projectTotal += paymentTotal;
  31. return (
  32. <Stack
  33. key={`${group.id}-${index}`}
  34. direction="row"
  35. justifyContent="space-between"
  36. >
  37. <Typography variant="subtitle2">{group.name}</Typography>
  38. <Typography>{moneyFormatter.format(paymentTotal)}</Typography>
  39. </Stack>
  40. );
  41. })}
  42. <Divider sx={{ paddingBlockStart: 2 }} />
  43. <Stack direction="row" justifyContent="space-between">
  44. <Typography variant="h6">{t("Project Total Fee")}</Typography>
  45. <Typography>{moneyFormatter.format(projectTotal)}</Typography>
  46. </Stack>
  47. {projectTotal !== expectedTotalFee && (
  48. <Typography variant="caption" color="warning.main" alignSelf="flex-end">
  49. {t("Project total fee should be same as the expected total fee!")}
  50. </Typography>
  51. )}
  52. </Stack>
  53. );
  54. };
  55. export default ProjectTotalFee;