Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function useCardsArr(defaultValue) {
const [value, setValue] = React.useState(defaultValue)

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

Expand All @@ -48,7 +48,7 @@ function useUpdater() {
}

function App() {
const [cardsArr, setCards] = useCardsArr([])
const [cardsArr, setCards] = useCardsArr(null)
const [editCardId, setEditCardId] = React.useState(null)
const [loading, setLoading] = React.useState({ state: false, res: false })

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

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

///////////
function onLogin(login) {
Expand Down Expand Up @@ -106,17 +107,22 @@ function App() {

function clearOldData() {
//console.log("clearOldData, logged:", logged)
if (!logged && !!cardsArr) deleteAll()
if (!logged) setCards(null)
}
///////////

///////////
function loadDataToServer() {
try {
let startUsername = userName
if (logged && userName) postData(cardsArr)
.then(res => {
if (!cardsArr) console.log("empty post!")
console.log('[onPostData]', res)
if (startUsername !== userName) {
console.log("упс, несостыковочка с userName");
loadDataToServer()
}
})
.catch(e => console.log(`Data post request error. Response: ${e}`))
}
Expand All @@ -127,12 +133,14 @@ function App() {

function loadDataFromServer() {
try {
let startUsername = userName
if (logged && userName) {
setLoading({ state: true, res: loading.res })
loadData()
.then(data => {
console.log('[onLoadData]', 'Данные с сервера загружены')
setLoadedCards(data)
if (startUsername === userName) setLoadedCards(data)
else console.log("упс, несостыковочка с userName");
setLoading({ state: false, res: true })
})
.catch(e => {
Expand Down Expand Up @@ -225,7 +233,7 @@ function App() {
<AddCard onCreate={addCard} onDeleteAll={deleteAll} />
<ModalCardEdit card={getCardByIndex(editCardId)} index={editCardId} />

{cardsArr.length ? (
{cardsArr && cardsArr.length ? (
<CardList cards={cardsArr} />
) : (loading.state || !loading.res) ? null : logged ? (
<div className="container text-center">
Expand Down
14 changes: 10 additions & 4 deletions client/src/Services/DataService.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export default function DataService() {
data: data,
},
})
.done(data => res(data))
.fail(data => rej(data))
.done(resdata => res(resdata))
.fail(rejdata => rej(rejdata))
/*.always(() => console.log(
`requested - newUser:"${user}" resolveData:"${data}" \n
request ${rc} - "${target}" ended \n `
Expand Down Expand Up @@ -74,6 +74,7 @@ export default function DataService() {
function checkData(data) {
//console.log('start check data')
try {
if( data === null) console.log("null data");
return data === null || data === [] || checkCardsArr(data)
} catch {
return false
Expand All @@ -92,13 +93,15 @@ export default function DataService() {
.then((d) => {
let data = tryParce(d)//here we parce json
//console.log("[DATA] from loadData(): ", data)
if(!data)console.log("empty data from server");
if (!checkData(data)) {
console.error("[loadData] Bad data format")
console.log(data)
if (user !== null) {
let checkDel = user !== null
if (checkDel && window.confirm("Bad data: " + data + ". Delete data on server?")) {
console.log('clear data')
requestPostData([new Card({ id: 0, color: "orange", name: "Error", text: "Данные были очищены из за ошибки" })]).then(() => loadData().then(res, rej), rej)//очистка данных
} else rej("Not format data & unlogged")
} else rej("Not format data" + !user ? " & unlogged" : "")
} else {
res(data || [])
}
Expand All @@ -118,7 +121,10 @@ export default function DataService() {
? Promise.reject(rej())
: loadData())
.then((d) => {
if(!data)console.log("empty data to post");
if(!d)console.log("empty loaded to check");
let pDat = data === null ? (d || []) : data
if(!pDat)console.log("empty will be posted");
requestPostData(pDat).then(res, rej)
})
.catch(rej)
Expand Down