Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 

178 lignes
7.8 KiB

  1. "use client";
  2. import Stack from "@mui/material/Stack";
  3. import Box from "@mui/material/Box";
  4. import Card from "@mui/material/Card";
  5. import CardContent from "@mui/material/CardContent";
  6. import Grid from "@mui/material/Grid";
  7. import TextField from "@mui/material/TextField";
  8. import Typography from "@mui/material/Typography";
  9. import { useTranslation } from "react-i18next";
  10. import CardActions from "@mui/material/CardActions";
  11. import RestartAlt from "@mui/icons-material/RestartAlt";
  12. import Button from "@mui/material/Button";
  13. import { Controller, useFormContext } from "react-hook-form";
  14. import { CustomerFormInputs } from "@/app/api/customer/actions";
  15. import { CustomerType } from "@/app/api/customer";
  16. import { FormControl, InputLabel, MenuItem, Select } from "@mui/material";
  17. import ContactInfo from "./ContactInfo";
  18. import { useCallback } from "react";
  19. interface Props {
  20. customerTypes: CustomerType[],
  21. }
  22. const CustomerInfo: React.FC<Props> = ({
  23. customerTypes,
  24. }) => {
  25. const { t } = useTranslation();
  26. const {
  27. register,
  28. formState: { errors, defaultValues },
  29. control,
  30. reset,
  31. resetField,
  32. setValue
  33. } = useFormContext<CustomerFormInputs>();
  34. const resetCustomer = useCallback(() => {
  35. console.log(defaultValues)
  36. if (defaultValues !== undefined) {
  37. resetField("code")
  38. resetField("name")
  39. resetField("address")
  40. resetField("district")
  41. resetField("typeId")
  42. resetField("brNo")
  43. // setValue("code", defaultValues.code ?? "")
  44. // reset({
  45. // code: defaultValues.code,
  46. // name: defaultValues.name,
  47. // address: defaultValues.address,
  48. // district: defaultValues.district,
  49. // typeId: defaultValues.typeId,
  50. // brNo: defaultValues.brNo
  51. // })
  52. }
  53. }, [defaultValues])
  54. return (
  55. <>
  56. <Card sx={{ display: "block" }}>
  57. <CardContent component={Stack} spacing={4}>
  58. <Box>
  59. <Typography variant="overline" display="block" marginBlockEnd={1}>
  60. {t("Customer Info")}
  61. </Typography>
  62. <Grid container spacing={2} columns={{ xs: 6, sm: 12 }}>
  63. <Grid item xs={6}>
  64. <TextField
  65. label={`${t("Customer Code")}*`}
  66. fullWidth
  67. {...register("code", {
  68. required: true,
  69. })}
  70. error={Boolean(errors.code)}
  71. helperText={Boolean(errors.code) && (errors.code?.message ? t(errors.code.message) : t("Please input correct customer code"))}
  72. />
  73. </Grid>
  74. <Grid item xs={6}>
  75. <TextField
  76. label={`${t("Customer Name")}*`}
  77. fullWidth
  78. {...register("name", {
  79. required: true,
  80. })}
  81. error={Boolean(errors.name)}
  82. helperText={Boolean(errors.name) && t("Please input correct customer name")}
  83. />
  84. </Grid>
  85. <Grid item xs={6}>
  86. <TextField
  87. label={t("Customer Address")}
  88. fullWidth
  89. {...register("address")}
  90. />
  91. </Grid>
  92. <Grid item xs={6}>
  93. <TextField
  94. label={t("Customer District")}
  95. fullWidth
  96. {...register("district")}
  97. />
  98. </Grid>
  99. {/* <Grid item xs={6}>
  100. <TextField
  101. label={t("Customer Email")}
  102. fullWidth
  103. {...register("email", {
  104. pattern: /^([a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})$/,
  105. })}
  106. error={Boolean(errors.email)}
  107. helperText={Boolean(errors.email) && t("Please input correct customer email")}
  108. />
  109. </Grid>
  110. <Grid item xs={6}>
  111. <TextField
  112. label={t("Customer Phone")}
  113. fullWidth
  114. {...register("phone")}
  115. />
  116. </Grid>
  117. <Grid item xs={6}>
  118. <TextField
  119. label={t("Customer Contact Name")}
  120. fullWidth
  121. {...register("contactName")}
  122. />
  123. </Grid> */}
  124. <Grid item xs={6}>
  125. <FormControl fullWidth>
  126. <InputLabel>{t("Customer Type")}</InputLabel>
  127. <Controller
  128. defaultValue={customerTypes[0].id}
  129. control={control}
  130. name="typeId"
  131. render={({ field }) => (
  132. <Select label={t("Project Category")} {...field}>
  133. {customerTypes.map((type, index) => (
  134. <MenuItem
  135. key={`${type.id}-${index}`}
  136. value={type.id}
  137. >
  138. {t(type.name)}
  139. </MenuItem>
  140. ))}
  141. </Select>
  142. )}
  143. />
  144. </FormControl>
  145. </Grid>
  146. <Grid item xs={6}>
  147. <TextField
  148. label={t("Customer Br No.")}
  149. fullWidth
  150. {...register("brNo", {
  151. pattern: /[0-9]{8}/,
  152. })}
  153. error={Boolean(errors.brNo)}
  154. helperText={Boolean(errors.brNo) && t("Please input correct customer br no.")}
  155. />
  156. </Grid>
  157. </Grid>
  158. </Box>
  159. <CardActions sx={{ justifyContent: "flex-end" }}>
  160. <Button onClick={resetCustomer} variant="text" startIcon={<RestartAlt />}>
  161. {t("Reset")}
  162. </Button>
  163. </CardActions>
  164. </CardContent>
  165. </Card>
  166. <ContactInfo />
  167. </>
  168. );
  169. };
  170. export default CustomerInfo;