Skip to content

Commit

Permalink
🍁 [Frontend] Integrate Delete Contact Us API Fixed (#934)
Browse files Browse the repository at this point in the history
* delete contact us

* Update Contact.jsx
  • Loading branch information
Hemu21 authored May 20, 2024
1 parent b9790b3 commit 60fde3a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
4 changes: 2 additions & 2 deletions frontend/src/pages/Admin/Components/Contact/Card/Card.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import style from "./card.module.scss";

export function Card({ content: { email, message, name, subject } }) {
export function Card({ content: { email, message, name, subject }, id , handleDelete }) {
return (
<div className={style["card-item"]}>
<div className={style["card-info"]}>
Expand All @@ -12,7 +12,7 @@ export function Card({ content: { email, message, name, subject } }) {
<a href={`mailto:${email}`}>
<button className={style["button-edit"]}>Reply</button>
</a>
<button className={style["button-delete"]}>Delete</button>
<button name={`${id}`} onClick={(e)=>handleDelete(e.currentTarget.name)} className={style["button-delete"]}>Delete</button>
</div>
</div>
</div>
Expand Down
74 changes: 70 additions & 4 deletions frontend/src/pages/Admin/Components/Contact/Contact.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ import Grid from "@material-ui/core/Grid";
import { Card } from "./Card/index.js";
import style from "./contactus.module.scss";
import Loader from "../../../../components/util/Loader";
import { SimpleToast } from "../../../../components/util/Toast/Toast.jsx";

export function Contact() {
const [contactUsData, setContactUsData] = useState([]);
const [isLoaded, setIsLoaded] = useState(false);
const [toast, setToast] = useState({
toastStatus: false,
toastType: "",
toastMessage: "",
});
const fetchJoinUs = async () => {
const response = await fetch(`${END_POINT}/getContactUs`, {
setIsLoaded(true);
try{
const response = await fetch(`${END_POINT}/contactus/getcontactus`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Expand All @@ -19,26 +27,84 @@ export function Contact() {
});
const data = await response.json();
setContactUsData(data.ContactUs);
setToast({
...toast,
toastMessage: "Successfully get data!",
toastStatus: true,
toastType: "success",
});
}catch(error){
setToast({
...toast,
toastMessage: "Unable to get data!",
toastStatus: true,
toastType: "error",
});
}
setIsLoaded(false);
};
const handleCloseToast = (event, reason) => {
if (reason === "clickaway") {
return;
}
setToast({ ...toast, toastStatus: false });
};
const handleDelete = async (id) => {
try {
const url = `${END_POINT}/contactus/deleteContactUs`;
const response = await fetch(url, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
body: JSON.stringify({ contactUsId: id }),
});

const data = await response.json();
setToast({
...toast,
toastMessage: "Successfully deleted!",
toastStatus: true,
toastType: "success",
});
fetchJoinUs()
} catch (error) {
console.log(error);
setToast({
...toast,
toastMessage: "Unable to delete!",
toastStatus: true,
toastType: "error",
});
}
};
useEffect(() => {
setIsLoaded(true);
fetchJoinUs();
}, []);
return (
<div>
<h1 style={{ textAlign: "center" }}> Contact Us </h1>
<div className={style["data-loader"]}>{isLoaded ? <Loader /> : null}</div>
{isLoaded ? <div className={style["data-loader"]}><Loader /></div>:
<div className={style["card-container"]}>
<Grid container spacing={2}>
<Grid item>
{contactUsData &&
contactUsData.map((data) => {
return <Card key={data._id} content={data} />;
return <Card key={data._id} id={data._id} handleDelete={handleDelete} content={data} />;
})}
</Grid>
</Grid>
</div>
</div>}
{toast.toastStatus && (
<SimpleToast
open={toast.toastStatus}
message={toast.toastMessage}
handleCloseToast={handleCloseToast}
severity={toast.toastType}
/>
)}
</div>
);
}

0 comments on commit 60fde3a

Please sign in to comment.