@@ -11,6 +11,7 @@ import { AVATAR_URL } from 'app/constants/api';
11
11
import { AVATAR_API } from 'envConstants' ;
12
12
import Cross from '../../app/assets/icons/cross.svg' ;
13
13
import {
14
+ _FORM_SUBMIT ,
14
15
_VALIDATE_PROPS ,
15
16
_WORKSPACE_FORM ,
16
17
_WORKSPACE_FORM_CHANGE ,
@@ -76,7 +77,7 @@ const AddWorkspace = () => {
76
77
}
77
78
} ;
78
79
79
- const validate : _VALIDATE_PROPS = ( name , value , files ) => {
80
+ const validate : _VALIDATE_PROPS = ( name , value , files ) => {
80
81
switch ( name ) {
81
82
case 'workspace' :
82
83
if ( ! value ) {
@@ -89,14 +90,14 @@ const AddWorkspace = () => {
89
90
return '' ;
90
91
case 'image' :
91
92
if ( ! FileList ) {
92
- return " File is required"
93
+ return ' File is required' ;
93
94
}
94
- return ''
95
+ return '' ;
95
96
case 'description' :
96
- if ( value . length > 200 ) {
97
- return " Description should be less then 200 characters"
97
+ if ( value . length > 200 ) {
98
+ return ' Description should be less then 200 characters' ;
98
99
}
99
- return ''
100
+ return '' ;
100
101
default :
101
102
return '' ;
102
103
}
@@ -121,59 +122,72 @@ const AddWorkspace = () => {
121
122
}
122
123
} ;
123
124
const handleBlur = ( e : React . FocusEvent < HTMLInputElement > ) => {
124
- const { name, value, files } = e . target ;
125
- const error = validate ( name , value , files ) ;
125
+ const { name, value, files } = e . target ;
126
+ const error = validate ( name , value , files ) ;
126
127
setFormErrors ( {
127
128
...formErrors ,
128
129
[ name ] : error ,
129
130
} ) ;
130
131
} ;
131
132
132
- const handleSubmit = ( ) => { } ;
133
-
134
- const SubmitHandler = async ( ) : Promise < void > => {
135
- if (
136
- form . description &&
137
- token &&
138
- form . workspace
139
- // &&
140
- // validName &&
141
- // uniqueName &&
142
- // validDescription
143
- ) {
144
- const func = async ( ) : Promise < void > => {
145
- const dataRes = await addOrg ( token , {
146
- name : form . workspace ,
147
- description : form . description ,
148
- } ) ;
149
-
150
- if ( form . image ) {
151
- try {
152
- const fileRes = await uploadIcon ( token , form . workspace , form . image ) ;
153
- } catch ( e ) { }
133
+ const handleSubmit : _FORM_SUBMIT = ( event ) => {
134
+ event . preventDefault ( ) ;
135
+ const workspace_error = validate ( 'workspace' , form . workspace , null ) ;
136
+ const desc_error = validate ( 'description' , form . description , null ) ;
137
+ const image_error = form . image ? '' : 'Image is required' ;
138
+ setFormErrors ( {
139
+ ...formErrors ,
140
+ workspace : workspace_error ,
141
+ description : desc_error ,
142
+ image : image_error ,
143
+ } ) ;
144
+ if ( workspace_error || desc_error || image_error )
145
+ toast . error ( 'Check workspace, icon and description fields' ) ;
146
+ else {
147
+ if ( token ) {
148
+ const newForm = form ;
149
+ if ( ! form . description ) {
150
+ newForm . description = " "
154
151
}
152
+ const func = async ( ) : Promise < void > => {
153
+ const dataRes = await addOrg ( token , {
154
+ name : newForm . workspace ,
155
+ description : newForm . description ,
156
+ } ) ;
155
157
156
- if ( form . members . length > 0 ) {
157
- try {
158
- const addMmebersRes = await addOrgMembers (
159
- token ,
160
- form . workspace ,
161
- form . members
162
- ) ;
163
- } catch ( e ) { }
164
- }
165
- navigate ( '/' ) ;
166
- } ;
158
+ if ( form . image ) {
159
+ try {
160
+ const fileRes = await uploadIcon (
161
+ token ,
162
+ form . workspace ,
163
+ form . image
164
+ ) ;
165
+ } catch ( e ) { }
166
+ }
167
167
168
- toast . promise ( func ( ) , {
169
- loading : 'Saving Workspace' ,
170
- success : < b > Workspace saved</ b > ,
171
- error : < b > Could not save</ b > ,
172
- } ) ;
173
- } else {
174
- toast . error ( 'Invalid inputs' ) ;
168
+ if ( form . members . length > 0 ) {
169
+ try {
170
+ const addMmebersRes = await addOrgMembers (
171
+ token ,
172
+ form . workspace ,
173
+ form . members
174
+ ) ;
175
+ } catch ( e ) { }
176
+ }
177
+ navigate ( '/' ) ;
178
+ } ;
179
+
180
+ toast . promise ( func ( ) , {
181
+ loading : 'Saving Workspace' ,
182
+ success : < b > Workspace saved</ b > ,
183
+ error : < b > Something's wrong with your inputs</ b > ,
184
+ } ) ;
185
+ } else {
186
+ toast . error ( `Something's wrong with your inputs` ) ;
187
+ }
175
188
}
176
189
} ;
190
+
177
191
const dataFetch = async ( ) => {
178
192
try {
179
193
if ( token ) {
@@ -207,7 +221,9 @@ const AddWorkspace = () => {
207
221
noValidate
208
222
>
209
223
< div className = 'single-form-element-container' >
210
- < p className = 'label' > Add Icon< span style = { { color :'red' , paddingLeft :'4px' } } > *</ span > </ p >
224
+ < p className = 'label' >
225
+ Add Icon< span style = { { color : 'red' , paddingLeft : '4px' } } > *</ span >
226
+ </ p >
211
227
< div className = 'file-input-container' >
212
228
< label htmlFor = 'icon-file' className = 'file-label' >
213
229
Choose image files here
@@ -230,7 +246,8 @@ const AddWorkspace = () => {
230
246
</ div >
231
247
< div className = 'single-form-element-container' >
232
248
< label className = 'label' htmlFor = 'workspace-name' >
233
- Workspace Name< span style = { { color :'red' , paddingLeft :'4px' } } > *</ span >
249
+ Workspace Name
250
+ < span style = { { color : 'red' , paddingLeft : '4px' } } > *</ span >
234
251
</ label >
235
252
< input
236
253
type = 'text'
@@ -242,7 +259,9 @@ const AddWorkspace = () => {
242
259
onChange = { handleChange }
243
260
placeholder = 'workspace name'
244
261
/>
245
- { formErrors . workspace && < p className = 'form-error' > { formErrors . workspace } </ p > }
262
+ { formErrors . workspace && (
263
+ < p className = 'form-error' > { formErrors . workspace } </ p >
264
+ ) }
246
265
</ div >
247
266
< div className = 'single-form-element-container' >
248
267
< label className = 'label' htmlFor = 'workspace-description' >
@@ -259,12 +278,15 @@ const AddWorkspace = () => {
259
278
placeholder = 'workspace description'
260
279
maxLength = { 201 }
261
280
/>
262
- { formErrors . description && < p className = 'form-error' > { formErrors . description } </ p > }
263
-
281
+ { formErrors . description && (
282
+ < p className = 'form-error' > { formErrors . description } </ p >
283
+ ) }
264
284
</ div >
265
285
< div className = 'single-form-element-container' >
266
- < label className = 'label' htmlFor = "add-member" > Add Members</ label >
267
- < div className = 'add-member-container' >
286
+ < label className = 'label' htmlFor = 'add-member' >
287
+ Add Members
288
+ </ label >
289
+ < div className = 'add-member-container' >
268
290
< input
269
291
type = 'text'
270
292
id = 'add-member'
0 commit comments