-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathindex.html
More file actions
306 lines (238 loc) · 9.12 KB
/
index.html
File metadata and controls
306 lines (238 loc) · 9.12 KB
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
<!-- Welcome!
========
This is a list view for employees that work at WillowTree.
What it does:
~~~~~~~~~~~~~
- Lists employees photos & names.
- Search for an employee by name.
- Sort employees by their first names.
- Sort empolyees by their last names.
- Shuffle the list order (because why not).
Implementation details:
~~~~~~~~~~~~~~~~~~~~~~~
- No compile step—Just open this file in a browser.
- The view layer is written in React (without JSX).
* If you don't know React, don't fret! The logic lives
outside of it for the most part.
So... what now?
~~~~~~~~~~~~~~~
Take a look at the code. It works on the surface, but can it be
improved? Are there things that you would do differently?
Change it & Have fun!
Misc. Notes
~~~~~~~~~~~
This code is only expected to work in the latest version of
Google Chrome. Feel free to use all the latest & greatest
things that are offered (new API's for JS, CSS, etc.).
Currently this project does _not_ have a build step (i.e. it runs
directly by opening the file in Google Chrome). If you finish
with improvements & feel like there isn't much else to do _then_
add in a build step if you wish. This is NOT strictly required.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Single Page Application</title>
<style>
body {
margin: 0px;
padding: 0px;
}
.app-container {
width: 400px;
margin: 0 auto 0 auto;
}
.image {
width: 100px;
height: 100px;
}
.list-container {
width: 100%;
border: 1px solid black;
}
</style>
</head>
<body>
<!-- Container for our application -->
<div id="app"></div>
<!-- 3rd Party Scripts -->
<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>
<!-- Our Application Logic -->
<script>
/*==================================================
API
***************************************************/
/**
* Get the data from the namegame endpoint.
*
* The data comes back in the format:
*
* [
* { firstName: 'Viju, lastName: 'Legard', headshot: { url: '...' } },
* { firstName: 'Matt', lastName: 'Seibert', headshot: { url: '...' } },
* ...
* ]
*/
function getPersonList() {
return new Promise((resolve, reject) => {
fetch('https://willowtreeapps.com/api/v1.0/profiles')
.then(response => {
if (response.status !== 200) {
reject(new Error("Error!"));
}
response.json().then(imageList => {
resolve(imageList);
});
});
});
}
/*==================================================
DATA TRANSFORMS
***************************************************/
function getLastName(person) {
return person.lastName;
}
const getFirstName = (person) => {
return person.firstName;
};
// headshot URLs are scheme relative //
// prepend http: to prevent invalid schemes like file:// or uri://
const getImageUrl = (person) => {
return `http:${person.headshot.url}`;
};
/**
* Fisher-Yates shuffle
*/
function shuffleList(list) {
// Make a copy & don't mutate the passed in list
let result = list.slice(1);
let tmp, j, i = list.length - 1
for (; i > 0; i -= 1) {
j = Math.floor(Math.random() * (i + 1));
tmp = list[i];
list[i] = list[j];
list[j] = tmp;
}
return result;
}
/**
* Remove any people that do not have the name we are
* searching for.
*/
function filterByName(searchForName, personList) {
return personList.filter((person) => {
return person.firstName === searchForName || person.lastName === searchForName;
});
}
/**
* Takes in a property of an object list, e.g. "name" below
*
* people = [{ name: 'Sam' }, { name: 'Jon' }, { name: 'Kevin' }]
*
* And returns a function that will sort that list, e.g.
*
* const sortPeopleByName = sortObjListByProp('name');
* const sortedPeople = sortPeopleByName(people);
*
* We now have:
*
* console.log(sortedPeople)
* > [{ name: 'Jon' }, { name: 'Kevin' }, { name: 'Sam' }]
*
*/
function sortObjListByProp(prop) {
return function(objList) {
// Make a copy & don't mutate the passed in list
let result = objList.slice(1);
result.sort((a, b) => {
if (a[prop] < b[prop]) {
return -1;
}
if (a[prop] > b[prop]) {
return 1;
}
return 1;
});
return result;
};
}
const sortByFirstName = sortObjListByProp('firstName');
const sortByLastName = (personList) => sortByFirstName(personList).reverse();
/*==================================================
VIEW (React)
***************************************************/
const Search = (props) => React.DOM.input({
type: 'input',
onChange: props.onChange
});
const Thumbnail = (props) => React.DOM.img({
className: 'image',
src: props.src
});
const ListRow = (props) => React.DOM.tr({ key: `${props.person.firstName} ${props.person.lastName}` }, [
React.DOM.td({ key: 'thumb' }, React.createElement(Thumbnail, { src: getImageUrl(props.person) })),
React.DOM.td({ key: 'first' }, null, getFirstName(props.person)),
React.DOM.td({ key: 'last' }, null, getLastName(props.person)),
]);
const ListContainer = (props) => React.DOM.table({ className: 'list-container' }, [
React.DOM.thead({ key: 'thead' }, React.DOM.tr({}, [
React.DOM.th({ key: 'thumb-h' }, null, 'Thumbnail'),
React.DOM.th({ key: 'first-h' }, null, 'First Name'),
React.DOM.th({ key: 'last-h' }, null, 'Last Name')
])),
React.DOM.tbody({ key: 'tbody' }, props.personList.map((person, i) =>
React.createElement(ListRow, { key: `person-${i}`, person })))
]);
const App = React.createClass({
getInitialState() {
return {
personList: [],
visiblePersonList: []
};
},
componentDidMount() {
getPersonList().then((personList) =>
this.setState({
personList,
visiblePersonList: personList
}));
},
_shuffleList() {
this.setState({
visiblePersonList: shuffleList(this.state.personList)
});
},
_sortByFirst() {
this.setState({
visiblePersonList: sortByFirstName(this.state.personList)
});
},
_sortByLast() {
this.setState({
visiblePersonList: sortByLastName(this.state.personList)
});
},
_onSearch(e) {
this.setState({
visiblePersonList: filterByName(e.target.value, this.state.personList)
});
},
render() {
const { visiblePersonList } = this.state;
return React.DOM.div({ className: 'app-container' }, [
React.createElement(Search, { key: 'search', onChange: this._onSearch }),
React.DOM.button({ key: 'shuffle', onClick: this._shuffleList }, null, 'Shuffle'),
React.DOM.button({ key: 'sort-first', onClick: this._sortByFirst }, null, 'Sort (First Name)'),
React.DOM.button({ key: 'sort-last', onClick: this._sortByLast }, null, 'Sort (Last Name)'),
React.createElement(ListContainer, { key: 'list', personList: visiblePersonList })
]);
}
});
ReactDOM.render(
React.createElement(App),
document.getElementById('app')
);
</script>
</body>
</html>