HummingBirdAnimeClient/public/js/anime_edit.js

57 lines
1.5 KiB
JavaScript
Raw Normal View History

/**
* Javascript for editing anime, if logged in
*/
2016-02-08 20:21:41 -05:00
((_) => {
2015-11-13 11:33:27 -05:00
2016-02-01 09:49:18 -05:00
'use strict';
// Action to increment episode count
_.on('body.anime.list', 'click', '.plus_one', e => {
let parent_sel = _.closestParent(e.target, 'article');
2016-02-08 20:21:41 -05:00
let watched_count = parseInt(_.$('.completed_number', parent_sel)[0].textContent, 10);
let total_count = parseInt(_.$('.total_number', parent_sel)[0].textContent, 10);
let title = _.$('.name a', parent_sel)[0].textContent;
// Setup the update data
2016-02-01 09:49:18 -05:00
let data = {
id: parent_sel.dataset.kitsuId,
mal_id: parent_sel.dataset.malId,
data: {
progress: watched_count + 1
}
};
// If the episode count is 0, and incremented,
// change status to currently watching
2016-02-01 09:49:18 -05:00
if (isNaN(watched_count) || watched_count === 0) {
data.data.status = 'current';
}
// If you increment at the last episode, mark as completed
if (( ! isNaN(watched_count)) && (watched_count + 1) == total_count) {
data.data.status = 'completed';
}
// okay, lets actually make some changes!
2016-02-08 20:21:41 -05:00
_.ajax(_.url('/anime/update'), {
2015-11-13 11:33:27 -05:00
data: data,
dataType: 'json',
2016-02-02 11:34:03 -05:00
type: 'POST',
2017-01-09 21:38:42 -05:00
success: () => {
if (data.data.status == 'completed') {
_.hide(parent_sel);
2016-02-02 11:34:03 -05:00
}
_.showMessage('success', `Successfully updated ${title}`);
2016-02-08 20:21:41 -05:00
_.$('.completed_number', parent_sel)[0].textContent = ++watched_count;
_.scrollToTop();
2016-02-02 11:34:03 -05:00
},
error: (xhr, errorType, error) => {
console.error(error);
_.showMessage('error', `Failed to update ${title}. `);
2016-02-08 20:21:41 -05:00
_.scrollToTop();
}
});
});
2016-02-08 20:21:41 -05:00
})(AnimeClient);