2020-04-10 15:07:08 -04:00
|
|
|
import _ from './anime-client.js'
|
2021-10-08 12:15:34 -04:00
|
|
|
import { renderSearchResults } from './template-helpers.js'
|
2023-03-17 11:09:43 -04:00
|
|
|
import { getNestedProperty, hasNestedProperty } from "./fns";
|
2018-08-22 12:54:06 -04:00
|
|
|
|
2021-12-29 17:04:55 -05:00
|
|
|
const search = (query, isCollection = false) => {
|
2018-08-22 12:54:06 -04:00
|
|
|
// Show the loader
|
2019-07-15 16:05:29 -04:00
|
|
|
_.show('.cssload-loader');
|
2018-08-22 12:54:06 -04:00
|
|
|
|
|
|
|
// Do the api search
|
2020-10-07 09:10:11 -04:00
|
|
|
return _.get(_.url('/anime-collection/search'), { query }, (searchResults, status) => {
|
2018-08-22 12:54:06 -04:00
|
|
|
searchResults = JSON.parse(searchResults);
|
|
|
|
|
|
|
|
// Hide the loader
|
2019-07-15 16:05:29 -04:00
|
|
|
_.hide('.cssload-loader');
|
2018-08-22 12:54:06 -04:00
|
|
|
|
|
|
|
// Show the results
|
2021-12-29 17:04:55 -05:00
|
|
|
_.$('#series-list')[ 0 ].innerHTML = renderSearchResults('anime', searchResults, isCollection);
|
2018-08-22 12:54:06 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-12-29 17:04:55 -05:00
|
|
|
// Anime list search
|
2018-08-22 12:54:06 -04:00
|
|
|
if (_.hasElement('.anime #search')) {
|
2020-10-07 09:10:11 -04:00
|
|
|
let prevRequest = null;
|
|
|
|
|
2019-07-10 13:32:05 -04:00
|
|
|
_.on('#search', 'input', _.throttle(250, (e) => {
|
2018-09-19 14:11:35 -04:00
|
|
|
const query = encodeURIComponent(e.target.value);
|
2018-08-22 12:54:06 -04:00
|
|
|
if (query === '') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-07 09:10:11 -04:00
|
|
|
if (prevRequest !== null) {
|
|
|
|
prevRequest.abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
prevRequest = search(query);
|
2018-08-22 12:54:06 -04:00
|
|
|
}));
|
|
|
|
}
|
2018-08-20 12:58:56 -04:00
|
|
|
|
2021-12-29 17:04:55 -05:00
|
|
|
// Anime collection search
|
|
|
|
if (_.hasElement('#search-anime-collection')) {
|
|
|
|
let prevRequest = null;
|
|
|
|
|
|
|
|
_.on('#search-anime-collection', 'input', _.throttle(250, (e) => {
|
|
|
|
const query = encodeURIComponent(e.target.value);
|
|
|
|
if (query === '') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prevRequest !== null) {
|
|
|
|
prevRequest.abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
prevRequest = search(query, true);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2018-08-20 12:58:56 -04:00
|
|
|
// Action to increment episode count
|
2018-11-02 10:48:20 -04:00
|
|
|
_.on('body.anime.list', 'click', '.plus-one', (e) => {
|
2018-08-20 12:58:56 -04:00
|
|
|
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,
|
2022-09-21 15:36:38 -04:00
|
|
|
anilist_id: parentSel.dataset.anilistId,
|
2018-08-20 12:58:56 -04:00
|
|
|
mal_id: parentSel.dataset.malId,
|
|
|
|
data: {
|
|
|
|
progress: watchedCount + 1
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-17 11:09:43 -04:00
|
|
|
const displayMessage = (type, message) => {
|
|
|
|
_.hide('#loading-shadow');
|
2023-05-19 16:33:14 -04:00
|
|
|
_.showMessage(type, `${message} ${title}`);
|
2023-03-17 11:09:43 -04:00
|
|
|
_.scrollToTop();
|
|
|
|
}
|
|
|
|
|
|
|
|
const showError = () => displayMessage('error', 'Failed to update');
|
|
|
|
|
2018-08-20 12:58:56 -04:00
|
|
|
// If the episode count is 0, and incremented,
|
|
|
|
// change status to currently watching
|
|
|
|
if (isNaN(watchedCount) || watchedCount === 0) {
|
2020-10-07 09:10:11 -04:00
|
|
|
data.data.status = 'CURRENT';
|
2018-08-20 12:58:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If you increment at the last episode, mark as completed
|
2018-08-22 12:54:06 -04:00
|
|
|
if ((!isNaN(watchedCount)) && (watchedCount + 1) === totalCount) {
|
2020-10-07 09:10:11 -04:00
|
|
|
data.data.status = 'COMPLETED';
|
2018-08-20 12:58:56 -04:00
|
|
|
}
|
|
|
|
|
2019-07-15 16:05:29 -04:00
|
|
|
_.show('#loading-shadow');
|
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) => {
|
2023-03-17 11:09:43 -04:00
|
|
|
try {
|
|
|
|
const resData = JSON.parse(res);
|
|
|
|
|
|
|
|
// Do a rough sanity check for weird errors
|
|
|
|
let updatedProgress = getNestedProperty(resData, 'data.libraryEntry.update.libraryEntry.progress');
|
|
|
|
if (hasNestedProperty(resData, 'error') || updatedProgress !== data.data.progress) {
|
|
|
|
showError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We've completed the series
|
|
|
|
if (getNestedProperty(resData, 'data.libraryEntry.update.libraryEntry.status') === 'COMPLETED') {
|
|
|
|
_.hide(parentSel);
|
|
|
|
displayMessage('success', 'Completed')
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just a normal update
|
|
|
|
_.$('.completed_number', parentSel)[ 0 ].textContent = ++watchedCount;
|
|
|
|
displayMessage('success', 'Updated');
|
|
|
|
} catch (_) {
|
|
|
|
showError();
|
2018-08-20 12:58:56 -04:00
|
|
|
}
|
|
|
|
},
|
2023-03-17 11:09:43 -04:00
|
|
|
error: showError,
|
2018-08-20 12:58:56 -04:00
|
|
|
});
|
2018-08-22 12:54:06 -04:00
|
|
|
});
|