diff --git a/public/js/logMovie.js b/public/js/logMovie.js index dc3825ca..6323c4c0 100644 --- a/public/js/logMovie.js +++ b/public/js/logMovie.js @@ -1,12 +1,13 @@ const elems = document.querySelectorAll('.datepicker_input') for (const elem of elems) { const datepicker = new Datepicker(elem, { - // 'format': 'dd.mm.yyyy', + format: 'dd.mm.yyyy', title: 'Watch date', }) } -$('#watchDateModal').on('show.bs.modal', function (e) { +const watchModal = document.getElementById('watchDateModal') +watchModal.addEventListener('show.bs.modal', async function (e) { let movieId = e.relatedTarget.id let movieTitle = document.getElementById(movieId + '_movieTitle').value let movieYear = document.getElementById(movieId + '_movieReleaseYear').value @@ -14,4 +15,163 @@ $('#watchDateModal').on('show.bs.modal', function (e) { document.getElementById('watchDate').value = '' document.getElementById('tmdbId').value = movieId document.getElementById('watchDateModalTitle').innerHTML = movieTitle + ' (' + movieYear + ')' + let ratingNumber = await fetchRating(movieId) + setRatingStars(ratingNumber) }) + +watchModal.addEventListener('hidden.bs.modal', function (e) { + document.getElementById('watchDate').value = '' + document.getElementById('tmdbId').value = '' + document.getElementById('watchDateModalTitle').innerHTML = '' + + document.getElementById('watchDate').style.borderStyle = '' + document.getElementById('watchDate').style.borderColor = '' + document.getElementById('ratingStars').style.marginTop = '0.5rem' + + document.getElementById('watchDateValidationRequiredErrorMessage').classList.remove('d-block') + document.getElementById('watchDateValidationFormatErrorMessage').classList.remove('d-block') + + setRatingStars(0) +}) + +async function fetchRating (tmdbId) { + const response = await fetch('/fetchMovieRatingByTmdbdId?tmdbId=' + tmdbId) + + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`) + } + + const data = await response.json() + + return data.personalRating +} + +function setRatingStars (ratingNumber) { + let skipFirstStar = false + + if (ratingNumber == 1 && getRatingFromStars() == 1) { + skipFirstStar = true + } + + for (let ratingStarNumber = 1; ratingStarNumber <= 10; ratingStarNumber++) { + document.getElementById('ratingStar' + ratingStarNumber).classList.remove('bi-star-fill') + document.getElementById('ratingStar' + ratingStarNumber).classList.remove('bi-star') + + if (ratingStarNumber <= ratingNumber && skipFirstStar === false) { + document.getElementById('ratingStar' + ratingStarNumber).classList.add('bi-star-fill') + } else { + document.getElementById('ratingStar' + ratingStarNumber).classList.add('bi-star') + } + } +} + +function getRatingFromStars () { + let rating = 0 + + for (let ratingStarNumber = 1; ratingStarNumber <= 10; ratingStarNumber++) { + if (document.getElementById('ratingStar' + ratingStarNumber).classList.contains('bi-star') === true) { + break + } + + rating = ratingStarNumber + } + + return rating +} + +function updateRatingStars (e) { + setRatingStars(e.id.substring(20, 10)) +} + +function getTmdbId () { + return document.getElementById('tmdbId').value +} + +function getWatchDate () { + return document.getElementById('watchDate').value +} + +const alertPlaceholder = document.getElementById('alerts') +const addAlertMessage = (message, type) => { + const wrapper = document.createElement('div') + wrapper.innerHTML = [`