Update.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { makeStyles, Theme } from '@material-ui/core';
  2. import { FC, useMemo, useState } from 'react';
  3. import { useTranslation } from 'react-i18next';
  4. import DialogTemplate from '@/components/customDialog/DialogTemplate';
  5. import CustomInput from '@/components/customInput/CustomInput';
  6. import { ITextfieldConfig } from '@/components/customInput/Types';
  7. import { useFormValidation } from '@/hooks';
  8. import { formatForm } from '@/utils/Form';
  9. import { UpdateUserParams, UpdateUserProps } from './Types';
  10. const useStyles = makeStyles((theme: Theme) => ({
  11. input: {
  12. margin: theme.spacing(1, 0, 0.5),
  13. },
  14. }));
  15. const UpdateUser: FC<UpdateUserProps> = ({
  16. handleClose,
  17. handleUpdate,
  18. username,
  19. }) => {
  20. const { t: userTrans } = useTranslation('user');
  21. const { t: btnTrans } = useTranslation('btn');
  22. const { t: warningTrans } = useTranslation('warning');
  23. const [form, setForm] = useState<
  24. Omit<UpdateUserParams, 'username'> & { confirmPassword: string }
  25. >({
  26. oldPassword: '',
  27. newPassword: '',
  28. confirmPassword: '',
  29. });
  30. const checkedForm = useMemo(() => {
  31. const { oldPassword, newPassword } = form;
  32. return formatForm({ oldPassword, newPassword });
  33. }, [form]);
  34. const { validation, checkIsValid, disabled } = useFormValidation(checkedForm);
  35. const classes = useStyles();
  36. const handleInputChange = (
  37. key: 'oldPassword' | 'newPassword' | 'confirmPassword',
  38. value: string
  39. ) => {
  40. setForm(v => ({ ...v, [key]: value }));
  41. };
  42. const createConfigs: ITextfieldConfig[] = [
  43. {
  44. label: userTrans('oldPassword'),
  45. key: 'oldPassword',
  46. onChange: (value: string) => handleInputChange('oldPassword', value),
  47. variant: 'filled',
  48. className: classes.input,
  49. placeholder: userTrans('oldPassword'),
  50. fullWidth: true,
  51. validations: [
  52. {
  53. rule: 'require',
  54. errorText: warningTrans('required', {
  55. name: userTrans('oldPassword'),
  56. }),
  57. },
  58. ],
  59. type: 'password',
  60. defaultValue: form.oldPassword,
  61. },
  62. {
  63. label: userTrans('newPassword'),
  64. key: 'newPassword',
  65. onChange: (value: string) => handleInputChange('newPassword', value),
  66. variant: 'filled',
  67. className: classes.input,
  68. placeholder: userTrans('newPassword'),
  69. fullWidth: true,
  70. validations: [
  71. {
  72. rule: 'require',
  73. errorText: warningTrans('required', {
  74. name: userTrans('newPassword'),
  75. }),
  76. },
  77. ],
  78. type: 'password',
  79. defaultValue: form.newPassword,
  80. },
  81. {
  82. label: userTrans('confirmPassword'),
  83. key: 'confirmPassword',
  84. onChange: (value: string) => handleInputChange('confirmPassword', value),
  85. variant: 'filled',
  86. className: classes.input,
  87. placeholder: userTrans('confirmPassword'),
  88. fullWidth: true,
  89. validations: [
  90. {
  91. rule: 'confirm',
  92. extraParam: {
  93. compareValue: form.newPassword,
  94. },
  95. errorText: userTrans('isNotSame'),
  96. },
  97. ],
  98. type: 'password',
  99. defaultValue: form.confirmPassword,
  100. },
  101. ];
  102. const handleUpdateUser = () => {
  103. handleUpdate({
  104. username,
  105. newPassword: form.newPassword,
  106. oldPassword: form.oldPassword,
  107. });
  108. };
  109. return (
  110. <DialogTemplate
  111. title={userTrans('updateTitle')}
  112. handleClose={handleClose}
  113. confirmLabel={btnTrans('create')}
  114. handleConfirm={handleUpdateUser}
  115. confirmDisabled={disabled}
  116. >
  117. <>
  118. {createConfigs.map(v => (
  119. <CustomInput
  120. type="text"
  121. textConfig={v}
  122. checkValid={checkIsValid}
  123. validInfo={validation}
  124. key={v.label}
  125. />
  126. ))}
  127. </>
  128. </DialogTemplate>
  129. );
  130. };
  131. export default UpdateUser;