Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 43 additions & 65 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,13 @@
* ...
* ]
*/
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);
});
});
});
const getPersonList = async () => {
try {
const res = await fetch('https://willowtreeapps.com/api/v1.0/profiles');
return await res.json();
} catch(err) {
console.log(err);
}
}


Expand All @@ -124,36 +118,25 @@
***************************************************/


function getLastName(person) {
return person.lastName;
}
const getLastName = person => person.lastName;

const getFirstName = (person) => {
return person.firstName;
};
const getFirstName = person => 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}`;
};
const getImageUrl = person => `http:${person.headshot.url}`;

/**
* Fisher-Yates shuffle
*/
function shuffleList(list) {
const shuffleList = (list) => {
// Make a copy & don't mutate the passed in list
let result = list.slice(1);

let tmp, j, i = list.length - 1
let result = list.slice();

for (; i > 0; i -= 1) {
j = Math.floor(Math.random() * (i + 1));
tmp = list[i];
list[i] = list[j];
list[j] = tmp;
for (let i = result.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
[result[i], result[j]] = [result[j], result[i]];
}

return result;
}

Expand All @@ -162,11 +145,11 @@
* 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;
});
}
const filterByName = (searchForName, personList) =>
personList.filter(person =>
person.firstName.toLowerCase().includes(searchForName.toLowerCase()) ||
person.lastName.toLowerCase().includes(searchForName.toLowerCase())
);


/**
Expand All @@ -185,30 +168,19 @@
* > [{ 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;
}
const sortObjListByProp = prop =>
// Make a copy & don't mutate the passed in list
objList =>
objList
.slice()
.sort((a, b) => a[prop] < b[prop] ? -1 : 1);

if (a[prop] > b[prop]) {
return 1;
}

return 1;
});

return result;
};
}
const shouldReverse = (shuffled, visible) =>
shuffled[0] === visible[0] ? shuffled.reverse() : shuffled;

const sortByFirstName = sortObjListByProp('firstName');

const sortByLastName = (personList) => sortByFirstName(personList).reverse();
const sortByLastName = sortObjListByProp('lastName');


/*==================================================
Expand Down Expand Up @@ -251,12 +223,12 @@
};
},

componentDidMount() {
getPersonList().then((personList) =>
this.setState({
personList,
visiblePersonList: personList
}));
async componentDidMount() {
const personList = await getPersonList();
this.setState({
personList,
visiblePersonList: personList
});
},

_shuffleList() {
Expand All @@ -267,13 +239,19 @@

_sortByFirst() {
this.setState({
visiblePersonList: sortByFirstName(this.state.personList)
visiblePersonList: shouldReverse(
sortByFirstName(this.state.personList),
this.state.visiblePersonList
)
});
},

_sortByLast() {
this.setState({
visiblePersonList: sortByLastName(this.state.personList)
visiblePersonList: shouldReverse(
sortByLastName(this.state.personList),
this.state.visiblePersonList
)
});
},

Expand Down