|
1 | 1 | import logo from './logo.svg'; |
2 | 2 | import './App.css'; |
3 | | - |
4 | 3 | import React from 'react' |
5 | 4 | import CardList from './Cards/CardList' |
6 | 5 | import AddCard from './Cards/AddCard' |
7 | 6 | import Context from './context' |
8 | 7 | import Loader from './Content/Loader' |
9 | 8 | import Modal from './Modal/Modal' |
10 | | - |
11 | | -import { lorem } from './Content/Lorem' |
| 9 | +import { loadData, saveData } from './DataService' |
12 | 10 |
|
13 | 11 | const testText = "My little cards-app c:"; |
14 | 12 |
|
15 | 13 | let cardCount = 1; |
16 | | - |
17 | | -function generateCardsArr() { |
18 | | - let arr = [] |
19 | | - for (let index = 0; index < 8; index++) { |
20 | | - let len = Math.floor(Math.random() * lorem.length); |
21 | | - arr.push({ |
22 | | - id: Number(cardCount++), |
23 | | - completed: Boolean(Math.floor(Math.random() * 2)), |
24 | | - text: lorem.slice(0, len) |
25 | | - }) |
26 | | - } |
27 | | - return arr |
28 | | -}; |
| 14 | +function calcCount(cardsArr) { |
| 15 | + let id = cardCount; |
| 16 | + new Array(...cardsArr).forEach(element => { |
| 17 | + if (Number(element.id) >= id) id = Number(element.id) |
| 18 | + }); |
| 19 | + return id |
| 20 | +} |
29 | 21 |
|
30 | 22 | function App() { |
31 | 23 | const [cardsArr, setCards] = React.useState([]) |
32 | 24 | const [editCardId, setEditCardId] = React.useState(null) |
33 | 25 | const [loading, setLoading] = React.useState(true) |
34 | | - React.useEffect(loadCards, []) |
35 | | - |
36 | | - function loadCards() { |
37 | | - setTimeout(() => { |
38 | | - setCards(generateCardsArr()) |
39 | | - setLoading(false) |
40 | | - }, 200) |
| 26 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 27 | + React.useEffect(() => { setLoading(true); loadData(setLoadedCards); }, []) |
| 28 | + React.useEffect(() => { saveData(cardsArr) }, [cardsArr]) |
| 29 | + |
| 30 | + function setLoadedCards(cards) { |
| 31 | + setCards([...cards]) |
| 32 | + setLoading(false) |
| 33 | + cardCount = calcCount(cardsArr) |
41 | 34 | } |
42 | 35 |
|
| 36 | + |
| 37 | + |
43 | 38 | function removeCard(index) { |
44 | 39 | cardsArr.splice(index, 1) |
45 | 40 | setCards([...cardsArr]) |
|
0 commit comments