Skip to content

Commit

Permalink
Merge pull request #302 from NIAEFEUP/feature/prepend-mailto
Browse files Browse the repository at this point in the history
Prepend mailto: when company inserts an email in one click application
  • Loading branch information
Process-ing authored Feb 28, 2023
2 parents 751ab78 + a2661d9 commit 6d13452
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/components/Offers/Edit/EditOfferForm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useCallback, useContext, useEffect, useState } from "react";
import { OfferConstants, parseRequestErrors } from "../Form/OfferUtils";
import { OfferConstants, parseApplyURL, parseRequestErrors } from "../Form/OfferUtils";
import OfferForm from "../Form/form-components/OfferForm";
import { editOffer } from "../../../services/offerService";
import { Redirect, useLocation, useParams } from "react-router-dom";
Expand Down Expand Up @@ -28,6 +28,7 @@ const parseOfferForm = ({
isPaid,
vacancies,
description,
applyURL,
...offer
}) => ({
jobDuration: [
Expand All @@ -41,6 +42,7 @@ const parseOfferForm = ({
vacancies: vacancies || "",
description,
descriptionText: parseDescription(description),
applyURL: applyURL.startsWith("mailto:") ? applyURL.substring(7) : applyURL,
...offer,
});

Expand Down Expand Up @@ -107,7 +109,7 @@ export const EditOfferController = () => {
requirements: data.requirements.map((val) => val.value),
isPaid: data.isPaid === "none" ? null : data.isPaid,
jobStartDate: !data.jobStartDate ? null : data.jobStartDate,
applyURL: data.applyURL || null,
applyURL: parseApplyURL(data.applyURL),
jobMinDuration,
jobMaxDuration,
})
Expand Down
8 changes: 8 additions & 0 deletions src/components/Offers/Edit/EditOfferForm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,14 @@ describe("Edit Offer Form", () => {
fireEvent.blur(input);
});

expect(await wrapper.findDescriptionOf(wrapper.getByLabelText("Application URL")))
.not.toHaveTextContent(HumanValidationReasons.BAD_APPLY_URL);

await act(() => {
fireEvent.change(input, { target: { value: "[email protected]" } });
fireEvent.blur(input);
});

expect(await wrapper.findDescriptionOf(wrapper.getByLabelText("Application URL")))
.not.toHaveTextContent(HumanValidationReasons.BAD_APPLY_URL);
});
Expand Down
11 changes: 10 additions & 1 deletion src/components/Offers/Form/OfferUtils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { generalHumanError, generalParseRequestErrors, HumanValidationReasons, validationRulesGenerator } from "../../../utils";
import { MailRegex } from "../../../utils/offer/OfferUtils";
import { DAY_IN_MS, MONTH_IN_MS, OFFER_MAX_LIFETIME_MONTHS } from "../../../utils/TimeUtils";
import { MAX_FIELDS, MIN_FIELDS } from "../../utils/offers/FieldOptions";
import { MAX_TECHNOLOGIES, MIN_TECHNOLOGIES } from "../../utils/offers/TechOptions";
Expand Down Expand Up @@ -53,9 +54,17 @@ const HumanReadableErrors = Object.freeze({
"company-disabled": () => "Company is disabled. Please enable it or contact the team for help.",
"must-be-ISO8601-date": () => HumanValidationReasons.DATE,
"date-already-past": () => HumanValidationReasons.DATE_EXPIRED,
"invalid-apply-url": () => "Invalid application URL. Ensure your URL starts with 'http(s):' or 'mailto:'",
"invalid-apply-url": () => "Invalid application URL. Ensure your URL starts with 'http(s):' or is a valid email",
});

export const getHumanError = (error) => generalHumanError(error, HumanReadableErrors);

export const parseRequestErrors = (error) => generalParseRequestErrors(error, getHumanError);

export const parseApplyURL = (applyURL) => {
if (!applyURL)
return null;
if (MailRegex.test(applyURL) && !applyURL.startsWith("mailto:"))
return `mailto:${applyURL}`;
return applyURL;
};
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const ApplyURLComponent = ({ disabled, errors, requestErrors, control, classes }
onChange={onChange}
helperText={
`${errors.applyURL?.message ||
requestErrors.applyURL?.message || "Ensure your URL starts with 'http(s):' or 'mailto:'."}`
requestErrors.applyURL?.message || "Ensure your URL starts with 'http(s):' or is a valid email."}`
}
fullWidth
disabled={disabled}
Expand Down
4 changes: 2 additions & 2 deletions src/components/Offers/New/CreateOfferForm.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import React, { useCallback } from "react";
import { parseRequestErrors } from "../Form/OfferUtils";
import { parseApplyURL, parseRequestErrors } from "../Form/OfferUtils";
import { newOffer } from "../../../services/offerService";
import useOfferForm from "../../../hooks/useOfferForm";
import OfferForm from "../Form/form-components/OfferForm";
Expand All @@ -23,7 +23,7 @@ export const CreateOfferController = () => {
isPaid: data.isPaid === "none" ? undefined : data.isPaid,
jobStartDate: !data.jobStartDate ? undefined : data.jobStartDate,
owner: data.owner || params.company,
applyURL: data.applyURL || undefined,
applyURL: parseApplyURL(data.applyURL),
jobMinDuration,
jobMaxDuration,
})
Expand Down
9 changes: 9 additions & 0 deletions src/components/Offers/New/CreateOfferForm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,15 @@ describe("Create Offer Form", () => {

expect(await wrapper.findDescriptionOf(wrapper.getByLabelText("Application URL")))
.not.toHaveTextContent(HumanValidationReasons.BAD_APPLY_URL);

await act(() => {
fireEvent.focus(input);
fireEvent.change(input, { target: { value: "[email protected]" } });
fireEvent.blur(input);
});

expect(await wrapper.findDescriptionOf(input))
.not.toHaveTextContent(HumanValidationReasons.BAD_APPLY_URL);
});

it("should be visible advanced settings if form error in these publication date", async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/offer/OfferUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ const HumanReadableErrors = Object.freeze({
export const getHumanError = (error) => generalHumanError(error, HumanReadableErrors);

export const HTTPRegex = /^https?:\/\/\S+\.\S+$/;
export const MailRegex = /^mailto:(\S+@\S+)$/;
export const MailRegex = /^(\S+@\S+)$/;

export const validApplyURLRegex = new RegExp(`${HTTPRegex.source}|${MailRegex.source}`);

0 comments on commit 6d13452

Please sign in to comment.