8
8
import React , { Component } from 'react' ;
9
9
import PropTypes from 'prop-types' ;
10
10
import Grid from '@material-ui/core/Grid' ;
11
-
11
+ import * as Yup from 'yup' ;
12
12
import { TextField } from '@postgres.ai/shared/components/TextField' ;
13
13
import { PageSpinner } from '@postgres.ai/shared/components/PageSpinner' ;
14
14
@@ -17,9 +17,18 @@ import Actions from 'actions/actions';
17
17
import { ErrorWrapper } from 'components/Error/ErrorWrapper' ;
18
18
import ConsolePageTitle from 'components/ConsolePageTitle' ;
19
19
import { Head , createTitle } from 'components/Head' ;
20
+ import CheckBoxOutlineBlankIcon from '@material-ui/icons/CheckBoxOutlineBlank' ;
21
+ import CheckBoxIcon from '@material-ui/icons/CheckBox' ;
22
+ import { Button , Checkbox , FormControlLabel , InputLabel } from "@material-ui/core" ;
23
+ import { Form , Formik } from "formik" ;
20
24
21
25
const PAGE_NAME = 'Profile' ;
22
26
27
+ const validationSchema = Yup . object ( {
28
+ first_name : Yup . string ( ) . required ( 'First name is required' ) ,
29
+ last_name : Yup . string ( ) . required ( 'Last name is required' ) ,
30
+ } ) ;
31
+
23
32
class Profile extends Component {
24
33
componentDidMount ( ) {
25
34
const that = this ;
@@ -31,6 +40,14 @@ class Profile extends Component {
31
40
32
41
that . setState ( { data : this . data } ) ;
33
42
43
+ if ( userProfile && ! userProfile . isProcessing && userProfile . data . info ) {
44
+ that . setState ( {
45
+ is_chats_email_notifications_enabled : userProfile . data . info . chats_email_notifications_enabled ,
46
+ first_name : userProfile . data . info . first_name ,
47
+ last_name : userProfile . data . info . last_name
48
+ } ) ;
49
+ }
50
+
34
51
if ( auth && auth . token && ! userProfile . isProcessed && ! userProfile . isProcessing &&
35
52
! userProfile . error ) {
36
53
Actions . getUserProfile ( auth . token ) ;
@@ -44,10 +61,28 @@ class Profile extends Component {
44
61
this . unsubscribe ( ) ;
45
62
}
46
63
64
+ handleSaveSettings = ( values ) => {
65
+ const auth = this . state . data ?. auth ;
66
+ if ( auth ) {
67
+ Actions . updateUserProfile ( auth . token , {
68
+ is_chats_email_notifications_enabled : values . is_chats_email_notifications_enabled ,
69
+ first_name : values . first_name ,
70
+ last_name : values . last_name ,
71
+ } ) ;
72
+ }
73
+ } ;
74
+
47
75
render ( ) {
48
76
const { classes } = this . props ;
49
77
const data = this . state && this . state . data ? this . state . data . userProfile : null ;
50
78
79
+ const initialValues = {
80
+ first_name : data ?. data ?. info ?. first_name || '' ,
81
+ last_name : data ?. data ?. info ?. last_name || '' ,
82
+ is_chats_email_notifications_enabled : data ?. data ?. info ?. chats_email_notifications_enabled || false ,
83
+ } ;
84
+
85
+
51
86
const headRendered = (
52
87
< Head title = { createTitle ( [ PAGE_NAME ] ) } />
53
88
) ;
@@ -85,38 +120,89 @@ class Profile extends Component {
85
120
{ headRendered }
86
121
87
122
{ pageTitle }
88
-
89
- < Grid
90
- item
91
- xs = { 12 }
92
- sm = { 6 }
93
- md = { 4 }
94
- lg = { 3 }
95
- xl = { 2 }
96
- className = { classes . container }
123
+ < Formik
124
+ initialValues = { initialValues }
125
+ validationSchema = { validationSchema }
126
+ onSubmit = { this . handleSaveSettings }
97
127
>
98
- < TextField
99
- disabled
100
- label = 'Email'
101
- fullWidth
102
- defaultValue = { data . data . info . email }
103
- className = { classes . textField }
104
- />
105
- < TextField
106
- disabled
107
- label = 'First name'
108
- fullWidth
109
- defaultValue = { data . data . info . first_name }
110
- className = { classes . textField }
111
- />
112
- < TextField
113
- disabled
114
- label = 'Last name'
115
- fullWidth
116
- defaultValue = { data . data . info . last_name }
117
- className = { classes . textField }
118
- />
119
- </ Grid >
128
+ { ( { values, handleChange, setFieldValue, errors, touched } ) => (
129
+ < Form >
130
+ < Grid
131
+ item
132
+ xs = { 12 }
133
+ sm = { 6 }
134
+ md = { 6 }
135
+ lg = { 4 }
136
+ xl = { 3 }
137
+ className = { classes . container }
138
+ >
139
+ < TextField
140
+ disabled
141
+ label = 'Email'
142
+ fullWidth
143
+ defaultValue = { data . data . info . email }
144
+ className = { classes . textField }
145
+ />
146
+ < TextField
147
+ label = "First name"
148
+ fullWidth
149
+ name = "first_name"
150
+ value = { values . first_name }
151
+ onChange = { handleChange }
152
+ className = { classes . textField }
153
+ error = { touched . first_name && ! ! errors . first_name }
154
+ helperText = { touched . first_name && errors . first_name }
155
+ />
156
+ < TextField
157
+ label = "Last name"
158
+ fullWidth
159
+ name = "last_name"
160
+ value = { values . last_name }
161
+ onChange = { handleChange }
162
+ className = { classes . textField }
163
+ error = { touched . last_name && ! ! errors . last_name }
164
+ helperText = { touched . last_name && errors . last_name }
165
+ />
166
+ < InputLabel className = { classes . label } id = "visibility-radio-buttons-group-label" >
167
+ Notifications settings
168
+ </ InputLabel >
169
+ < FormControlLabel
170
+ className = { classes . formControlLabel }
171
+ control = {
172
+ < Checkbox
173
+ icon = { < CheckBoxOutlineBlankIcon fontSize = "large" /> }
174
+ checkedIcon = { < CheckBoxIcon fontSize = "large" /> }
175
+ name = "is_chats_email_notifications_enabled"
176
+ className = { classes . formControlLabelCheckbox }
177
+ checked = { values . is_chats_email_notifications_enabled }
178
+ onChange = { ( event ) =>
179
+ setFieldValue ( 'is_chats_email_notifications_enabled' , event . target . checked )
180
+ }
181
+ />
182
+ }
183
+ label = "Notify about new messages in the AI Assistant"
184
+ />
185
+ </ Grid >
186
+ < Grid
187
+ item
188
+ xs = { 12 }
189
+ sm = { 12 }
190
+ lg = { 8 }
191
+ className = { classes . updateButtonContainer }
192
+ >
193
+ < Button
194
+ variant = "contained"
195
+ color = "primary"
196
+ disabled = { data ?. isProcessing }
197
+ id = "userSaveButton"
198
+ type = "submit"
199
+ >
200
+ Save
201
+ </ Button >
202
+ </ Grid >
203
+ </ Form >
204
+ ) }
205
+ </ Formik >
120
206
</ div >
121
207
) ;
122
208
}
0 commit comments