5
5
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
6
*/
7
7
8
- import { useCallback } from 'react' ;
8
+ import { useCallback , useMemo } from 'react' ;
9
9
import { Grid } from '@mui/material' ;
10
10
import { type DateOrTimeView } from '@mui/x-date-pickers' ;
11
11
import { useIntl } from 'react-intl' ;
12
- import { type Option , SubmitButton , useSnackMessage } from '@gridsuite/commons-ui' ;
12
+ import { SubmitButton , useSnackMessage } from '@gridsuite/commons-ui' ;
13
13
import yup from '../../utils/yup-config' ;
14
14
import { type InferType } from 'yup' ;
15
15
import { type SubmitHandler , useForm } from 'react-hook-form' ;
@@ -28,19 +28,26 @@ export type AddAnnouncementFormProps = {
28
28
onAnnouncementCreated ?: ( ) => void ;
29
29
} ;
30
30
31
- const severitySelect : Option [ ] = Object . values ( UserAdminSrv . AnnouncementSeverity ) . map ( ( value ) => ( {
32
- id : value ,
33
- label : `announcements.severity.${ value } ` ,
34
- } ) ) ;
35
-
36
31
const formSchema = yup
37
32
. object ( )
38
33
. shape ( {
39
- [ MESSAGE ] : yup . string ( ) . trim ( ) . min ( 1 ) . required ( ) ,
40
- [ START_DATE ] : yup . string ( ) . datetime ( { precision : 0 } ) . required ( ) ,
41
- [ END_DATE ] : yup . string ( ) . datetime ( { precision : 0 } ) . required ( ) ,
34
+ [ MESSAGE ] : yup . string ( ) . nullable ( ) . trim ( ) . min ( 1 ) . required ( ) ,
35
+ [ START_DATE ] : yup . string ( ) . nullable ( ) . datetime ( ) . required ( ) ,
36
+ [ END_DATE ] : yup
37
+ . string ( )
38
+ . nullable ( )
39
+ . datetime ( )
40
+ . required ( )
41
+ . when ( START_DATE , ( startDate , schema ) =>
42
+ schema . test (
43
+ 'is-after-start' ,
44
+ 'End date must be after start date' ,
45
+ ( endDate ) => ! startDate || ! endDate || new Date ( endDate ) > new Date ( startDate as unknown as string )
46
+ )
47
+ ) ,
42
48
[ SEVERITY ] : yup
43
49
. string < UserAdminSrv . AnnouncementSeverity > ( )
50
+ . nullable ( )
44
51
. oneOf ( Object . values ( UserAdminSrv . AnnouncementSeverity ) )
45
52
. required ( ) ,
46
53
} )
@@ -59,20 +66,23 @@ export default function AddAnnouncementForm({ onAnnouncementCreated }: Readonly<
59
66
60
67
const formContext = useForm ( {
61
68
resolver : yupResolver ( formSchema ) ,
62
- /*TODO defaultValues: {
69
+ defaultValues : {
70
+ // @ts -expect-error: nullable() is called, so null is accepted as default value
63
71
[ MESSAGE ] : null ,
72
+ // @ts -expect-error: nullable() is called, so null is accepted as default value
64
73
[ START_DATE ] : null ,
74
+ // @ts -expect-error: nullable() is called, so null is accepted as default value
65
75
[ END_DATE ] : null ,
76
+ // @ts -expect-error: nullable() is called, so null is accepted as default value
66
77
[ SEVERITY ] : null ,
67
- },*/
78
+ } ,
68
79
} ) ;
69
80
const { formState, getValues } = formContext ;
70
81
const startDateValue = getValues ( START_DATE ) ;
71
82
72
83
const onSubmit = useCallback < SubmitHandler < FormSchema > > (
73
84
( params ) => {
74
85
UserAdminSrv . addAnnouncement ( {
75
- //id: crypto.randomUUID(),
76
86
message : params . message ,
77
87
startDate : params . startDate ,
78
88
endDate : params . endDate ,
@@ -90,51 +100,68 @@ export default function AddAnnouncementForm({ onAnnouncementCreated }: Readonly<
90
100
) ;
91
101
92
102
return (
93
- < FormContainer < FormSchema > formContext = { formContext } onSuccess = { onSubmit } >
94
- < DateTimePickerElement < FormSchema > name = { END_DATE } />
95
- < Grid container spacing = { 1 } >
96
- < Grid item xs = { 4 } >
97
- < TextareaAutosizeElement < FormSchema >
98
- name = { MESSAGE }
99
- label = { intl . formatMessage ( { id : 'announcements.form.message' } ) }
100
- minRows = { 2 }
101
- maxRows = { 5 }
102
- fullWidth
103
- //inputProps={{ maxLength: 200 }}
104
- />
105
- </ Grid >
106
- < Grid item xs = { 2 } >
107
- < DateTimePickerElement < FormSchema >
108
- name = { START_DATE }
109
- label = { intl . formatMessage ( { id : 'announcements.table.startDate' } ) }
110
- transform = { datetimePickerTransform }
111
- timezone = "system"
112
- views = { pickerView }
113
- timeSteps = { { hours : 1 , minutes : 1 , seconds : 0 } }
114
- disablePast
115
- />
103
+ < FormContainer < FormSchema >
104
+ formContext = { formContext }
105
+ onSuccess = { onSubmit }
106
+ //criteriaMode="all"
107
+ //mode="all"
108
+ mode = "onChange"
109
+ reValidateMode = "onChange"
110
+ //reValidateMode="onChange"
111
+ FormProps = { { style : { height : '100%' } } }
112
+ >
113
+ < Grid container direction = "column" spacing = { 1.5 } height = "100%" >
114
+ < Grid item container xs = "auto" spacing = { 1 } >
115
+ < Grid item xs = { 12 } lg = { 6 } >
116
+ < DateTimePickerElement < FormSchema >
117
+ name = { START_DATE }
118
+ label = { intl . formatMessage ( { id : 'announcements.table.startDate' } ) }
119
+ transform = { datetimePickerTransform }
120
+ timezone = "system"
121
+ views = { pickerView }
122
+ timeSteps = { { hours : 1 , minutes : 1 , seconds : 0 } }
123
+ disablePast
124
+ />
125
+ </ Grid >
126
+ < Grid item xs = { 12 } lg = { 6 } >
127
+ < DateTimePickerElement < FormSchema >
128
+ name = { END_DATE }
129
+ label = { intl . formatMessage ( { id : 'announcements.table.endDate' } ) }
130
+ transform = { datetimePickerTransform }
131
+ timezone = "system"
132
+ views = { pickerView }
133
+ timeSteps = { { hours : 1 , minutes : 1 , seconds : 0 } }
134
+ disablePast
135
+ minDateTime = { startDateValue ? new Date ( startDateValue ) : undefined }
136
+ />
137
+ </ Grid >
116
138
</ Grid >
117
- < Grid item xs = { 2 } >
118
- < DateTimePickerElement < FormSchema >
119
- name = { END_DATE }
120
- label = { intl . formatMessage ( { id : 'announcements.table.endDate' } ) }
121
- transform = { datetimePickerTransform }
122
- timezone = "system"
123
- views = { pickerView }
124
- timeSteps = { { hours : 1 , minutes : 1 , seconds : 0 } }
125
- disablePast
126
- minDateTime = { startDateValue ? new Date ( startDateValue ) : undefined }
127
- />
128
- </ Grid >
129
- < Grid item xs = { 2 } >
139
+ < Grid item xs = "auto" >
130
140
< SelectElement < FormSchema >
131
141
name = { SEVERITY }
132
142
label = { intl . formatMessage ( { id : 'announcements.severity' } ) }
133
- options = { severitySelect }
143
+ options = { useMemo (
144
+ ( ) =>
145
+ Object . values ( UserAdminSrv . AnnouncementSeverity ) . map ( ( value ) => ( {
146
+ id : value ,
147
+ label : intl . formatMessage ( { id : `announcements.severity.${ value } ` } ) ,
148
+ } ) ) ,
149
+ [ intl ]
150
+ ) }
134
151
fullWidth
135
152
/>
136
153
</ Grid >
137
- < Grid item xs = { 2 } >
154
+ < Grid item xs >
155
+ < TextareaAutosizeElement < FormSchema >
156
+ name = { MESSAGE }
157
+ label = { intl . formatMessage ( { id : 'announcements.form.message' } ) }
158
+ style = { { height : '100%' } }
159
+ rows = { 5 } // why does it do nothing even if the field is set as multiline?!
160
+ fullWidth
161
+ //inputProps={{ maxLength: 200 }}
162
+ />
163
+ </ Grid >
164
+ < Grid item xs = "auto" >
138
165
< SubmitButton
139
166
variant = "outlined"
140
167
type = "submit"
0 commit comments