/
Page.js
72 lines (59 loc) · 1.7 KB
/
Page.js
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
import React from 'react';
import ReactDOM from 'react-dom';
import EventEmitterFactory from 'event_emitter';
import KeyValueEditor from './KeyValueEditor.js';
// Sample controller for the page.
export default class Page extends React.Component {
constructor() {
super();
this._emitter = EventEmitterFactory.getEventEmitter();
this._emitter.on('SAVE_RESPONSE', this.onSaveResponse, this);
this._emitter.on('ADD_SETTING_CLICKED', this.onAddSettingClick, this);
this.state = { items: [] };
}
onSaveResponse(response) {
if(response.success) {
alert('Item saved.')
let copy = this.state.items.slice();
for(let i = 0; i < copy.length; i++) {
if(copy[i].name === response.data.name) {
copy[i].value = response.data.value;
}
}
this.setState({ items: copy });
}
else {
alert('Error saving item.');
}
}
onAddSettingClick() {
let currentItems = this.state.items.slice();
currentItems.push({ name: "New Setting", value: "New Value" });
this.setState({ items: currentItems });
}
render() {
return (
<KeyValueEditor
title={"Account Profile"}
items={this.state.items} />
)
}
componentWillMount () {
this.getItems().then((items) => {
this.setState({ items: items });
});
}
getItems() {
return new Promise((resolve, reject) => {
let items = [
{ name: 'Test1', value: 'Value1'},
{ name: 'Test2', value: 'Value2'},
{ name: 'Test3', value: 'Value3'},
{ name: 'Test4', value: 'Value4'},
{ name: 'Test5', value: 'Value5'},
{ name: 'Test6', value: 'Value6'}
];
resolve(items);
});
}
}