FPSMS-frontend
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 

161 righe
5.5 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 from "react";
  13. import MailField from "../MailField/MailField";
  14. import { MailSetting } from "@/app/api/mail";
  15. import { MailSave } from "@/app/api/mail/actions";
  16. import { Checkbox, IconButton, InputAdornment } from "@mui/material";
  17. import { Visibility, VisibilityOff } from "@mui/icons-material";
  18. interface Props {
  19. isActive: boolean;
  20. }
  21. const SettingDetails: React.FC<Props> = ({ isActive }) => {
  22. const requiredFields = ["host", "port", "username"];
  23. const { t } = useTranslation();
  24. const {
  25. register,
  26. formState: { errors },
  27. control,
  28. watch,
  29. } = useFormContext<MailSave>();
  30. const { fields } = useFieldArray({
  31. control,
  32. name: "settings",
  33. });
  34. const [showSMTPPassword, setShowSMTPPassword] = React.useState(false);
  35. const handleClickShowPassword = () => setShowSMTPPassword((show) => !show);
  36. const handleMouseDownPassword = (
  37. event: React.MouseEvent<HTMLButtonElement>,
  38. ) => {
  39. event.preventDefault();
  40. };
  41. return (
  42. <Card sx={{ display: isActive ? "block" : "none" }}>
  43. <CardContent component={Stack} spacing={4}>
  44. <Box>
  45. <Typography variant="overline" display="block" marginBlockEnd={1}>
  46. {t("Settings")}
  47. </Typography>
  48. {fields.map((field, index) => (
  49. <Grid
  50. container
  51. key={"row-" + index}
  52. justifyContent="flex-start"
  53. alignItems="center"
  54. spacing={2}
  55. columns={{ xs: 6, sm: 12 }}
  56. sx={{ mt: 1 }}
  57. >
  58. <Grid item key={"col-1-" + index} xs={4}>
  59. <Typography variant="body2">{`${t(field.name)}${
  60. requiredFields.some((name) =>
  61. field.name.toLowerCase().includes(name),
  62. )
  63. ? "*"
  64. : ""
  65. }`}</Typography>
  66. </Grid>
  67. {field.name.toLowerCase().includes("password") === true ? (
  68. <Grid item key={"col-2-" + index} xs={8}>
  69. <TextField
  70. label={t(field.name)}
  71. type={showSMTPPassword ? "text" : "password"}
  72. InputProps={{
  73. endAdornment: (
  74. <InputAdornment position="end">
  75. <IconButton
  76. aria-label="toggle password visibility"
  77. onClick={handleClickShowPassword}
  78. onMouseDown={handleMouseDownPassword}
  79. >
  80. {showSMTPPassword ? (
  81. <Visibility />
  82. ) : (
  83. <VisibilityOff />
  84. )}
  85. </IconButton>
  86. </InputAdornment>
  87. ),
  88. }}
  89. fullWidth
  90. {...register(`settings.${index}.value`)}
  91. />
  92. </Grid>
  93. ) : field.type.toLowerCase() === "boolean" ? (
  94. <Grid item xs={8}>
  95. <Checkbox
  96. {...register(`settings.${index}.value`)}
  97. checked={Boolean(watch(`settings.${index}.value`))}
  98. />
  99. </Grid>
  100. ) : field.type.toLowerCase() === "integer" ? (
  101. <Grid item xs={8}>
  102. <TextField
  103. label={t(field.name)}
  104. type="number"
  105. InputProps={{
  106. inputProps: {
  107. step: 1,
  108. min: 0,
  109. },
  110. }}
  111. fullWidth
  112. {...register(`settings.${index}.value`, {
  113. required: requiredFields.some((name) =>
  114. field.name.toLowerCase().includes(name),
  115. ),
  116. })}
  117. error={Boolean(
  118. errors.settings &&
  119. errors.settings.length &&
  120. errors.settings.length > index &&
  121. errors.settings[index]?.value,
  122. )}
  123. />
  124. </Grid>
  125. ) : (
  126. <Grid item xs={8}>
  127. <TextField
  128. label={t(field.name)}
  129. fullWidth
  130. {...register(`settings.${index}.value`, {
  131. required: requiredFields.some((name) =>
  132. field.name.toLowerCase().includes(name),
  133. ),
  134. })}
  135. error={Boolean(
  136. errors.settings &&
  137. errors.settings.length &&
  138. errors.settings.length > index &&
  139. errors.settings[index]?.value,
  140. )}
  141. />
  142. </Grid>
  143. )}
  144. </Grid>
  145. ))}
  146. </Box>
  147. </CardContent>
  148. </Card>
  149. );
  150. };
  151. export default SettingDetails;