Skip to content

Commit

Permalink
Moved parseApplyURL to OfferUtils.js.
Browse files Browse the repository at this point in the history
Used startWith to verify 'mailto:' prefix.
  • Loading branch information
Process-ing committed Feb 8, 2023
1 parent 1b34491 commit 6f9c38d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 21 deletions.
13 changes: 2 additions & 11 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 All @@ -8,7 +8,6 @@ import useOfferForm from "../../../hooks/useOfferForm";
import { INITIAL_JOB_DURATION } from "../../../reducers/searchOffersReducer";
import useSession from "../../../hooks/useSession";
import EditOfferSchema from "./EditOfferSchema";
import { MailRegex } from "../../../utils/offer/OfferUtils";

export const EditOfferControllerContext = React.createContext();

Expand Down Expand Up @@ -43,18 +42,10 @@ const parseOfferForm = ({
vacancies: vacancies || "",
description,
descriptionText: parseDescription(description),
applyURL: /^mailto:/.test(applyURL) ? applyURL.substring(7) : applyURL,
applyURL: applyURL.startsWith("mailto:") ? applyURL.substring(7) : applyURL,
...offer,
});

const parseApplyURL = (applyURL) => {
if (!applyURL)
return null;
if (MailRegex.test(applyURL) && /^(?!mailto:)/.test(applyURL))
return `mailto:${applyURL}`;
return applyURL;
};

export const EditOfferController = () => {
const { id } = useParams();
const { offer, error: errorOffer, loading: loadingOffer } = useOffer(id);
Expand Down
9 changes: 9 additions & 0 deletions 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 @@ -59,3 +60,11 @@ const HumanReadableErrors = Object.freeze({
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;
};
11 changes: 1 addition & 10 deletions src/components/Offers/New/CreateOfferForm.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@

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";
import CreateOfferSchema from "./CreateOfferSchema";
import { MailRegex } from "../../../utils/offer/OfferUtils";

const parseApplyURL = (applyURL) => {
if (!applyURL)
return null;
if (MailRegex.test(applyURL) && /^(?!mailto:)/.test(applyURL))
return `mailto:${applyURL}`;
return applyURL;
};

export const CreateOfferControllerContext = React.createContext();

Expand Down

0 comments on commit 6f9c38d

Please sign in to comment.