123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- import { makeStyles, Theme } from '@material-ui/core';
- import { FC, useMemo, useState } from 'react';
- import { useTranslation } from 'react-i18next';
- import DialogTemplate from '@/components/customDialog/DialogTemplate';
- import CustomInput from '@/components/customInput/CustomInput';
- import { ITextfieldConfig } from '@/components/customInput/Types';
- import { useFormValidation } from '@/hooks';
- import { formatForm } from '@/utils/Form';
- import { UpdateUserParams, UpdateUserProps } from './Types';
- const useStyles = makeStyles((theme: Theme) => ({
- input: {
- margin: theme.spacing(1, 0, 0.5),
- },
- }));
- const UpdateUser: FC<UpdateUserProps> = ({
- handleClose,
- handleUpdate,
- username,
- }) => {
- const { t: userTrans } = useTranslation('user');
- const { t: btnTrans } = useTranslation('btn');
- const { t: warningTrans } = useTranslation('warning');
- const [form, setForm] = useState<
- Omit<UpdateUserParams, 'username'> & { confirmPassword: string }
- >({
- oldPassword: '',
- newPassword: '',
- confirmPassword: '',
- });
- const checkedForm = useMemo(() => {
- const { oldPassword, newPassword } = form;
- return formatForm({ oldPassword, newPassword });
- }, [form]);
- const { validation, checkIsValid, disabled } = useFormValidation(checkedForm);
- const classes = useStyles();
- const handleInputChange = (
- key: 'oldPassword' | 'newPassword' | 'confirmPassword',
- value: string
- ) => {
- setForm(v => ({ ...v, [key]: value }));
- };
- const createConfigs: ITextfieldConfig[] = [
- {
- label: userTrans('oldPassword'),
- key: 'oldPassword',
- onChange: (value: string) => handleInputChange('oldPassword', value),
- variant: 'filled',
- className: classes.input,
- placeholder: userTrans('oldPassword'),
- fullWidth: true,
- validations: [
- {
- rule: 'require',
- errorText: warningTrans('required', {
- name: userTrans('oldPassword'),
- }),
- },
- ],
- type: 'password',
- defaultValue: form.oldPassword,
- },
- {
- label: userTrans('newPassword'),
- key: 'newPassword',
- onChange: (value: string) => handleInputChange('newPassword', value),
- variant: 'filled',
- className: classes.input,
- placeholder: userTrans('newPassword'),
- fullWidth: true,
- validations: [
- {
- rule: 'require',
- errorText: warningTrans('required', {
- name: userTrans('newPassword'),
- }),
- },
- ],
- type: 'password',
- defaultValue: form.newPassword,
- },
- {
- label: userTrans('confirmPassword'),
- key: 'confirmPassword',
- onChange: (value: string) => handleInputChange('confirmPassword', value),
- variant: 'filled',
- className: classes.input,
- placeholder: userTrans('confirmPassword'),
- fullWidth: true,
- validations: [
- {
- rule: 'confirm',
- extraParam: {
- compareValue: form.newPassword,
- },
- errorText: userTrans('isNotSame'),
- },
- ],
- type: 'password',
- defaultValue: form.confirmPassword,
- },
- ];
- const handleUpdateUser = () => {
- handleUpdate({
- username,
- newPassword: form.newPassword,
- oldPassword: form.oldPassword,
- });
- };
- return (
- <DialogTemplate
- title={userTrans('updateTitle')}
- handleClose={handleClose}
- confirmLabel={btnTrans('create')}
- handleConfirm={handleUpdateUser}
- confirmDisabled={disabled}
- >
- <>
- {createConfigs.map(v => (
- <CustomInput
- type="text"
- textConfig={v}
- checkValid={checkIsValid}
- validInfo={validation}
- key={v.label}
- />
- ))}
- </>
- </DialogTemplate>
- );
- };
- export default UpdateUser;
|