-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e99cf88
commit 1619289
Showing
4 changed files
with
148 additions
and
82 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
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,41 +1,9 @@ | ||
const content = document.getElementById('content') | ||
|
||
// change url | ||
function changeUrl(param) { | ||
deleteDom() | ||
window.history.pushState({}, null, param); | ||
const pages = window.location.pathname.slice(1); | ||
function firstLoad() { | ||
const page = window.location.pathname; | ||
const search = new URL(window.location.href).searchParams.get('page'); | ||
page = pages === 'news' ? 'newest' : pages; | ||
loadingDom(true); | ||
fetchAsync(page, search) | ||
.then(data => window.renderList(data)) | ||
.catch(err => isError()) | ||
} | ||
|
||
function isError() { | ||
deleteDom() | ||
window.renderLoading('ERROR!!!') | ||
} | ||
|
||
function loadingDom(param) { | ||
if (param) { | ||
window.renderLoading('Loading...') | ||
} else { | ||
deleteDom() | ||
} | ||
} | ||
|
||
function deleteDom() { | ||
while (content.firstChild) { | ||
content.removeChild(content.firstChild) | ||
if (page === '/' && search === null) { | ||
window.history.pushState({}, null, '/?page=1'); | ||
} | ||
} | ||
|
||
// get data function | ||
async function fetchAsync(page, search) { | ||
let response = await fetch(`https://hnpwa.com/api/v0/${page === '' ? 'news' : page}.json?page=${search}`); | ||
let data = await response.json(); | ||
loadingDom(false) | ||
return data; | ||
} | ||
firstLoad() |
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
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