Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to edit offer redirects you to home page #324

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/components/Offers/Edit/EditOfferForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import useOfferForm from "../../../hooks/useOfferForm";
import { INITIAL_JOB_DURATION } from "../../../reducers/searchOffersReducer";
import useSession from "../../../hooks/useSession";
import EditOfferSchema from "./EditOfferSchema";
import { CardContent, CircularProgress, Grid } from "@material-ui/core";

export const EditOfferControllerContext = React.createContext();

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

Expand All @@ -59,11 +60,11 @@ export const EditOfferController = () => {
(offer?.owner === user?.company?._id) || user?.isAdmin);
}, [offer, user]);

const [canEdit, setCanEdit] = useState(shouldRevalidateEditingPermissions());
const [canEdit, setCanEdit] = useState(undefined);

useEffect(() => {
setCanEdit(shouldRevalidateEditingPermissions());
}, [shouldRevalidateEditingPermissions, loadingOffer, offer, user]);
if (!isValidating && !loadingOffer) setCanEdit(shouldRevalidateEditingPermissions());
}, [isValidating, loadingOffer, shouldRevalidateEditingPermissions]);

const location = useLocation();

Expand Down Expand Up @@ -154,11 +155,25 @@ const EditOfferForm = () => {
loadingOffer,
errorOffer,
redirectProps,
isValidating,
canEdit,
} = useContext(EditOfferControllerContext);

if (errorOffer || (!loadingOffer && !isValidating && canEdit === false)) {
if (canEdit === undefined)
return (
<Grid
container
direction="column"
alignItems="center"
>
<Grid item xs={3}>
<CardContent>
<CircularProgress />
</CardContent>
</Grid>
</Grid>
);

if (errorOffer || canEdit === false) {
return <Redirect {...redirectProps} />;
}

Expand Down
20 changes: 19 additions & 1 deletion src/components/Offers/Edit/EditOfferForm.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,23 @@ describe("Edit Offer Form", () => {

expect(screen.findByText("Test Redirect"));
});
it("should show circular progress bar while data is being fetch", () => {
useSession.mockImplementation(() => ({ isValidating: true, isLoggedIn: true, data: { company: { _id: "company_id" } } }));
useOffer.mockImplementation(() => ({ offer, loading: false, error: null, mutate: () => {} }));

renderWithStoreAndTheme(
<BrowserRouter>
<MuiPickersUtilsProvider utils={DateFnsUtils}>
<EditOfferWrapper>
<EditOfferPage />
</EditOfferWrapper>
</MuiPickersUtilsProvider>
</BrowserRouter>,
{ initialState, theme }
);
expect(screen.queryByRole("progressbar")).toBeInTheDocument();

});

it("should render enabled form", () => {
useSession.mockImplementation(() => ({ isLoggedIn: true, data: { company: { _id: "company_id" } } }));
Expand Down Expand Up @@ -579,7 +596,8 @@ describe("Edit Offer Form", () => {
});

it("should fail validation if applyURL not following the regex", async () => {
useSession.mockImplementation(() => ({ isLoggedIn: true, data: { company: { name: "Company Name" } } }));
useSession.mockImplementation(() => ({ isLoggedIn: true, data: { company: { _id: "company_id" } } }));
useOffer.mockImplementation(() => ({ offer, loading: false, error: null, mutate: () => {} }));

const wrapper = renderWithStoreAndTheme(
<BrowserRouter>
Expand Down
3 changes: 2 additions & 1 deletion src/components/Offers/New/CreateOfferForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
(data) => {
params.setLoading(true);
const [jobMinDuration, jobMaxDuration] = data.jobDuration;
const parsedApplyURL = parseApplyURL(data.applyURL);

Check warning on line 18 in src/components/Offers/New/CreateOfferForm.js

View check run for this annotation

Codecov / codecov/patch

src/components/Offers/New/CreateOfferForm.js#L18

Added line #L18 was not covered by tests
newOffer({
...data,
vacancies: data.vacancies || undefined,
Expand All @@ -23,7 +24,7 @@
isPaid: data.isPaid === "none" ? undefined : data.isPaid,
jobStartDate: !data.jobStartDate ? undefined : data.jobStartDate,
owner: data.owner || params.company,
applyURL: parseApplyURL(data.applyURL),
applyURL: !parsedApplyURL ? undefined : parsedApplyURL,

Check warning on line 27 in src/components/Offers/New/CreateOfferForm.js

View check run for this annotation

Codecov / codecov/patch

src/components/Offers/New/CreateOfferForm.js#L27

Added line #L27 was not covered by tests
jobMinDuration,
jobMaxDuration,
})
Expand Down
Loading