From cf00ca3326bba5dd5d003811cad60cd9c53f9a8b Mon Sep 17 00:00:00 2001 From: matushl Date: Sat, 11 Nov 2023 17:19:30 +0100 Subject: [PATCH 1/3] Add change password dialog --- src/components/Profile/PasswordChangeForm.tsx | 91 +++++++++++++++++++ src/components/Profile/ProfileDetail.tsx | 20 ++-- 2 files changed, 102 insertions(+), 9 deletions(-) create mode 100644 src/components/Profile/PasswordChangeForm.tsx diff --git a/src/components/Profile/PasswordChangeForm.tsx b/src/components/Profile/PasswordChangeForm.tsx new file mode 100644 index 00000000..33bbced4 --- /dev/null +++ b/src/components/Profile/PasswordChangeForm.tsx @@ -0,0 +1,91 @@ +import {useMutation} from '@tanstack/react-query' +import axios from 'axios' +import {FC} from 'react' +import {SubmitHandler, useForm} from 'react-hook-form' + +import styles from '@/components/FormItems/Form.module.scss' +import {IGeneralPostResponse} from '@/types/api/general' + +import {Button} from '../Clickable/Clickable' +import {Dialog} from '../Dialog/Dialog' +import {FormInput} from '../FormItems/FormInput/FormInput' + +type PasswordChangeDialogValues = { + new_password1?: string + new_password2?: string +} + +const defaultValues: PasswordChangeDialogValues = { + new_password1: '', + new_password2: '', +} + +interface PasswordChangeDialogProps { + open: boolean + close: () => void +} + +export const PasswordChangeDialog: FC = ({open, close}) => { + const {handleSubmit, reset, control, getValues} = useForm({defaultValues}) + + const onSuccess = () => { + alert('Zmena hesla prebehla úspešne.') + reset() + close() + } + + const {mutate: submitFormData} = useMutation({ + mutationFn: (data: PasswordChangeDialogValues) => { + return axios.post(`/api/user/password/change`, data) + }, + onSuccess: onSuccess, + }) + + const onSubmit: SubmitHandler = async (data) => { + submitFormData(data) + } + + const requiredRule = {required: '* Toto pole nemôže byť prázdne.'} + + return ( + + + { + if (val !== getValues().new_password1) return '* Zadané heslá sa nezhodujú.' + }, + }} + /> + + } + actions={ + + } + /> + ) +} diff --git a/src/components/Profile/ProfileDetail.tsx b/src/components/Profile/ProfileDetail.tsx index f2985763..9779ae56 100644 --- a/src/components/Profile/ProfileDetail.tsx +++ b/src/components/Profile/ProfileDetail.tsx @@ -1,13 +1,14 @@ import {Stack, Typography} from '@mui/material' import {useQuery} from '@tanstack/react-query' import axios from 'axios' -import {FC} from 'react' +import {FC, useState} from 'react' import {Profile} from '@/types/api/personal' import {AuthContainer} from '@/utils/AuthContainer' import {useSeminarInfo} from '@/utils/useSeminarInfo' import {Button, Link} from '../Clickable/Clickable' +import {PasswordChangeDialog} from './PasswordChangeForm' import styles from './ProfileDetail.module.scss' type ProfileLineInput = { @@ -29,6 +30,12 @@ export const ProfileDetail: FC = () => { const {isAuthed} = AuthContainer.useContainer() const {seminar} = useSeminarInfo() + const [openPasswordDialog, setOpenPasswordDialog] = useState(false) + + const toggleOpenPasswordDialog = () => { + setOpenPasswordDialog((prev) => !prev) + } + const {data} = useQuery({ queryKey: ['personal', 'profiles', 'myprofile'], queryFn: () => axios.get(`/api/personal/profiles/myprofile`), @@ -47,15 +54,10 @@ export const ProfileDetail: FC = () => { - upraviť údaje - + Upraviť údaje + + ) } From 214010fc38a986a93b354ceeca561a0eb6aa1939 Mon Sep 17 00:00:00 2001 From: matushl Date: Sat, 11 Nov 2023 18:03:29 +0100 Subject: [PATCH 2/3] Style Dialog --- src/components/Dialog/Dialog.tsx | 16 ++++++++++++++-- src/theme.ts | 24 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/components/Dialog/Dialog.tsx b/src/components/Dialog/Dialog.tsx index 1434ff08..1840c092 100644 --- a/src/components/Dialog/Dialog.tsx +++ b/src/components/Dialog/Dialog.tsx @@ -1,4 +1,11 @@ -import {Dialog as MuiDialog, DialogActions, DialogContent, DialogContentText, DialogTitle} from '@mui/material' +import { + Dialog as MuiDialog, + DialogActions, + DialogContent, + DialogContentText, + DialogTitle, + Typography, +} from '@mui/material' import {FC, ReactNode} from 'react' type DialogProps = { @@ -17,8 +24,13 @@ export const Dialog: FC = ({open, close, title, contentText, action onClose={close} aria-labelledby="alert-dialog-title" aria-describedby="alert-dialog-description" + sx={{border: '10px solid black'}} > - {title && {title}} + {title && ( + + {title} + + )} {contentText && ( {contentText} diff --git a/src/theme.ts b/src/theme.ts index c06ff592..830ab17c 100644 --- a/src/theme.ts +++ b/src/theme.ts @@ -143,4 +143,28 @@ export const theme: Theme = { fontStyle: 'italic', }, }, + components: { + MuiDialog: { + styleOverrides: { + paper: { + '& > div.MuiDialogContent-root': {paddingTop: '20px'}, + border: '10px solid black', + }, + }, + }, + MuiDialogActions: { + styleOverrides: { + root: { + paddingBottom: '20px', + }, + }, + }, + MuiDialogTitle: { + styleOverrides: { + root: { + textAlign: 'center', + }, + }, + }, + }, } From 9aea583d9c074bc973567d10e59da4a95be49247 Mon Sep 17 00:00:00 2001 From: matushl Date: Sat, 11 Nov 2023 20:43:33 +0100 Subject: [PATCH 3/3] fix form values type --- src/components/Profile/PasswordChangeForm.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/Profile/PasswordChangeForm.tsx b/src/components/Profile/PasswordChangeForm.tsx index 33bbced4..ab61904a 100644 --- a/src/components/Profile/PasswordChangeForm.tsx +++ b/src/components/Profile/PasswordChangeForm.tsx @@ -11,8 +11,8 @@ import {Dialog} from '../Dialog/Dialog' import {FormInput} from '../FormItems/FormInput/FormInput' type PasswordChangeDialogValues = { - new_password1?: string - new_password2?: string + new_password1: string + new_password2: string } const defaultValues: PasswordChangeDialogValues = {