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' ;
2
3
import { 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' ;
4
5
import React from 'react' ;
5
6
import { useTranslation } from 'react-i18next' ;
6
7
import { useNavigate } from 'react-router' ;
7
8
import UserRoutes from '../../api/UserRoutes' ;
8
9
import { useAlert } from '../../hooks/useAlert' ;
10
+ import { useAuth } from '../../hooks/useAuth' ;
9
11
import useForm from '../../hooks/useForm' ;
10
12
import { useLoader } from '../../hooks/useLoader' ;
11
13
import catchError from '../../utils/catchError' ;
12
14
13
15
interface Data {
14
16
id ?: number ;
15
17
admin : boolean ;
18
+ lang : Language ;
16
19
login : string ;
17
20
password : string ;
18
21
idperson ?: number ;
@@ -32,15 +35,17 @@ const UserForm = ({ data }: Props) => {
32
35
const Loader = useLoader ( ) ;
33
36
const { t } = useTranslation ( 'user' ) ;
34
37
const navigate = useNavigate ( ) ;
38
+ const { user, updateData } = useAuth ( ) ;
35
39
36
40
const { register, handleSubmit, formState : { isSubmitting } , reset } = useForm < Data > ( 'user' , {
37
41
defaultValues : data ,
38
42
} ) ;
39
43
40
44
// Submit user data
41
45
const onSubmit = async ( formData : Data ) => {
42
- const processedData : Prisma . UserUpdateInput = {
46
+ const processedData = {
43
47
admin : formData . admin ,
48
+ lang : formData . lang ,
44
49
login : formData . login ,
45
50
active : true ,
46
51
} ;
@@ -64,13 +69,25 @@ const UserForm = ({ data }: Props) => {
64
69
update : personData ,
65
70
} ,
66
71
} ) . 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
+
67
84
Alert . open ( 'success' , t ( 'common:saved' ) ) ;
68
85
} ) . catch ( catchError ( Alert ) ) ;
69
86
Loader . close ( ) ;
70
87
} else { // Addition
71
- processedData . password = formData . password ;
72
88
await UserRoutes . insert ( {
73
89
...processedData ,
90
+ password : formData . password ,
74
91
connexionToken : '' ,
75
92
person : {
76
93
create : personData ,
@@ -87,12 +104,22 @@ const UserForm = ({ data }: Props) => {
87
104
return (
88
105
< form autoComplete = "off" onSubmit = { handleSubmit ( onSubmit ) } >
89
106
< Grid container spacing = { 3 } sx = { { pb : 2 } } >
90
- < Grid item xs = { 12 } >
107
+ < Grid item xs = { 12 } sm = { 6 } >
91
108
< FormControlLabel
92
109
control = { < Checkbox { ...register ( 'admin' , 'checkbox' ) } defaultChecked = { data . admin } /> }
93
110
label = { t ( 'giveAdminRights' ) }
94
111
/>
95
112
</ 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 >
96
123
< Grid item md = { 6 } xs = { 12 } >
97
124
< TextField { ...register ( 'login' , 'text' , { required : true } ) } fullWidth />
98
125
</ Grid >
0 commit comments