Skip to content

Commit

Permalink
fix(edit-modal): open profil modal if no selected profil
Browse files Browse the repository at this point in the history
  • Loading branch information
Mihoub2 committed Jun 17, 2024
1 parent 68ccecf commit 904f57f
Show file tree
Hide file tree
Showing 7 changed files with 362 additions and 303 deletions.
160 changes: 98 additions & 62 deletions src/api/send-mail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ import { Button, Col, Container, Row, TextArea } from "@dataesr/dsfr-plus";
import { Contribution, Contribute_Production } from "../../types";
import { postHeaders } from "../../config/api";
import { toast } from "react-toastify";
import ProfileModal from "../../components/profil-modal";

function EmailSender({
contribution,
refetch,
}: {
type EmailSenderProps = {
contribution: Contribution | Contribute_Production;
refetch;
}) {
refetch: () => void;
};

function EmailSender({ contribution, refetch }: EmailSenderProps) {
const [, setEmailSent] = useState(false);
const [userResponse, setUserResponse] = useState("");
const [showProfileModal, setShowProfileModal] = useState(false);
const [selectedProfile, setSelectedProfile] = useState(
sessionStorage.getItem("selectedProfile") || ""
);

const isDevelopment = import.meta.env.VITE_HEADER_TAG === "Development";
let basePath = "contact";
if (window.location.pathname.includes("contributionpage")) {
Expand All @@ -26,15 +31,24 @@ function EmailSender({
const scanRUrl = isDevelopment
? `https://scanr-api.dataesr.ovh/${basePath}/${contribution?._id}`
: `/api/${basePath}/${contribution?._id}`;
const [selectedProfile, setSelectedProfile] = useState("");

useEffect(() => {
const profileFromLocalStorage = sessionStorage.getItem("selectedProfile");
if (profileFromLocalStorage) {
setSelectedProfile(profileFromLocalStorage);
}
}, []);

const sendEmail = async () => {
if (
!selectedProfile ||
selectedProfile === "null" ||
selectedProfile === ""
) {
setShowProfileModal(true);
return;
}

const dataForBrevo = {
sender: {
email: "[email protected]",
Expand All @@ -48,70 +62,92 @@ function EmailSender({
],
subject: `Réponse à votre contribution`,
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>
`,
<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(brevoUrl, {
method: "POST",
headers: {
"api-key": import.meta.env.VITE_BREVO_API_AUTHORIZATION,
},
body: JSON.stringify(dataForBrevo),
});

if (!responseBrevo.ok) {
throw new Error(`HTTP error! status: ${responseBrevo.status}`);
}
try {
const responseBrevo = await fetch(brevoUrl, {
method: "POST",
headers: {
"api-key": import.meta.env.VITE_BREVO_API_AUTHORIZATION,
"Content-Type": "application/json",
},
body: JSON.stringify(dataForBrevo),
});

const dataForScanR = {
mailSent: userResponse,
mailSentDate: new Date(),
responseFrom: selectedProfile,
};
if (!responseBrevo.ok) {
throw new Error(`HTTP error! status: ${responseBrevo.status}`);
}

const dataForScanR = {
mailSent: userResponse,
mailSentDate: new Date(),
responseFrom: selectedProfile,
};

const responseScanR = await fetch(scanRUrl, {
method: "PATCH",
headers: postHeaders,
body: JSON.stringify(dataForScanR),
});
const responseScanR = await fetch(scanRUrl, {
method: "PATCH",
headers: postHeaders,
body: JSON.stringify(dataForScanR),
});

if (!responseScanR.ok) {
throw new Error(`HTTP error! status: ${responseScanR.status}`);
if (!responseScanR.ok) {
throw new Error(`HTTP error! status: ${responseScanR.status}`);
}

setEmailSent(true);
refetch();
setUserResponse("");
toast.success("Mail envoyé!");
} catch (error) {
console.error("Erreur lors de l'envoi du mail", error);
toast.error("Erreur lors de l'envoi du mail");
}
setEmailSent(true);
refetch();
setUserResponse("");
toast.success("Mail envoyé!");
};

const handleProfileSelect = (profile) => {
setSelectedProfile(profile);
sessionStorage.setItem("selectedProfile", profile);
setShowProfileModal(false);
};

return (
<Container>
<Row gutters>
<Col offsetMd="2" md="8">
<TextArea
value={userResponse}
onChange={(e) => setUserResponse(e.target.value)}
placeholder="Votre réponse..."
rows={2}
/>
</Col>
<Col>
<Button
className="fr-mt-1w"
variant="secondary"
onClick={sendEmail}
size="sm"
>
{contribution?.mailSent ? "Renvoyer un mail" : "Répondre"}
</Button>
</Col>
</Row>
</Container>
<>
<Container>
<Row gutters>
<Col offsetMd="2" md="8">
<TextArea
value={userResponse}
onChange={(e) => setUserResponse(e.target.value)}
placeholder="Votre réponse..."
rows={2}
/>
</Col>
<Col>
<Button
className="fr-mt-1w"
variant="secondary"
onClick={sendEmail}
size="sm"
>
{contribution?.mailSent ? "Renvoyer un mail" : "Répondre"}
</Button>
</Col>
</Row>
</Container>
<ProfileModal
isOpen={showProfileModal}
selectedProfile={selectedProfile}
onClose={() => setShowProfileModal(false)}
onSelectProfile={handleProfileSelect}
/>
</>
);
}

Expand Down
Loading

0 comments on commit 904f57f

Please sign in to comment.