-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(edit-modal): open profil modal if no selected profil
- Loading branch information
Showing
7 changed files
with
362 additions
and
303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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")) { | ||
|
@@ -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]", | ||
|
@@ -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} | ||
/> | ||
</> | ||
); | ||
} | ||
|
||
|
Oops, something went wrong.