parent
1ba2a430fe
commit
b8f41fe691
3 changed files with 0 additions and 95 deletions
@ -1,24 +0,0 @@ |
||||
export async function getPosts(id) { |
||||
const BASE_URL = 'https://raymon.dev' |
||||
const BASE_ENDPOINT = '/api/posts' |
||||
const URL = BASE_URL + BASE_ENDPOINT + (id ? `/${id}` : '?sort=-1') |
||||
|
||||
let posts = [] |
||||
|
||||
return await fetch(URL) |
||||
.then(res => res.json()) |
||||
.then(res => { |
||||
if (id !== undefined) { |
||||
if (res === undefined) { |
||||
throw Error("Response body empty") |
||||
} |
||||
return [res] |
||||
} |
||||
else { |
||||
return res |
||||
} |
||||
}) |
||||
.catch(err => { |
||||
console.log(`Error: ${err}`) |
||||
}) |
||||
} |
@ -1,61 +0,0 @@ |
||||
import { getPosts } from "./lib/remote.mjs" |
||||
|
||||
function toMonthYearString(str) { |
||||
const date = new Date(str) |
||||
return `${date.toLocaleString('default', { month: 'long' })} ${date.getFullYear()}` |
||||
} |
||||
async function showPost(id) { |
||||
const post_list = await getPosts(id) |
||||
const post = post_list[0] |
||||
const title = document.createElement("h1") |
||||
title.textContent = post.title |
||||
|
||||
const postsDOM = document.getElementById("posts") |
||||
postsDOM.appendChild(title) |
||||
|
||||
post.content.split('\n').forEach(paragraph => { |
||||
const p = document.createElement("p") |
||||
p.innerHTML = paragraph |
||||
postsDOM.appendChild(p) |
||||
}); |
||||
} |
||||
|
||||
async function updatePosts() { |
||||
const posts = await getPosts() |
||||
|
||||
const months = posts.map(post => toMonthYearString(post.date)) |
||||
const uniques = Array.from(new Set(posts.map(post => toMonthYearString(post.date)))) |
||||
const month_lists = uniques.map(month => |
||||
posts.filter( |
||||
post => toMonthYearString(post.date) === month |
||||
) |
||||
) |
||||
const postsDOM = document.getElementById("posts") |
||||
uniques.forEach((month, i) => { |
||||
const month_DOM = document.createElement("h1") |
||||
month_DOM.textContent = month |
||||
|
||||
month_lists[i].forEach((post, i) => { |
||||
const post_DOM = document.createElement("h2") |
||||
post_DOM.textContent = `${post.date.substring(0, 10)} - ` |
||||
|
||||
const post_link = document.createElement("a") |
||||
post_link.href = 'posts?post=' + post._id |
||||
post_link.textContent = post.title |
||||
post_DOM.appendChild(post_link) |
||||
|
||||
month_DOM.appendChild(post_DOM) |
||||
}) |
||||
|
||||
postsDOM.appendChild(month_DOM) |
||||
}) |
||||
} |
||||
|
||||
// Check if a specific post is requested
|
||||
let url = new URL(document.location.href); |
||||
url.searchParams.sort(); |
||||
let post_id = url.searchParams.values().next().value; |
||||
|
||||
const reg = /([0-9]|[a-f]){24}/ |
||||
if (post_id && reg.test(post_id)) showPost(post_id) |
||||
else updatePosts() |
Loading…
Reference in new issue