|
1 | 1 | import $ from "jquery"; |
2 | 2 | export default function DataService() { |
3 | 3 | //////////////////////////////////////////////////////////// |
4 | | - let user = null |
| 4 | + var user = null |
5 | 5 | const deploy = true |
6 | 6 | const url = deploy ? 'http://php-server-notes.std-1033.ist.mospolytech.ru/' : 'http://php-server-notes/' |
7 | 7 | //let recuestCount = 1; |
8 | 8 |
|
9 | 9 | //////////////////////////////////////////////////////////// |
10 | 10 | 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()) |
28 | 34 | }) |
29 | 35 | } |
30 | 36 |
|
31 | 37 | 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 | + } |
43 | 44 | } |
44 | 45 | //////////////////////////////////////////////////////////// |
45 | 46 |
|
@@ -99,28 +100,30 @@ export default function DataService() { |
99 | 100 | function loadData() { |
100 | 101 | return new Promise((res, rej) => { |
101 | 102 | (user === null |
102 | | - ? login(null) |
103 | | - : Promise.resolve(null)) |
104 | | - .then(() => requestGetData(null)) |
| 103 | + ? rej() |
| 104 | + : requestGetData()) |
105 | 105 | .then((d) => { |
106 | 106 | let data = tryParce(d)//here we parce json |
107 | 107 | //console.log("[DATA] from loadData(): ", data) |
108 | 108 | res(data || []) |
109 | | - }) |
| 109 | + }, rej) |
110 | 110 | .catch(rej) |
111 | 111 | }) |
112 | 112 | } |
113 | 113 |
|
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 | + }) |
122 | 125 | } |
123 | 126 | //////////////////////////////////////////////////////////// |
124 | 127 |
|
125 | | - return { loadData, postData, tryParce } |
| 128 | + return { loadData, postData, login } |
126 | 129 | } |
0 commit comments