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.
 
 

67 lines
2.2 KiB

  1. "use client"
  2. import { ImportTestingForm, testImportPo } from "@/app/api/settings/importTesting/actions";
  3. import { Card, CardContent, Grid, Stack, Typography } from "@mui/material";
  4. import React, { BaseSyntheticEvent, FormEvent, useCallback, useState } from "react";
  5. import { FormProvider, SubmitErrorHandler, useForm } from "react-hook-form";
  6. import { useTranslation } from "react-i18next";
  7. import ImportPo from "./ImportPo";
  8. import { ImportPoForm } from "@/app/api/settings/importTesting/actions";
  9. interface Props {
  10. }
  11. const ImportTesting: React.FC<Props> = ({
  12. }) => {
  13. const { t } = useTranslation()
  14. const [isLoading, setIsLoading] = useState(false)
  15. const formProps = useForm<ImportTestingForm>()
  16. const onSubmit = useCallback(async (data: ImportTestingForm, event?: BaseSyntheticEvent) => {
  17. const buttonId = (event?.nativeEvent as SubmitEvent).submitter?.id
  18. console.log(data.po)
  19. switch (buttonId) {
  20. case "importPo":
  21. setIsLoading(() => true)
  22. const response = await testImportPo(data.po)
  23. console.log(response)
  24. if (response) {
  25. setIsLoading(() => false)
  26. }
  27. break;
  28. default:
  29. break;
  30. }
  31. }, [])
  32. const onSubmitError = useCallback<SubmitErrorHandler<ImportTestingForm>>(
  33. (errors) => {
  34. console.log(errors)
  35. },
  36. [],
  37. );
  38. return (
  39. <Card>
  40. <CardContent sx={{ display: "flex", flexDirection: "column", gap: 1 }}>
  41. <Typography variant="overline">{t("Status: ")}{isLoading ? t("Importing...") : t("Ready to import")}</Typography>
  42. <FormProvider {...formProps}>
  43. <Stack
  44. spacing={2}
  45. component={"form"}
  46. onSubmit={formProps.handleSubmit(onSubmit, onSubmitError)}
  47. >
  48. <Grid container>
  49. <ImportPo />
  50. </Grid>
  51. </Stack>
  52. </FormProvider>
  53. </CardContent>
  54. </Card>
  55. )
  56. }
  57. export default ImportTesting;