Skip to content

Commit 523ec1c

Browse files
committed
test data from url
1 parent b96c052 commit 523ec1c

File tree

6 files changed

+139
-90
lines changed

6 files changed

+139
-90
lines changed

src/App.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ function App() {
113113
useEffect(() => {
114114

115115
if (selectedSubArticle !== null) {
116-
const selectedSubArticleTest = data.historyList[selectedArticle]?.subtopics?.[selectedSubArticle]?.subArticleTest;
116+
const selectedSubArticleTest = data.historyList[selectedArticle]?.subtopics?.[selectedSubArticle]?.sub_article_test_questions;
117117

118118
if (selectedSubArticleTest?.questions && selectedSubArticleTest?.options && selectedSubArticleTest?.correctAnswers) {
119119
const {questions, options, correctAnswers} = selectedSubArticleTest;
@@ -128,7 +128,7 @@ function App() {
128128

129129
} else {
130130

131-
const selectedArticleTestData = data.historyList[selectedArticle]?.mainArticleTest;
131+
const selectedArticleTestData = data.historyList[selectedArticle]?.main_article_test_questions;
132132

133133
if (selectedArticleTestData?.questions && selectedArticleTestData?.options && selectedArticleTestData?.correctAnswers) {
134134

src/components/Article/Article.tsx

+38-20
Original file line numberDiff line numberDiff line change
@@ -122,36 +122,52 @@ const Article: React.FC<IArticleProps> = ({
122122

123123
const handleGoToSubTestNow = (articleIndex: number, subArticleIndex: number) => {
124124
// debugger
125+
// setSelectedArticle(articleIndex)
126+
// setSelectedSubArticle(subArticleIndex)
125127
navigate(`/test/${articleIndex}/${subArticleIndex}`);
126-
setSelectedArticle(articleIndex)
127-
setSelectedSubArticle(subArticleIndex)
128+
128129
}
129130

130131

131132
const handleGoToSubArticleTest = (articleIndex: number) => {
132-
// Check if the article has subarticles
133+
// Знаходимо перший тест, де completed === false
134+
if (currentUser === null) {
135+
return false
136+
}
137+
let tests_completed_list = currentUser.tests_completed_list;
133138

134-
if (!historyList || selectedArticleNumber === undefined || selectedArticleNumber < 0 || selectedArticleNumber >= historyList.length) {
135-
return; // або можете показати повідомлення про помилку
139+
if (!tests_completed_list) {
140+
console.error('User tests_completed_list is not available.');
141+
return;
142+
}
143+
const firstIncompleteTest = tests_completed_list.find(test =>
144+
test.test_type === "Sub Article" &&
145+
test.event_id === articleIndex + 1 &&
146+
!test.completed
147+
);
148+
149+
if (!firstIncompleteTest) {
150+
console.error('No incomplete test found.');
151+
return;
136152
}
137153

138-
const selectedArticle = historyList[selectedArticleNumber];
154+
const subArticle = historyList[articleIndex]?.subtopics;
139155

140-
if (selectedArticle.subtopics && selectedArticle.subtopics.length > 0) {
141-
// Find the index of the first uncompleted subarticle
142-
const firstUncompletedIndex = selectedArticle.subtopics.findIndex((subArticle, subIndex) => {
143-
return !subArticleSuccessLevels[articleIndex]?.[subIndex];
144-
});
145-
146-
// If there is an uncompleted subarticle, navigate to its test page
147-
if (firstUncompletedIndex !== -1) {
148-
handleGoToSubTestNow(articleIndex, firstUncompletedIndex);
149-
} else {
150-
handleGoToTestNow(articleIndex);
151-
}
152-
} else {
153-
handleGoToTestNow(articleIndex);
156+
if (!subArticle) {
157+
console.error('SubArticle is not available.');
158+
return;
154159
}
160+
161+
const findIndexBySubArticleTestId = (testId: number): number => {
162+
return subArticle.findIndex(subArticle => subArticle.sub_article_test_id === testId);
163+
};
164+
165+
const id = firstIncompleteTest.test_id;
166+
167+
const selectedSubArticleIndex = findIndexBySubArticleTestId(id);
168+
169+
setSelectedArticle(articleIndex);
170+
handleGoToSubTestNow(articleIndex, selectedSubArticleIndex);
155171
};
156172

157173

@@ -302,9 +318,11 @@ const Article: React.FC<IArticleProps> = ({
302318
<Grid item key={index + "card"} xs={12} sm={6} md={4} xl={3}>
303319
<SubtopicCard
304320
done={isCompleted}
321+
selectedArticleNumber={selectedArticleNumber}
305322
subArticleIndex={index}
306323
title={subtopic.title}
307324
content={subtopic.content}
325+
handleGoToSubTestNow={handleGoToSubTestNow}
308326
/>
309327
</Grid>
310328
);

src/components/Article/components/SubtopicCard/SubtopicCard.tsx

+13-19
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,21 @@ import "./subtopics_card.scss"
1414
import ExpandLessIcon from '@mui/icons-material/ExpandLess';
1515
import ArrowForwardIosIcon from "@mui/icons-material/ArrowForwardIos";
1616
import QuestionMarkIcon from '@mui/icons-material/QuestionMark';
17-
import {useNavigate, useParams} from "react-router-dom";
1817
import CheckIcon from '@mui/icons-material/Check';
1918
import CheckBoxOutlineBlankIcon from '@mui/icons-material/CheckBoxOutlineBlank';
2019
import {ISubtopicCardProps} from "../../../../types/types";
2120
import {contentRenderFunction} from "../../../../utils/utils";
22-
import {styled} from "@mui/material/styles";
2321
import CloseIcon from '@mui/icons-material/Close';
2422

2523

26-
const SubtopicCard = ({content, title, subArticleIndex, done}: ISubtopicCardProps) => {
24+
const SubtopicCard = ({
25+
content,
26+
title,
27+
subArticleIndex,
28+
done,
29+
handleGoToSubTestNow,
30+
selectedArticleNumber
31+
}: ISubtopicCardProps) => {
2732

2833
const [expanded, setExpanded] = React.useState(false);
2934
const [openModalAdditional, setOpenModalAdditional] = React.useState(false);
@@ -32,20 +37,7 @@ const SubtopicCard = ({content, title, subArticleIndex, done}: ISubtopicCardProp
3237
setOpenModalAdditional(false);
3338
};
3439

35-
const handleExpandClick = () => {
36-
setExpanded(!expanded);
37-
};
38-
39-
const {selectedArticle} = useParams();
40-
const navigate = useNavigate();
41-
42-
// Your logic to extract subtopics from historyList based on selectedArticle
4340

44-
const handleGoToTest = () => {
45-
// Navigate to the test page with the selectedArticle parameter
46-
47-
navigate(`/test/${selectedArticle}/${subArticleIndex}`);
48-
};
4941
return (
5042
<React.Fragment>
5143
<Card elevation={done ? 6 : 0} className={"subtopics_card"}>
@@ -77,7 +69,9 @@ const SubtopicCard = ({content, title, subArticleIndex, done}: ISubtopicCardProp
7769
</CardActions>}
7870

7971
{!done ? <CardActions disableSpacing>
80-
<Button onClick={handleGoToTest} endIcon={<ArrowForwardIosIcon/>} color={"secondary"}
72+
<Button
73+
onClick={() => handleGoToSubTestNow(selectedArticleNumber, subArticleIndex)}
74+
endIcon={<ArrowForwardIosIcon/>} color={"secondary"}
8175
size={"small"}
8276
fullWidth
8377
variant={"outlined"}
@@ -100,7 +94,7 @@ const SubtopicCard = ({content, title, subArticleIndex, done}: ISubtopicCardProp
10094

10195
{expanded && <CardActions disableSpacing>
10296

103-
<Button onClick={()=>setOpenModalAdditional(true)}
97+
<Button onClick={() => setOpenModalAdditional(true)}
10498
endIcon={<ExpandLessIcon/>}
10599
color={"secondary"}
106100
aria-expanded={expanded} size={"small"} fullWidth variant={"contained"}
@@ -132,7 +126,7 @@ const SubtopicCard = ({content, title, subArticleIndex, done}: ISubtopicCardProp
132126
<DialogActions>
133127
<Grid container spacing={2} justifyContent={"end"}>
134128
<Grid item xs={"auto"}>
135-
<Button onClick={handleGoToTest} endIcon={<ArrowForwardIosIcon/>} color={"primary"}
129+
<Button onClick={() => handleGoToSubTestNow(selectedArticleNumber, subArticleIndex)} endIcon={<ArrowForwardIosIcon/>} color={"primary"}
136130
size={"medium"}
137131

138132
variant={"contained"}

src/components/HistoryTimeline/HistoryTimeline.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ const HistoryTimeline: React.FC<IHistoryTimelineProps> = ({
236236
color: iconColorState(successLevels > index),
237237
}}
238238
contentStyle={{padding: 0, boxShadow: "none"}}
239-
icon={successLevels === (index + 1) ? <CheckCircleOutlineIcon/> :
239+
icon={successLevels >= (index + 1) ? <CheckCircleOutlineIcon/> :
240240
<RadioButtonUncheckedRoundedIcon/>}
241241

242242
>

0 commit comments

Comments
 (0)