Skip to content

Commit

Permalink
feat(requests): add useQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihoub2 committed May 29, 2024
1 parent bea7392 commit 06e08b7
Show file tree
Hide file tree
Showing 27 changed files with 532 additions and 166 deletions.
109 changes: 98 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
"dependencies": {
"@dataesr/dsfr-plus": "^0.3.2",
"@getbrevo/brevo": "^2.0.0-beta.4",
"@tanstack/react-query": "^4.29.5",
"@tanstack/react-query": "^4.36.1",
"@tanstack/react-query-devtools": "^4.29.6",
"classnames": "^2.3.2",
"highcharts": "^11.4.1",
"highcharts-react-official": "^3.2.1",
"prop-types": "^15.8.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-query": "^3.39.3",
"react-router-dom": "^6.11.1",
"react-select": "^5.8.0",
"react-toastify": "^10.0.5",
Expand All @@ -41,4 +42,4 @@
"typescript": "^5.2.2",
"vite": "^5.0.4"
}
}
}
32 changes: 32 additions & 0 deletions src/api/contribution-api/getData.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useQuery } from "@tanstack/react-query";
import { postHeaders } from "../../config/api";
import { buildURL } from "../utils/buildURL";

const ContributionData = ({
location,
sort,
status,
query,
page,
searchInMessage,
}) => {
const fetchContributions = async () => {
const url = buildURL(location, sort, status, query, page, searchInMessage);
const response = await fetch(url, {
headers: postHeaders,
});
if (!response.ok) {
throw new Error("Network response was not ok");
}
return response.json();
};

const { data, isLoading, isError, refetch } = useQuery(
[location, sort, status, query, page, searchInMessage],
fetchContributions
);

return { data, isLoading, isError, refetch };
};

export default ContributionData;
42 changes: 25 additions & 17 deletions src/api/send-mail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { toast } from "react-toastify";

function EmailSender({
contribution,
setResponseScanR,
refetch,
}: {
contribution: Contribution | Contribute_Production;
setResponseScanR: any;
refetch;
}) {
const [, setEmailSent] = useState(false);
const [userResponse, setUserResponse] = useState("");
Expand Down Expand Up @@ -38,15 +38,22 @@ function EmailSender({
to: [
{
email: "[email protected]",
name: "Mihoub mihoub",
name: "Mihoub Debache",
},
],
subject: `Réponse à votre contribution`,
htmlContent: userResponse,
htmlContent: `
<h1>Réponse à votre contribution</h1>
<p>Bonjour,</p>
<p>En réponse à votre contribution :</p>
<blockquote>${contribution.message}</blockquote>
<p>Voici notre réponse :</p>
<p>${userResponse}</p>
`,
};

const responseBrevo = await fetch("/email/", {
// const responseBrevo = await fetch("https://api.brevo.com/v3/smtp/email", {
console.log(contribution);
// const responseBrevo = await fetch("/email/", {
const responseBrevo = await fetch("https://api.brevo.com/v3/smtp/email", {
method: "POST",
headers: {
"api-key": import.meta.env.VITE_BREVO_API_AUTHORIZATION,
Expand All @@ -64,22 +71,23 @@ function EmailSender({
responseFrom: selectedProfile,
};

const responseScanR = await fetch(`/api/${basePath}/${contribution._id}`, {
// const responseScanR = await fetch(
// `https://scanr-api.dataesr.ovh/${basePath}/${contribution._id}`,
// {
method: "PATCH",
headers: postHeaders,
body: JSON.stringify(dataForScanR),
});
// const responseScanR = await fetch(`/api/${basePath}/${contribution._id}`, {
const responseScanR = await fetch(
`https://scanr-api.dataesr.ovh/${basePath}/${contribution._id}`,
{
method: "PATCH",
headers: postHeaders,
body: JSON.stringify(dataForScanR),
}
);

if (!responseScanR.ok) {
throw new Error(`HTTP error! status: ${responseScanR.status}`);
}
setResponseScanR(dataForScanR);
setEmailSent(true);
toast.success("Mail envoyé!");
refetch();
setUserResponse("");
toast.success("Mail envoyé!");
};

return (
Expand Down
Loading

0 comments on commit 06e08b7

Please sign in to comment.