FPSMS-frontend
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

86 lines
2.7 KiB

  1. import { JoDetail } from "@/app/api/jo";
  2. import { Box, Button, Stack, Typography } from "@mui/material";
  3. import { useMemo } from "react";
  4. import { useFormContext } from "react-hook-form";
  5. import { useTranslation } from "react-i18next";
  6. import StartIcon from "@mui/icons-material/Start";
  7. import PlayCircleFilledWhiteIcon from '@mui/icons-material/PlayCircleFilledWhite';
  8. type Props = {
  9. handleRelease: () => void;
  10. handleStart: () => void;
  11. };
  12. interface ErrorEntry {
  13. qtyErr: boolean;
  14. scanErr: boolean;
  15. }
  16. const ActionButtons: React.FC<Props> = ({
  17. handleRelease,
  18. handleStart
  19. }) => {
  20. const { t } = useTranslation("jo");
  21. const { watch } = useFormContext<JoDetail>();
  22. const status = useMemo(() => {
  23. return watch("status").toLowerCase()
  24. }, [watch("status")])
  25. const pickLines = useMemo(() => {
  26. return watch("pickLines")
  27. }, [watch("pickLines")])
  28. // Check Error Handling (e.g. start jo)
  29. const errors: ErrorEntry = useMemo(() => {
  30. let qtyErr = false;
  31. let scanErr = false;
  32. pickLines.forEach((line) => {
  33. if (!qtyErr) {
  34. const pickedQty = line.pickedLotNo?.reduce((acc, cur) => acc + cur.qty, 0) ?? 0
  35. qtyErr = pickedQty > 0 && pickedQty >= line.reqQty
  36. }
  37. if (!scanErr) {
  38. scanErr = line.pickedLotNo?.some((lotNo) => Boolean(lotNo.isScanned) === false) ?? false // default false
  39. }
  40. })
  41. return {
  42. qtyErr: qtyErr,
  43. scanErr: scanErr
  44. }
  45. }, [pickLines])
  46. return (
  47. <Stack direction="row" justifyContent="flex-start" gap={1}>
  48. {status === "planning" && (
  49. <Button
  50. variant="outlined"
  51. startIcon={<StartIcon />}
  52. onClick={handleRelease}
  53. >
  54. {t("Release")}
  55. </Button>
  56. )}
  57. {status === "pending" && (
  58. <Box sx={{ flexDirection: 'column'}}>
  59. <Button
  60. variant="outlined"
  61. startIcon={<PlayCircleFilledWhiteIcon />}
  62. onClick={handleStart}
  63. disabled={errors.qtyErr || errors.scanErr}
  64. >
  65. {t("Start Job Order")}
  66. </Button>
  67. {errors.scanErr && (<Typography variant="h3" color="error">{t("Please scan the item qr code.")}</Typography>)}
  68. {errors.qtyErr && (<Typography variant="h3" color="error">{t("Please make sure the qty is enough.")}</Typography>)}
  69. </Box>
  70. )}
  71. </Stack>
  72. )
  73. }
  74. export default ActionButtons;