-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
257 lines (228 loc) · 10.7 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<!-- These 3 required for bootstrap to operate correctly -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/png" href="/favicon.ico">
<link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
<!-- Latest compiled and minified CSS -->
<!-- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> -->
<link rel="stylesheet" href="static/css/bootstrap.min.css">
<link rel="stylesheet" href="static/css/style.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<!-- jQuery + bootstrap-->
<script src="static/js/jquery-2.2.1.min.js"></script>
<script src="static/js/bootstrap.min.js"></script>
<!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> -->
<!-- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script> -->
<!-- Babel to transpile js on the browser -->
<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script>-->
<script src="static/js/browser.min.js"></script>
<!-- React Things-->
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react.js"></script> -->
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react-dom.js"></script> -->
<script src="static/js/react.js"></script>
<script src="static/js/react-dom.js"></script>
<title>Lighthouse</title>
</head>
<body>
<div class="container">
<div class="header clearfix">
<nav>
<ul class="nav nav-pills pull-right">
<li role="presentation" class="active"><a href="/admin">Admin</a></li>
<li role="presentation"><a href="/about">About</a></li>
</ul>
</nav>
<h3 class="text-muted">Lighthouse - Refugee Information via SMS</h3>
</div>
<div id="menuadmincontainer">
<p>
If you can see this, something is most definitely broken
</p>
</div>
<script type='text/babel'>
// Initial state populated by props, and then props are never used again.
// Pass back your state to your parent.
var MenuComponent = React.createClass({
componentWillMount: function() {
this.setState({menu: this.props.menu, callback: this.props.callback})
},
uniqueId: function() {
return Math.floor((1 + Math.random()) * 0x100000000).toString(16).substring(1);
},
handleChange: function(event) {
const updatedMenu = Object.assign(this.state.menu)
updatedMenu.description = event.target.value
// Let our parent know that we are updated. Then, update our own state.
// TODO(parth): can we actually flip these?
this.state.callback(updatedMenu)
this.setState({menu: updatedMenu})
},
handleStateChange: function(newChildMenu) {
const currentMenuChildren = this.state.menu.children
console.log("----")
console.log(currentMenuChildren)
console.log(newChildMenu)
// Look for the menu in the list, and then update or add if necessary.
const idx = currentMenuChildren.findIndex(function(elem) {
return elem.id == newChildMenu.id
})
console.log(idx)
console.log("----")
if (idx == -1) {
currentMenuChildren.push(newChildMenu)
} else {
currentMenuChildren[idx] = newChildMenu
}
const updatedMenu = Object.assign(this.state.menu)
updatedMenu.children = currentMenuChildren
// update our menu, then update our parents
this.setState({menu: updatedMenu})
this.state.callback(this.state.menu)
},
handleCreateNewChildMenu: function() {
const currentMenuChildren = this.state.menu.children
const updatedMenu = Object.assign(this.state.menu)
// TODO(parth): children need ids!
console.log({"id": this.uniqueId()})
currentMenuChildren.push(
{"id": this.uniqueId(), "description": "Default Description", "children": [{"id": this.uniqueId(), "description": "This is an answer", "children": []}]})
updatedMenu.children = currentMenuChildren
// update our menu, then update our parents
this.setState({menu: updatedMenu})
this.state.callback(this.state.menu)
},
convertToMenu: function() {
const currentMenuChildren = this.state.menu.children
const updatedMenu = Object.assign(this.state.menu)
// TODO(parth): need children:[] or no
currentMenuChildren.push({"id": this.uniqueId(), "description": "Menu Item 1", "children": []})
updatedMenu.children = currentMenuChildren
updatedMenu.description = "Menu Item 1"
// update our menu, then update our parents
this.setState({menu: updatedMenu})
this.state.callback(this.state.menu)
},
render: function() {
// Only use state! Never use props again.
var menu = this.state.menu;
var stack = this.state.stack;
var handleStateChange = this.handleStateChange;
var isTopLevel = this.props.isTopLevel;
// There are a few situations we can find ourselves in:
// 1) You have no children, therefore you are a leaf node.
// 2) You have 1 child, which means that you are a terminal menu.
// 3) You have 2 children, which means theres a menu below this.
return (
<ol>
{/* If we're not the top-most, then render the description. (hack since top-level one is not that great.) */}
<div className="row">
<div className="col-md-1">
{!isTopLevel && menu.children && menu.children.length > 0 && <div className="row up-row pull-right"><i className="fa fa-angle-up"></i></div>}
{!isTopLevel && menu.children && menu.children.length > 0 && <div className="row down-row pull-right"><i className="fa fa-angle-down"></i></div>}
</div>
{ !isTopLevel && <div className="col-md-10"><li><input required={true} errorMessage="Description cannot be empty. " className="form-control" type="text" onChange={this.handleChange} value={menu.description} /></li></div>}
{/* So long as we are not an answer, we can be deleted */}
{menu.children && menu.children.length == 1 && <div className="col-md-1"><button onClick={this.convertToMenu} className="btn btn-danger"><i className="fa fa-trash"></i></button></div>}
{/* If we are an answer */}
{menu.children && menu.children.length == 0 && <div className="col-md-1"><button onClick={this.convertToMenu} className="btn btn-primary"><i className="fa fa-plus-square"></i> add subitem</button></div>}
</div>
{/* No matter what, just render all children. */}
{menu.children &&
menu.children.map(function(menuItem, idx) {
return <MenuComponent isTopLevel={false} menu={menuItem} callback={handleStateChange}/>
})
}
{/* If we have 1 child with a child, we have a submenu! */}
{menu.children && menu.children[0] && menu.children[0].children[0] && <ol>
<li><button className="btn btn-primary add-square-button" onClick={this.handleCreateNewChildMenu}><i className="fa fa-plus-square"></i> new menu option</button></li>
</ol>
}
</ol>
);
}
});
var MenuContainer = React.createClass({
getInitialState: function() {
return {isSuccess: false, isFailure: false};
},
componentDidMount: function() {
$.ajax({
url: "/menu",
dataType: 'json',
cache: false,
success: function(data) {
console.log(data);
this.setState({menu: data});
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
},
handleCallback: function(updatedMenu) {
console.log("new menu")
console.log(updatedMenu)
this.setState({menu: updatedMenu})
},
handleSubmit: function(updatedMenu) {
console.log("doing put request: ")
console.log(this.state.menu)
$.ajax({
url: '/menu',
contentType: "application/json; charset=utf-8",
type: 'PUT',
dataType: 'json',
data: JSON.stringify(this.state.menu),
//data: this.state.menu,
success: function(data) {
console.log("POST success");
console.log(data);
if (data.success) {
this.setState({isSuccess: true, isFailure: false})
}
if (data.error) {
this.setState({isSuccess: false, isFailure: true, errors: data.error})
}
}.bind(this),
error: function(xhr, status, err) {
console.log("error!")
}
});
},
render: function() {
var menu = this.state.menu;
return (
<div>
{
menu && (
<div>
<h1>SMS Script Editor</h1>
<p>Manage your sms script.</p>
{this.state.isSuccess &&
<div id="success" className="alert alert-success .alert-dismissible" role="alert">
<button type="button" className="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>Success!</div>}
{this.state.isFailure &&
<div id="error" className="alert alert-danger alert-dismissible" role="alert"><button type="button" className="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>{this.state.errors}</div>}
<div id="wrapper" style={{"margin-left": "-90px"}}>
<MenuComponent isTopLevel={true} menu={menu} callback={this.handleCallback}/>
</div>
<button className="btn btn-primary" onClick={this.handleSubmit}>Update Menu</button>
</div>
)
}
</div>
);
}
});
ReactDOM.render(
<MenuContainer />,
document.getElementById('menuadmincontainer')
);
</script>
</div>
</body>
</html>