Skip to content

Commit

Permalink
Improve JS Script for retriveing JSON Data List
Browse files Browse the repository at this point in the history
  • Loading branch information
MrRowey committed May 22, 2024
1 parent 575fb52 commit a66be15
Showing 1 changed file with 27 additions and 40 deletions.
67 changes: 27 additions & 40 deletions scripts/populatePatches.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,46 @@
async function populate() {
const requestURL = '../assets/data/patches.json';
const request = new Request(requestURL);

const response = await fetch(request);
const patches = await response.json();
try {
const response = await fetch(requestURL);

BalancePatch(patches);
GamePatches(patches);
}


function BalancePatch(obj) {
const container = document.querySelector(".BalanceJSONList");
const list = document.createElement("ul");

const patches = obj.balance;

for (const patch in patches) {
const listitem = document.createElement("li");
const link = document.createElement("a");
const date = document.createElement("span");

link.textContent = patches[patch].patch;
link.href = patches[patch].link;
if (!response.ok) {
throw new Error(`Network response was not ok: ${response.statusText}`);
}

date.textContent = patches[patch].date;
const patches = await response.json();

listitem.appendChild(link);
listitem.appendChild(date);
list.appendChild(listitem);
renderPatchList(patches.balance, ".BalanceJSONList");
renderPatchList(patches.game, ".GameJSONList");
} catch (error) {
console.error('There has been a problem with your fetch operation:', error);
}
container.appendChild(list);
}

function GamePatches(obj) {
const container = document.querySelector(".GameJSONList");
function renderPatchList(patchList, containerSelector) {
const container = document.querySelector(containerSelector);
if (!container) {
console.error(`Container with selector ${containerSelector} not found`);
return;
}
const list = document.createElement("ul");

const patches = obj.game;
patchList.forEach(patch => {
const listItem = document.createElement("li");

for (const patch in patches) {
const listitem = document.createElement("li");
const link = document.createElement("a");
link.textContent = patch.patch;
link.href = patch.link;

const date = document.createElement("span");

link.textContent = patches[patch].patch;
link.href = patches[patch].link;
date.textContent = patch.date;

date.textContent = patches[patch].date;
listItem.appendChild(link);
listItem.appendChild(date);
list.appendChild(listItem);
});

listitem.appendChild(link);
listitem.appendChild(date);
list.appendChild(listitem);
}
container.appendChild(list);
}

populate();
populate();

0 comments on commit a66be15

Please sign in to comment.