-
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(emailsender): add badge color for types, rework message display, …
…add toast
- Loading branch information
Showing
9 changed files
with
155 additions
and
64 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
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 |
---|---|---|
@@ -1,17 +1,10 @@ | ||
import { useEffect, useState } from "react"; | ||
import { | ||
Button, | ||
Col, | ||
Container, | ||
Row, | ||
Text, | ||
TextArea, | ||
} from "@dataesr/dsfr-plus"; | ||
import { Button, Col, Container, Row, TextArea } from "@dataesr/dsfr-plus"; | ||
import { Contribution } from "../../types"; | ||
import { postHeaders } from "../../config/api"; | ||
|
||
import { toast } from "react-toastify"; | ||
function EmailSender({ contribution }: { contribution: Contribution }) { | ||
const [emailSent, setEmailSent] = useState(false); | ||
const [, setEmailSent] = useState(false); | ||
const [userResponse, setUserResponse] = useState(""); | ||
const basePath = window.location.pathname.includes("contact") | ||
? "contact" | ||
|
@@ -26,7 +19,7 @@ function EmailSender({ contribution }: { contribution: Contribution }) { | |
} | ||
}, []); | ||
const sendEmail = async () => { | ||
const data = { | ||
const dataForBrevo = { | ||
sender: { | ||
email: "[email protected]", | ||
name: "Debache ", | ||
|
@@ -41,31 +34,35 @@ function EmailSender({ contribution }: { contribution: Contribution }) { | |
htmlContent: userResponse, | ||
}; | ||
|
||
const response = await fetch("/email/", { | ||
const responseBrevo = await fetch("/email/", { | ||
method: "POST", | ||
headers: { | ||
"api-key": import.meta.env.VITE_BREVO_API_AUTHORIZATION, | ||
}, | ||
body: JSON.stringify(data), | ||
body: JSON.stringify(dataForBrevo), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
if (!responseBrevo.ok) { | ||
throw new Error(`HTTP error! status: ${responseBrevo.status}`); | ||
} | ||
|
||
const responsePatch = await fetch(`/api/${basePath}/${contribution._id}`, { | ||
const dataForScanR = { | ||
comment: userResponse, | ||
responseFrom: selectedProfile, | ||
}; | ||
|
||
const responseScanR = await fetch(`/api/${basePath}/${contribution._id}`, { | ||
method: "PATCH", | ||
headers: postHeaders, | ||
body: JSON.stringify(data), | ||
body: JSON.stringify(dataForScanR), | ||
}); | ||
|
||
if (!responsePatch.ok) { | ||
throw new Error( | ||
`Erreur pendant la mise à jour de l'api ! status: ${response.status}` | ||
); | ||
if (!responseScanR.ok) { | ||
throw new Error(`HTTP error! status: ${responseScanR.status}`); | ||
} | ||
|
||
setEmailSent(true); | ||
toast.success("Mail envoyé!"); | ||
}; | ||
|
||
return ( | ||
|
@@ -79,12 +76,11 @@ function EmailSender({ contribution }: { contribution: Contribution }) { | |
rows={2} | ||
/> | ||
</Col> | ||
<Col className="fr-mt-10w"> | ||
<Col> | ||
<Button variant="secondary" onClick={sendEmail} size="sm"> | ||
{contribution.comment ? "Renvoyer un mail" : "Répondre"} | ||
</Button> | ||
</Col> | ||
{emailSent && <Text>Mail envoyé!</Text>} | ||
</Row> | ||
</Container> | ||
); | ||
|
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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export const BadgeColor = ({ type }) => { | ||
let badgeColor; | ||
switch (type) { | ||
case "structures": | ||
badgeColor = "yellow-tournesol"; | ||
break; | ||
case "persons": | ||
badgeColor = "orange-terre-battue"; | ||
break; | ||
case "publications": | ||
badgeColor = "pink-macaron"; | ||
break; | ||
case "project": | ||
badgeColor = "green-emeraude"; | ||
break; | ||
case "patent": | ||
badgeColor = "blue-ecume"; | ||
break; | ||
default: | ||
badgeColor = "purple-glycine"; | ||
} | ||
return badgeColor; | ||
}; |
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