Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 

160 wiersze
5.0 KiB

  1. "use client";
  2. import Check from "@mui/icons-material/Check";
  3. import Close from "@mui/icons-material/Close";
  4. import Button from "@mui/material/Button";
  5. import Stack from "@mui/material/Stack";
  6. import Print from '@mui/icons-material/Print';
  7. // import { CreateInvoiceInputs, saveInvoice } from "@/app/api/companys/actions";
  8. import { useRouter } from "next/navigation";
  9. import React, { useCallback, useState, useLayoutEffect } from "react";
  10. import { useTranslation } from "react-i18next";
  11. import {
  12. FieldErrors,
  13. FormProvider,
  14. SubmitErrorHandler,
  15. SubmitHandler,
  16. useForm,
  17. } from "react-hook-form";
  18. import { useSearchParams } from 'next/navigation'
  19. import { InvoiceResult } from "@/app/api/invoices";
  20. import { InvoiceInformation, fetchInvoiceInfoById, fetchProjectInvoiceById } from "@/app/api/invoices/actions";
  21. import InvoiceDetails from "./InvoiceDetails";
  22. import ProjectDetails from "./ProjectDetails";
  23. import ProjectTotalFee from "./ProjectTotalFee";
  24. import { timestampToDateString } from "@/app/utils/formatUtil";
  25. import dayjs from "dayjs";
  26. const CreateInvoice: React.FC = ({
  27. }) => {
  28. const { t } = useTranslation();
  29. const searchParams = useSearchParams()
  30. const router = useRouter()
  31. const [projectDetail, setProjectDetail] = useState<InvoiceResult>()
  32. const [invoiceDetail, setInvoiceDetail] = useState<InvoiceInformation>()
  33. const [serverError, setServerError] = useState("");
  34. // const { getValues } = useForm();
  35. const fetchProjectDetails = async () =>{
  36. const projectId = searchParams.get("id")
  37. try{
  38. if (projectId !== null && parseInt(projectId) > 0) {
  39. const projectDetail = await fetchProjectInvoiceById(parseInt(projectId))
  40. // console.log(projectDetail)
  41. const updatedPrijectDetail = projectDetail.map(detail => {
  42. return { ...detail, paymentMilestoneDate: timestampToDateString(detail.paymentMilestoneDate) }; // Update the age of person with id 2
  43. });
  44. setProjectDetail(updatedPrijectDetail[0])
  45. }
  46. } catch (error){
  47. console.log(error)
  48. setServerError(t("An error has occurred. Please try again later."));
  49. }
  50. }
  51. const fetchInvoiceDetails = async () =>{
  52. const projectId = searchParams.get("id")
  53. try{
  54. if (projectId !== null && parseInt(projectId) > 0) {
  55. const invoiceInfo = await fetchInvoiceInfoById(parseInt(projectId))
  56. console.log(invoiceInfo)
  57. const updatedInvoiceInfo = invoiceInfo.map(info => {
  58. return { ...info,
  59. invoiceDate: dayjs.unix(parseFloat(info.invoiceDate)/1000).format('YYYY/MM/DD'),
  60. dueDate: dayjs.unix(parseFloat(info.dueDate)/1000).format('YYYY/MM/DD')
  61. };
  62. });
  63. setInvoiceDetail(updatedInvoiceInfo[0])
  64. }
  65. } catch (error){
  66. console.log(error)
  67. setServerError(t("An error has occurred. Please try again later."));
  68. }
  69. }
  70. useLayoutEffect(() => {
  71. fetchProjectDetails()
  72. fetchInvoiceDetails()
  73. }, [])
  74. const handleCancel = () => {
  75. router.back();
  76. };
  77. const handlePrintout = () => {
  78. // const formData = getValues();
  79. console.log("Printing in Progress")
  80. }
  81. const onSubmit = useCallback<SubmitHandler<InvoiceResult>>(
  82. async (data) => {
  83. try {
  84. console.log(data);
  85. setServerError("");
  86. // console.log(JSON.stringify(data));
  87. // await saveCompany(data)
  88. // router.replace("/invoices");
  89. } catch (e) {
  90. setServerError(t("An error has occurred. Please try again later."));
  91. }
  92. },
  93. [router, t],
  94. );
  95. const onSubmitError = useCallback<SubmitErrorHandler<InvoiceResult>>(
  96. (errors) => {
  97. console.log(errors)
  98. },
  99. [],
  100. );
  101. const formProps = useForm<InvoiceResult>({
  102. defaultValues: {
  103. },
  104. });
  105. const errors = formProps.formState.errors;
  106. return(
  107. <FormProvider {...formProps}>
  108. <Stack
  109. spacing={2}
  110. component="form"
  111. onSubmit={formProps.handleSubmit(onSubmit, onSubmitError)}
  112. >
  113. {
  114. projectDetail && <ProjectDetails projectDetails={projectDetail}/>
  115. }
  116. {
  117. invoiceDetail && <InvoiceDetails invoiceinfo={invoiceDetail}/>
  118. }
  119. {
  120. <ProjectTotalFee />
  121. }
  122. <Stack direction="row" justifyContent="flex-end" gap={1}>
  123. <Button
  124. variant="outlined"
  125. startIcon={<Close />}
  126. onClick={handleCancel}
  127. >
  128. {t("Cancel")}
  129. </Button>
  130. <Button variant="contained" startIcon={<Check />} type="submit">
  131. {t("Confirm")}
  132. </Button>
  133. <Button
  134. variant="contained"
  135. color="secondary"
  136. startIcon={<Print />}
  137. onClick={handlePrintout}
  138. >
  139. {t("Generate Print Out")}
  140. </Button>
  141. </Stack>
  142. </Stack>
  143. </FormProvider>
  144. )
  145. }
  146. export default CreateInvoice;