@@ -6,102 +6,9 @@ import AddCard from './Cards/AddCard'
66import Context from './context'
77import Loader from './Content/Loader'
88import 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
10613const testText = "My little cards-app c:" ;
10714
0 commit comments