1
1
import React , { Component } from 'react' ;
2
2
import ReactDOM from 'react-dom' ;
3
3
import qs from 'qs' ;
4
+ import Async from 'react-promise' ;
4
5
5
6
const REACT_SYNC_DATA = window [ window . ReactSyncGlobal ] ;
6
- // This componenet maps specifically to a Laravel model.
7
+ // This componenet maps specifically to a Laravel model.
8
+
9
+ Async . defaultPending = (
10
+ < span > loading...</ span >
11
+ )
7
12
8
13
export class ModelComponent extends Component {
9
14
constructor ( props ) {
10
15
super ( props ) ;
11
16
this . buttonData = { } ;
12
- this . handleInputChange = this . handleInputChange . bind ( this ) ;
13
-
17
+ this . handleInputChange = this . handleInputChange . bind ( this ) ;
18
+
19
+ }
20
+
21
+ static find ( id ) {
22
+ let prom = axios . get ( `/api/${ this . name . toLowerCase ( ) } /${ id } ` ) ;
23
+ return < Async promise = { prom } then = { d => {
24
+ return React . createElement ( this , { ...d . data } ) ;
25
+ } } />
14
26
}
15
-
16
-
27
+
28
+ static where ( a , b , c ) {
29
+ let query = [ ] . slice . call ( arguments ) ;
30
+ let prom = axios . get ( '/api/countries' , { params : { where : query } } ) ;
31
+ return < Async promise = { prom } then = { d => {
32
+ let els = d . data . map ( ( e ) => {
33
+ return React . createElement ( this , { ...e } ) ;
34
+ } ) ;
35
+ return < div > { els } </ div >
36
+ } } />
37
+
38
+ }
39
+
40
+ static all ( ) {
41
+ let prom = axios . get ( `/api/countries` ) ;
42
+ return < Async promise = { prom } then = { d => {
43
+ let els = d . data . data . map ( ( e ) => {
44
+ return React . createElement ( this , { ...e } ) ;
45
+ } ) ;
46
+ return < div > { els } </ div >
47
+ } } />
48
+ }
49
+
50
+
17
51
getComponentFormData ( ) {
18
52
let $this = $ ( ReactDOM . findDOMNode ( this ) ) ;
19
53
if ( ! $this . is ( 'form' ) )
@@ -22,39 +56,37 @@ export class ModelComponent extends Component{
22
56
if ( ! $this . length ) {
23
57
// This means that there isn't a form element in the component. This is OK!, we will find any inputs to determine the components data.
24
58
// Note!! To call different methods (eg. delete, save, create) within a rendered component, you have to use separate forms.
25
-
59
+
26
60
formdata = $ ( ReactDOM . findDOMNode ( this ) ) . find ( ':input' ) . serialize ( ) ;
27
- formdata = qs . parse ( formdata ) ;
61
+ formdata = qs . parse ( formdata ) ;
28
62
}
29
63
else {
30
- formdata = qs . parse ( $this . serialize ( ) ) ;
64
+ formdata = qs . parse ( $this . serialize ( ) ) ;
31
65
}
32
66
formdata = g3n1us_helpers . array_merge ( this . props , formdata ) ;
33
67
formdata = g3n1us_helpers . array_merge ( formdata , this . buttonData ) ;
34
68
if ( ! formdata . model_classname )
35
69
formdata . model_classname = this . _reactInternalInstance . getName ( ) ;
36
70
return formdata ;
37
71
}
38
-
39
-
72
+
73
+
40
74
getApp ( ) {
41
- // return this._reactInternalInstance._currentElement._owner._instance.app;
42
-
43
75
return REACT_SYNC_DATA ;
44
76
}
45
-
46
-
77
+
78
+
47
79
componentDidMount ( ) {
48
80
let $this = $ ( ReactDOM . findDOMNode ( this ) ) ;
49
81
if ( this . props . updateOnChange ) {
50
82
$this . on ( 'change' , ':input' , ( e ) => {
51
83
this . handleInputChange ( e ) ;
52
- } ) ;
84
+ } ) ;
53
85
}
54
-
86
+
55
87
if ( ! $this . is ( 'form' ) )
56
88
$this = $this . find ( 'form' ) ;
57
-
89
+
58
90
// if this isn't a form and no child nodes are forms, then ignore the rest and return
59
91
if ( ! $this . length )
60
92
return ;
@@ -71,8 +103,8 @@ export class ModelComponent extends Component{
71
103
this . updateRequest ( formdata ) ;
72
104
} ) ;
73
105
}
74
-
75
-
106
+
107
+
76
108
updateRequest ( formdata ) {
77
109
let axios_method = window . g3n1us_helpers . array_get ( formdata , '_method' , 'post' ) . toLowerCase ( ) ;
78
110
axios ( {
@@ -82,34 +114,34 @@ export class ModelComponent extends Component{
82
114
} )
83
115
. then ( ( response ) => {
84
116
REACT_SYNC_DATA . update ( ) ;
85
-
117
+
86
118
this . setState ( response . data || { } ) ;
87
-
119
+
88
120
let level = response . status < 400 ? 'success' : 'danger' ;
89
121
90
122
ReactDOM . render (
91
123
< Alert message = "Saved" level = { level } /> ,
92
124
document . getElementById ( 'notification_outer' ) ,
93
125
) ;
94
-
126
+
95
127
} ) ;
96
128
}
97
-
98
-
129
+
130
+
99
131
handleInputChange ( event ) {
100
132
101
133
event . preventDefault ( ) ;
102
-
134
+
103
135
const target = event . target ;
104
-
136
+
105
137
const value = target . type === 'checkbox' ? target . checked : target . value ;
106
-
138
+
107
139
const name = target . name ;
108
140
109
141
let formd = this . getComponentFormData ( ) ;
110
142
111
143
let thiskey = _ . keys ( qs . parse ( name ) ) [ 0 ] ;
112
-
144
+
113
145
let filtered_formdata = g3n1us_helpers . array_only ( formd , [ thiskey , 'model_classname' , '_method' , 'id' ] ) ;
114
146
let final_filtered = { } ;
115
147
for ( let i in filtered_formdata ) {
@@ -122,8 +154,8 @@ export class ModelComponent extends Component{
122
154
123
155
this . updateRequest ( final_filtered ) ;
124
156
}
125
-
126
-
157
+
158
+
127
159
}
128
160
129
161
@@ -136,15 +168,15 @@ export class MasterComponent extends Component{
136
168
REACT_SYNC_DATA . components . push ( this ) ;
137
169
}
138
170
139
-
140
-
171
+
172
+
141
173
componentDidMount ( ) {
142
174
$ ( this ) . on ( 'refresh-state' , ( e ) => {
143
175
console . log ( 'refresh-state !!!' , e ) ;
144
176
this . setState ( REACT_SYNC_DATA . page_data ) ;
145
- } ) ;
177
+ } ) ;
146
178
}
147
-
179
+
148
180
}
149
181
150
182
@@ -162,27 +194,27 @@ export class Alert extends Component{
162
194
} , 3000 ) ;
163
195
164
196
}
165
-
197
+
166
198
render ( ) {
167
-
199
+
168
200
return this . state . show ? < div style = { { position : 'fixed' , margin : 'auto' , left : 0 , right : 0 , zIndex : '99999' } } className = { `alert fade show alert-${ this . props . level || 'success' } ` } > { this . props . message } < a className = "close text-muted" data-dismiss = "alert" > ×</ a > </ div > : null ;
169
201
170
- }
171
-
202
+ }
203
+
172
204
}
173
205
174
206
175
207
export class Pagination extends Component {
176
208
constructor ( props ) {
177
209
super ( props ) ;
178
210
}
179
-
211
+
180
212
render ( ) {
181
213
let links = [ ] ;
182
214
let current_page = 1
183
- if ( current_page == this . props . last_page )
215
+ if ( current_page == this . props . last_page )
184
216
return null ; // There is only one page, so return nothing
185
-
217
+
186
218
while ( current_page <= this . props . last_page ) {
187
219
if ( current_page == this . props . current_page ) {
188
220
links . push (
@@ -196,7 +228,7 @@ export class Pagination extends Component{
196
228
< li className = "page-item" key = { g3n1us_helpers . str_rand ( 20 ) } >
197
229
< a className = "page-link" href = { `?${ qs . stringify ( req ) } ` } > { current_page } </ a >
198
230
</ li >
199
- ) ;
231
+ ) ;
200
232
}
201
233
current_page ++ ;
202
234
}
@@ -217,13 +249,13 @@ export class Pagination extends Component{
217
249
}
218
250
else {
219
251
tmplinks . push ( < li className = "page-item disabled" key = { g3n1us_helpers . str_rand ( 20 ) } > < span className = "page-link" > ...</ span > </ li > ) ;
220
- tmplinks = tmplinks . concat ( links . slice ( ( this . props . last_page - 2 ) ) ) ;
252
+ tmplinks = tmplinks . concat ( links . slice ( ( this . props . last_page - 2 ) ) ) ;
221
253
}
222
254
223
- links = tmplinks ;
224
-
255
+ links = tmplinks ;
256
+
225
257
}
226
-
258
+
227
259
let req = REACT_SYNC_DATA . request ;
228
260
req . page = this . props . current_page - 1 ;
229
261
let prev_page_url = `?${ qs . stringify ( req ) } ` ;
@@ -232,22 +264,22 @@ export class Pagination extends Component{
232
264
return (
233
265
< div className = "d-flex justify-content-center" >
234
266
< ul className = "pagination" >
235
- { this . props . prev_page_url
236
- ?
267
+ { this . props . prev_page_url
268
+ ?
237
269
< li className = "page-item" > < a className = "page-link" href = { prev_page_url } rel = "previous" > «</ a > </ li >
238
270
:
239
271
< li className = "page-item disabled" > < span className = "page-link" > «</ span > </ li >
240
- }
272
+ }
241
273
{ links }
242
- { this . props . next_page_url
243
- ?
274
+ { this . props . next_page_url
275
+ ?
244
276
< li className = "page-item" > < a className = "page-link" href = { next_page_url } rel = "next" > »</ a > </ li >
245
277
:
246
278
< li className = "page-item disabled" > < span className = "page-link" > »</ span > </ li >
247
- }
279
+ }
248
280
</ ul >
249
- </ div >
281
+ </ div >
250
282
) ;
251
283
}
252
-
284
+
253
285
}
0 commit comments