diff --git a/public/js/download-manager.js b/public/js/download-manager.js index 4ff6e2e..0dec026 100644 --- a/public/js/download-manager.js +++ b/public/js/download-manager.js @@ -1,28 +1,64 @@ -$(() => { - $('input.uk-checkbox').each((i, e) => { - $(e).change(() => { - loadConfig(); - }); - }); - loadConfig(); - load(); - - const intervalMS = 5000; - setTimeout(() => { - setInterval(() => { - if (globalConfig.autoRefresh !== true) return; - load(); - }, intervalMS); - }, intervalMS); -}); -var globalConfig = {}; -var loading = false; - -const loadConfig = () => { - globalConfig.autoRefresh = $('#auto-refresh').prop('checked'); +/** + * Set an alpine.js property + * + * @function setProp + * @param {string} key - Key of the data property + * @param {*} prop - The data property + */ +const setProp = (key, prop) => { + $('#root').get(0).__x.$data[key] = prop; }; -const remove = (id) => { - var url = base_url + 'api/admin/mangadex/queue/delete'; + +/** + * Get an alpine.js property + * + * @function getProp + * @param {string} key - Key of the data property + * @return {*} The data property + */ +const getProp = (key) => { + return $('#root').get(0).__x.$data[key]; +}; + +/** + * Get the current queue and update the view + * + * @function load + */ +const load = () => { + try { + setProp('loading', true); + } catch {} + $.ajax({ + type: 'GET', + url: base_url + 'api/admin/mangadex/queue', + dataType: 'json' + }) + .done(data => { + if (!data.success && data.error) { + alert('danger', `Failed to fetch download queue. Error: ${data.error}`); + return; + } + setProp('jobs', data.jobs); + setProp('paused', data.paused); + }) + .fail((jqXHR, status) => { + alert('danger', `Failed to fetch download queue. Error: [${jqXHR.status}] ${jqXHR.statusText}`); + }) + .always(() => { + setProp('loading', false); + }); +}; + +/** + * Perform an action on either a specific job or the entire queue + * + * @function jobAction + * @param {string} action - The action to perform. Should be either 'delete' or 'retry' + * @param {string?} id - (Optional) A job ID. When omitted, apply the action to the queue + */ +const jobAction = (action, id) => { + let url = `${base_url}api/admin/mangadex/queue/${action}`; if (id !== undefined) url += '?' + $.param({ id: id @@ -35,42 +71,24 @@ const remove = (id) => { }) .done(data => { if (!data.success && data.error) { - alert('danger', `Failed to remove job from download queue. Error: ${data.error}`); + alert('danger', `Failed to ${action} job from download queue. Error: ${data.error}`); return; } load(); }) .fail((jqXHR, status) => { - alert('danger', `Failed to remove job from download queue. Error: [${jqXHR.status}] ${jqXHR.statusText}`); - }); -}; -const refresh = (id) => { - var url = base_url + 'api/admin/mangadex/queue/retry'; - if (id !== undefined) - url += '?' + $.param({ - id: id - }); - console.log(url); - $.ajax({ - type: 'POST', - url: url, - dataType: 'json' - }) - .done(data => { - if (!data.success && data.error) { - alert('danger', `Failed to restart download job. Error: ${data.error}`); - return; - } - load(); - }) - .fail((jqXHR, status) => { - alert('danger', `Failed to restart download job. Error: [${jqXHR.status}] ${jqXHR.statusText}`); + alert('danger', `Failed to ${action} job from download queue. Error: [${jqXHR.status}] ${jqXHR.statusText}`); }); }; + +/** + * Pause/resume the download + * + * @function toggle + */ const toggle = () => { - $('#pause-resume-btn').attr('disabled', ''); - const paused = $('#pause-resume-btn').text() === 'Resume download'; - const action = paused ? 'resume' : 'pause'; + setProp('toggling', true); + const action = getProp('paused') ? 'resume' : 'pause'; const url = `${base_url}api/admin/mangadex/queue/${action}`; $.ajax({ type: 'POST', @@ -82,64 +100,47 @@ const toggle = () => { }) .always(() => { load(); - $('#pause-resume-btn').removeAttr('disabled'); + setProp('toggling', false); }); }; -const load = () => { - if (loading) return; - loading = true; - console.log('fetching'); - $.ajax({ - type: 'GET', - url: base_url + 'api/admin/mangadex/queue', - dataType: 'json' - }) - .done(data => { - if (!data.success && data.error) { - alert('danger', `Failed to fetch download queue. Error: ${data.error}`); - return; - } - console.log(data); - const btnText = data.paused ? "Resume download" : "Pause download"; - $('#pause-resume-btn').text(btnText); - $('#pause-resume-btn').removeAttr('hidden'); - const rows = data.jobs.map(obj => { - var cls = 'label '; - if (obj.status === 'Pending') - cls += 'label-pending'; - if (obj.status === 'Completed') - cls += 'label-success'; - if (obj.status === 'Error') - cls += 'label-danger'; - if (obj.status === 'MissingPages') - cls += 'label-warning'; - const info = obj.status_message.length > 0 ? '' : ''; - const statusSpan = `${obj.status} ${info}`; - const dropdown = obj.status_message.length > 0 ? `