Skip to content

Commit ce985f4

Browse files
committed
connect to server
1 parent 4693d29 commit ce985f4

File tree

3 files changed

+53
-25
lines changed

3 files changed

+53
-25
lines changed

src/App.js

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,40 @@
11
import logo from './logo.svg';
22
import './App.css';
3-
43
import React from 'react'
54
import CardList from './Cards/CardList'
65
import AddCard from './Cards/AddCard'
76
import Context from './context'
87
import Loader from './Content/Loader'
98
import Modal from './Modal/Modal'
10-
11-
import { lorem } from './Content/Lorem'
9+
import { loadData, saveData } from './DataService'
1210

1311
const testText = "My little cards-app c:";
1412

1513
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+
}
2921

3022
function App() {
3123
const [cardsArr, setCards] = React.useState([])
3224
const [editCardId, setEditCardId] = React.useState(null)
3325
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)
4134
}
4235

36+
37+
4338
function removeCard(index) {
4439
cardsArr.splice(index, 1)
4540
setCards([...cardsArr])

src/Content/Lorem.js

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

src/DataService.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const url = 'http://php-server-notes.std-1033.ist.mospolytech.ru/'
2+
const user = fetch("https://l2.io/ip.json").then(response => response.json()['ip']) || "default" // parses JSON response into native JavaScript objects
3+
4+
export function loadData(after) {
5+
console.log("loadData");
6+
console.log("user " + user);
7+
fetch(url, {
8+
method: 'GET', // *GET, POST, PUT, DELETE, etc.
9+
headers: {
10+
'Content-Type': 'application/json',
11+
'user': String(user)
12+
},
13+
})
14+
.then(response => response.json()) // parses JSON response into native JavaScript objects
15+
.then((data) => {
16+
data ? after(data) : console.log("not loaded")
17+
return data
18+
})
19+
.then(console.log)
20+
}
21+
22+
export function saveData(data) {
23+
console.log("saveData"); console.log(data);
24+
console.log("user " + user);
25+
fetch(url, {
26+
method: 'POST', // *GET, POST, PUT, DELETE, etc.
27+
headers: {
28+
'Content-Type': 'application/json',
29+
'user': String(user)
30+
},
31+
body: JSON.stringify(data) // body data type must match "Content-Type" header
32+
})
33+
.then(response => response.json()) // parses JSON response into native JavaScript objects
34+
.then(console.log)
35+
}

0 commit comments

Comments
 (0)