FPSMS-frontend
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

240 строки
10 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 { Controller, useFieldArray, useFormContext } from "react-hook-form";
  11. import Link from "next/link";
  12. import React, { SyntheticEvent, useCallback, useMemo, useState } from "react";
  13. import MailField from "../MailField/MailField";
  14. import { MailSave } from "@/app/api/mail/actions";
  15. import { MailTemplate } from "@/app/api/mail";
  16. import { isEmpty, keys } from "lodash";
  17. import { Autocomplete, ListSubheader, MenuItem, UseAutocompleteProps } from "@mui/material";
  18. interface Props {
  19. isActive: boolean;
  20. }
  21. interface OptionType {
  22. value: number,
  23. label: string,
  24. group: string,
  25. }
  26. type fieldKeys = keyof MailTemplate
  27. const TemplateDetails: React.FC<Props> = ({ isActive }) => {
  28. const { t } = useTranslation();
  29. // const disabledFields: fieldKeys[] = []
  30. // const requiredFields: fieldKeys[] = []
  31. // const skipFields: fieldKeys[] = ["code", "subjectEng", "contentEng"]
  32. const [selectedIndex, setSelectedIndex] = useState(0)
  33. const {
  34. register,
  35. formState: { errors },
  36. control,
  37. watch,
  38. getValues
  39. } = useFormContext<MailSave>();
  40. const {
  41. fields,
  42. } = useFieldArray({
  43. control,
  44. name: "templates"
  45. })
  46. const options: OptionType[] = useMemo(() => (
  47. fields.map((field, index) => {
  48. return {
  49. value: index,
  50. label: `${field.code} - ${field.description}`,
  51. group: field.type
  52. } as OptionType
  53. })
  54. ), [fields])
  55. const makeAutocompleteChangeHandler = useCallback(() => {
  56. return (e: SyntheticEvent, newValue: OptionType) => {
  57. setSelectedIndex(() => newValue.value);
  58. };
  59. }, []);
  60. // const renderFieldsBySwitchCases = useCallback(() => {
  61. // const fieldNames = keys(fields[0]) as fieldKeys[];
  62. // return fieldNames
  63. // .filter((name) => !skipFields.includes(name))
  64. // .map((name: fieldKeys, index: number) => {
  65. // switch (name) {
  66. // case "contentCht":
  67. // return (
  68. // <Grid item xs={12}>
  69. // <Controller
  70. // control={control}
  71. // name={`templates.${selectedIndex}.contentCht`}
  72. // render={({ field }) => (
  73. // <MailField
  74. // content={field.value}
  75. // onChange={field.onChange}
  76. // error={Boolean(errors.templates?.[selectedIndex]?.contentCht)}
  77. // />
  78. // )}
  79. // rules={{
  80. // required: requiredFields.includes(name),
  81. // }}
  82. // />
  83. // </Grid>
  84. // )
  85. // default:
  86. // return (
  87. // <Grid item xs={8}>
  88. // <TextField
  89. // label={t(name)}
  90. // fullWidth
  91. // {...register(`templates.${selectedIndex}.cc`),
  92. // {
  93. // required: requiredFields.includes(name)
  94. // }}
  95. // />
  96. // </Grid>
  97. // )
  98. // }
  99. // })
  100. // }, [fields])
  101. return (
  102. <Card sx={{ display: isActive ? "block" : "none" }}>
  103. <CardContent component={Stack} spacing={4}>
  104. <Box>
  105. <Typography variant="overline" display="block" marginBlockEnd={1}>
  106. {t("Template")}
  107. </Typography>
  108. <Grid container spacing={2} columns={{ xs: 6, sm: 12 }}>
  109. <Grid item xs={8}>
  110. <Autocomplete
  111. disableClearable
  112. options={options}
  113. value={options[selectedIndex]}
  114. onChange={makeAutocompleteChangeHandler}
  115. groupBy={(option) => (
  116. option.group && option.group.trim() !== '' ? option.group : 'Ungrouped'
  117. )}
  118. renderGroup={(params) => (
  119. <React.Fragment>
  120. <ListSubheader>{params.group}</ListSubheader>
  121. {params.children}
  122. </React.Fragment>
  123. )}
  124. renderOption={(
  125. params: React.HTMLAttributes<HTMLLIElement> & { key?: React.Key },
  126. option,
  127. { selected },
  128. ) => {
  129. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  130. const { key, ...rest } = params;
  131. return (
  132. <MenuItem
  133. {...rest}
  134. disableRipple
  135. value={option.value}
  136. >
  137. {option.label}
  138. </MenuItem>
  139. );
  140. }}
  141. renderInput={(params) => <TextField {...params} variant="outlined" label={t("Select Template (View By Code - Description)")} />}
  142. />
  143. </Grid>
  144. <Grid item xs={8}>
  145. <TextField
  146. label={t("Code")}
  147. fullWidth
  148. {...register(`templates.${selectedIndex}.code`, {
  149. required: "Code is required!"
  150. })}
  151. required
  152. error={Boolean(errors.templates?.[selectedIndex]?.code)}
  153. />
  154. </Grid>
  155. <Grid item xs={8}>
  156. <TextField
  157. label={t("Description")}
  158. fullWidth
  159. {...register(`templates.${selectedIndex}.description`, {
  160. required: "Description is required!"
  161. })}
  162. required
  163. error={Boolean(errors.templates?.[selectedIndex]?.description)}
  164. />
  165. </Grid>
  166. {/* <Grid item xs={8}>
  167. <TextField
  168. label={t("To")}
  169. fullWidth
  170. {...register(`templates.${selectedIndex}.to`, {
  171. required: "To is required!"
  172. })}
  173. required
  174. error={Boolean(errors.templates?.[selectedIndex]?.to)}
  175. />
  176. </Grid>
  177. <Grid item xs={8}>
  178. <TextField
  179. label={t("Cc")}
  180. fullWidth
  181. {...register(`templates.${selectedIndex}.cc`)}
  182. />
  183. </Grid>
  184. <Grid item xs={8}>
  185. <TextField
  186. label={t("Required Params (split by ',')")}
  187. fullWidth
  188. {...register(`templates.${selectedIndex}.params`)}
  189. />
  190. </Grid> */}
  191. <Grid item xs={8}>
  192. <TextField
  193. label={t("Subject CHT")}
  194. fullWidth
  195. {...register(`templates.${selectedIndex}.subjectCht`,
  196. {
  197. required: "Mail Subject is required!",
  198. })}
  199. required
  200. error={Boolean(errors.templates?.[selectedIndex]?.subjectCht)}
  201. />
  202. </Grid>
  203. <Grid item xs={12}>
  204. <Controller
  205. control={control}
  206. name={`templates.${selectedIndex}.contentCht`}
  207. render={({ field }) => (
  208. <MailField
  209. content={field.value}
  210. onChange={field.onChange}
  211. error={Boolean(errors.templates?.[selectedIndex]?.contentCht)}
  212. />
  213. )}
  214. rules={{
  215. required: "Mail Content is required!",
  216. }}
  217. />
  218. </Grid>
  219. </Grid>
  220. </Box>
  221. </CardContent>
  222. </Card>
  223. );
  224. };
  225. export default TemplateDetails;