diff --git a/public/index.html b/public/index.html index 92af880..cea9722 100644 --- a/public/index.html +++ b/public/index.html @@ -37,6 +37,10 @@ padding: 50px 20px 20px; min-height: 100vh; } + .divLoading { + text-align: center; + margin-top: 100px; + } diff --git a/public/js/app.js b/public/js/app.js index 10268e9..edaae53 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -3,22 +3,25 @@ const content = document.getElementById('content') // change url function changeUrl(param) { deleteDom() - window.history.pushState({}, null, param) + window.history.pushState({}, null, param); const page = window.location.pathname.slice(1); const search = new URL(window.location.href).searchParams.get('page'); loadingDom(true); fetchAsync(page, search) .then(data => window.renderList(data)) - .catch(err => console.log(err)) + .catch(err => isError()) +} + +function isError() { + deleteDom() + window.renderLoading('ERROR!!!') } function loadingDom(param) { if (param) { - window.renderLoading() + window.renderLoading('Loading...') } else { - while (content.firstChild) { - content.removeChild(content.firstChild) - } + deleteDom() } } @@ -30,8 +33,8 @@ function deleteDom() { // get data function async function fetchAsync(page, search) { - loadingDom(false); let response = await fetch(`https://hnpwa.com/api/v0/${page === '' ? 'top' : page}.json?page=${search}`); let data = await response.json(); + loadingDom(false) return data; } \ No newline at end of file diff --git a/public/js/renderDom.js b/public/js/renderDom.js index 086825f..ba37321 100644 --- a/public/js/renderDom.js +++ b/public/js/renderDom.js @@ -29,10 +29,10 @@ function renderList(data) { )) } -function renderLoading() { +function renderLoading(param) { const content = document.getElementById('content') content.appendChild(div({ className: 'divLoading' }, - p('Loading...') + p(param) )) }