-
-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Friends App by natashafir #465
base: main
Are you sure you want to change the base?
Conversation
|
||
function initialApp() { | ||
fetch(URL) | ||
.then(response => response.json()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should handle fetch response status - https://www.tjvantoll.com/2015/09/13/fetch-and-errors/
const input = document.querySelector('input'); | ||
|
||
function initialApp() { | ||
fetch(URL) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should fetch data and handle requests in a separate function.
function getSortedFriends(dataToSort, choosedRadio) { | ||
if (choosedRadio == 'old-first' || choosedRadio == 'young-first'){ | ||
dataToSort.sort(function(x, y){ | ||
return x.age - y.age; | ||
}); | ||
if(choosedRadio == 'old-first'){ | ||
dataToSort.reverse(); | ||
} | ||
} else if (choosedRadio == 'name-AZ' || choosedRadio == 'name-ZA'){ | ||
dataToSort.sort(function(x, y){ | ||
let a = x.name.toUpperCase(), | ||
b = y.name.toUpperCase(); | ||
return a == b ? 0 : a > b ? 1 : -1; | ||
}); | ||
if (choosedRadio == 'name-ZA'){ | ||
dataToSort.reverse(); | ||
} | ||
} else if (choosedRadio == 'all' || choosedRadio == 'female' || choosedRadio == 'male'){ | ||
filterBy = choosedRadio; | ||
} | ||
|
||
if (filterBy === 'all') { | ||
return dataToSort; | ||
} else { | ||
return dataToSort.filter(element => element.gender === filterBy); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Single responsibility - your functions should do only one job. As an example function in which you fetch users should only fetch them and not render, transform or process them in other ways. All these things should be done in another place, outside your function.
The same applied to the sort function, which usually does all types of sorting 😉
So, simplify this function
CARDS.innerHTML = ''; | ||
let cards = ''; | ||
item.forEach(elem => { | ||
cards +=createCardItem(elem); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO, this function is redundant. Just put string here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I do, app doesn't work. Or I don’t understand what you meant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please show an example of code how you doing this.
Friends App
Demo
Code base
The code is submitted in a dedicated feature branch.
Please, review.