@@ -2,74 +2,82 @@ import React from 'react';
2
2
import './App.css' ;
3
3
import Header from './components/Header/Header' ;
4
4
import Content from './components/Content/Content' ;
5
-
6
- const data = [ {
7
- name : "games" ,
8
- graphs : [ {
9
- name : "Roofcampers in rust" ,
10
- type : "noobs" ,
11
- data : [ { x : new Date ( 2013 , 1 , 1 ) , y : 140 } ,
12
- { x : new Date ( 1987 , 1 , 1 ) , y : 12 } ,
13
- { x : new Date ( 1993 , 1 , 1 ) , y : 14 } ,
14
- { x : new Date ( 1997 , 1 , 1 ) , y : 18 } ,
15
- { x : new Date ( 2001 , 1 , 1 ) , y : 52 } ,
16
- { x : new Date ( 2005 , 1 , 1 ) , y : 80 } ,
17
- { x : new Date ( 2011 , 1 , 1 ) , y : 150 } ,
18
- { x : new Date ( 2015 , 1 , 1 ) , y : 170 } ]
19
- } ,
20
- {
21
- name : "Balanced matches in dota" ,
22
- type : "matches" ,
23
- data : [ { x : new Date ( 2013 , 1 , 1 ) , y : 400 } ,
24
- { x : new Date ( 1987 , 1 , 1 ) , y : 320 } ,
25
- { x : new Date ( 1993 , 1 , 1 ) , y : 280 } ,
26
- { x : new Date ( 1997 , 1 , 1 ) , y : 150 } ,
27
- { x : new Date ( 2001 , 1 , 1 ) , y : 210 } ,
28
- { x : new Date ( 2005 , 1 , 1 ) , y : 120 } ,
29
- { x : new Date ( 2011 , 1 , 1 ) , y : 80 } ,
30
- { x : new Date ( 2015 , 1 , 1 ) , y : 2 } ]
31
- }
32
- ]
33
- } ,
34
- {
35
- name : "ugh" ,
36
- graphs : [ {
37
- name : "uga" ,
38
- type : "meme" ,
39
- data : [ { x : new Date ( 2013 , 1 , 1 ) , y : 140 } ,
40
- { x : new Date ( 1987 , 1 , 1 ) , y : 12 } ,
41
- { x : new Date ( 1993 , 1 , 1 ) , y : 14 } ,
42
- { x : new Date ( 1997 , 1 , 1 ) , y : 18 } ,
43
- { x : new Date ( 2001 , 1 , 1 ) , y : 52 } ,
44
- { x : new Date ( 2005 , 1 , 1 ) , y : 80 } ,
45
- { x : new Date ( 2011 , 1 , 1 ) , y : 150 } ,
46
- { x : new Date ( 2015 , 1 , 1 ) , y : 170 } ]
47
- } ,
48
- {
49
- name : "dada" ,
50
- type : "xd" ,
51
- data : [ { x : new Date ( 2013 , 1 , 1 ) , y : 400 } ,
52
- { x : new Date ( 1987 , 1 , 1 ) , y : 320 } ,
53
- { x : new Date ( 1993 , 1 , 1 ) , y : 280 } ,
54
- { x : new Date ( 1997 , 1 , 1 ) , y : 150 } ,
55
- { x : new Date ( 2001 , 1 , 1 ) , y : 210 } ,
56
- { x : new Date ( 2005 , 1 , 1 ) , y : 120 } ,
57
- { x : new Date ( 2011 , 1 , 1 ) , y : 80 } ,
58
- { x : new Date ( 2015 , 1 , 1 ) , y : 2 } ]
59
- }
60
- ]
61
- }
62
- ]
5
+ import axios from 'axios' ;
6
+ import Nothing from './components/Nothing'
7
+
8
+
9
+ let data = [ ] ;
10
+
11
+ // let data = [{
12
+ // name: "games",
13
+ // graphs: [{
14
+ // name: "Roofcampers in rust",
15
+ // type: "noobs",
16
+ // data: [{ x: new Date(2013, 1, 1), y: 140 },
17
+ // { x: new Date(1987, 1, 1), y: 12 },
18
+ // { x: new Date(1993, 1, 1), y: 14 },
19
+ // { x: new Date(1997, 1, 1), y: 18 },
20
+ // { x: new Date(2001, 1, 1), y: 52 },
21
+ // { x: new Date(2005, 1, 1), y: 80 },
22
+ // { x: new Date(2011, 1, 1), y: 150 },
23
+ // { x: new Date(2015, 1, 1), y: 170 }]
24
+ // },
25
+ // {
26
+ // name: "Balanced matches in dota",
27
+ // type: "matches",
28
+ // data: [{ x: new Date(2013, 1, 1), y: 400 },
29
+ // { x: new Date(1987, 1, 1), y: 320 },
30
+ // { x: new Date(1993, 1, 1), y: 280 },
31
+ // { x: new Date(1997, 1, 1), y: 150 },
32
+ // { x: new Date(2001, 1, 1), y: 210 },
33
+ // { x: new Date(2005, 1, 1), y: 120 },
34
+ // { x: new Date(2011, 1, 1), y: 80 },
35
+ // { x: new Date(2015, 1, 1), y: 2 }]
36
+ // }
37
+ // ]
38
+ // },
39
+ // {
40
+ // name: "ugh",
41
+ // graphs: [{
42
+ // name: "uga",
43
+ // type: "meme",
44
+ // data: [{ x: new Date(2013, 1, 1), y: 140 },
45
+ // { x: new Date(1987, 1, 1), y: 12 },
46
+ // { x: new Date(1993, 1, 1), y: 14 },
47
+ // { x: new Date(1997, 1, 1), y: 18 },
48
+ // { x: new Date(2001, 1, 1), y: 52 },
49
+ // { x: new Date(2005, 1, 1), y: 80 },
50
+ // { x: new Date(2011, 1, 1), y: 150 },
51
+ // { x: new Date(2015, 1, 1), y: 170 }]
52
+ // },
53
+ // {
54
+ // name: "dada",
55
+ // type: "xd",
56
+ // data: [{ x: new Date(2013, 1, 1), y: 400 },
57
+ // { x: new Date(1987, 1, 1), y: 320 },
58
+ // { x: new Date(1993, 1, 1), y: 280 },
59
+ // { x: new Date(1997, 1, 1), y: 150 },
60
+ // { x: new Date(2001, 1, 1), y: 210 },
61
+ // { x: new Date(2005, 1, 1), y: 120 },
62
+ // { x: new Date(2011, 1, 1), y: 80 },
63
+ // { x: new Date(2015, 1, 1), y: 2 }]
64
+ // }
65
+ // ]
66
+ // }
67
+ // ]
63
68
64
69
65
70
class App extends React . Component {
66
71
constructor ( props ) {
67
72
super ( props ) ;
68
- this . first = data [ 0 ] ;
69
73
70
- this . state = { current : { name : this . first . graphs [ 0 ] . name ,
71
- type : this . first . graphs [ 0 ] . type ,
72
- data : this . first . graphs [ 0 ] . data , category : 0 , graph : 0 } } ;
74
+ // if(data.length > 0){
75
+ // this.first = data[0];
76
+
77
+ // this.state = { current: { name: this.first.graphs[0].name,
78
+ // type: this.first.graphs[0].type,
79
+ // data: this.first.graphs[0].data, category: 0, graph: 0 } };
80
+ // }
73
81
74
82
this . onGraphChange = this . onGraphChange . bind ( this ) ;
75
83
this . newValue = this . newValue . bind ( this ) ;
@@ -78,16 +86,28 @@ class App extends React.Component {
78
86
this . graphUpdate = this . graphUpdate . bind ( this ) ;
79
87
}
80
88
81
- componentDidMount ( ) {
82
- document . title = "Hey" ;
89
+ async componentDidMount ( ) {
90
+ await getData ( ) ;
91
+
92
+ if ( data . length > 0 ) {
93
+ this . first = data [ 0 ] ;
94
+
95
+ this . state = { current : { name : this . first . graphs [ 0 ] . name ,
96
+ type : this . first . graphs [ 0 ] . type ,
97
+ data : this . first . graphs [ 0 ] . data , category : 0 , graph : 0 } } ;
98
+ }
99
+
100
+ document . title = "Progress View" ;
83
101
}
84
102
85
- newValue ( date , value , category , graph , special , special_data ) {
103
+ async newValue ( date , value , category , graph , special , special_data ) {
86
104
if ( ! special )
87
105
data [ category ] . graphs [ graph ] . data . push ( { x : date , y : parseInt ( value ) } ) ;
88
106
else
89
107
data [ category ] . graphs [ graph ] . data . push ( { x : date , y : parseInt ( value ) , special_data : special_data } ) ;
90
108
109
+ await saveData ( data ) ;
110
+
91
111
this . setState ( { current : { name : data [ category ] . graphs [ graph ] . name ,
92
112
type : data [ category ] . graphs [ graph ] . type ,
93
113
data : data [ category ] . graphs [ graph ] . data ,
@@ -96,11 +116,13 @@ class App extends React.Component {
96
116
console . log ( data ) ;
97
117
}
98
118
99
- newGraph ( category , graph , valueType ) {
119
+ async newGraph ( category , graph , valueType ) {
100
120
let a = newGraphParse ( category , graph , valueType )
101
121
let category_index = a . category_index ;
102
122
let graph_index = a . graph_index ;
103
123
124
+ await saveData ( data ) ;
125
+
104
126
this . setState ( { current : { name : data [ category_index ] . graphs [ graph_index ] . name ,
105
127
type : data [ category_index ] . graphs [ graph_index ] . type ,
106
128
data : data [ category_index ] . graphs [ graph_index ] . data ,
@@ -114,20 +136,27 @@ class App extends React.Component {
114
136
category : category , graph : graph } } ) ;
115
137
}
116
138
117
- delete ( ) {
139
+ async delete ( ) {
118
140
let cat = this . state . current . category ;
119
141
let grf = this . state . current . graph ;
120
142
data [ cat ] . graphs . splice ( grf , 1 ) ;
121
143
if ( data [ cat ] . graphs . length === 0 )
122
144
data . splice ( cat , 1 ) ;
123
145
124
- let first = data [ 0 ] ;
125
- this . setState ( { current : { name : first . graphs [ 0 ] . name ,
126
- type : first . graphs [ 0 ] . type ,
127
- data : first . graphs [ 0 ] . data , category : 0 , graph : 0 } } ) ;
146
+ await saveData ( data ) ;
147
+
148
+ if ( data . length > 0 ) {
149
+ let first = data [ 0 ] ;
150
+ this . setState ( { current : { name : first . graphs [ 0 ] . name ,
151
+ type : first . graphs [ 0 ] . type ,
152
+ data : first . graphs [ 0 ] . data , category : 0 , graph : 0 } } ) ;
153
+ }
154
+ else {
155
+ this . setState ( { nothing : true } )
156
+ }
128
157
}
129
158
130
- graphUpdate ( graph ) {
159
+ async graphUpdate ( graph ) {
131
160
let parsed
132
161
try {
133
162
parsed = JSON . parse ( graph ) ;
@@ -143,15 +172,28 @@ class App extends React.Component {
143
172
data [ cat ] . graphs [ grf ] . data = parsed ;
144
173
let current = data [ cat ] . graphs [ grf ] ;
145
174
175
+ await saveData ( data ) ;
176
+
146
177
this . setState ( { current : { name : current . name , type : current . type , data : parsed , category : cat , graph : grf } } ) ;
147
178
}
148
179
149
180
render ( ) {
181
+
182
+ let content = < Nothing /> ;
183
+ let header = < Header title = { "" } onGraphChange = { this . onGraphChange } data = { data } newGraph = { this . newGraph } />
184
+ console . log ( "================================" ) ;
185
+ console . log ( data ) ;
186
+ console . log ( "================================" ) ;
187
+ if ( data . length > 0 ) {
188
+ content = < Content data = { this . state . current . data } title = { this . state . current . name } newValue = { this . newValue } category = { this . state . current . category }
189
+ graph = { this . state . current . graph } valueType = { this . state . current . type } delete = { this . delete } graphUpdate = { this . graphUpdate } />
190
+
191
+ header = < Header title = { this . state . current . name } onGraphChange = { this . onGraphChange } data = { data } newGraph = { this . newGraph } />
192
+ }
150
193
return (
151
194
< >
152
- < Header title = { this . state . current . name } onGraphChange = { this . onGraphChange } data = { data } newGraph = { this . newGraph } />
153
- < Content data = { this . state . current . data } title = { this . state . current . name } newValue = { this . newValue } category = { this . state . current . category }
154
- graph = { this . state . current . graph } valueType = { this . state . current . type } delete = { this . delete } graphUpdate = { this . graphUpdate } />
195
+ { header }
196
+ { content }
155
197
</ >
156
198
) ;
157
199
}
@@ -181,3 +223,32 @@ function newGraphParse(category,graph,valueType){
181
223
182
224
return { category_index, graph_index} ;
183
225
}
226
+
227
+
228
+ async function getData ( ) {
229
+ try {
230
+ let res = await axios . get ( '/api/' ) ;
231
+ if ( res . status )
232
+ return res . data ;
233
+ else
234
+ alert ( "error" ) ;
235
+ }
236
+ catch {
237
+ alert ( "error contacting server" ) ;
238
+ }
239
+ return [ ] ;
240
+ }
241
+
242
+ async function saveData ( data ) {
243
+ try {
244
+ let res = await axios . post ( '/api/' , { data :data } ) ;
245
+ if ( res . status )
246
+ console . log ( res . msg ) ;
247
+ else
248
+ alert ( "error" ) ;
249
+ }
250
+ catch {
251
+ alert ( "error contacting server" ) ;
252
+ }
253
+ }
254
+
0 commit comments