Skip to content

Commit 8773b99

Browse files
committed
feat: add loading status
1 parent 9fb76f6 commit 8773b99

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

src/components/Loading/Loading.css

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
.loading {
2+
display: flex;
3+
white-space: pre;
4+
}
5+
6+
.loading:after {
7+
content: ' .';
8+
animation: dots 1s steps(5, end) infinite;}
9+
10+
@keyframes dots {
11+
0%, 20% {
12+
color: rgba(0,0,0,0);
13+
text-shadow:
14+
.25em 0 0 rgba(0,0,0,0),
15+
.5em 0 0 rgba(255, 179, 179, 0);}
16+
40% {
17+
color: black;
18+
text-shadow:
19+
.25em 0 0 rgba(0,0,0,0),
20+
.5em 0 0 rgba(0,0,0,0);}
21+
60% {
22+
text-shadow:
23+
.25em 0 0 black,
24+
.5em 0 0 rgba(0,0,0,0);}
25+
80%, 100% {
26+
text-shadow:
27+
.25em 0 0 black,
28+
.5em 0 0 black;}}

src/components/Loading/Loading.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react';
22

3+
import styles from './Loading.css';
34
// eslint-disable-next-line prettier/prettier
4-
const Loading = () => (<div>Loading...</div>);
5+
const Loading = () => (<p className={styles.loading}>Loading</p>);
56

67
export default Loading;

src/scenes/Exam/Exam.jsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Form from '../../components/Form/Form';
88
import Header from '../../components/Header/Header.tsx';
99
import ProgressBar from '../../components/ProgressBar.tsx';
1010
import ErrorBoundary from '../../components/ErrorBoundary';
11+
import Loading from '../../components/Loading/Loading';
1112

1213
import grid from '../../components/Grid/Grid.css';
1314
import { Code } from '../../components/Code/Code';
@@ -17,16 +18,18 @@ let iteration = 1;
1718
let successCounter = 0;
1819
let failureCounter = 0;
1920

20-
const displayQuestion = (callback) => {
21+
const displayQuestion = (callback, setIsLoaded) => {
2122
getRandomDocument('questions')
2223
.then(({ data }) => {
2324
if (data) {
25+
setIsLoaded(true);
2426
callback(data);
2527
} else {
2628
console.error('No such document!');
2729
}
2830
})
2931
.catch((error) => {
32+
setIsLoaded(true);
3033
console.error('Error getting document:', error);
3134
});
3235
};
@@ -39,7 +42,7 @@ const upProgressBar = (isCorrect) => {
3942
}
4043
};
4144

42-
const addAnswer = (answer, questionCallback, resultsCallback) => {
45+
const addAnswer = (answer, questionCallback, setIsLoaded, resultsCallback) => {
4346
const isCorrectAnswer = functions.httpsCallable('isCorrectAnswer');
4447
isCorrectAnswer(answer).then((result) => {
4548
upProgressBar(result.data.correct);
@@ -50,7 +53,7 @@ const addAnswer = (answer, questionCallback, resultsCallback) => {
5053
total: questionsLength,
5154
});
5255
} else {
53-
displayQuestion(questionCallback);
56+
displayQuestion(questionCallback, setIsLoaded);
5457
iteration += 1;
5558
}
5659
});
@@ -65,6 +68,7 @@ const ProgressBarWrapper = ({ success = 0, failure = 0, overall }) => {
6568
};
6669

6770
const Exam = ({ results }) => {
71+
const [isLoaded, setIsLoaded] = useState(false);
6872
const [question, setQuestion] = useState({
6973
id: '',
7074
name: '',
@@ -73,7 +77,7 @@ const Exam = ({ results }) => {
7377
});
7478

7579
useEffect(() => {
76-
displayQuestion(setQuestion);
80+
displayQuestion(setQuestion, setIsLoaded);
7781
return function cleanup() {
7882
iteration = 1;
7983
successCounter = 0;
@@ -95,10 +99,13 @@ const Exam = ({ results }) => {
9599
<ErrorBoundary>
96100
<Code codeString={question.value} />
97101
</ErrorBoundary>
102+
{!isLoaded && <Loading />}
98103
</section>
99104
<section className={grid.container}>
100105
<Form
101-
userAnswer={(answer) => addAnswer(answer, setQuestion, results)}
106+
userAnswer={(answer) =>
107+
addAnswer(answer, setQuestion, setIsLoaded, results)
108+
}
102109
/>
103110
</section>
104111
</>

0 commit comments

Comments
 (0)