-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve JS Script for retriveing JSON Data List
- Loading branch information
Showing
1 changed file
with
27 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |