-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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 #5957 from jimchamp/feature/async-ratings
Async star ratings
- Loading branch information
Showing
5 changed files
with
88 additions
and
13 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
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,62 @@ | ||
import { FadingToast } from '../Toast.js'; | ||
|
||
export function initRatingHandlers(ratingForms) { | ||
for (const form of ratingForms) { | ||
form.addEventListener('submit', function(e) { | ||
handleRatingSubmission(e, form); | ||
}) | ||
} | ||
} | ||
|
||
function handleRatingSubmission(event, form) { | ||
event.preventDefault(); | ||
// Continue only if selected star is different from previous rating | ||
if (!event.submitter.classList.contains('star-selected')) { | ||
|
||
// Construct form data object: | ||
const formData = new FormData(form); | ||
let rating; | ||
if (event.submitter.value) { | ||
rating = Number(event.submitter.value) | ||
formData.append('rating', event.submitter.value) | ||
} | ||
formData.append('ajax', true); | ||
|
||
// Make AJAX call | ||
fetch(form.action, { | ||
method: 'POST', | ||
headers: { | ||
'content-type': 'application/x-www-form-urlencoded' | ||
}, | ||
body: new URLSearchParams(formData) | ||
}) | ||
.then((response) => { | ||
if (!response.ok) { | ||
throw new Error('Ratings update failed') | ||
} | ||
// Repaint stars | ||
form.querySelectorAll('.star-selected').forEach((elem) => { | ||
elem.classList.remove('star-selected'); | ||
if (elem.hasAttribute('property')) { | ||
elem.removeAttribute('property'); | ||
} | ||
}) | ||
|
||
const clearButton = form.querySelector('.star-messaging'); | ||
if (rating) { | ||
clearButton.classList.remove('hidden'); | ||
form.querySelectorAll(`.star-${rating}`).forEach((elem) => { | ||
elem.classList.add('star-selected'); | ||
if (elem.tagName === 'LABEL') { | ||
elem.setAttribute('property', 'ratingValue') | ||
} | ||
}) | ||
} else { | ||
clearButton.classList.add('hidden'); | ||
} | ||
}) | ||
.catch((error) => { | ||
new FadingToast(error.message).show(); | ||
}) | ||
} | ||
} |
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