Skip to content

Commit b67cca0

Browse files
authored
Merge pull request #7 from maxchistt/dev
Update DataService.js
2 parents 0496829 + b0496d3 commit b67cca0

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

client/src/App.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function useCardsArr(defaultValue) {
2929
const [value, setValue] = React.useState(defaultValue)
3030

3131
function trySetValue(cardsArr) {
32-
if (checkCardsArr(cardsArr)) setValue(cardsArr)
32+
if (checkCardsArr(cardsArr)||cardsArr===null) setValue(cardsArr)
3333
else console.error('Массив cardsArr не прошел проверку \n', cardsArr)
3434
}
3535

@@ -48,7 +48,7 @@ function useUpdater() {
4848
}
4949

5050
function App() {
51-
const [cardsArr, setCards] = useCardsArr([])
51+
const [cardsArr, setCards] = useCardsArr(null)
5252
const [editCardId, setEditCardId] = React.useState(null)
5353
const [loading, setLoading] = React.useState({ state: false, res: false })
5454

@@ -59,8 +59,9 @@ function App() {
5959
const [updaterVal] = useUpdater()
6060

6161
React.useEffect(loadDataFromServer, [logged, userName, updaterVal]) // eslint-disable-line react-hooks/exhaustive-deps
62-
useDebouncedEffect(loadDataToServer, [cardsArr], 1000) // eslint-disable-line react-hooks/exhaustive-deps
63-
React.useEffect(clearOldData, [logged]) // eslint-disable-line react-hooks/exhaustive-deps
62+
useDebouncedEffect(loadDataToServer, [cardsArr], 300) // eslint-disable-line react-hooks/exhaustive-deps
63+
//React.useEffect(loadDataToServer, [cardsArr]) // eslint-disable-line react-hooks/exhaustive-deps
64+
React.useEffect(clearOldData, [logged, userName]) // eslint-disable-line react-hooks/exhaustive-deps
6465

6566
///////////
6667
function onLogin(login) {
@@ -106,17 +107,22 @@ function App() {
106107

107108
function clearOldData() {
108109
//console.log("clearOldData, logged:", logged)
109-
if (!logged && !!cardsArr) deleteAll()
110+
if (!logged) setCards(null)
110111
}
111112
///////////
112113

113114
///////////
114115
function loadDataToServer() {
115116
try {
117+
let startUsername = userName
116118
if (logged && userName) postData(cardsArr)
117119
.then(res => {
118120
if (!cardsArr) console.log("empty post!")
119121
console.log('[onPostData]', res)
122+
if (startUsername !== userName) {
123+
console.log("упс, несостыковочка с userName");
124+
loadDataToServer()
125+
}
120126
})
121127
.catch(e => console.log(`Data post request error. Response: ${e}`))
122128
}
@@ -127,12 +133,14 @@ function App() {
127133

128134
function loadDataFromServer() {
129135
try {
136+
let startUsername = userName
130137
if (logged && userName) {
131138
setLoading({ state: true, res: loading.res })
132139
loadData()
133140
.then(data => {
134141
console.log('[onLoadData]', 'Данные с сервера загружены')
135-
setLoadedCards(data)
142+
if (startUsername === userName) setLoadedCards(data)
143+
else console.log("упс, несостыковочка с userName");
136144
setLoading({ state: false, res: true })
137145
})
138146
.catch(e => {
@@ -225,7 +233,7 @@ function App() {
225233
<AddCard onCreate={addCard} onDeleteAll={deleteAll} />
226234
<ModalCardEdit card={getCardByIndex(editCardId)} index={editCardId} />
227235

228-
{cardsArr.length ? (
236+
{cardsArr && cardsArr.length ? (
229237
<CardList cards={cardsArr} />
230238
) : (loading.state || !loading.res) ? null : logged ? (
231239
<div className="container text-center">

client/src/Services/DataService.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ export default function DataService() {
4242
data: data,
4343
},
4444
})
45-
.done(data => res(data))
46-
.fail(data => rej(data))
45+
.done(resdata => res(resdata))
46+
.fail(rejdata => rej(rejdata))
4747
/*.always(() => console.log(
4848
`requested - newUser:"${user}" resolveData:"${data}" \n
4949
request ${rc} - "${target}" ended \n `
@@ -74,6 +74,7 @@ export default function DataService() {
7474
function checkData(data) {
7575
//console.log('start check data')
7676
try {
77+
if( data === null) console.log("null data");
7778
return data === null || data === [] || checkCardsArr(data)
7879
} catch {
7980
return false
@@ -92,13 +93,15 @@ export default function DataService() {
9293
.then((d) => {
9394
let data = tryParce(d)//here we parce json
9495
//console.log("[DATA] from loadData(): ", data)
96+
if(!data)console.log("empty data from server");
9597
if (!checkData(data)) {
9698
console.error("[loadData] Bad data format")
9799
console.log(data)
98-
if (user !== null) {
100+
let checkDel = user !== null
101+
if (checkDel && window.confirm("Bad data: " + data + ". Delete data on server?")) {
99102
console.log('clear data')
100103
requestPostData([new Card({ id: 0, color: "orange", name: "Error", text: "Данные были очищены из за ошибки" })]).then(() => loadData().then(res, rej), rej)//очистка данных
101-
} else rej("Not format data & unlogged")
104+
} else rej("Not format data" + !user ? " & unlogged" : "")
102105
} else {
103106
res(data || [])
104107
}
@@ -118,7 +121,10 @@ export default function DataService() {
118121
? Promise.reject(rej())
119122
: loadData())
120123
.then((d) => {
124+
if(!data)console.log("empty data to post");
125+
if(!d)console.log("empty loaded to check");
121126
let pDat = data === null ? (d || []) : data
127+
if(!pDat)console.log("empty will be posted");
122128
requestPostData(pDat).then(res, rej)
123129
})
124130
.catch(rej)

0 commit comments

Comments
 (0)