Skip to content

Commit 688ab68

Browse files
improvement(app): typescript
1 parent d3f36af commit 688ab68

File tree

15 files changed

+507
-139
lines changed

15 files changed

+507
-139
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@
2121
npm-debug.log*
2222
yarn-debug.log*
2323
yarn-error.log*
24+
25+
/.idea
26+
/.code

package-lock.json

Lines changed: 324 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@
1111
"@testing-library/jest-dom": "^4.2.4",
1212
"@testing-library/react": "^9.5.0",
1313
"@testing-library/user-event": "^7.2.1",
14+
"@types/jest": "^26.0.22",
15+
"@types/node": "^14.14.39",
16+
"@types/react": "^17.0.3",
17+
"@types/react-dom": "^17.0.3",
1418
"axios": "^0.21.1",
1519
"gh-pages": "^3.1.0",
1620
"node-sass": "^4.14.1",
1721
"react": "^16.13.1",
1822
"react-dom": "^16.13.1",
1923
"react-router": "^5.2.0",
2024
"react-scripts": "3.4.3",
25+
"typescript": "^4.2.4",
2126
"underscore": "^1.10.2"
2227
},
2328
"scripts": {

src/App.jsx renamed to src/App.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ function App() {
1616

1717
return (
1818
<div className="app flex">
19-
<Header></Header>
19+
<Header/>
2020
<main>
21-
{ questions.length ? <MainContent questions={questions}></MainContent> : <Spinner></Spinner> }
21+
{ questions.length ? <MainContent questions={questions}/> : <Spinner/> }
2222
</main>
23-
<Footer></Footer>
23+
<Footer/>
2424
</div>
2525
);
2626
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import React from 'react';
2-
import '../style/FinalResult.scss'
3-
import {Button} from '@material-ui/core';
2+
import '../style/FinalResult.scss';
3+
import { Button } from '@material-ui/core';
44

5-
function FinalResult(props) {
6-
const {score, total} = props.score
5+
interface FinalResultProps {
6+
score: {
7+
score: number,
8+
total: number
9+
}
10+
}
11+
12+
function FinalResult(props: FinalResultProps) {
13+
const { score, total } = props.score;
714
return (
815
<div className="final-result">
916
<div className="container">
@@ -16,7 +23,7 @@ function FinalResult(props) {
1623
</Button>
1724
</div>
1825
</div>
19-
)
26+
);
2027
}
2128

22-
export default FinalResult
29+
export default FinalResult;

src/containers/MainContent.jsx

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/containers/MainContent.tsx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import React, { useState } from 'react';
2+
import QuestionBox from './QuestionBoxInterface';
3+
import FinalResult from '../components/FinalResult';
4+
import '../style/MainContent.scss';
5+
import { MobileStepper } from '@material-ui/core';
6+
7+
8+
interface MainContentInterface {
9+
questions: {
10+
question: string,
11+
correct_answer: string,
12+
incorrect_answers: string
13+
}[]
14+
}
15+
16+
17+
function MainContent({ questions }: MainContentInterface) {
18+
const [currentQuestion, setCurrentQuestion] = useState(0);
19+
const counter = () => `${currentQuestion + 1}/${questions.length}`;
20+
const [score, setScore] = useState(0);
21+
const answerQuestion = (newScore: number): void => {
22+
setScore(score + newScore);
23+
setCurrentQuestion(currentQuestion + 1);
24+
};
25+
26+
return (
27+
<div className="main-content">
28+
<div className="container">
29+
{currentQuestion < questions.length ?
30+
<React.Fragment>
31+
<MobileStepper
32+
variant="progress"
33+
steps={10}
34+
position="static"
35+
activeStep={currentQuestion}
36+
className="stepper"
37+
backButton=""
38+
nextButton=""
39+
/>
40+
<h2>Question {counter()} </h2>
41+
<QuestionBox
42+
question={questions[currentQuestion]}
43+
answerQuestion={answerQuestion}>
44+
</QuestionBox>
45+
</React.Fragment> :
46+
<FinalResult score={{ score, total: questions.length }}/>
47+
}
48+
</div>
49+
</div>
50+
);
51+
}
52+
53+
export default MainContent;

0 commit comments

Comments
 (0)