-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.min.js
1 lines (1 loc) · 4.1 KB
/
index.min.js
1
"use strict";const contentHn=document.getElementById("contentHn"),postBtn=document.getElementById("posts");let rowData="";function loadData(channel){switch(document.getElementById("nextBtn").hidden=!0,document.getElementById("prev").hidden=!0,document.getElementById("loading").hidden=!1,document.getElementById("contentHn").hidden=!0,channel){case"hackernews":axios.get("/hackernews",{responseType:"json"}).then(response=>{postBtn.hidden=!0,toHnTable(response.data.items)}).catch(err=>{document.getElementById("loading").hidden=!0,document.getElementById("contentHn").hidden=!1,console.log(err)});break;case"hackernewsTop":postBtn.hidden=!0,axios.get("https://hacker-news.firebaseio.com/v0/topstories.json",{responseType:"json"}).then(response=>{postBtn.hidden=!0,toHnlTable(response.data)}).catch(err=>{document.getElementById("loading").hidden=!0,document.getElementById("contentHn").hidden=!1,console.log(err)}),postBtn.hidden=!1;break;case"hackerearth":axios.get("/hackerearth",{responseType:"document"}).then(response=>{console.log(response.data)}).catch(err=>{document.getElementById("loading").hidden=!0,document.getElementById("contentHn").hidden=!1,console.log(err)});break;default:console.log("default: "+channel)}}function toHnTable(data){contentHn.innerHTML="",console.log(data);for(let i=0;i<data.length;i++)contentHn.innerHTML+='<tr><td colspan="2"><p>'+data[i].content_html.replace(" URL","").replace(/">.*\s<.*\s.*\s.*/g,`" target="_blank" > ${data[i].title}`)+"</a></div><div>Author: "+data[i].author+"       Published on: "+new Date(data[i].date_published).toLocaleString("en-GB",{timeZoneName:"short",hc:"h24",day:"numeric",month:"long",year:"numeric",hour:"2-digit",minute:"2-digit",second:"2-digit"})+"</div></p></tdcolspan></tr>";postBtn.hidden=!1,document.getElementById("contentHn").hidden=!1,document.getElementById("loading").hidden=!0}function toHnlTable(data){contentHn.innerHTML="",console.log(data.length+"\n"+data);let start=0+(Number(window.location.hash.split("#")[2])||1),end=10+start;console.log("start ",start," end ",end);let data1=data.slice(start,end),getData;(async()=>{let postData=data1.map(id=>`https://hacker-news.firebaseio.com/v0/item/${id}.json`).map(url=>axios.get(url,{responseType:"json"}));try{return Promise.resolve((await Promise.all(postData)).map(res=>res.data))}catch(err){return console.error(err),Promise.reject(new Error(rowData))}})().then(res=>{rowData=res.map(row=>`<tr>\n <td colspan="2">\n <p><a href ="${row.url}"trget="_blank" rel="noopener"> ${row.title}</a>\n </div>\n <div>Author: ${row.by}        \n Published on: ${new Date(1e3*row.time).toLocaleString("en-GB",{timeZoneName:"short",hc:"h24",day:"numeric",month:"long",year:"numeric",hour:"2-digit",minute:"2-digit",second:"2-digit"})} \n </div></p>\n </td>\n </tr>`)}).catch(err=>console.log(".catch => ERR:\t"+err)).finally(()=>{console.log(rowData.length),postBtn.hidden=!0,document.getElementById("nextBtn").hidden=!1,document.getElementById("prev").hidden=!1;for(let element of rowData)contentHn.innerHTML+=element;postBtn.hidden=!1,document.getElementById("contentHn").hidden=!1,document.getElementById("loading").hidden=!0})}document.getElementById("nextBtn").addEventListener("click",()=>{let hash=window.location.hash.split("#");hash[2]=Number(hash[2])+1||1,console.log(hash.join("#")),window.location.hash=hash.join("#")}),document.getElementById("prev").onclick=()=>{let hash=window.location.hash.split("#");hash[2]=Number(hash[2])-1||1,console.log(hash.join("#")),window.location.hash=hash.join("#")},window.onload=()=>{window.pos=0,window.location.hash?loadData(window.location.hash.split("#")[1]):(loadData("hackernews"),document.location.hash="#hackernews")},window.onhashchange=param=>loadData(param.newURL.split("#")[1]),document.getElementById("btnReload").onclick=()=>loadData(window.location.hash.replace("#",""));let countClick=1;postBtn.onclick=event=>{countClick++,countClick%2==0?(window.location.hash="hackernewsTop",event.srcElement.innerHTML="Show LATEST"):(window.location.hash="hackernews",event.srcElement.innerHTML="Show TOP")};