-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7d5295d
commit 6164261
Showing
8 changed files
with
161 additions
and
10 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
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 |
---|---|---|
@@ -1 +1,34 @@ | ||
import APIService from './00-api'; | ||
const apiService = new APIService(); | ||
|
||
const modalExercises = document.querySelector('.modal-exercises'); | ||
|
||
modalExercises.addEventListener('click', onExercisesCardClick); | ||
|
||
let array = []; | ||
|
||
async function onExercisesCardClick(event) { | ||
if (!event.target.closest('.modal-exercises__btn-favorites')) { | ||
return; | ||
} | ||
|
||
try { | ||
const exerciseID = event.target | ||
.closest('.modal-exercises__btn-favorites') | ||
.getAttribute('data-id'); | ||
|
||
const exerciseData = await apiService.getExercisesById(exerciseID); | ||
|
||
const local = JSON.parse(localStorage.getItem('exerciseData')); | ||
|
||
if (local?.some(item => item._id === exerciseData._id)) { | ||
return; | ||
} else { | ||
array.push(exerciseData); | ||
} | ||
|
||
localStorage.setItem('exerciseData', JSON.stringify(array)); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
} |
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,51 @@ | ||
import icons from '../img/sprite.svg'; | ||
|
||
const localFavorite = document.querySelector('.favorites__list'); | ||
const buttonFavorite = document.getElementById('favoritesButton'); | ||
|
||
buttonFavorite.addEventListener('click', renderFavorite()); | ||
|
||
function renderFavorite() { | ||
const local = JSON.parse(localStorage.getItem('exerciseData')); | ||
if (local === null) { | ||
return; | ||
} else { | ||
localFavorite.innerHTML = ''; | ||
|
||
const markup = local | ||
.map(({ _id, name, burnedCalories, bodyPart, target }) => { | ||
return ` | ||
<li class="filters__item-card"> | ||
<div class="card__wrap"> | ||
<div class="card__block-btn"> | ||
<p class="card__badge">Workout</p> | ||
<button class="card__btn js-remove-btn" data-id="${_id}" type="button">Remove | ||
<svg class="card__btn-arrow" width="16" height="16"> | ||
<use href="${icons}#icon-arrow-menu-mobile"></use> | ||
</svg> | ||
</button> | ||
<button class="card__btn" data-id="${_id}" type="button">Start | ||
<svg class="card__btn-arrow" width="16" height="16"> | ||
<use href="${icons}#icon-arrow-menu-mobile"></use> | ||
</svg> | ||
</button> | ||
</div> | ||
<div class="card__wrap-title"> | ||
<svg class="card__title-svg" width="24" height="24"> | ||
<use href="${icons}#icon-running-stick-figure"></use> | ||
</svg> | ||
<h2 class="card__title">${name}</h2> | ||
</div> | ||
<div class="card__block-info"> | ||
<p class="card__text-info"><span>Burned calories:</span>${burnedCalories}</p> | ||
<p class="card__text-info"><span>Body part:</span>${bodyPart}</p> | ||
<p class="card__text-info"><span>Target:</span>${target}</p> | ||
</div> | ||
</div> | ||
</li>`; | ||
}) | ||
.join(''); | ||
|
||
localFavorite.insertAdjacentHTML('beforeend', markup); | ||
} | ||
} |
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,65 @@ | ||
const localFavorite = document.querySelector('.favorites__list'); | ||
|
||
localFavorite.addEventListener('click', removeElement); | ||
|
||
function removeElement(event) { | ||
if (!event.target.closest('.js-remove-btn')) { | ||
return; | ||
} | ||
|
||
const exerciseId = event.target | ||
.closest('.js-remove-btn') | ||
.getAttribute('data-id'); | ||
|
||
const local = JSON.parse(localStorage.getItem('exerciseData')); | ||
|
||
const newLocal = local.filter(item => item._id !== exerciseId); | ||
|
||
localStorage.setItem('exerciseData', JSON.stringify(newLocal)); | ||
|
||
createMarkup(newLocal); | ||
} | ||
|
||
function createMarkup(newLocal) { | ||
if (newLocal.length === 0) { | ||
return; | ||
} else { | ||
localFavorite.innerHTML = ''; | ||
|
||
const markup = newLocal | ||
.map(({ _id, name, burnedCalories, bodyPart, target }) => { | ||
return ` | ||
<li class="filters__item-card"> | ||
<div class="card__wrap"> | ||
<div class="card__block-btn"> | ||
<p class="card__badge">Workout</p> | ||
<button class="card__btn" id="remove" data-id="${_id}" type="button">Remove | ||
<svg class="card__btn-arrow" width="16" height="16"> | ||
<use href="${icons}#icon-arrow-menu-mobile"></use> | ||
</svg> | ||
</button> | ||
<button class="card__btn" data-id="${_id}" type="button">Start | ||
<svg class="card__btn-arrow" width="16" height="16"> | ||
<use href="${icons}#icon-arrow-menu-mobile"></use> | ||
</svg> | ||
</button> | ||
</div> | ||
<div class="card__wrap-title"> | ||
<svg class="card__title-svg" width="24" height="24"> | ||
<use href="${icons}#icon-running-stick-figure"></use> | ||
</svg> | ||
<h2 class="card__title">${name}</h2> | ||
</div> | ||
<div class="card__block-info"> | ||
<p class="card__text-info"><span>Burned calories:</span>${burnedCalories}</p> | ||
<p class="card__text-info"><span>Body part:</span>${bodyPart}</p> | ||
<p class="card__text-info"><span>Target:</span>${target}</p> | ||
</div> | ||
</div> | ||
</li>`; | ||
}) | ||
.join(''); | ||
|
||
localFavorite.insertAdjacentHTML('beforeend', markup); | ||
} | ||
} |
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