Skip to content

Commit 69a5a4b

Browse files
committed
Rework FAQ click logic (#1091)
1 parent 3d23acd commit 69a5a4b

File tree

2 files changed

+16
-31
lines changed

2 files changed

+16
-31
lines changed

src/components/FAQCard/accordionSection.js

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import React, { useState, useEffect } from 'react';
2-
import axios from "axios";
1+
import React from 'react';
32
import sanitizeHtml from 'sanitize-html';
43
import Accordion from '@material-ui/core/Accordion';
54
import AccordionSummary from '@material-ui/core/AccordionSummary';
@@ -55,50 +54,27 @@ const SanitizedAnswer = ({ answer }) => {
5554
};
5655

5756
const AccordionSection = (props) => {
58-
const [currentFaq, setCurrentFaq] = useState([]);
59-
const [sendRequest, setSendRequest] = useState(false);
6057
const classes = useStyles();
6158

62-
useEffect(() => {
63-
const incrementViewCount = async function () {
64-
const requestBody = {
65-
question: currentFaq.question,
66-
answer: currentFaq.answer,
67-
view_count: currentFaq.view_count,
68-
};
69-
await axios.post(`${process.env.REACT_APP_API_URL}/api/faqs/${currentFaq.id}/increment_count/`, requestBody);
70-
};
71-
if (sendRequest) {
72-
// send the request
73-
incrementViewCount()
74-
setSendRequest(false);
75-
}
76-
}, [sendRequest, currentFaq]);
77-
7859
return (
7960
<Grid item xs={12} sm={9} lg={11}>
8061
{props.faqs.map((faq) => (
8162
<Box key={faq.id}>
8263
<Accordion
8364
className={classes.accordion}
8465
expanded={props.expandedFaqs.indexOf(faq.id.toString()) > -1}
85-
onClick={() => props.onFaqClick(faq.id)}
66+
onClick={() => props.onFaqClick(faq)}
8667
>
8768
<AccordionSummary
8869
className={classes.summary}
8970
data-cy='faq-question'
90-
disabled={sendRequest}
9171
expandIcon={<ExpandMoreRoundedIcon />}
92-
onClick={() => {
93-
setSendRequest(true);
94-
setCurrentFaq(faq);
95-
}}
9672
>
9773
<Typography variant='h6'>{faq.question}</Typography>
9874
</AccordionSummary>
9975
<Divider />
10076
<AccordionDetails data-cy='faq-answer' className={classes.detail}>
101-
<Typography>
77+
<Typography onClick={(e) => e.stopPropagation()}>
10278
<SanitizedAnswer answer={faq.answer} />
10379
</Typography>
10480
</AccordionDetails>

src/pages/Faq/index.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,25 @@ const Faq = () => {
7171
);
7272

7373
useEffect(() => {
74-
getFaqData(query, largeScreen);
74+
getFaqData(query, false);
7575
}, [pageNum, largeScreen]);
7676

77-
const handleFaqClick = (id) => {
77+
const handleFaqClick = (faq) => {
7878
const expanded = [...expandedFaqs];
79-
const idx = expanded.indexOf(id.toString());
79+
const idx = expanded.indexOf(faq.id.toString());
8080
if (idx > -1) {
8181
expanded.splice(idx, 1);
8282
} else {
83-
expanded.push(id.toString());
83+
const requestBody = {
84+
question: faq.question,
85+
answer: faq.answer,
86+
view_count: faq.view_count,
87+
};
88+
axios.post(
89+
`${process.env.REACT_APP_API_URL}/api/faqs/${faq.id}/increment_count/`,
90+
requestBody
91+
);
92+
expanded.push(faq.id.toString());
8493
}
8594
setExpandedFaqs(expanded);
8695
};

0 commit comments

Comments
 (0)