This repository has been archived by the owner on Jun 18, 2021. It is now read-only.
/
listEdit.jsx
343 lines (302 loc) · 9.64 KB
/
listEdit.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
var React = require('react');
var Modal = require('react-bootstrap/lib/Modal');
var mixins = require('baobab-react/mixins');
var debounce = require('debounce');
var PureRenderMixin = require('react-addons-pure-render-mixin');
var i18n = require('i18next-client');
var actions = require('../../actions');
var info = require('../../info');
var AppCell = require('../appinfo/appCell');
var Alerts = require('../helpers/alerts');
module.exports = React.createClass({
displayName: 'ListEdit',
mixins: [
mixins.branch,
PureRenderMixin,
],
props: {
show: React.PropTypes.bool.isRequired,
onHide: React.PropTypes.func.isRequired,
list: React.PropTypes.object,
},
cursors: {
loading: ['loading'],
apps: ['apps'],
lng: ['lng'],
},
getInitialState: function() {
return {
list: {
_id: null,
name: '',
sort: '-points',
packages: [],
},
list_apps: [],
term: '',
search_key: null,
};
},
componentWillMount: function() {
this.debounceSearch = debounce(this.search, 300);
if (this.props.list && this.props.list._id) {
if (this.props.list.packages.length > 0) {
var paging = {query: {name: {'$in': this.props.list.packages}, mini: true}};
var self = this;
actions.getApps(paging).then(function(data) {
self.setState({list_apps: data.apps});
});
}
this.setState({list: this.props.list});
}
},
componentWillUpdate: function(newProps) {
if (
(this.props.list === null && newProps.list !== null) ||
(this.props.list !== null && newProps.list === null) ||
(this.props.list && newProps.list && this.props.list._id != newProps.list._id)
) {
if (newProps.list && newProps.list.packages.length > 0) {
var paging = {query: {name: {'$in': newProps.list.packages}}, mini: true};
var self = this;
actions.getApps(paging).then(function(data) {
self.setState({list_apps: data.apps});
});
}
this.setState({
list: {
_id: newProps.list ? newProps.list._id : null,
name: newProps.list ? newProps.list.name : '',
sort: newProps.list ? newProps.list.sort : '-points',
packages: newProps.list ? newProps.list.packages : [],
},
list_apps: [],
});
}
},
onHide: function(saved) {
this.props.onHide(saved);
},
changeName: function(event) {
var list = JSON.parse(JSON.stringify(this.state.list));
list.name = event.target.value;
this.setState({list: list});
},
changeSort: function(event) {
this.state.list.sort = event.target.value;
this.setState({list: this.state.list});
},
addApp: function(app, event) {
event.preventDefault();
event.stopPropagation();
if (this.state.list.packages.indexOf(app.name) == -1) {
var packages = [];
for (var i = 0; i < this.state.list.packages.length; i++) {
packages.push(this.state.list.packages[i]);
}
packages.push(app.name);
var list_apps = [];
var found = false;
for (var j = 0; j < this.state.list_apps.length; j++) {
list_apps.push(this.state.list_apps[j]);
if (this.state.list_apps[j].name == app.name) {
found = true;
}
}
if (!found) {
list_apps.push(app);
}
this.setState({
list: {
_id: this.state.list._id,
name: this.state.list.name,
sort: this.state.list.sort,
packages: packages,
},
list_apps: list_apps,
});
}
},
removeApp: function(app) {
var index = this.state.list.packages.indexOf(app.name);
var packages = [];
for (var i = 0; i < this.state.list.packages.length; i++) {
if (i != index) {
packages.push(this.state.list.packages[i]);
}
}
this.setState({list: {
_id: this.state.list._id,
name: this.state.list.name,
sort: this.state.list.sort,
packages: packages,
}});
},
search: function(event) {
if (this.state.term != event.target.value) {
var paging = {
search: event.target.value,
sort: this.state.list.sort,
mini: true,
limit: 5,
};
actions.getApps(paging);
this.setState({
term: event.target.value,
search_key: JSON.stringify(paging),
});
}
},
save: function() {
var self = this;
if (this.state.list._id) { //Update
actions.updateUserList(this.state.list._id, {
name: this.state.list.name,
sort: this.state.list.sort,
packages: this.state.list.packages,
}).then(function() {
self.props.onHide(true);
self.setState({
list: {
_id: null,
name: '',
sort: '-points',
packages: [],
},
list_apps: [],
});
});
}
else { //Create
actions.createUserList({
name: this.state.list.name,
sort: this.state.list.sort,
packages: this.state.list.packages,
}).then(function() {
self.props.onHide(true);
self.setState({
list: {
_id: null,
name: '',
sort: '-points',
packages: [],
},
list_apps: [],
});
});
}
},
render: function() {
var title = i18n.t('Create List');
var save = (
<span>
<i className="fa fa-plus"></i> {i18n.t('Create')}
</span>
);
if (this.state.list && this.state.list._id) {
title = i18n.t('Edit List');
save = (
<span>
<i className="fa fa-check"></i> {i18n.t('Save')}
</span>
);
}
var disabled = '';
if (this.state.list.name === '') {
disabled = 'disabled';
}
var list_apps = [];
if (!this.state.loading && this.state.list_apps) {
list_apps = this.state.list_apps;
}
var search_apps = [];
if (!this.state.loading && this.state.search_key && this.state.apps[this.state.search_key]) {
search_apps = this.state.apps[this.state.search_key].apps;
}
var not_found = '';
if (!this.state.loading && search_apps.length === 0 && this.state.term !== '') {
not_found = (
<div className="text-center">
{i18n.t('No apps found, try searching again.')}
</div>
);
}
return (
<Modal show={this.props.show} onHide={this.onHide.bind(this, false)} backdrop={'static'}>
<Modal.Header closeButton>
<Modal.Title>{title}</Modal.Title>
</Modal.Header>
<Modal.Body>
<Alerts />
<form className="form-horizontal list-edit" role="form">
<div className="form-group">
<label htmlFor="name" className="col-sm-3 control-label">{i18n.t('List Name:')}</label>
<div className="col-sm-9">
<input type="text" className="form-control" id="name" value={this.state.list.name} onChange={this.changeName} />
</div>
</div>
<div className="form-group">
<label htmlFor="sort" className="col-sm-3 control-label">{i18n.t('Sort Apps By:')}</label>
<div className="col-sm-9">
<select id="sort" className="form-control" value={this.state.list.sort} onChange={this.changeSort}>
{info.sorts().map(function(sort) {
return <option value={sort.value} key={sort.value}>{sort.label}</option>;
}, this)}
</select>
</div>
</div>
<div className="form-group">
<label htmlFor="name" className="col-sm-3 control-label">{i18n.t('Apps:')}</label>
<div className="col-sm-9">
{list_apps.map(function(app) {
var component = '';
if (this.state.list.packages.indexOf(app.name) != -1) {
component = (
<div key={'list' + app.name}>
{app.title} <a onClick={this.removeApp.bind(this, app)} className="clickable"><i className="fa fa-remove"></i></a>
</div>
);
}
return component;
}, this)}
</div>
</div>
<div className="form-group">
<label htmlFor="search" className="col-sm-3 control-label">{i18n.t('Add Apps:')}</label>
<div className="col-sm-9">
<div className="input-group search-box">
<input type="text" className="form-control" id="search" onChange={this.debounceSearch} defaultValue={this.state.term} />
<span className="input-group-addon">
<i className="fa fa-search"></i>
</span>
</div>
{search_apps.map(function(app) {
return (
<div className="row" key={app.name}>
<div className="col-md-12">
<AppCell onClick={this.addApp.bind(this, app)} app={app} />
</div>
</div>
);
}, this)}
{not_found}
</div>
</div>
</form>
</Modal.Body>
<Modal.Footer>
<span>
<a className="btn btn-info" onClick={this.onHide.bind(this, false)}>
<i className="fa fa-close"></i> {i18n.t('Close')}
</a>
</span>
<span>
<a className="btn btn-success" onClick={this.save} disabled={disabled}>
{save}
</a>
</span>
</Modal.Footer>
</Modal>
);
}
});