Skip to content

Commit 502236c

Browse files
committed
login and data update
1 parent cd223e9 commit 502236c

File tree

2 files changed

+62
-49
lines changed

2 files changed

+62
-49
lines changed

src/App.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Loader from './Content/Loader'
88
import Modal from './Modal/Modal'
99
import DataService from './DataService'
1010

11-
const { loadData, postData, tryParce } = DataService()
11+
const { loadData, postData, login } = DataService()
1212

1313
const testText = "My little cards-app c:";
1414

@@ -26,25 +26,35 @@ function App() {
2626
const [editCardId, setEditCardId] = React.useState(null)
2727
const [loading, setLoading] = React.useState(true)
2828
// eslint-disable-next-line react-hooks/exhaustive-deps
29+
React.useEffect(tryLogin, [])
2930
React.useEffect(loadDataFromServer, [])
3031
React.useEffect(loadDataToServer, [cardsArr])
3132

33+
///////////
34+
function tryLogin() {
35+
login().then(loadDataFromServer, alert)
36+
}
37+
3238
function loadDataToServer() {
3339
//console.log("***\n[HOOK] - loadDataToServer (onCardsArr)\n***")
34-
postData(cardsArr)
40+
postData(cardsArr).then(console.log, dataError)
3541
}
3642

3743
function loadDataFromServer() {
3844
//console.log("[HOOK] - loadDataFromServer")
39-
loadData().then(setLoadedCards)
45+
loadData().then(setLoadedCards, dataError)
46+
}
47+
48+
function dataError(msg) {
49+
console.log(`Data request error. Response: ${msg}`)
4050
}
4151

4252
function setLoadedCards(cards) {
43-
cards = tryParce(cards)
4453
setCards([...cards])
45-
cardCount = calcCount(tryParce(cards))
54+
cardCount = calcCount(cards)
4655
setLoading(false)
4756
}
57+
///////////
4858

4959
///////////
5060
function removeCard(index) {
@@ -80,7 +90,7 @@ function App() {
8090
}
8191
///////////
8292

83-
///
93+
///////////
8494
function getCardByIndex(index) {
8595
return index !== null ? cardsArr[index] : null
8696
}
@@ -90,7 +100,7 @@ function App() {
90100
function unsetEditCard() {
91101
setEditCardId(null)
92102
}
93-
///
103+
///////////
94104

95105

96106
return (

src/DataService.js

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,46 @@
11
import $ from "jquery";
22
export default function DataService() {
33
////////////////////////////////////////////////////////////
4-
let user = null
4+
var user = null
55
const deploy = true
66
const url = deploy ? 'http://php-server-notes.std-1033.ist.mospolytech.ru/' : 'http://php-server-notes/'
77
//let recuestCount = 1;
88

99
////////////////////////////////////////////////////////////
1010
function login() {
11-
let submit = null
12-
function tryLogin(username) {
13-
new Promise((res, rej) => {
14-
checkLogin(prompt("Введите логин", username || "")).then(res, rej)
15-
}).then(onLogin, tryLogin)
16-
}
17-
function onLogin(username) {
18-
user = username
19-
submit()
20-
console.log('Login: ', user)
21-
const label = document.getElementsByClassName("show_login")[0]
22-
label.textContent = ''
23-
label.onclick = () => label.style.opacity = label.style.opacity !== '0' ? '0' : '1'
24-
}
25-
return new Promise((resolve) => {
26-
submit = resolve
27-
tryLogin()
11+
return new Promise((submit, dismiss) => {
12+
function tryLogin(def) {
13+
let input = prompt(def ? "Исправьте логин" : "Введите логин", def || "")
14+
input === null
15+
? onDismiss()
16+
: checkLogin(input.trim())
17+
? onLogin(input.trim())
18+
: tryLogin(input.trim())
19+
}
20+
function onLogin(username) {
21+
user = username
22+
submit(username)
23+
console.log('Login: ', user)
24+
const label = document.getElementsByClassName("show_login")[0]
25+
label.textContent = ''
26+
label.onclick = () => label.style.opacity = label.style.opacity !== '0' ? '0' : '1'
27+
}
28+
function onDismiss() {
29+
user = null
30+
dismiss("Вы не залогинились")
31+
console.log('Login dismissed')
32+
}
33+
$(() => tryLogin())
2834
})
2935
}
3036

3137
function checkLogin(str) {
32-
return new Promise((res, rej) => {
33-
let username
34-
try {
35-
username = str.replace(/@|;|:|\.|,|\/|\\|\||\$|\?|!|#|%|\*|\^|\+|=|\[|\]| |\\ |«|<|>/gi, "").trim()
36-
username && username.length > 3 && username.length < 20 && username === str
37-
? res(username)
38-
: rej(username)
39-
} catch {
40-
rej(username)
41-
}
42-
})
38+
try {
39+
let filtered = str.replace(/@|;|:|\.|,|\/|\\|\||\$|\?|!|#|%|\*|\^|\+|=|\[|\]| |\\ |«|<|>/gi, "").trim()
40+
return (filtered && filtered.length > 3 && filtered.length < 20 && filtered === str)
41+
} catch {
42+
return false
43+
}
4344
}
4445
////////////////////////////////////////////////////////////
4546

@@ -99,28 +100,30 @@ export default function DataService() {
99100
function loadData() {
100101
return new Promise((res, rej) => {
101102
(user === null
102-
? login(null)
103-
: Promise.resolve(null))
104-
.then(() => requestGetData(null))
103+
? rej()
104+
: requestGetData())
105105
.then((d) => {
106106
let data = tryParce(d)//here we parce json
107107
//console.log("[DATA] from loadData(): ", data)
108108
res(data || [])
109-
})
109+
}, rej)
110110
.catch(rej)
111111
})
112112
}
113113

114-
function postData(postData) {
115-
if (user !== null) {
116-
loadData()
117-
.then((data) => {
118-
let pDat = postData === null ? (data || []) : postData
119-
requestPostData(pDat)
120-
})
121-
}
114+
function postData(data) {
115+
return new Promise((res, rej) => {
116+
(user === null
117+
? rej()
118+
: loadData())
119+
.then((d) => {
120+
let pDat = data === null ? (d || []) : data
121+
requestPostData(pDat).then(res, rej)
122+
}, rej)
123+
.catch(rej)
124+
})
122125
}
123126
////////////////////////////////////////////////////////////
124127

125-
return { loadData, postData, tryParce }
128+
return { loadData, postData, login }
126129
}

0 commit comments

Comments
 (0)