Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

feat(annuaire): remove load more psy functionality (voir plus de psy - pagination) to avoid people to crawl all the data #126

Merged
merged 1 commit into from
Mar 31, 2022
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
41 changes: 21 additions & 20 deletions src/__tests__/api/contact.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable jest/no-conditional-expect */
import axios from "axios";

import config from "../../services/config";
Expand Down Expand Up @@ -62,30 +61,32 @@ describe("/api/contact", () => {
];
errors.map((error) =>
it(`Should return a 400 for a contact ${error.label}`, async () => {
expect.assertions(2);
try {
const defaultValues = {
department: "06 - Alpes-Maritimes",
email: "jane.dane@msp.fr",
firstName: "Jane",
lastName: "Dane",
message: "Hello you !",
userType: CONTACT_USER_TYPE.OTHER,
};
let result;

const resutSuccess = await axios.post(
`${config.nextAuthUrl}/api/contact`,
defaultValues
);
expect(resutSuccess.status).toEqual(200);
const defaultValues = {
department: "06 - Alpes-Maritimes",
email: "jane.dane@msp.fr",
firstName: "Jane",
lastName: "Dane",
message: "Hello you !",
userType: CONTACT_USER_TYPE.OTHER,
};

await axios.post(`${config.nextAuthUrl}/api/contact`, {
const resutSuccess = await axios.post(
`${config.nextAuthUrl}/api/contact`,
defaultValues
);
expect(resutSuccess.status).toEqual(200);

await axios
.post(`${config.nextAuthUrl}/api/contact`, {
...defaultValues,
...error.values,
})
.catch((e) => {
result = e;
});
} catch (e) {
expect(e.response.status).toEqual(400);
}
expect(result.response.status).toEqual(400);
})
);
});
15 changes: 11 additions & 4 deletions src/__tests__/api/psychologists.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,24 @@ import axios from "axios";
import config from "../../services/config";

describe("/api/psychologists", () => {
it("should return bad request if pass wrong params in query", async () => {
let result;
await axios
.get(`${config.nextAuthUrl}/api/psychologists?wrong=0`)
.catch((e) => {
result = e;
});
expect(result.response.status).toEqual(400);
});
it("should return empty array if no params", async () => {
const result = await axios.get(
`${config.nextAuthUrl}/api/psychologists?pageindex=0`
);
const result = await axios.get(`${config.nextAuthUrl}/api/psychologists`);

expect(result.status).toEqual(200);
expect(result.data.length).toBeGreaterThan(1);
});
it("should return some results if coordinates passed", async () => {
const result = await axios.get(
`${config.nextAuthUrl}/api/psychologists?pageindex=0&longitude=1.4328&latitude=43.6007`
`${config.nextAuthUrl}/api/psychologists?longitude=1.4328&latitude=43.6007`
);

expect(result.status).toEqual(200);
Expand Down
5 changes: 1 addition & 4 deletions src/components/Directory/Results.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Button, Col } from "@dataesr/react-dsfr";
import { Col } from "@dataesr/react-dsfr";
import React, { Dispatch, MutableRefObject, SetStateAction } from "react";

import { Coordinates } from "../../types/coordinates";
Expand All @@ -9,7 +9,6 @@ import PsychologistsMap from "./PsychologistsMap";

const Results = ({
psychologists,
loadMorePsychologists,
resultsRef,
psychologistsRefs,
selectedPsychologist,
Expand All @@ -20,7 +19,6 @@ const Results = ({
setMapZoom,
}: {
psychologists: PsychologistType[];
loadMorePsychologists: () => void;
resultsRef: MutableRefObject<any>;
psychologistsRefs: any;
selectedPsychologist: number;
Expand Down Expand Up @@ -61,7 +59,6 @@ const Results = ({
</div>
</div>
))}
<Button onClick={loadMorePsychologists}>Plus de psychologues</Button>
</Psychologists>
<Col n="12 md-7">
{mapCenter && (
Expand Down
6 changes: 2 additions & 4 deletions src/components/Directory/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const SearchBar = ({
setCoords: Dispatch<SetStateAction<Coordinates>>;
geoLoading: boolean;
setGeoLoading: Dispatch<SetStateAction<boolean>>;
loadPsychologists: (page: number) => void;
loadPsychologists: () => void;
}) => {
const [filterText, setFilterText] = useState("");
const [geoStatus, setGeoStatus] = useState(geoStatusEnum.UNKNOWN);
Expand Down Expand Up @@ -151,9 +151,7 @@ const SearchBar = ({
<Button
className="fr-ml-1w fr-mt-1w"
disabled={!coords || geoLoading}
onClick={() => {
loadPsychologists(0);
}}
onClick={loadPsychologists}
>
{geoLoading ? "Chargement..." : "Rechercher"}
</Button>
Expand Down
35 changes: 10 additions & 25 deletions src/components/Directory/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import SearchBar from "./SearchBar";
const Directory = () => {
const router = useRouter();

const currentPageRef = useRef(0);

const [coords, setCoords] = useState<Coordinates>();
const [geoLoading, setGeoLoading] = useState(false);

Expand All @@ -36,11 +34,6 @@ const Directory = () => {
[FILTER.PUBLIC]: PUBLIC.BOTH,
});

const loadMorePsychologists = () => {
loadPsychologists(currentPageRef.current + 1);
currentPageRef.current = currentPageRef.current + 1;
};

const APPROX_50_KM = 0.5;
const APPROX_40_KM = 0.4;

Expand All @@ -55,8 +48,8 @@ const Directory = () => {
}
}

const loadPsychologists = (currentPage) => {
let query = `?${FILTER.PAGE_INDEX}=${currentPage}`;
const loadPsychologists = () => {
let query = `?`;
setNoPsychologist(false);
setIsLoading(true);
setPsychologists([]);
Expand All @@ -73,22 +66,15 @@ const Directory = () => {
axios.get(`/api/psychologists${query}`).then((response) => {
setIsLoading(false);

if (currentPage === 0) {
const refs = {};
setupMapAccordingToPsy(response.data[0].distance);
response.data.forEach((x) => (refs[x.id] = createRef()));
psychologistsRefs.current = refs;
if (resultsRef.current) {
resultsRef.current.scrollTo({ top: 0 });
}
setSelectedPsychologist(null);
setPsychologists(response.data);
} else {
response.data.forEach(
(x) => (psychologistsRefs.current[x.id] = createRef())
);
setPsychologists(psychologists.concat(response.data));
const refs = {};
setupMapAccordingToPsy(response.data[0].distance);
response.data.forEach((x) => (refs[x.id] = createRef()));
psychologistsRefs.current = refs;
if (resultsRef.current) {
resultsRef.current.scrollTo({ top: 0 });
}
setSelectedPsychologist(null);
setPsychologists(response.data);
});
};

Expand Down Expand Up @@ -152,7 +138,6 @@ A noter, certains psychologues acceptent les séances à distance après la 1èr
)}
{psychologists?.length > 0 && (
<Results
loadMorePsychologists={loadMorePsychologists}
psychologists={psychologists}
resultsRef={resultsRef}
psychologistsRefs={psychologistsRefs}
Expand Down
9 changes: 9 additions & 0 deletions src/pages/api/psychologists/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@ import { NextApiRequest, NextApiResponse } from "next";
import { handleApiError } from "../../../services/api";
import config from "../../../services/config";
import { getAll } from "../../../services/psychologists";
import { API_ENDPOINT_FILTER } from "../../../types/enums/filters";

const FILTERS = Object.values(API_ENDPOINT_FILTER);
function hasParamsNotAllowed(filters) {
return Object.keys(filters).filter((q) => !FILTERS.includes(q)).length;
}

const psychologists = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === "GET") {
if (!config.newFeatures) {
return res.status(200).json([]);
}
const filters = req.query;
if (hasParamsNotAllowed(filters)) {
return res.status(400).send("Query params not alloaed");
}
const psychologists = await getAll(filters);

return res.status(200).json(
Expand Down
6 changes: 5 additions & 1 deletion src/types/enums/filters.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
export enum FILTER {
enum FILTER {
LONGITUDE = "longitude",
LATITUDE = "latitude",
PAGE_INDEX = "pageindex",
PAGE_SIZE = "pagesize",
TELECONSULTATION = "teleconsultation",
PUBLIC = "public",
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { PAGE_INDEX, PAGE_SIZE, ...API_ENDPOINT_FILTER } = FILTER;
export { API_ENDPOINT_FILTER, FILTER };