Skip to content
This repository was archived by the owner on Dec 25, 2022. It is now read-only.

Commit cf8d8c0

Browse files
committed
add crawl data
1 parent 388a254 commit cf8d8c0

File tree

4 files changed

+157
-15
lines changed

4 files changed

+157
-15
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "quizlet-learn",
3-
"version": "1.0.10",
3+
"version": "1.0.11",
44
"private": true,
55
"dependencies": {
66
"@nextui-org/react": "^1.0.0-beta.9",

src/components/CrawlData/CrawlData.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { Modal, Text, Spacer, Button, Input } from '@nextui-org/react';
2+
import { useState, Fragment, useRef } from 'react';
3+
import { nanoid } from 'nanoid';
4+
5+
const CrawlData = ({ open, setClose, onSuccess }) => {
6+
const [isGettingData, setIsGettingData] = useState(false);
7+
const [isFailed, setIsFailed] = useState(false);
8+
9+
const urlRef = useRef();
10+
11+
const handleCrawl = () => {
12+
setIsGettingData(true);
13+
setIsFailed(false);
14+
fetch('https://short-link-adonisgm.azurewebsites.net/api/quizlet/crawl-data', {
15+
method: 'POST',
16+
headers: {
17+
'Content-Type': 'application/json',
18+
},
19+
body: JSON.stringify({
20+
url: urlRef.current.value.trim(),
21+
}),
22+
})
23+
.then((res) => res.json())
24+
.then((res) => {
25+
let data = res.data.course;
26+
data = data.map((item) => {
27+
return {
28+
...item,
29+
i: nanoid(6),
30+
learned: false,
31+
};
32+
});
33+
const course = {
34+
name: res.data.title,
35+
data,
36+
createdAt: new Date(),
37+
learnedAt: null,
38+
};
39+
localStorage.setItem(nanoid(15), JSON.stringify(course));
40+
onSuccess();
41+
})
42+
.catch(() => {
43+
setIsFailed(true);
44+
setIsGettingData(false);
45+
});
46+
};
47+
48+
return (
49+
<Modal
50+
open={open}
51+
blur
52+
closeButton
53+
onClose={() => {
54+
setClose(false);
55+
}}
56+
>
57+
<Modal.Body>
58+
<Fragment>
59+
<Text
60+
p
61+
b
62+
size={14}
63+
css={{
64+
textAlign: 'center',
65+
}}
66+
>
67+
Crawl data from URL
68+
</Text>
69+
<Spacer y={0.5} />
70+
<Input
71+
placeholder="https://quizlet.com/vn/652631/demo-crawl-data"
72+
ref={urlRef}
73+
/>
74+
<Button
75+
auto
76+
css={{
77+
margin: '0 auto',
78+
}}
79+
onClick={() => {
80+
handleCrawl();
81+
}}
82+
disabled={isGettingData}
83+
color={isFailed ? 'error' : 'primary'}
84+
>
85+
Import
86+
</Button>
87+
</Fragment>
88+
</Modal.Body>
89+
</Modal>
90+
);
91+
};
92+
93+
export default CrawlData;

src/components/CrawlData/CrawlData.module.css

Whitespace-only changes.

src/components/Home/Home.js

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@ import {
66
Button,
77
Modal,
88
Input,
9+
Dropdown,
910
} from '@nextui-org/react';
1011
import { useEffect, useState, useRef, Fragment } from 'react';
1112
import { useNavigate } from 'react-router-dom';
1213
import classes from './Home.module.css';
1314
import { nanoid } from 'nanoid';
15+
import CrawlData from '../CrawlData/CrawlData';
16+
import { FcDownload, FcFile, FcMultipleInputs } from 'react-icons/fc';
1417

1518
const Home = () => {
1619
const [listCourse, setListCourse] = useState([]);
1720
const [showImport, setShowImport] = useState(false);
1821
const [isGettingData, setIsGettingData] = useState(false);
1922
const [isFailed, setIsFailed] = useState(false);
23+
const [showModalCrawl, setShowModalCrawl] = useState(false);
2024

2125
const codeRef = useRef();
2226
const navigate = useNavigate();
@@ -76,6 +80,10 @@ const Home = () => {
7680

7781
return (
7882
<div>
83+
<CrawlData open={showModalCrawl} setClose={setShowModalCrawl} onSuccess={() => {
84+
setShowModalCrawl(false);
85+
getData();
86+
}}/>
7987
<Modal
8088
open={showImport}
8189
blur
@@ -123,20 +131,61 @@ const Home = () => {
123131
columnGap: '1rem',
124132
}}
125133
>
126-
<Button flat auto onPress={handleCreate}>
127-
Create from Quizlet
128-
</Button>
129-
<Button
130-
flat
131-
auto
132-
color={'gradient'}
133-
onPress={() => {
134-
setShowImport(true);
135-
setIsFailed(false);
136-
}}
137-
>
138-
Import by share code
139-
</Button>
134+
<Dropdown disableAnimation>
135+
<Dropdown.Button flat color="secondary">
136+
Create new course
137+
</Dropdown.Button>
138+
<Dropdown.Menu
139+
color="secondary"
140+
aria-label="Actions"
141+
css={{ $$dropdownMenuWidth: '280px' }}
142+
onAction={(e) => {
143+
switch (e) {
144+
case 'quizlet':
145+
handleCreate();
146+
break;
147+
case 'import':
148+
setShowImport(true);
149+
setIsFailed(false);
150+
break;
151+
case 'crawl':
152+
setShowModalCrawl(true);
153+
break;
154+
default:
155+
break;
156+
}
157+
}}
158+
>
159+
<Dropdown.Section title="Automatic">
160+
<Dropdown.Item
161+
key="crawl"
162+
description="Crawl data from Quizlet"
163+
color="success"
164+
icon={<FcMultipleInputs />}
165+
>
166+
Crawl data <b>{'(recommended)'}</b>
167+
</Dropdown.Item>
168+
</Dropdown.Section>
169+
<Dropdown.Section title="Manual">
170+
<Dropdown.Item
171+
key="quizlet"
172+
description="Create new course by export Quizlet"
173+
color="primary"
174+
icon={<FcFile />}
175+
>
176+
Quizlet
177+
</Dropdown.Item>
178+
<Dropdown.Item
179+
key="import"
180+
description="Import course by share code"
181+
color="warning"
182+
icon={<FcDownload />}
183+
>
184+
Import code
185+
</Dropdown.Item>
186+
</Dropdown.Section>
187+
</Dropdown.Menu>
188+
</Dropdown>
140189
</div>
141190
</div>
142191
<Spacer y={1.4} />

0 commit comments

Comments
 (0)