-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgithub-practices.tsx
More file actions
50 lines (44 loc) · 1.41 KB
/
github-practices.tsx
File metadata and controls
50 lines (44 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import axios from 'axios';
import { marked } from 'marked';
import React, { useEffect } from 'react';
import { parsePractices, withId } from '../../practice-parser/lexer';
import { Practices } from '../../components/practices/practices';
import { isEmptyPractices, usePracticesWithLocalStorage } from '../../components/practices/practices.hook';
import { convertGithubLink, recorrectQuestionIndex } from './utils';
interface GithubPracticesProps {
githubLink: string;
}
const GithubPractices: React.FC<GithubPracticesProps> = ({ githubLink }) => {
const link = convertGithubLink(githubLink);
const getPractices = () => axios.get(link)
.then((response) => {
const lexer = marked.lexer(response.data);
const practices = recorrectQuestionIndex(parsePractices(lexer));
return withId(practices);
});
const [practices, {
handleSubmit,
handleSelectionChange,
setPractices,
resetStorage,
shuffleHandler,
}] = usePracticesWithLocalStorage(link, {});
useEffect(() => {
if (isEmptyPractices(practices)) {
getPractices().then((data) => {
setPractices(data);
});
}
}, [practices]);
return (
<Practices
data={practices}
onSubmit={handleSubmit}
onSelectionChange={handleSelectionChange}
baseImageURL={link}
onResetPractices={resetStorage}
onShuffleQuestions={shuffleHandler}
/>
);
};
export default GithubPractices;