From 42cab26c8c5297b8fff9ba00e4751fbf178d20cc Mon Sep 17 00:00:00 2001
From: Francisco Cardoso <franciscocardoso.3003@gmail.com>
Date: Sat, 11 Feb 2023 14:30:16 +0000
Subject: [PATCH] Added appropriate error messages for application

Co-authored-by: Daniel Ferreira <dsantosferreira@users.noreply.github.com>
---
 .../Apply/Company/ApplicationConfirmation.js  |  3 +-
 .../Apply/Company/CompanyApplicationUtils.js  | 30 +++++++-
 src/pages/ValidationPage.js                   | 77 ++++++++++---------
 src/services/companyApplicationService.js     |  8 +-
 4 files changed, 73 insertions(+), 45 deletions(-)

diff --git a/src/components/Apply/Company/ApplicationConfirmation.js b/src/components/Apply/Company/ApplicationConfirmation.js
index 9e28e83a..86613e17 100644
--- a/src/components/Apply/Company/ApplicationConfirmation.js
+++ b/src/components/Apply/Company/ApplicationConfirmation.js
@@ -22,7 +22,8 @@ const ApplicationConfirmation = () => {
             <CardHeader title="Application Submitted" />
             <CardContent className={classes.content}>
                 <Typography variant="body2">
-                    Application Submitted, you should receive a confirmation email shortly. If not, please contact us:
+                    Application Submitted, you should receive an email with a link to confirm your application,
+                    please confirm it in 10 minutes or else the link will expire. If you did not receive any email, please contact us:
                     {" "}
                     <Link color="secondary" href={`mailto:${Constants.CONTACT_US_EMAIL}`}>
                         {Constants.CONTACT_US_EMAIL}
diff --git a/src/components/Apply/Company/CompanyApplicationUtils.js b/src/components/Apply/Company/CompanyApplicationUtils.js
index 82038643..bd356022 100644
--- a/src/components/Apply/Company/CompanyApplicationUtils.js
+++ b/src/components/Apply/Company/CompanyApplicationUtils.js
@@ -1,7 +1,5 @@
 import { validationRulesGenerator, generalHumanError } from "../../../utils";
 import { AuthConstants } from "../../Navbar/Auth/AuthUtils";
-
-
 export const CompanyApplicationConstants = {
     password: AuthConstants.password,
     motivation: {
@@ -19,6 +17,34 @@ export const generateValidationRule = validationRulesGenerator(CompanyApplicatio
 const HumanReadableErrors = Object.freeze({
     "email-already-exists": "The provided email is already associated to our platform.",
     "company-application-duplicate-email": "There is already an application associated with that email.",
+    "company-application-created-recently": "There is an application created less than 10 minutes ago associated with this email.",
 });
 
+const ValidationErrors = Object.freeze({
+    "invalid-token": {
+        title: "Error! Application does not exist!",
+        text: "An error has occured while validating your application! The application you are trying to validate does not exist,",
+    },
+    "expired-token": {
+        title: "Error! Link has expired!",
+        text: "An error has occured while validating your application! The link sent to you has expired, \
+        you now need to create a new application,",
+    },
+    "application-already-validated": {
+        title: "Application is already validated!",
+        text: "This application is already validated",
+    },
+});
+
+export const getValidationError = (error) => {
+    const errorMsg = { title: "Unexpected Error!", text: "An unexpected error has occured while validating your application, " };
+    if (!error) {
+        return errorMsg;
+    }
+    if (typeof ValidationErrors[error] === "object") {
+        return ValidationErrors[error];
+    }
+    return errorMsg;
+};
+
 export const getHumanError = (error) => generalHumanError(error, HumanReadableErrors);
diff --git a/src/pages/ValidationPage.js b/src/pages/ValidationPage.js
index 8faaa918..00f9bd88 100644
--- a/src/pages/ValidationPage.js
+++ b/src/pages/ValidationPage.js
@@ -6,6 +6,7 @@ import { CardContent, CircularProgress, makeStyles, Button } from "@material-ui/
 import { useMobile } from "../utils/media-queries";
 import { Redirect, useParams } from "react-router-dom";
 import { validateApplication } from "../services/companyApplicationService";
+import { getValidationError } from "../components/Apply/Company/CompanyApplicationUtils.js";
 
 const useStyles = (isMobile) => makeStyles((theme) => ({
     content: {
@@ -27,7 +28,7 @@ const useStyles = (isMobile) => makeStyles((theme) => ({
 
     },
     button: {
-        background: "rgb(250,80,80)",
+        background: "rgb(250,70,70)",
         color: "white",
         marginTop: "3vh",
 
@@ -41,31 +42,44 @@ const ValidationPage = () => {
     const [loading, setLoading] = useState(true);
     const [success, setSuccess] = useState(false);
     const [buttonPressed, setButtonPressed] = useState(false);
-    const [err,setErr] = useState("");
+    const [error, setError] = useState("");
 
 
-    useEffect( () => {
-        try {
-            async function ola()
-            {
-                try {
-                    setLoading(false);
-                    setSuccess(true);
-                    await validateApplication(token);
-                } catch ( error){
-                    setLoading(false);
-                    setSuccess(false);
-                    setErr(error.message);
-                }
-
+    useEffect(() => {
+        async function validate() {
+            try {
+                setLoading(false);
+                setSuccess(true);
+                await validateApplication(token);
+            } catch (error_) {
+                setError(error_[0].msg);
+                setLoading(false);
+                setSuccess(false);
             }
-            ola();
-        } catch(error) {
-            setLoading(false);
-            setSuccess(false);
-            setErr(error.message);
+
         }
+        validate();
+
     }, [token]);
+    const errorMessage = (error) => {
+        const { title, text } = getValidationError(error);
+        return (
+            <CardContent className={classes.content}>
+                <h2 className={classes.title}>
+                    {title}
+                </h2>
+                <span className={classes.text}>
+                    {text}
+                    for more information contact us:
+                    <a href="mailto:nijobs@aefeup.pt"> nijobs@aefeup.pt</a>
+                    !
+                </span>
+                <Button className={classes.button} variant="contained" onClick={() => setButtonPressed(true) }>
+                    Click here to go to Home page
+                </Button>
+            </CardContent>
+        );
+    };
 
     if (buttonPressed) {
         return <Redirect to="/" />;
@@ -83,30 +97,17 @@ const ValidationPage = () => {
                 <CardContent className={classes.content}>
                     <h2 className={classes.title}>Your application has been validated successfully! </h2>
                     <span className={classes.text}>
-                        We will now review your application, and in case you're approved,
-                         you will receive another email with further instructions in order to complete your registration.
+                        You should receive a confirmation email shortly. If not, please contact us:
+                         <a href="mailto:nijobs@aefeup.pt"> nijobs@aefeup.pt</a>
                     </span>
                     <Button className={classes.button} variant="contained" onClick={() => setButtonPressed(true) }>
                         Click here to go to Home page
                     </Button>
                 </CardContent>
             );
-
+    } else {
+       return  errorMessage(error);
     }
 
-    return (
-        <CardContent className={classes.content}>
-            <h2 className={classes.title}> {err}! </h2>
-            <span className={classes.text}>
-                An error has occur when validating your application for more information contact
-                <a href="mailto:nijobs@aefeup.pt"> nijobs@aefeup.pt</a>
-                !
-                </span>
-            <Button className={classes.button} variant="contained" onClick={() => setButtonPressed(true) }>
-                Click here to go to Home page
-            </Button>
-        </CardContent>
-    );
 };
-
 export default ValidationPage;
diff --git a/src/services/companyApplicationService.js b/src/services/companyApplicationService.js
index ed44eea4..2181136c 100644
--- a/src/services/companyApplicationService.js
+++ b/src/services/companyApplicationService.js
@@ -75,15 +75,15 @@ export const validateApplication = async (token) => {
             credentials: "include",
 
         });
-        const json =  res.json();
-       if (! res.ok) {
+        const json = await res.json();
+        if (!res.ok) {
             throw json.errors;
         }
         return json;
 
     } catch (error) {
-        console.log(error);
-        const errorArray = Array.isArray(error) ? new Error(error.message) : new Error(Constants.UNEXPECTED_ERROR_MESSAGE);
+        const errorArray = Array.isArray(error) ? error :
+            [{ msg: Constants.UNEXPECTED_ERROR_MESSAGE }];
         throw errorArray;
     }