2018-09-14 11:56:48 -04:00
|
|
|
import _ from './base/AnimeClient.js'
|
|
|
|
import { renderAnimeSearchResults } from './template-helpers.js'
|
2018-08-22 12:54:06 -04:00
|
|
|
|
|
|
|
const search = (query) => {
|
|
|
|
// Show the loader
|
|
|
|
_.$('.cssload-loader')[ 0 ].removeAttribute('hidden');
|
|
|
|
|
|
|
|
// Do the api search
|
|
|
|
_.get(_.url('/anime-collection/search'), { query }, (searchResults, status) => {
|
|
|
|
searchResults = JSON.parse(searchResults);
|
|
|
|
|
|
|
|
// Hide the loader
|
|
|
|
_.$('.cssload-loader')[ 0 ].setAttribute('hidden', 'hidden');
|
|
|
|
|
|
|
|
// Show the results
|
|
|
|
_.$('#series_list')[ 0 ].innerHTML = renderAnimeSearchResults(searchResults.data);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
if (_.hasElement('.anime #search')) {
|
2018-09-19 14:11:35 -04:00
|
|
|
_.on('#search', 'keyup', _.throttle(250, (e) => {
|
|
|
|
const query = encodeURIComponent(e.target.value);
|
2018-08-22 12:54:06 -04:00
|
|
|
if (query === '') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
search(query);
|
|
|
|
}));
|
|
|
|
}
|
2018-08-20 12:58:56 -04:00
|
|
|
|
|
|
|
// Action to increment episode count
|
|
|
|
_.on('body.anime.list', 'click', '.plus_one', (e) => {
|
|
|
|
let parentSel = _.closestParent(e.target, 'article');
|
2018-08-22 12:54:06 -04:00
|
|
|
let watchedCount = parseInt(_.$('.completed_number', parentSel)[ 0 ].textContent, 10) || 0;
|
|
|
|
let totalCount = parseInt(_.$('.total_number', parentSel)[ 0 ].textContent, 10);
|
|
|
|
let title = _.$('.name a', parentSel)[ 0 ].textContent;
|
2018-08-20 12:58:56 -04:00
|
|
|
|
|
|
|
// Setup the update data
|
|
|
|
let data = {
|
|
|
|
id: parentSel.dataset.kitsuId,
|
|
|
|
mal_id: parentSel.dataset.malId,
|
|
|
|
data: {
|
|
|
|
progress: watchedCount + 1
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// If the episode count is 0, and incremented,
|
|
|
|
// change status to currently watching
|
|
|
|
if (isNaN(watchedCount) || watchedCount === 0) {
|
|
|
|
data.data.status = 'current';
|
|
|
|
}
|
|
|
|
|
|
|
|
// If you increment at the last episode, mark as completed
|
2018-08-22 12:54:06 -04:00
|
|
|
if ((!isNaN(watchedCount)) && (watchedCount + 1) === totalCount) {
|
2018-08-20 12:58:56 -04:00
|
|
|
data.data.status = 'completed';
|
|
|
|
}
|
|
|
|
|
2018-08-22 12:54:06 -04:00
|
|
|
_.show(_.$('#loading-shadow')[ 0 ]);
|
2018-08-20 12:58:56 -04:00
|
|
|
|
|
|
|
// okay, lets actually make some changes!
|
2018-09-20 10:41:28 -04:00
|
|
|
_.ajax(_.url('/anime/increment'), {
|
2018-08-20 12:58:56 -04:00
|
|
|
data,
|
|
|
|
dataType: 'json',
|
|
|
|
type: 'POST',
|
|
|
|
success: (res) => {
|
|
|
|
const resData = JSON.parse(res);
|
|
|
|
|
|
|
|
if (resData.errors) {
|
|
|
|
_.hide(_.$('#loading-shadow')[ 0 ]);
|
|
|
|
_.showMessage('error', `Failed to update ${title}. `);
|
|
|
|
_.scrollToTop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resData.data.attributes.status === 'completed') {
|
|
|
|
_.hide(parentSel);
|
|
|
|
}
|
|
|
|
|
2018-08-22 12:54:06 -04:00
|
|
|
_.hide(_.$('#loading-shadow')[ 0 ]);
|
2018-08-20 12:58:56 -04:00
|
|
|
|
|
|
|
_.showMessage('success', `Successfully updated ${title}`);
|
2018-08-22 12:54:06 -04:00
|
|
|
_.$('.completed_number', parentSel)[ 0 ].textContent = ++watchedCount;
|
2018-08-20 12:58:56 -04:00
|
|
|
_.scrollToTop();
|
|
|
|
},
|
2018-09-19 14:11:35 -04:00
|
|
|
error: () => {
|
2018-08-22 12:54:06 -04:00
|
|
|
_.hide(_.$('#loading-shadow')[ 0 ]);
|
2018-08-20 12:58:56 -04:00
|
|
|
_.showMessage('error', `Failed to update ${title}. `);
|
|
|
|
_.scrollToTop();
|
|
|
|
}
|
|
|
|
});
|
2018-08-22 12:54:06 -04:00
|
|
|
});
|