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

Revalidate watch date input when input loses focus #37

Merged
merged 1 commit into from
Jul 4, 2022
Merged
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
38 changes: 23 additions & 15 deletions public/js/logMovie.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
const elems = document.querySelectorAll('.datepicker_input')
for (const elem of elems) {
const datepicker = new Datepicker(elem, {
format: 'dd.mm.yyyy',
title: 'Watch date',
})
}
const datepicker = new Datepicker(document.getElementById('watchDate'), {
format: 'dd.mm.yyyy',
title: 'Watch date',
})

const watchModal = document.getElementById('watchDateModal')
watchModal.addEventListener('show.bs.modal', async function (e) {
Expand Down Expand Up @@ -99,12 +96,7 @@ const addAlertMessage = (message, type) => {
alertPlaceholder.append(wrapper)
}

function logMovie () {
let rating = getRatingFromStars()
let tmdbId = getTmdbId()
let watchDate = getWatchDate()
let movieTitle = document.getElementById('watchDateModalTitle').innerHTML

function validateWatchDate (watchDate) {
document.getElementById('watchDateValidationRequiredErrorMessage').classList.remove('d-block')
document.getElementById('watchDateValidationFormatErrorMessage').classList.remove('d-block')

Expand All @@ -113,20 +105,36 @@ function logMovie () {
document.getElementById('watchDate').style.borderColor = '#dc3545'
document.getElementById('ratingStars').style.marginTop = '0'
document.getElementById('watchDateValidationRequiredErrorMessage').classList.add('d-block')
return

return false
}

if (isValidDate(watchDate) === false) {
document.getElementById('watchDate').style.borderStyle = 'solid'
document.getElementById('watchDate').style.borderColor = '#dc3545'
document.getElementById('ratingStars').style.marginTop = '0'
document.getElementById('watchDateValidationFormatErrorMessage').classList.add('d-block')
return

return false
}

document.getElementById('watchDate').style.borderStyle = ''
document.getElementById('watchDate').style.borderColor = ''
document.getElementById('ratingStars').style.marginTop = '0.5rem'

return true
}

function logMovie () {
let rating = getRatingFromStars()
let tmdbId = getTmdbId()
let watchDate = getWatchDate()
let movieTitle = document.getElementById('watchDateModalTitle').innerHTML

if (validateWatchDate(watchDate) === false) {
return
}

fetch('/log-movie', {
method: 'post', headers: {
'Content-type': 'application/json',
Expand Down
2 changes: 1 addition & 1 deletion templates/page/logMovie.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<input type="hidden" value="" name="tmdbId" id="tmdbId" required>
<div class="input-group" id="watchDateGroup">
<i class="bi bi-calendar-date input-group-text"></i>
<input type="text" class="datepicker_input form-control" placeholder="Watch date" aria-label="Watch date" name="watchDate" id="watchDate" required>
<input type="text" class="datepicker_input form-control" placeholder="Watch date" aria-label="Watch date" name="watchDate" id="watchDate" required onfocusout="validateWatchDate(this.value)">
<div class="invalid-feedback" id="watchDateValidationRequiredErrorMessage" style="padding-bottom: 0;margin-bottom: 0">
Watch date is required!
</div>
Expand Down