diff --git a/frontend/src/pages/Resources/components/ResourceSharingForm/ResourceSharingForm.jsx b/frontend/src/pages/Resources/components/ResourceSharingForm/ResourceSharingForm.jsx index b9f6aa33..909a9a9d 100644 --- a/frontend/src/pages/Resources/components/ResourceSharingForm/ResourceSharingForm.jsx +++ b/frontend/src/pages/Resources/components/ResourceSharingForm/ResourceSharingForm.jsx @@ -1,31 +1,43 @@ import React, { useState } from "react"; +import { NavLink, useHistory } from "react-router-dom"; import Joi from "joi-browser"; import { Button2 } from "../../../../components/util/Button/index"; import style from "./resource-sharing-form.module.scss"; +import { END_POINT } from "../../../../config/api"; +import Loader from "../../../../components/util/Loader"; +import { SimpleToast } from "../../../../components/util/Toast"; export function ResourceSharingForm(props) { + const [resourceToast, setResourceToast] = useState(""); + const [openSubmitResourceSuccess, setOpenSubmitResourceSuccess] = useState( + false + ); + const [isLoading, setIsLoading] = useState(false); + const [resourceToastStatus, setResourceToastStatus] = useState(""); + let dark = props.theme; const [formdata, setFormData] = useState({ name: "", email: "", - link: "", + url: "", description: "", - trust: null, - dov: "", - info: "", + trustLevel: null, + expiryDate: "", + additionalInfo: "", }); const [formerrors, setFormErrors] = useState({}); + const [submitted, setSubmitted] = useState(false); const schema = { - name: Joi.string().required(), - email: Joi.string().email().required(), - dov: Joi.date().required(), - link: Joi.string().uri().required(), - description: Joi.string(), - trust: Joi.required(), - info: Joi.string(), + name: Joi.string().trim().required().min(3).label("Name"), + email: Joi.string().trim().email().required().label("Email"), + url: Joi.string().uri().required().label("Url"), + description: Joi.string().trim().required().min(8).label("Description"), + trustLevel: Joi.required().label("TrustLevel"), + expiryDate: Joi.date().required().label("ExpiryDate"), + additionalInfo: Joi.string().trim().label("AdditionalInfo"), }; const validate = () => { @@ -55,16 +67,15 @@ export function ResourceSharingForm(props) { } return 0; }); - if (errors["info"]) { - delete errors["info"]; - } - if (Object.keys(errors).length !== 0) { + if (errors !== 0) { setFormErrors(errors); } - if (Object.keys(errors).length !== 0) { - console.log(errors); + if (errors) { + setSubmitted(false); } else { - //Call the Server + setSubmitted(true); + submitResourceFormData(formdata); + setFormData(""); console.log("Submitted"); } }; @@ -82,6 +93,44 @@ export function ResourceSharingForm(props) { setFormErrors(errors); }; + const submitResourceFormData = async (formdata) => { + var api = `${END_POINT}/resources/`; + setIsLoading(true); + return await fetch(api, { + method: "POST", + body: JSON.stringify(formdata), + headers: { + "Content-type": "application/json", + }, + }) + .then((res) => res.json()) + .then((data) => { + if ( + data.message === "Database Error" || + data.message === "Sendgrid Error" + ) { + setIsLoading(false); + setResourceToast(data.message); + setResourceToastStatus("error"); + } else { + setIsLoading(false); + setResourceToastStatus("success"); + setResourceToast(data.message); + setOpenSubmitResourceSuccess(true); + } + }) + .catch((err) => { + console.info(err); + }); + }; + + console.log(resourceToast); + + const handleCloseResourceToast = () => { + setOpenSubmitResourceSuccess(false); + setResourceToast(""); + }; + return (
+ Sorry for the inconvenience caused, our servers are currently
+ facing some issues. 🔧
+ Please try again later!
+
+ Congratulations !!! ✨
+ Your Resource has been successfully added. 😄
+