-
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
289dba5
commit a399782
Showing
5 changed files
with
74 additions
and
37 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 |
---|---|---|
|
@@ -12,4 +12,9 @@ | |
} | ||
.commentCount { | ||
text-decoration: underline; | ||
} | ||
.linkMenu { | ||
margin: 0 20px; | ||
color: #FFF; | ||
cursor: pointer; | ||
} |
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,42 +1,30 @@ | ||
import { | ||
p, | ||
div, | ||
span, | ||
a, | ||
} from './buatElement.js' | ||
// change url | ||
|
||
|
||
async function fetchAsync() { | ||
let response = await fetch('https://hnpwa.com/api/v0/news.json?page=1'); | ||
let data = await response.json(); | ||
return data; | ||
let data = { | ||
loading: false, | ||
} | ||
|
||
fetchAsync() | ||
.then(data => renderList(data)) | ||
.catch(err => console.log(err.message)) | ||
function changeUrl(param) { | ||
deleteDom() | ||
console.log(data.loading) | ||
window.history.pushState({}, null, param) | ||
const page = window.location.pathname.slice(1); | ||
const search = new URL(window.location.href).searchParams.get('page'); | ||
fetchAsync(page, search) | ||
.then(data => window.renderList(data)) | ||
.catch(err => console.log(err)) | ||
} | ||
|
||
function renderList(data) { | ||
function deleteDom() { | ||
const content = document.getElementById('content') | ||
console.log(data) | ||
content.appendChild(div( | ||
data.map(n => ( | ||
div({ className: 'divWrap' }, | ||
a({ | ||
className: 'typoTitle', | ||
target: '_blank', | ||
rel: `noopener external`, | ||
title: `${n.title}`, | ||
href: `${n.url}`, | ||
}, n.title), | ||
p(n.domain), | ||
div({ className: 'details'}, | ||
span(`${n.points || 0} ★`), | ||
span(` by ${n.user}`), | ||
span(` ${n.time_ago} `), | ||
a({ className: 'commentCount' }, `${n.comments_count} comments`), | ||
) | ||
) | ||
)) | ||
)) | ||
while (content.firstChild) { | ||
content.removeChild(content.firstChild) | ||
} | ||
} | ||
|
||
// get data function | ||
async function fetchAsync(page, search) { | ||
let response = await fetch(`https://hnpwa.com/api/v0/${page === '' ? 'top' : page}.json?page=${search}`); | ||
let data = await response.json(); | ||
return data; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { | ||
p, | ||
div, | ||
span, | ||
a, | ||
} from './buatElement.js' | ||
|
||
function renderList(data) { | ||
const content = document.getElementById('content') | ||
content.appendChild(div( | ||
data.map(n => ( | ||
div({ className: 'divWrap' }, | ||
a({ | ||
className: 'typoTitle', | ||
target: '_blank', | ||
rel: `noopener external`, | ||
title: `${n.title}`, | ||
href: `${n.url}`, | ||
}, n.title), | ||
p(n.domain), | ||
div({ className: 'details'}, | ||
span(`${n.points || 0} ★`), | ||
span(` by ${n.user}`), | ||
span(` ${n.time_ago} `), | ||
a({ className: 'commentCount' }, `${n.comments_count} comments`), | ||
) | ||
) | ||
)) | ||
)) | ||
} | ||
|
||
window.renderList = renderList |