-
Notifications
You must be signed in to change notification settings - Fork 317
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
60fde3a
commit 3c13683
Showing
4 changed files
with
212 additions
and
128 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
224 changes: 152 additions & 72 deletions
224
frontend/src/pages/Admin/Components/Faq/ManageFaq/ManageFaq.jsx
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,86 +1,166 @@ | ||
import React from "react" | ||
import React from "react"; | ||
import { useEffect, useState } from "react"; | ||
import Accordion from "@material-ui/core/Accordion"; | ||
import AccordionDetails from "@material-ui/core/AccordionDetails"; | ||
import AccordionSummary from "@material-ui/core/AccordionSummary"; | ||
import Typography from "@material-ui/core/Typography"; | ||
import Button from "@material-ui/core/Button" | ||
import { Edit, Delete } from "@material-ui/icons" | ||
import Button from "@material-ui/core/Button"; | ||
import { Edit, Delete } from "@material-ui/icons"; | ||
import ExpandMoreIcon from "@material-ui/icons/ExpandMore"; | ||
import { END_POINT } from "../../../../../config/api"; | ||
import Loader from "../../../../../components/util/Loader"; | ||
import style from "./manage-faq.module.scss" | ||
export function ManageFaq() { | ||
const [faqs, setFaqs] = useState([]); | ||
const [expanded, setExpanded] = React.useState(false); | ||
const [isFetching, setIsFetching] = useState(false) | ||
const handleChange = (panel) => (event, isExpanded) => { | ||
setExpanded(isExpanded ? panel : false); | ||
import style from "./manage-faq.module.scss"; | ||
import { SimpleToast } from "../../../../../components/util/Toast"; | ||
|
||
export function ManageFaq(props) { | ||
const [faqs, setFaqs] = useState([]); | ||
const [expanded, setExpanded] = useState(false); | ||
const [isFetching, setIsFetching] = useState(true); | ||
const [loading, setLoading] = useState(false); | ||
const [open, setOpenToast] = useState(false); | ||
const [toastMessage, setToastMessage] = useState(""); | ||
const [severity, setSeverity] = useState("success"); | ||
const [reload, setReload] = useState(true); | ||
const handleCloseToast = () => { | ||
setTimeout(() => { | ||
setOpenToast(false); | ||
}, 500); | ||
}; | ||
const handleChange = (panel) => (event, isExpanded) => { | ||
setExpanded(isExpanded ? panel : false); | ||
}; | ||
async function fetchAllFaq() { | ||
try { | ||
const response = await fetch(`${END_POINT}/getFaq`); | ||
const data = await response.json(); | ||
console.log(data); | ||
setFaqs(data.Faq); | ||
setIsFetching(false); | ||
} catch (err) { | ||
console.log(err.message); | ||
setIsFetching(false); | ||
} | ||
} | ||
|
||
const deleteFaq = async (faqId) => { | ||
setLoading(true); | ||
const url = `${END_POINT}/deleteFaq`; | ||
const body = { | ||
userId: props.adminData._id, | ||
faqId: faqId, | ||
}; | ||
const headers = { | ||
"Content-Type": "application/json", | ||
}; | ||
async function fetchAllFaq() { | ||
try { | ||
const response = await fetch(`${END_POINT}/getFaq`); | ||
const data = await response.json(); | ||
console.log(data) | ||
setFaqs(data.Faq) | ||
setIsFetching(false) | ||
} | ||
catch (err) { | ||
console.log(err.message) | ||
} | ||
try { | ||
const response = await fetch(url, { | ||
method: "PUT", | ||
headers: headers, | ||
body: JSON.stringify(body), | ||
}); | ||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
const data = await response.json(); | ||
setToastMessage(data.message); | ||
setOpenToast(true); | ||
setSeverity("success"); | ||
setReload(!reload); | ||
} catch (error) { | ||
setToastMessage("Failed to delete Faq"); | ||
setOpenToast(true); | ||
setSeverity("error"); | ||
} finally { | ||
setLoading(false); | ||
} | ||
useEffect(() => { | ||
setIsFetching(true) | ||
fetchAllFaq() | ||
}, []) | ||
}; | ||
useEffect(() => { | ||
fetchAllFaq(); | ||
}, [reload]); | ||
|
||
return ( | ||
<div> | ||
<h1 style={{ textAlign: "center" }}>Manage FAQ</h1> | ||
<div className={style["faq"]}> | ||
<div className={`${style["faq-block"]}`}> | ||
{isFetching ? ( | ||
<Loader></Loader> | ||
return ( | ||
<div> | ||
<h1 style={{ textAlign: "center" }}>Manage FAQ</h1> | ||
<div className={style["faq"]}> | ||
<div className={`${style["faq-block"]}`}> | ||
{isFetching ? ( | ||
<Loader /> | ||
) : ( | ||
faqs.map((faq) => ( | ||
<Accordion | ||
key={faq._id} | ||
className={style["accord-dark"]} | ||
expanded={expanded === `panel1-${faq._id}`} | ||
onChange={handleChange(`panel1-${faq._id}`)} | ||
> | ||
<AccordionSummary | ||
style={{ color: "white" }} | ||
expandIcon={ | ||
<ExpandMoreIcon | ||
style={{ color: "white", fontSize: "27px" }} | ||
/> | ||
} | ||
aria-controls="panel1a-content" | ||
id="panel1a-header" | ||
> | ||
<div | ||
style={{ | ||
display: "flex", | ||
justifyContent: "space-between", | ||
alignItems: "center", | ||
width: "100%", | ||
}} | ||
> | ||
<h3 className={style["faq-question"]}> | ||
<i | ||
className="fa fa-question-circle" | ||
aria-hidden="true" | ||
></i> | ||
{faq.question} | ||
</h3> | ||
</div> | ||
</AccordionSummary> | ||
<AccordionDetails className={style["accord-details"]}> | ||
<Typography style={{ color: "white" }}> | ||
{faq.answer} | ||
</Typography> | ||
<div className={style["btns-container"]}> | ||
<Button | ||
id={style["update-btn"]} | ||
className={style["btns"]} | ||
variant="contained" | ||
endIcon={<Edit />} | ||
> | ||
UPDATE | ||
</Button> | ||
{loading ? ( | ||
<div className={style["data-loader"]}> | ||
<Loader /> | ||
</div> | ||
) : ( | ||
faqs.map((faq) => ( | ||
<Accordion | ||
key={faq._id} | ||
className={style["accord-dark"]} | ||
expanded={expanded === `panel1-${faq._id}`} | ||
onChange={handleChange(`panel1-${faq._id}`)} | ||
> | ||
<AccordionSummary | ||
style={{ color: "white" }} | ||
expandIcon={ | ||
<ExpandMoreIcon | ||
style={{ color: "white", fontSize: "27px" }} | ||
/> | ||
} | ||
aria-controls="panel1a-content" | ||
id="panel1a-header" | ||
> | ||
<h3 className={style["faq-question"]}> | ||
<i | ||
className="fa fa-question-circle" | ||
aria-hidden="true" | ||
></i> | ||
{faq.question} | ||
</h3> | ||
</AccordionSummary> | ||
<AccordionDetails className={style["accord-details"]}> | ||
<Typography style={{ color: "white" }}> | ||
{faq.answer} | ||
</Typography> | ||
<div className={style["btns-container"]}> | ||
<Button id={style["update-btn"]} className={style["btns"]} variant="contained" endIcon={<Edit />}>UPDATE</Button> | ||
<Button id={style["delete-btn"]} className={style["btns"]} variant="contained" endIcon={<Delete />}>DELETE</Button> | ||
</div> | ||
</AccordionDetails> | ||
</Accordion> | ||
)) | ||
<Button | ||
id={style["delete-btn"]} | ||
className={style["btns"]} | ||
variant="contained" | ||
endIcon={<Delete />} | ||
onClick={() => deleteFaq(faq._id)} | ||
> | ||
DELETE | ||
</Button> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
</AccordionDetails> | ||
</Accordion> | ||
)) | ||
)} | ||
</div> | ||
); | ||
} | ||
</div> | ||
<SimpleToast | ||
open={open} | ||
message={toastMessage} | ||
severity={severity} | ||
handleCloseToast={handleCloseToast} | ||
/> | ||
</div> | ||
); | ||
} |
Oops, something went wrong.