[assets] Only update status if the promise is successful:

- Use promises in `ajaxPost` and `interact`.
- Add some animations in CSS.
This commit is contained in:
Fabien Basmaison 2021-04-07 09:24:34 +02:00
parent 1c05107f2b
commit 5d569e8926
2 changed files with 67 additions and 12 deletions

View file

@ -224,3 +224,31 @@ html {
content: "\e905"; content: "\e905";
right: 0; right: 0;
} }
/** Animations and transitions
******************************************************************************/
@keyframes beating {
0% { font-size: 100%; }
25% { font-size: 125%; }
100% { font-size: 75%; }
}
@keyframes turning {
from { transform: rotateZ(0deg); }
to { transform: rotateZ(360deg); }
}
.is-processing .icon-heart::before {
animation: beating 0.5s infinite alternate ease-in-out;
}
.is-processing .icon-boost::before {
animation: turning 0.5s infinite ease-in-out;
}
@media (prefers-reduced-motion: reduce) {
.is-processing .icon::before {
animation-duration: 1.5s;
}
}

View file

@ -241,8 +241,6 @@ let BookWyrm = new class {
* Make a request and update the UI accordingly. * Make a request and update the UI accordingly.
* This function is used for boosts and favourites. * This function is used for boosts and favourites.
* *
* @todo Only update status if the promise is successful.
*
* @param {Event} event * @param {Event} event
* *
* @return {undefined} * @return {undefined}
@ -250,15 +248,44 @@ let BookWyrm = new class {
interact(event) { interact(event) {
event.preventDefault(); event.preventDefault();
this.ajaxPost(event.target); const bookwyrm = this;
// @todo This probably should be done with IDs. let allTriggers = document.querySelectorAll(`.${event.target.dataset.id}`);
document.querySelectorAll(`.${event.target.dataset.id}`)
.forEach(node => this.addRemoveClass( // Change icon to show ongoing activity on the current UI.
node, allTriggers.forEach(node => bookwyrm.addRemoveClass(
'hidden', node,
node.className.indexOf('hidden') == -1 'is-processing',
)); true
));
this.ajaxPost(event.target)
.finally(() => {
// Change icon to remove ongoing activity on the current UI.
allTriggers.forEach(node => bookwyrm.addRemoveClass(
node,
'is-processing',
false
));
})
.then(function() {
allTriggers.forEach(node => bookwyrm.addRemoveClass(
node,
'hidden',
node.className.indexOf('hidden') == -1
));
})
.catch(error => {
// @todo Display a notification in the UI instead.
// For now, the absence of change will be enough.
console.warn('Request failed:', error);
allTriggers.forEach(node => bookwyrm.addRemoveClass(
node,
'has-error',
node.className.indexOf('hidden') == -1
));
});
} }
/** /**
@ -266,10 +293,10 @@ let BookWyrm = new class {
* *
* @param {object} form - Form to be submitted * @param {object} form - Form to be submitted
* *
* @return {undefined} * @return {Promise}
*/ */
ajaxPost(form) { ajaxPost(form) {
fetch(form.action, { return fetch(form.action, {
method : "POST", method : "POST",
body: new FormData(form) body: new FormData(form)
}); });