Skip to content

Commit

Permalink
Refactor this code to not nest functions more than 4 levels deep
Browse files Browse the repository at this point in the history
This commit broke promises and callback functions into separate named functions
  • Loading branch information
Yukiiyi committed Sep 5, 2024
1 parent cf3dc73 commit ac737ab
Showing 1 changed file with 11 additions and 30 deletions.
41 changes: 11 additions & 30 deletions public/src/admin/dashboard/topics.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,19 @@ define('admin/dashboard/topics', ['admin/modules/dashboard-line-graph', 'hooks']

ACP.updateTable = () => {
if (window.fetch) {
const url = `${config.relative_path}/api${ajaxify.data.url}${window.location.search}`;
fetch(url, { credentials: 'include' })
.then(handleFetchResponse)
.catch(handleFetchError);
fetch(`${config.relative_path}/api${ajaxify.data.url}${window.location.search}`, { credentials: 'include' }).then((response) => {
if (response.ok) {
response.json().then(function (payload) {
app.parseAndTranslate(ajaxify.data.template.name, 'topics', payload, function (html) {
const tbodyEl = document.querySelector('.topics-list tbody');
tbodyEl.innerHTML = '';
tbodyEl.append(...html.map((idx, el) => el));
});
});
}
});
}
};

function handleFetchResponse(response) {
if (response.ok) {
response.json()
.then(handleJsonPayload)
.catch(handleJsonError);
}
}

function handleJsonPayload(payload) {
app.parseAndTranslate(ajaxify.data.template.name, 'topics', payload, updateTableContent);
}

function updateTableContent(html) {
const tbodyEl = document.querySelector('.topics-list tbody');
tbodyEl.innerHTML = '';
tbodyEl.append(...html.map((idx, el) => el));
}

function handleFetchError(error) {
console.error('Fetch error:', error);
}

function handleJsonError(error) {
console.error('JSON parsing error:', error);
}

return ACP;
});

0 comments on commit ac737ab

Please sign in to comment.