1- import { Prisma } from '@fullstack-typescript-monorepo/prisma' ;
1+ import { DEFAULT_LANGUAGE , Language } from '@fullstack-typescript-monorepo/core' ;
2+ import { Lang } from '@fullstack-typescript-monorepo/prisma' ;
23import { LoadingButton } from '@mui/lab' ;
3- import { Box , Checkbox , Divider , FormControlLabel , Grid , TextField } from '@mui/material' ;
4+ import { Box , Checkbox , Divider , FormControl , FormControlLabel , Grid , InputLabel , MenuItem , Select , TextField } from '@mui/material' ;
45import React from 'react' ;
56import { useTranslation } from 'react-i18next' ;
67import { useNavigate } from 'react-router' ;
78import UserRoutes from '../../api/UserRoutes' ;
89import { useAlert } from '../../hooks/useAlert' ;
10+ import { useAuth } from '../../hooks/useAuth' ;
911import useForm from '../../hooks/useForm' ;
1012import { useLoader } from '../../hooks/useLoader' ;
1113import catchError from '../../utils/catchError' ;
1214
1315interface Data {
1416 id ?: number ;
1517 admin : boolean ;
18+ lang : Language ;
1619 login : string ;
1720 password : string ;
1821 idperson ?: number ;
@@ -32,15 +35,17 @@ const UserForm = ({ data }: Props) => {
3235 const Loader = useLoader ( ) ;
3336 const { t } = useTranslation ( 'user' ) ;
3437 const navigate = useNavigate ( ) ;
38+ const { user, updateData } = useAuth ( ) ;
3539
3640 const { register, handleSubmit, formState : { isSubmitting } , reset } = useForm < Data > ( 'user' , {
3741 defaultValues : data ,
3842 } ) ;
3943
4044 // Submit user data
4145 const onSubmit = async ( formData : Data ) => {
42- const processedData : Prisma . UserUpdateInput = {
46+ const processedData = {
4347 admin : formData . admin ,
48+ lang : formData . lang ,
4449 login : formData . login ,
4550 active : true ,
4651 } ;
@@ -64,13 +69,25 @@ const UserForm = ({ data }: Props) => {
6469 update : personData ,
6570 } ,
6671 } ) . then ( ( ) => {
72+ // Update user data if it's the current user
73+ if ( user . id === formData . id ) {
74+ updateData ( ( prev ) => ( {
75+ ...prev ,
76+ ...processedData ,
77+ person : {
78+ ...prev . person ,
79+ ...personData ,
80+ } ,
81+ } ) ) ;
82+ }
83+
6784 Alert . open ( 'success' , t ( 'common:saved' ) ) ;
6885 } ) . catch ( catchError ( Alert ) ) ;
6986 Loader . close ( ) ;
7087 } else { // Addition
71- processedData . password = formData . password ;
7288 await UserRoutes . insert ( {
7389 ...processedData ,
90+ password : formData . password ,
7491 connexionToken : '' ,
7592 person : {
7693 create : personData ,
@@ -87,12 +104,22 @@ const UserForm = ({ data }: Props) => {
87104 return (
88105 < form autoComplete = "off" onSubmit = { handleSubmit ( onSubmit ) } >
89106 < Grid container spacing = { 3 } sx = { { pb : 2 } } >
90- < Grid item xs = { 12 } >
107+ < Grid item xs = { 12 } sm = { 6 } >
91108 < FormControlLabel
92109 control = { < Checkbox { ...register ( 'admin' , 'checkbox' ) } defaultChecked = { data . admin } /> }
93110 label = { t ( 'giveAdminRights' ) }
94111 />
95112 </ Grid >
113+ < Grid item xs = { 12 } sm = { 6 } >
114+ < FormControl fullWidth >
115+ < InputLabel > { t ( 'lang' ) } </ InputLabel >
116+ < Select { ...register ( 'lang' , 'select' , { required : true } ) } defaultValue = { data . lang || DEFAULT_LANGUAGE } >
117+ { Object . keys ( Lang ) . map ( ( lang ) => (
118+ < MenuItem key = { lang } value = { lang } > { t ( lang ) } </ MenuItem >
119+ ) ) }
120+ </ Select >
121+ </ FormControl >
122+ </ Grid >
96123 < Grid item md = { 6 } xs = { 12 } >
97124 < TextField { ...register ( 'login' , 'text' , { required : true } ) } fullWidth />
98125 </ Grid >
0 commit comments