Skip to content

Commit d2d8a91

Browse files
committed
bfix: updated form submit implementation
Signed-off-by: Karthik Ayangar <[email protected]>
1 parent 8bfb963 commit d2d8a91

File tree

1 file changed

+77
-55
lines changed

1 file changed

+77
-55
lines changed

frontend/src/features/AddWorkspace/index.tsx

+77-55
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { AVATAR_URL } from 'app/constants/api';
1111
import { AVATAR_API } from 'envConstants';
1212
import Cross from '../../app/assets/icons/cross.svg';
1313
import {
14+
_FORM_SUBMIT,
1415
_VALIDATE_PROPS,
1516
_WORKSPACE_FORM,
1617
_WORKSPACE_FORM_CHANGE,
@@ -76,7 +77,7 @@ const AddWorkspace = () => {
7677
}
7778
};
7879

79-
const validate: _VALIDATE_PROPS = (name, value,files) => {
80+
const validate: _VALIDATE_PROPS = (name, value, files) => {
8081
switch (name) {
8182
case 'workspace':
8283
if (!value) {
@@ -89,14 +90,14 @@ const AddWorkspace = () => {
8990
return '';
9091
case 'image':
9192
if (!FileList) {
92-
return "File is required"
93+
return 'File is required';
9394
}
94-
return ''
95+
return '';
9596
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';
9899
}
99-
return ''
100+
return '';
100101
default:
101102
return '';
102103
}
@@ -121,59 +122,72 @@ const AddWorkspace = () => {
121122
}
122123
};
123124
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);
126127
setFormErrors({
127128
...formErrors,
128129
[name]: error,
129130
});
130131
};
131132

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 = " "
154151
}
152+
const func = async (): Promise<void> => {
153+
const dataRes = await addOrg(token, {
154+
name: newForm.workspace,
155+
description: newForm.description,
156+
});
155157

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+
}
167167

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+
}
175188
}
176189
};
190+
177191
const dataFetch = async () => {
178192
try {
179193
if (token) {
@@ -207,7 +221,9 @@ const AddWorkspace = () => {
207221
noValidate
208222
>
209223
<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>
211227
<div className='file-input-container'>
212228
<label htmlFor='icon-file' className='file-label'>
213229
Choose image files here
@@ -230,7 +246,8 @@ const AddWorkspace = () => {
230246
</div>
231247
<div className='single-form-element-container'>
232248
<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>
234251
</label>
235252
<input
236253
type='text'
@@ -242,7 +259,9 @@ const AddWorkspace = () => {
242259
onChange={handleChange}
243260
placeholder='workspace name'
244261
/>
245-
{formErrors.workspace && <p className='form-error'>{formErrors.workspace}</p>}
262+
{formErrors.workspace && (
263+
<p className='form-error'>{formErrors.workspace}</p>
264+
)}
246265
</div>
247266
<div className='single-form-element-container'>
248267
<label className='label' htmlFor='workspace-description'>
@@ -259,12 +278,15 @@ const AddWorkspace = () => {
259278
placeholder='workspace description'
260279
maxLength={201}
261280
/>
262-
{formErrors.description && <p className='form-error'>{formErrors.description}</p>}
263-
281+
{formErrors.description && (
282+
<p className='form-error'>{formErrors.description}</p>
283+
)}
264284
</div>
265285
<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'>
268290
<input
269291
type='text'
270292
id='add-member'

0 commit comments

Comments
 (0)