-
-
Notifications
You must be signed in to change notification settings - Fork 81
Friends App by natashafir #465
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" | ||
| content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> | ||
| <meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
| <title>Found your new friends</title> | ||
| <link href="style.css" rel="stylesheet"> | ||
| </head> | ||
| <body> | ||
| <div class="wrapper"> | ||
| <h1 class="page-name">Find your new friends</h1> | ||
| <div class="buttons-wrapper"> | ||
| <input type="text" class="search-form" placeholder="Enter name" name="search"/> | ||
| <form class="gender"> | ||
| <input type="radio" id="all" name="gender" value="all" checked> | ||
| <label for="all">All</label> | ||
| <input type="radio" id="male" name="gender" value="male"> | ||
| <label for="male">Male</label> | ||
| <input type="radio" id="female" name="gender" value="female"> | ||
| <label for="female">Female</label> | ||
| </form> | ||
| <form class="age"> | ||
| <input type="radio" id="young-first" name="age" value="young-first"> | ||
| <label for="young-first">Young-Old</label> | ||
| <input type="radio" id="old-first" name="age" value="old-first"> | ||
| <label for="old-first">Old-Young</label> | ||
| </form> | ||
| <form class="name"> | ||
| <input type="radio" id="az" name="name" value="name-AZ"> | ||
| <label for="az">A-Z</label> | ||
| <input type="radio" id="za" name="name" value="name-ZA"> | ||
| <label for="za">Z-A</label> | ||
| </form> | ||
| </div> | ||
| <main id="section__memory" class="section-memory"> | ||
| <div class="cards"></div> | ||
| </main> | ||
| </div> | ||
| <script src="./index.js"></script> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| const URL = 'https://randomuser.me/api/?results=50'; | ||
| const CARDS = document.querySelector('.cards'); | ||
| let filterBy = 'all'; | ||
| let allFriends = []; | ||
| let friends = []; | ||
| let sortedFriends; | ||
| const input = document.querySelector('input'); | ||
|
|
||
| function initialApp() { | ||
| fetch(URL) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should fetch data and handle requests in a separate function. |
||
| .then(response => response.json()) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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/ |
||
| .then((data) => (allFriends = data.results)) | ||
| .then(() => userTemplate(allFriends)) | ||
natashafir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .then(()=> renderUsers(allFriends)) | ||
| .catch(err => { | ||
| console.log(err); | ||
| alert('ERROR in fetching the url'); | ||
| }) | ||
| } | ||
|
|
||
| function userTemplate(array) { | ||
| allFriends = array.map(user => { | ||
| return { | ||
| name: `${user.name.first} ${user.name.last}`, | ||
| age: user.dob.age, | ||
| gender: user.gender, | ||
| photo: user.picture.large | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| function createCardItem(item) { | ||
| const template = `<div class="card-wrapper"><img class="img-card" src="${item.photo}"></img><p>${item.name}</p><p>Age: ${item.age}</p></div>` | ||
natashafir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return template; | ||
| }; | ||
|
|
||
| document.querySelector(".gender").addEventListener("click", filterByChoose); | ||
| document.querySelector(".age").addEventListener("click", filterByChoose); | ||
| document.querySelector(".name").addEventListener("click", filterByChoose); | ||
natashafir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| input.addEventListener('input', filterByChoose); | ||
|
|
||
| function filterByChoose({target}) { | ||
| let sortedArr = allFriends; | ||
| if (target.type != 'radio') { | ||
| sortedArr = sortedArr.filter(element => | ||
| element.name.toLowerCase().includes(target.value.toLowerCase())); | ||
| } | ||
| sortedArr = getSortedFriends(sortedArr, target.value); | ||
natashafir marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return renderUsers(sortedArr); | ||
| } | ||
|
|
||
|
|
||
| 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); | ||
| } | ||
| } | ||
|
Comment on lines
+53
to
+79
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
|
||
| function renderUsers(item) { | ||
| CARDS.innerHTML = ''; | ||
| let cards = ''; | ||
| item.forEach(elem => { | ||
| cards +=createCardItem(elem); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO, this function is redundant. Just put string here.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please show an example of code how you doing this. |
||
| }); | ||
| CARDS.innerHTML = cards; | ||
| } | ||
|
|
||
| document.addEventListener('DOMContentLoaded', initialApp); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
|
|
||
| .wrapper { | ||
| padding: 0 10%; | ||
| } | ||
|
|
||
| .page-name { | ||
| text-align: center; | ||
| } | ||
|
|
||
| .section-memory { | ||
| margin: 0 auto; | ||
| background-color: #f9f3f3; | ||
| } | ||
|
|
||
| .cards { | ||
| max-width: 840px; | ||
| margin: 0 auto; | ||
| /*background-color: lightpink;*/ | ||
| display: flex; | ||
| flex-wrap: wrap; | ||
| /*margin: 0 auto;*/ | ||
| } | ||
|
|
||
| .card-wrapper { | ||
| width: calc(25% - 10px); | ||
| height: calc(25% - 10px); | ||
| padding: 2px; | ||
| margin: 2px; | ||
| background-color: #fff; | ||
|
|
||
| } | ||
|
|
||
| .img-card { | ||
| background-color: aliceblue; | ||
| margin: 5px; | ||
| } | ||
|
|
||
| .buttons-wrapper { | ||
| background-color: #dddddd; | ||
| padding: 20px 10px; | ||
| } | ||
|
|
||
| .search-form { | ||
| width: 190px; | ||
| height: 30px; | ||
| margin-bottom: 10px; | ||
|
|
||
| } | ||
|
|
||
|
|
Uh oh!
There was an error while loading. Please reload this page.