2015-06-24 16:01:35 -04:00
|
|
|
/**
|
|
|
|
* 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';
|
2015-06-24 16:01:35 -04:00
|
|
|
|
|
|
|
// Action to increment episode count
|
2017-02-16 11:47:54 -05:00
|
|
|
_.on('body.anime.list', 'click', '.plus_one', (e) => {
|
2017-02-15 14:08:15 -05:00
|
|
|
let parentSel = _.closestParent(e.target, 'article');
|
2017-04-10 15:31:35 -04:00
|
|
|
let watchedCount = parseInt(_.$('.completed_number', parentSel)[0].textContent, 10) || 0;
|
2017-02-15 14:08:15 -05:00
|
|
|
let totalCount = parseInt(_.$('.total_number', parentSel)[0].textContent, 10);
|
|
|
|
let title = _.$('.name a', parentSel)[0].textContent;
|
2015-06-24 16:01:35 -04:00
|
|
|
|
|
|
|
// Setup the update data
|
2016-02-01 09:49:18 -05:00
|
|
|
let data = {
|
2017-02-15 14:08:15 -05:00
|
|
|
id: parentSel.dataset.kitsuId,
|
|
|
|
mal_id: parentSel.dataset.malId,
|
2017-01-06 21:39:01 -05:00
|
|
|
data: {
|
2017-02-15 14:08:15 -05:00
|
|
|
progress: watchedCount + 1
|
2017-01-06 21:39:01 -05:00
|
|
|
}
|
2015-06-24 16:01:35 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// If the episode count is 0, and incremented,
|
|
|
|
// change status to currently watching
|
2017-02-15 14:08:15 -05:00
|
|
|
if (isNaN(watchedCount) || watchedCount === 0) {
|
2017-01-06 21:39:01 -05:00
|
|
|
data.data.status = 'current';
|
2015-06-24 16:01:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If you increment at the last episode, mark as completed
|
2017-02-16 11:47:54 -05:00
|
|
|
if (( ! isNaN(watchedCount)) && (watchedCount + 1) === totalCount) {
|
2017-01-06 21:39:01 -05:00
|
|
|
data.data.status = 'completed';
|
2015-06-24 16:01:35 -04:00
|
|
|
}
|
|
|
|
|
2017-07-12 16:40:56 -04:00
|
|
|
_.show(_.$('#loading-shadow')[0]);
|
|
|
|
|
2015-06-24 16:01:35 -04:00
|
|
|
// okay, lets actually make some changes!
|
2016-02-08 20:21:41 -05:00
|
|
|
_.ajax(_.url('/anime/update'), {
|
2017-02-16 11:47:54 -05:00
|
|
|
data,
|
2015-11-13 11:33:27 -05:00
|
|
|
dataType: 'json',
|
2016-02-02 11:34:03 -05:00
|
|
|
type: 'POST',
|
2018-08-09 11:31:15 -04:00
|
|
|
success: (res) => {
|
|
|
|
const resData = JSON.parse(res);
|
|
|
|
|
|
|
|
if (resData.errors) {
|
|
|
|
_.hide(_.$('#loading-shadow')[ 0 ]);
|
|
|
|
_.showMessage('error', `Failed to update ${title}. `);
|
|
|
|
_.scrollToTop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-16 11:47:54 -05:00
|
|
|
if (data.data.status === 'completed') {
|
2017-02-15 14:08:15 -05:00
|
|
|
_.hide(parentSel);
|
2016-02-02 11:34:03 -05:00
|
|
|
}
|
|
|
|
|
2017-07-12 16:40:56 -04:00
|
|
|
_.hide(_.$('#loading-shadow')[0]);
|
|
|
|
|
2017-01-13 16:51:31 -05:00
|
|
|
_.showMessage('success', `Successfully updated ${title}`);
|
2017-02-15 14:08:15 -05:00
|
|
|
_.$('.completed_number', parentSel)[0].textContent = ++watchedCount;
|
2016-02-08 20:21:41 -05:00
|
|
|
_.scrollToTop();
|
2016-02-02 11:34:03 -05:00
|
|
|
},
|
|
|
|
error: (xhr, errorType, error) => {
|
2017-07-12 16:40:56 -04:00
|
|
|
_.hide(_.$('#loading-shadow')[0]);
|
2017-01-06 21:39:01 -05:00
|
|
|
_.showMessage('error', `Failed to update ${title}. `);
|
2016-02-08 20:21:41 -05:00
|
|
|
_.scrollToTop();
|
2015-06-24 16:01:35 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-02-08 20:21:41 -05:00
|
|
|
})(AnimeClient);
|