Skip to content

Commit 570af19

Browse files
authored
Merge pull request #5 from g3n1us/add-queries-to-model
add queries to ModelComponent
2 parents a626790 + 7beb881 commit 570af19

File tree

2 files changed

+93
-55
lines changed

2 files changed

+93
-55
lines changed

src/assets/LaravelReactSync.js

Lines changed: 86 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,53 @@
11
import React, { Component } from 'react';
22
import ReactDOM from 'react-dom';
33
import qs from 'qs';
4+
import Async from 'react-promise';
45

56
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+
)
712

813
export class ModelComponent extends Component{
914
constructor(props){
1015
super(props);
1116
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+
}} />
1426
}
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+
1751
getComponentFormData(){
1852
let $this = $(ReactDOM.findDOMNode(this));
1953
if(!$this.is('form'))
@@ -22,39 +56,37 @@ export class ModelComponent extends Component{
2256
if(!$this.length){
2357
// 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.
2458
// Note!! To call different methods (eg. delete, save, create) within a rendered component, you have to use separate forms.
25-
59+
2660
formdata = $(ReactDOM.findDOMNode(this)).find(':input').serialize();
27-
formdata = qs.parse(formdata);
61+
formdata = qs.parse(formdata);
2862
}
2963
else{
30-
formdata = qs.parse($this.serialize());
64+
formdata = qs.parse($this.serialize());
3165
}
3266
formdata = g3n1us_helpers.array_merge(this.props, formdata);
3367
formdata = g3n1us_helpers.array_merge(formdata, this.buttonData);
3468
if(!formdata.model_classname)
3569
formdata.model_classname = this._reactInternalInstance.getName();
3670
return formdata;
3771
}
38-
39-
72+
73+
4074
getApp(){
41-
// return this._reactInternalInstance._currentElement._owner._instance.app;
42-
4375
return REACT_SYNC_DATA;
4476
}
45-
46-
77+
78+
4779
componentDidMount(){
4880
let $this = $(ReactDOM.findDOMNode(this));
4981
if(this.props.updateOnChange){
5082
$this.on('change', ':input', (e) => {
5183
this.handleInputChange(e);
52-
});
84+
});
5385
}
54-
86+
5587
if(!$this.is('form'))
5688
$this = $this.find('form');
57-
89+
5890
// if this isn't a form and no child nodes are forms, then ignore the rest and return
5991
if(!$this.length)
6092
return;
@@ -71,8 +103,8 @@ export class ModelComponent extends Component{
71103
this.updateRequest(formdata);
72104
});
73105
}
74-
75-
106+
107+
76108
updateRequest(formdata){
77109
let axios_method = window.g3n1us_helpers.array_get(formdata, '_method', 'post').toLowerCase();
78110
axios({
@@ -82,34 +114,34 @@ export class ModelComponent extends Component{
82114
})
83115
.then((response) => {
84116
REACT_SYNC_DATA.update();
85-
117+
86118
this.setState(response.data || {});
87-
119+
88120
let level = response.status < 400 ? 'success' : 'danger';
89121

90122
ReactDOM.render(
91123
<Alert message="Saved" level={level} />,
92124
document.getElementById('notification_outer'),
93125
);
94-
126+
95127
});
96128
}
97-
98-
129+
130+
99131
handleInputChange(event) {
100132

101133
event.preventDefault();
102-
134+
103135
const target = event.target;
104-
136+
105137
const value = target.type === 'checkbox' ? target.checked : target.value;
106-
138+
107139
const name = target.name;
108140

109141
let formd = this.getComponentFormData();
110142

111143
let thiskey = _.keys(qs.parse(name))[0];
112-
144+
113145
let filtered_formdata = g3n1us_helpers.array_only(formd, [thiskey, 'model_classname', '_method', 'id']);
114146
let final_filtered = {};
115147
for(let i in filtered_formdata){
@@ -122,8 +154,8 @@ export class ModelComponent extends Component{
122154

123155
this.updateRequest(final_filtered);
124156
}
125-
126-
157+
158+
127159
}
128160

129161

@@ -136,15 +168,15 @@ export class MasterComponent extends Component{
136168
REACT_SYNC_DATA.components.push(this);
137169
}
138170

139-
140-
171+
172+
141173
componentDidMount(){
142174
$(this).on('refresh-state', (e) => {
143175
console.log('refresh-state !!!', e);
144176
this.setState(REACT_SYNC_DATA.page_data);
145-
});
177+
});
146178
}
147-
179+
148180
}
149181

150182

@@ -162,27 +194,27 @@ export class Alert extends Component{
162194
}, 3000);
163195

164196
}
165-
197+
166198
render() {
167-
199+
168200
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">&times;</a></div> : null;
169201

170-
}
171-
202+
}
203+
172204
}
173205

174206

175207
export class Pagination extends Component{
176208
constructor(props){
177209
super(props);
178210
}
179-
211+
180212
render(){
181213
let links = [];
182214
let current_page = 1
183-
if(current_page == this.props.last_page)
215+
if(current_page == this.props.last_page)
184216
return null; // There is only one page, so return nothing
185-
217+
186218
while(current_page <= this.props.last_page){
187219
if(current_page == this.props.current_page){
188220
links.push(
@@ -196,7 +228,7 @@ export class Pagination extends Component{
196228
<li className="page-item" key={g3n1us_helpers.str_rand(20)}>
197229
<a className="page-link" href={`?${qs.stringify(req)}`}>{current_page}</a>
198230
</li>
199-
);
231+
);
200232
}
201233
current_page++;
202234
}
@@ -217,13 +249,13 @@ export class Pagination extends Component{
217249
}
218250
else{
219251
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)));
221253
}
222254

223-
links = tmplinks;
224-
255+
links = tmplinks;
256+
225257
}
226-
258+
227259
let req = REACT_SYNC_DATA.request;
228260
req.page = this.props.current_page - 1;
229261
let prev_page_url = `?${qs.stringify(req)}`;
@@ -232,22 +264,22 @@ export class Pagination extends Component{
232264
return (
233265
<div className="d-flex justify-content-center">
234266
<ul className="pagination">
235-
{this.props.prev_page_url
236-
?
267+
{this.props.prev_page_url
268+
?
237269
<li className="page-item"><a className="page-link" href={prev_page_url} rel="previous">«</a></li>
238270
:
239271
<li className="page-item disabled"><span className="page-link">«</span></li>
240-
}
272+
}
241273
{links}
242-
{this.props.next_page_url
243-
?
274+
{this.props.next_page_url
275+
?
244276
<li className="page-item"><a className="page-link" href={next_page_url} rel="next">»</a></li>
245277
:
246278
<li className="page-item disabled"><span className="page-link">»</span></li>
247-
}
279+
}
248280
</ul>
249-
</div>
281+
</div>
250282
);
251283
}
252-
284+
253285
}

src/assets/package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
"dependencies": {
1313
"g3n1us_helpers": "^1.0.5",
1414
"on-change": "^0.1.0",
15-
"qs": "^6.5.1"
15+
"qs": "^6.5.1",
16+
"react": "^15.4.2",
17+
"react-dom": "^15.4.2",
18+
"react-promise": "^2.0.3",
19+
"lodash": "^4.17.5",
20+
"jquery": "^3.2",
21+
"axios": "^0.18"
1622
}
1723
}

0 commit comments

Comments
 (0)