generated from goitacademy/vanilla-app-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from konstantin-it-lysenko/Favorites-Anatolii
Favorites Anatolii 22.10
- Loading branch information
Showing
5 changed files
with
371 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { API_PROPS } from '../api/api'; | ||
import axios from 'axios'; | ||
import { Notify } from 'notiflix/build/notiflix-notify-aio'; | ||
|
||
const { BASE_URL, QUOTE_ENDPOINT } = API_PROPS; | ||
|
||
async function getQuote() { | ||
const resp = await axios(`${BASE_URL}${QUOTE_ENDPOINT}`); | ||
return resp.data; | ||
} | ||
|
||
const save = (key, value) => { | ||
try { | ||
const serializedState = JSON.stringify(value); | ||
localStorage.setItem(key, serializedState); | ||
} catch (error) { | ||
Notify.failure('Save data error: ', error.message); | ||
} | ||
}; | ||
|
||
const load = key => { | ||
try { | ||
const serializedState = localStorage.getItem(key); | ||
return serializedState === null ? undefined : JSON.parse(serializedState); | ||
} catch (error) { | ||
Notify.failure('Load data error: ', error.message); | ||
} | ||
}; | ||
|
||
// Test favor exercises | ||
async function getExercises() { | ||
const resp = await axios( | ||
'https://your-energy.b.goit.study/api/exercises?page=1&limit=26' | ||
); | ||
return resp.data; | ||
} | ||
|
||
// Test favor exercises | ||
|
||
export { getQuote, save, load, getExercises }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { Notify } from 'notiflix/build/notiflix-notify-aio'; | ||
import { | ||
createMarkupExercises, | ||
createMarkupPagination, | ||
} from './templates/favourites-markup'; | ||
import { | ||
getQuote, | ||
save, | ||
load, | ||
getExercises, | ||
} from './api-service/favourites-api'; | ||
|
||
const refs = { | ||
quote: document.querySelector('.favor-quote-wrap p'), | ||
quoteAuthor: document.querySelector('.favor-quote-wrap h4'), | ||
exercises: document.querySelector('.favor-exercises-list'), | ||
noExercises: document.querySelector('.favor-exercises'), | ||
pagination: document.querySelector('.pag-list'), | ||
}; | ||
|
||
let pagination; | ||
let paginationPages = 1; | ||
let currentPage = 0; | ||
let page; | ||
|
||
window.addEventListener('load', takeScreenParams); | ||
window.addEventListener('resize', takeScreenParams); | ||
|
||
function takeScreenParams() { | ||
pagination = 8; | ||
if (window.innerWidth >= 768 && window.innerWidth < 1440) { | ||
pagination = 10; | ||
} else if (window.innerWidth >= 1440) { | ||
pagination = 0; | ||
} | ||
getFavorExercises(); | ||
} | ||
|
||
async function getCurrentQuote() { | ||
try { | ||
const quote = load('quote-current-day'); | ||
const date = new Date().toDateString(); | ||
if (quote && date === quote.quoteDate) { | ||
refs.quote.textContent = quote.quote; | ||
refs.quoteAuthor.textContent = quote.author; | ||
} else { | ||
const currentQuote = await getQuote(); | ||
currentQuote.quoteDate = date; | ||
refs.quote.textContent = currentQuote.quote; | ||
refs.quoteAuthor.textContent = currentQuote.author; | ||
save('quote-current-day', currentQuote); | ||
} | ||
} catch (err) { | ||
Notify.failure(`Oops! Something went wrong! Try reloading the page!`); | ||
} | ||
} | ||
|
||
function getFavorExercises() { | ||
try { | ||
const favorExercises = load('favor-exercises'); | ||
if (favorExercises) { | ||
refs.noExercises.classList.remove('favor-exercises-noitems'); | ||
const totalExercises = favorExercises.length; | ||
if (pagination === 0 || totalExercises <= pagination) { | ||
page = favorExercises; | ||
refs.pagination.innerHTML = createMarkupPagination(''); | ||
} else { | ||
paginationPages = Math.ceil(totalExercises / pagination); | ||
refs.pagination.innerHTML = createMarkupPagination(paginationPages); | ||
setCurrentPage(currentPage); | ||
page = favorExercises.slice( | ||
0 + currentPage * pagination, | ||
pagination * (1 + currentPage) | ||
); | ||
} | ||
refs.exercises.innerHTML = createMarkupExercises(page); | ||
} else { | ||
refs.noExercises.classList.add('favor-exercises-noitems'); | ||
// Notify.failure(`There are no exercises in your favorites`); | ||
} | ||
} catch (err) { | ||
// Notify.failure(`Oops012! Something went wrong! Try reloading the page!`); | ||
} | ||
} | ||
|
||
function setCurrentPage(num) { | ||
let currentPage = num + 1; | ||
const activeBtn = document.getElementById(`p-${currentPage}`); | ||
activeBtn.classList.add('pag-btn-active'); | ||
} | ||
|
||
getCurrentQuote(); | ||
|
||
// Test favor exercises | ||
async function getManyExercises() { | ||
const { results } = await getExercises(); | ||
const dataExers = results.map( | ||
({ _id, name, burnedCalories, bodyPart, target }) => ({ | ||
_id: `${_id}`, | ||
name: `${name}`, | ||
burnedCalories: `${burnedCalories}`, | ||
bodyPart: `${bodyPart}`, | ||
target: `${target}`, | ||
}) | ||
); | ||
save('favor-exercises', dataExers); | ||
} | ||
getManyExercises(); | ||
// Test favor exercises |
Oops, something went wrong.