diff --git a/frontend/src/features/AddWorkspace/index.tsx b/frontend/src/features/AddWorkspace/index.tsx index d291075..f333413 100644 --- a/frontend/src/features/AddWorkspace/index.tsx +++ b/frontend/src/features/AddWorkspace/index.tsx @@ -11,6 +11,7 @@ import { AVATAR_URL } from 'app/constants/api'; import { AVATAR_API } from 'envConstants'; import Cross from '../../app/assets/icons/cross.svg'; import { + _FORM_SUBMIT, _VALIDATE_PROPS, _WORKSPACE_FORM, _WORKSPACE_FORM_CHANGE, @@ -76,7 +77,7 @@ const AddWorkspace = () => { } }; - const validate: _VALIDATE_PROPS = (name, value,files) => { + const validate: _VALIDATE_PROPS = (name, value, files) => { switch (name) { case 'workspace': if (!value) { @@ -89,14 +90,14 @@ const AddWorkspace = () => { return ''; case 'image': if (!FileList) { - return "File is required" + return 'File is required'; } - return '' + return ''; case 'description': - if (value.length>200) { - return "Description should be less then 200 characters" + if (value.length > 200) { + return 'Description should be less then 200 characters'; } - return '' + return ''; default: return ''; } @@ -121,59 +122,72 @@ const AddWorkspace = () => { } }; const handleBlur = (e: React.FocusEvent) => { - const { name, value,files } = e.target; - const error = validate(name, value,files); + const { name, value, files } = e.target; + const error = validate(name, value, files); setFormErrors({ ...formErrors, [name]: error, }); }; - const handleSubmit = () => {}; - - const SubmitHandler = async (): Promise => { - if ( - form.description && - token && - form.workspace - // && - // validName && - // uniqueName && - // validDescription - ) { - const func = async (): Promise => { - const dataRes = await addOrg(token, { - name: form.workspace, - description: form.description, - }); - - if (form.image) { - try { - const fileRes = await uploadIcon(token, form.workspace, form.image); - } catch (e) {} + const handleSubmit: _FORM_SUBMIT = (event) => { + event.preventDefault(); + const workspace_error = validate('workspace', form.workspace, null); + const desc_error = validate('description', form.description, null); + const image_error = form.image ? '' : 'Image is required'; + setFormErrors({ + ...formErrors, + workspace: workspace_error, + description: desc_error, + image: image_error, + }); + if (workspace_error || desc_error || image_error) + toast.error('Check workspace, icon and description fields'); + else { + if (token) { + const newForm = form; + if (!form.description) { + newForm.description = " " } + const func = async (): Promise => { + const dataRes = await addOrg(token, { + name: newForm.workspace, + description: newForm.description, + }); - if (form.members.length > 0) { - try { - const addMmebersRes = await addOrgMembers( - token, - form.workspace, - form.members - ); - } catch (e) {} - } - navigate('/'); - }; + if (form.image) { + try { + const fileRes = await uploadIcon( + token, + form.workspace, + form.image + ); + } catch (e) {} + } - toast.promise(func(), { - loading: 'Saving Workspace', - success: Workspace saved, - error: Could not save, - }); - } else { - toast.error('Invalid inputs'); + if (form.members.length > 0) { + try { + const addMmebersRes = await addOrgMembers( + token, + form.workspace, + form.members + ); + } catch (e) {} + } + navigate('/'); + }; + + toast.promise(func(), { + loading: 'Saving Workspace', + success: Workspace saved, + error: Something's wrong with your inputs, + }); + } else { + toast.error(`Something's wrong with your inputs`); + } } }; + const dataFetch = async () => { try { if (token) { @@ -207,7 +221,9 @@ const AddWorkspace = () => { noValidate >
-

Add Icon*

+

+ Add Icon* +

{ onChange={handleChange} placeholder='workspace name' /> - {formErrors.workspace &&

{formErrors.workspace}

} + {formErrors.workspace && ( +

{formErrors.workspace}

+ )}
- -
+ +