diff --git a/index.html b/index.html
index b9bef462..59fb8a11 100644
--- a/index.html
+++ b/index.html
@@ -101,20 +101,30 @@
* ...
* ]
*/
- function getPersonList() {
- return new Promise((resolve, reject) => {
- fetch('http://api.namegame.willowtreemobile.com/').then(function(response) {
- if (response.status !== 200) {
- reject(new Error("Error!"));
- }
-
- response.json().then((imageList) => {
- resolve(imageList);
- });
- });
- });
- }
+ window.visiblePersonList = []
+
+ const getPersonList = () => {
+ fetch('http://api.namegame.willowtreemobile.com/').then(function(response) {
+ if (response.status !== 200) {
+ new Error("Error!");
+ }
+ response.json().then((personList) => {
+ personList.map((person) =>
+ window.visiblePersonList.push(
+ {name: person.name,
+ first: getFirstName(person.name),
+ last: getLastName(person.name),
+ url: person.url}))
+ ReactDOM.render(
+ React.createElement(App, {props: visiblePersonList}),
+ document.getElementById('app')
+ );
+ ;
+ });
+ })
+ }
+ getPersonList()
/*==================================================
@@ -123,20 +133,20 @@
***************************************************/
- function getLastName(fullName) {
- return fullName.match(/\w+/g)[1];
- }
-
const getFirstName = (fullName) => {
return fullName.match(/\w+/g)[0];
};
+ const getLastName = (fullName) => {
+ return fullName.match(/\w+/g)[1];
+ };
+
/**
* Fisher-Yates shuffle
*/
function shuffleList(list) {
// Make a copy & don't mutate the passed in list
- let result = list.slice(1);
+ let result = Object.assign([], list);
let tmp, j, i = list.length - 1
@@ -150,17 +160,33 @@
return result;
}
+ const renderShuffle = (event) => {
+ ReactDOM.render(
+ React.createElement(App, {props: shuffleList(visiblePersonList)}),
+ document.getElementById('app')
+ );
+
+ }
/**
* Remove any people that do not have the name we are
* searching for.
*/
- function filterByName(searchForName, personList) {
- return personList.filter((person) => {
- return person.name === searchForName;
+ function filterByName(searchForName) {
+ var searchNoCase = searchForName.toLowerCase()
+ return visiblePersonList.filter((person) => {
+ return person.name.toLowerCase().includes(searchNoCase)
});
}
+ const searches = (event) => {
+ ReactDOM.render(
+ React.createElement(App, {props: filterByName(event.target.value)}),
+ document.getElementById('app')
+ );
+ }
+
+
/**
* Takes in a property of an object list, e.g. "name" below
@@ -178,121 +204,73 @@
* > [{ name: 'Jon' }, { name: 'Kevin' }, { name: 'Sam' }]
*
*/
+
+ const sortByFirstName = (event) => {
+ ReactDOM.render(
+ React.createElement(App, {props: sortObjListByProp('first')}),
+ document.getElementById('app')
+ );
+ };
+
+ const sortByLastName = (event) => {
+ ReactDOM.render(
+ React.createElement(App, {props: sortObjListByProp('last')}),
+ document.getElementById('app')
+ );
+ };
+
function sortObjListByProp(prop) {
- return function(objList) {
// Make a copy & don't mutate the passed in list
- let result = objList.slice(1);
+ let result = Object.assign([], visiblePersonList);
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('name');
-
- 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.name }, [
- React.DOM.td({ key: 'thumb' }, React.createElement(Thumbnail, { src: props.person.url })),
- React.DOM.td({ key: 'first' }, null, getFirstName(props.person.name)),
- React.DOM.td({ key: 'last' }, null, getLastName(props.person.name)),
- ]);
-
- 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')
- );
+ // **************************************************/
+
+ const App = ({props}) => {
+
+ const Thumbnail = (props) => React.DOM.img({
+ className: 'image',
+ src: props.src
+ });
+
+ const ListRow = (props) => React.DOM.tr({ key: props.person.name }, [
+ React.DOM.td({ key: 'thumb' }, React.createElement(Thumbnail, { src: props.person.url })),
+ React.DOM.td({ key: 'first' }, props.person.first),
+ React.DOM.td({ key: 'last' }, props.person.last),
+ ]);
+
+ 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 })))
+ ]);
+
+ return React.DOM.div({className: 'app-container' }, [
+ React.DOM.input({ type: 'input', key: 'search', onChange: searches }),
+ React.DOM.button({ key: 'shuffle', onClick: renderShuffle }, null, 'Shuffle'),
+ React.DOM.button({ key: 'sort-first', onClick: sortByFirstName }, null, 'Sort (First Name)'),
+ React.DOM.button({ key: 'sort-last', onClick: sortByLastName }, null, 'Sort (Last Name)'),
+ React.createElement(ListContainer, { key: 'list', personList: props })
+ ]);
+ }