generated from goitacademy/vanilla-app-template
-
Notifications
You must be signed in to change notification settings - Fork 1
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
a4aea19
commit da71372
Showing
3 changed files
with
87 additions
and
39 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,69 +1,92 @@ | ||
import { refs } from "./modal-window"; | ||
import { patchAccess } from "./helper/get-access"; | ||
import { idExercises, refs } from "./modal-window"; | ||
import iziToast from 'izitoast'; | ||
|
||
const numberRating = document.querySelector('.js-rating'); | ||
const labelgInputs = document.querySelectorAll('.rating__label'); | ||
const backdrop = document.querySelector('.backdrop-rating-thumb') | ||
const closeRatingModal = document.querySelector('.js-rating-window'); | ||
const ratingBtn = document.querySelector('.js-open-rating'); | ||
const refsRating = { | ||
numberRating: document.querySelector('.js-rating'), | ||
labelsInputs: document.querySelectorAll('.rating__label'), | ||
backdrop: document.querySelector('.backdrop-rating-thumb'), | ||
closeRatingModal: document.querySelector('.js-rating-window'), | ||
ratingBtn: document.querySelector('.js-open-rating'), | ||
inputEmail: document.querySelector('.js-input'), | ||
textareaComment: document.querySelector('.js-textarea'), | ||
formRating: document.querySelector('.js-form'), | ||
} | ||
const pattern = /^\w+(\.\w+)?@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/; | ||
const inputEmail = document.querySelector('.js-input'); | ||
const textareaComment = document.querySelector('.js-textarea') | ||
const formRating = document.querySelector('.js-form') | ||
let lastClickedRating = 0; | ||
|
||
// налаштування зірочок | ||
let lastClickedRating = 0; | ||
labelgInputs.forEach(input => { | ||
refsRating.labelsInputs.forEach(input => { | ||
input.addEventListener('mouseover', function (event) { | ||
const rating = Number(event.target.getAttribute('data-value')); | ||
numberRating.textContent = rating.toFixed(1); | ||
refsRating.numberRating.textContent = rating.toFixed(1); | ||
}); | ||
input.addEventListener('mouseout', function () { | ||
if (lastClickedRating > 0) { | ||
numberRating.textContent = lastClickedRating.toFixed(1); | ||
refsRating.numberRating.textContent = lastClickedRating.toFixed(1); | ||
} else { | ||
numberRating.textContent = '0.0'; | ||
refsRating.numberRating.textContent = '0.0'; | ||
} | ||
}); | ||
input.addEventListener('click', function (event) { | ||
lastClickedRating = Number(event.target.getAttribute('data-value')); | ||
numberRating.textContent = lastClickedRating.toFixed(1); | ||
refsRating.numberRating.textContent = lastClickedRating.toFixed(1); | ||
}); | ||
}); | ||
|
||
|
||
ratingBtn.addEventListener('click', openModal); | ||
refsRating.ratingBtn.addEventListener('click', openModal); | ||
|
||
// Відкриття модалки | ||
function openModal() { | ||
backdrop.classList.remove('is-close'); | ||
refsRating.backdrop.classList.remove('is-close'); | ||
document.body.style.overflow = 'hidden'; | ||
} | ||
|
||
// Закриття модального вікна | ||
closeRatingModal.addEventListener('click', closeModal); | ||
refsRating.closeRatingModal.addEventListener('click', closeModal); | ||
|
||
function closeModal() { | ||
backdrop.classList.add('is-close'); | ||
refsRating.backdrop.classList.add('is-close'); | ||
document.body.style.overflow = ''; | ||
refs.backdrop.classList.remove('is-open'); | ||
} | ||
|
||
backdrop.addEventListener('click', function (event) { | ||
refsRating.backdrop.addEventListener('click', function (event) { | ||
if (event.target === this) { | ||
refs.backdrop.classList.remove('is-open'); | ||
closeModal(); | ||
} | ||
}); | ||
|
||
formRating.addEventListener('submit', sendRating) | ||
export const timeToClose = () => setTimeout(() => closeModal(), 1500) | ||
|
||
// Функція відправки рейтенгу | ||
refsRating.formRating.addEventListener('submit', sendRating) | ||
function sendRating(event) { | ||
event.preventDefault(); | ||
const email = inputEmail.value.trim(); | ||
|
||
const objectDate = { | ||
rate: lastClickedRating, | ||
email: refsRating.inputEmail.value.trim(), | ||
review: refsRating.textareaComment.value.trim(), | ||
} | ||
|
||
const email = refsRating.inputEmail.value.trim(); | ||
if (!pattern.test(email)) { | ||
iziToast.error({ | ||
message: 'Enter the correct email!' | ||
}); | ||
} else { | ||
patchAccess({ | ||
id: idExercises, | ||
formData: objectDate, | ||
}) | ||
.then(() => { | ||
timeToClose(); | ||
refsRating.formRating.reset(); | ||
}) | ||
.catch((error) => { | ||
console.log(error); | ||
}) | ||
} | ||
} |