Skip to content
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 done #715

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
226 changes: 226 additions & 0 deletions submissions/andysmokk/friends-app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="./dist/css/styles.min.css" />
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Roboto+Slab:wght@300;400;700;900&display=swap"
rel="stylesheet"
/>
<title>Friends App</title>
</head>

<body>
<div class="overlay">
<header class="header">
<div class="container">
<div class="logo-box">
<svg class="logo-img" width=" 300" height="50">
<use href="./images/sprite.svg#friends"></use>
</svg>
<h1 class="header-title">App</h1>
</div>
</div>
</header>
<main class="main">
<div class="container">
<div class="main-box">
<div class="main-box-left-panel">
<form action="" class="search-form" id="search-form">
<input
class="search-form-input"
type="text"
name="nameInput"
placeholder="Enter name"
id="name"
title="For example: Amina"
/>
</form>
<form
action=""
class="filter-name-age-form"
id="filter-name-age-form"
>
<fieldset class="filter-name-fieldset">
<legend class="filter-name-legend">Name</legend>
<div class="ïmg">
<input
class="filter-name-input"
type="radio"
id="firstNameA-z"
name="nameSortInput"
value="first name A-Z"
/>
<label class="filter-name-label" for="firstNameA-z"
><svg class="filter-name-svg" width="25" height="25">
<use href="./images/sprite.svg#a-z"></use></svg
><span class="filter-name-label-span"
>First name</span
></label
>
</div>
<div>
<input
class="filter-name-input"
type="radio"
id="firstNameZ-a"
name="nameSortInput"
value="first name Z-A"
/>
<label class="filter-name-label" for="firstNameZ-a"
><svg class="filter-name-svg" width="25" height="25">
<use href="./images/sprite.svg#z-a"></use></svg
><span class="filter-name-label-span"
>First name</span
></label
>
</div>
<div>
<input
class="filter-name-input"
type="radio"
id="lastNameA-z"
name="nameSortInput"
value="last name A-Z"
/>
<label class="filter-name-label" for="lastNameA-z"
><svg class="filter-name-svg" width="25" height="25">
<use href="./images/sprite.svg#a-z"></use></svg
><span class="filter-name-label-span"
>Last name</span
></label
>
</div>
<div>
<input
class="filter-name-input"
type="radio"
id="lastNameZ-a"
name="nameSortInput"
value="last name Z-A"
/>
<label class="filter-name-label" for="lastNameZ-a"
><svg class="filter-name-svg" width="25" height="25">
<use href="./images/sprite.svg#z-a"></use></svg
><span class="filter-name-label-span"
>Last name</span
></label
>
</div>
</fieldset>
<fieldset class="filter-age-fieldset">
<legend class="filter-age-legend">Age</legend>
<div>
<input
class="filter-age-input"
type="radio"
id="age-ascending"
name="ageInput"
value="age-ascending"
/>
<label class="filter-age-label" for="age-ascending"
><svg class="filter-age-svg" width="26" height="26">
<use href="./images/sprite.svg#ascending"></use></svg
><span class="filter-name-label-span"
>Ascending</span
></label
>
</div>
<div>
<input
class="filter-age-input"
type="radio"
id="age-descending"
name="ageInput"
value="age-descending"
/>
<label class="filter-age-label" for="age-descending">
<svg class="filter-age-svg" width="26" height="26">
<use href="./images/sprite.svg#descending"></use></svg
><span class="filter-name-label-span">Descending</span>
</label>
</div>
</fieldset>
</form>
<form
action=""
class="filter-gender-form"
id="filter-gender-form"
>
<fieldset class="filter-gender-fieldset">
<legend class="filter-gender-legend">Gender</legend>
<div>
<input
class="filter-gender-input"
type="radio"
id="all"
name="genderInput"
value="all"
checked
/>
<label class="filter-gender-label active" for="all">
<svg class="filter-gender-svg" width="25" height="25">
<use
href="./images/sprite.svg#female-male-filter"
></use></svg
><span class="filter-name-label-span">All</span>
</label>
</div>
<div>
<input
class="filter-gender-input"
type="radio"
id="female"
name="genderInput"
value="female"
/>
<label class="filter-gender-label" for="female">
<svg class="filter-gender-svg" width="24" height="24">
<use href="./images/sprite.svg#female"></use></svg
><span class="filter-name-label-span">Female</span>
</label>
</div>
<div>
<input
class="filter-gender-input"
type="radio"
id="male"
name="genderInput"
value="male"
/>
<label class="filter-gender-label" for="male">
<svg class="filter-gender-svg" width="25" height="25">
<use href="./images/sprite.svg#male"></use></svg
><span class="filter-name-label-span">Male</span>
</label>
</div>
</fieldset>
</form>
</div>
<ul class="main-box-list"></ul>
</div>
</div>
</main>
<footer class="footer">
<div class="container">
<div class="footer-box">
<a
class="footer-link"
href="https://github.com/andysmokk/friends-app"
>
<svg class="github-img" width="40" height="40">
<use href="./images/sprite.svg#github"></use>
</svg>
</a>
<p class="footer-text">2022</p>
</div>
</div>
</footer>
</div>
<script type="module" src="./js/index.js"></script>
</body>
</html>
15 changes: 15 additions & 0 deletions submissions/andysmokk/friends-app/js/fetchFriends.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const BASE_URL = "https://randomuser.me/api/1.4/";

