Skip to content

Commit 755dfad

Browse files
committed
separated DataService file
1 parent 214fb84 commit 755dfad

File tree

3 files changed

+96
-100
lines changed

3 files changed

+96
-100
lines changed

src/App.js

Lines changed: 2 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -6,102 +6,9 @@ import AddCard from './Cards/AddCard'
66
import Context from './context'
77
import Loader from './Content/Loader'
88
import Modal from './Modal/Modal'
9-
import $ from "jquery";
10-
//import { } from './DataService'
11-
12-
13-
///////////////////////////////////
14-
15-
const url = 'http://php-server-notes/'
16-
let user = null
17-
//let recuestCount = 1;
18-
function request(target, data) {
19-
//const rc = recuestCount++
20-
//console.log(` \nrequest ${rc} - "${target}" started \n params - user:"${user}" data:"${trimStr(data)}"`)
21-
return new Promise((res, rej) => {
22-
$.ajax({
23-
url: url,
24-
type: "POST",
25-
dataType: "html",
26-
data: {
27-
user: user,
28-
target: target,
29-
data: data,
30-
},
31-
})
32-
.done(data => res(data))
33-
.fail(data => rej(data))
34-
//.always(() => console.log(`requested - newUser:"${user}" resolveData:"${trimStr(data)}" \n request ${rc} - "${target}" ended \n `))
35-
})
36-
}
37-
///////////////////////////////////
38-
39-
///////////////////////////////////
40-
function requestUser() {
41-
return request('ip', null).then(prom => {
42-
user = String(prom) || "default"
43-
console.log("user ", user)
44-
})
45-
}
46-
function requestGetData() {
47-
return request('getData', null)
48-
}
49-
function requestPostData(data) {
50-
return request('setData', data || [])
51-
}
52-
///////////////////////////////////
53-
54-
///
55-
function trimStr(str) {
56-
const trimLen = 50
57-
try {
58-
return (str.length > trimLen) ? str.slice(0, trimLen) + "..." : str
59-
} catch (e) {
60-
return str
61-
}
62-
}
63-
function tryParce(str) {
64-
try {
65-
return JSON.parse(str, reviver);
66-
} catch (e) {
67-
return str;
68-
}
69-
}
70-
function reviver(key, value) {
71-
if (typeof value == 'string' && (Boolean(value) !== undefined)) {
72-
if (value === "false") return false;
73-
if (value === "true") return true;
74-
}
75-
return value;
76-
}
77-
///
78-
79-
//////////////
80-
function loadData() {
81-
return new Promise((res, rej) => {
82-
(user === null
83-
? requestUser(null)
84-
: Promise.resolve(null))
85-
.then(() => requestGetData(null))
86-
.then((d) => {
87-
let data = tryParce(d)//here we parce json
88-
console.log("[DATA] from loadData(): ", data)
89-
res(data || [])
90-
})
91-
.catch(rej)
92-
})
93-
}
94-
function postData(postData) {
95-
(user === null
96-
? loadData(null)
97-
: Promise.resolve(null))
98-
.then((data) => {
99-
let pDat = postData === null ? (data || []) : postData
100-
requestPostData(pDat)
101-
})
102-
}
103-
//////////////////
9+
import DataService from './DataService'
10410

11+
const { loadData, postData, tryParce } = DataService()
10512

10613
const testText = "My little cards-app c:";
10714

src/Cards/CardList.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import StackGrid, { transitions } from "react-stack-grid"
55
import sizeMe from 'react-sizeme'
66
const { scaleDown } = transitions
77

8-
9-
108
function calcWidth() {
119
const small = 576
1210
const middle = 768
@@ -21,8 +19,6 @@ function calcWidth() {
2119
else return '100%'
2220
}
2321

24-
25-
2622
function CardList(props) {
2723
const [grid, setGrid] = React.useState(null)
2824
gridRedraw()

src/DataService.js

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,94 @@
1-
import $ from "jquery";
1+
import $ from "jquery";
2+
export default function DataService() {
3+
////////////////////////////////////////////////////////////
4+
let user = null
5+
const url = 'http://php-server-notes/'
6+
//let recuestCount = 1;
7+
8+
function request(target, data) {
9+
//const rc = recuestCount++
10+
//console.log(` \nrequest ${rc} - "${target}" started \n params - user:"${user}" data:"${data}"`)
11+
return new Promise((res, rej) => {
12+
$.ajax({
13+
url: url,
14+
type: "POST",
15+
dataType: "html",
16+
data: {
17+
user: user,
18+
target: target,
19+
data: data,
20+
},
21+
})
22+
.done(data => res(data))
23+
.fail(data => rej(data))
24+
/*.always(() => console.log(
25+
`requested - newUser:"${user}" resolveData:"${data}" \n
26+
request ${rc} - "${target}" ended \n `
27+
))*/
28+
})
29+
}
30+
////////////////////////////////////////////////////////////
31+
32+
////////////////////////////////////////////////////////////
33+
function requestUser() {
34+
return request('ip', null).then(prom => {
35+
user = String(prom) || "default"
36+
console.log("user ", user)
37+
})
38+
}
39+
40+
function requestGetData() {
41+
return request('getData', null)
42+
}
43+
44+
function requestPostData(data) {
45+
return request('setData', data || [])
46+
}
47+
////////////////////////////////////////////////////////////
48+
49+
////////////////////////////////////////////////////////////
50+
function tryParce(str) {
51+
try {
52+
return JSON.parse(str, reviver);
53+
} catch (e) {
54+
return str;
55+
}
56+
}
57+
function reviver(key, value) {
58+
if (typeof value == 'string' && (Boolean(value) !== undefined)) {
59+
if (value === "false") return false;
60+
if (value === "true") return true;
61+
}
62+
return value;
63+
}
64+
////////////////////////////////////////////////////////////
65+
66+
////////////////////////////////////////////////////////////
67+
function loadData() {
68+
return new Promise((res, rej) => {
69+
(user === null
70+
? requestUser(null)
71+
: Promise.resolve(null))
72+
.then(() => requestGetData(null))
73+
.then((d) => {
74+
let data = tryParce(d)//here we parce json
75+
console.log("[DATA] from loadData(): ", data)
76+
res(data || [])
77+
})
78+
.catch(rej)
79+
})
80+
}
81+
82+
function postData(postData) {
83+
(user === null
84+
? loadData(null)
85+
: Promise.resolve(null))
86+
.then((data) => {
87+
let pDat = postData === null ? (data || []) : postData
88+
requestPostData(pDat)
89+
})
90+
}
91+
////////////////////////////////////////////////////////////
92+
93+
return { loadData, postData, tryParce }
94+
}

0 commit comments

Comments
 (0)