const fetchFriends = async () => {
try {
const response = await fetch(
`${BASE_URL}?results=20&inc=gender,name,picture,cell,location,dob&noinfo`
);
const dataResponse = await response.json();
return dataResponse.results;
} catch (error) {
console.error(e.message);
}
};

export { fetchFriends };
53 changes: 53 additions & 0 deletions submissions/andysmokk/friends-app/js/filters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const sortByName = (users, name) => {
return users.filter((user) => {
const searchName = name.toLowerCase();
const firsName = user.name.first.toLowerCase();
const lassName = user.name.last.toLowerCase();

if (firsName.includes(searchName) || lassName.includes(searchName)) {
return user;
}
});
};

const sortByGender = (users, gender) => {
if (gender === "all") {
return users;
}

return users.filter((user) => user.gender === gender);
};

const sortNameByAlphabet = (users, sorting) => {
return users.sort((a, b) => {
if (sorting === "first name A-Z") {
return a.name.first > b.name.first ? 1 : -1;
}

if (sorting === "first name Z-A") {
return a.name.first < b.name.first ? 1 : -1;
}

if (sorting === "last name A-Z") {
return a.name.last > b.name.last ? 1 : -1;
}

if (sorting === "last name Z-A") {
return a.name.last < b.name.last ? 1 : -1;
}
});
};

const sortByAge = (users, sorting) => {
return users.sort((a, b) => {
if (sorting === "age-ascending") {
return a.dob.age - b.dob.age;
}

if (sorting === "age-descending") {
return b.dob.age - a.dob.age;
}
});
};

export { sortByName, sortByGender, sortNameByAlphabet, sortByAge };
72 changes: 72 additions & 0 deletions submissions/andysmokk/friends-app/js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { fetchFriends } from "./fetchFriends.js";
import {
sortByName,
sortByGender,
sortNameByAlphabet,
sortByAge,
} from "./filters.js";
import { renderFriends } from "./renders.js";
import {
boxForm,
searchForm,
filterNameAgeForm,
filterNameInputs,
filterAgeInputs,
filterGenderInputs,
filterGenderForm,
inputNames,
inputAge,
} from "./refs.js";
import { addClassActive } from "./styles.js";

const listFriend = await fetchFriends();
let sortedFriends = [];

renderFriends(listFriend);

const onFilter = (friends) => {
const valueNameInput = searchForm.nameInput.value;
const valueGenderInput = filterGenderForm.genderInput.value;
const valueSortNameInput = filterNameAgeForm.nameSortInput.value;
const valueAgeInput = filterNameAgeForm.ageInput.value;

const sortedByName = sortByName(friends, valueNameInput);
sortedFriends = sortedByName;

const sortedByGender = sortByGender(sortedFriends, valueGenderInput);
sortedFriends = sortedByGender;

const sortedNameByAlphabet = sortNameByAlphabet(
sortedFriends,
valueSortNameInput
);
sortedFriends = sortedNameByAlphabet;

const sortedByAge = sortByAge(sortedFriends, valueAgeInput);
sortedFriends = sortedByAge;

renderFriends(sortedFriends);
};

const onClick = ({ target }) => {
inputNames.forEach((input) => {
input.value === target.value
? (input.checked = true)
: (input.checked = false);
});

inputAge.forEach((input) => {
input.value === target.value
? (input.checked = true)
: (input.checked = false);
});

addClassActive(filterNameInputs);
addClassActive(filterAgeInputs);
};

boxForm.addEventListener("input", () => onFilter(listFriend));
filterNameAgeForm.addEventListener("click", (e) => onClick(e));
filterGenderForm.addEventListener("click", () =>
addClassActive(filterGenderInputs)
);
23 changes: 23 additions & 0 deletions submissions/andysmokk/friends-app/js/refs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const boxForm = document.querySelector(".main-box-left-panel");
const boxFriends = document.querySelector(".main-box-list");
const searchForm = document.querySelector("#search-form");
const filterNameAgeForm = document.querySelector("#filter-name-age-form");
const filterNameInputs = document.querySelectorAll(".filter-name-input");
const filterAgeInputs = document.querySelectorAll(".filter-age-input");
const filterGenderInputs = document.querySelectorAll(".filter-gender-input");
const filterGenderForm = document.querySelector("#filter-gender-form");
const inputNames = document.getElementsByName("nameSortInput");
const inputAge = document.getElementsByName("ageInput");

export {
boxForm,
boxFriends,
searchForm,
filterNameAgeForm,
filterNameInputs,
filterAgeInputs,
filterGenderInputs,
filterGenderForm,
inputNames,
inputAge,
};
Loading