Skip to content

Commit

Permalink
use dayjs to format dates
Browse files Browse the repository at this point in the history
  • Loading branch information
spcbfr committed Mar 26, 2024
1 parent 6508806 commit 60314da
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 33 deletions.
Binary file modified bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions db/config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { NOW, column, defineDb, defineTable } from 'astro:db';
import { column, defineDb, defineTable } from 'astro:db';

const Note = defineTable({
columns: {
content: column.text(),
published: column.date({ default: NOW })
published: column.number()
}
})

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@types/react-dom": "^18.2.22",
"@vercel/analytics": "^1.2.2",
"astro": "^4.5.5",
"dayjs": "^1.11.10",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"svelte": "^4.2.12",
Expand Down
7 changes: 5 additions & 2 deletions src/pages/api/micropub.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { APIContext } from "astro";
import { db, Note } from "astro:db";
import { record } from "zod";
import { Dayjs } from "dayjs"

interface SuccessfulIndieToken {
me: string;
Expand Down Expand Up @@ -82,17 +83,19 @@ export async function POST({ request, site, url }: APIContext) {
if (!hasOwnProperty(formBodyObject, 'content')) {
return Error(422)
}
const sinceEpoch = new Dayjs().unix

const records = await db.insert(Note).values({
content: formBodyObject.content
content: formBodyObject.content,
published: sinceEpoch
}).returning()
console.log(records)

return new Response(null, {
statusText: "Created",
status: 201,
headers: {
"Location": "https://yusuf.fyi/notes/" + records[0].published.getTime()
"Location": "https://yusuf.fyi/notes/" + records[0].published
}
})
}
Expand Down
21 changes: 4 additions & 17 deletions src/pages/notes/[slug].astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@ import { db, Note } from "astro:db";
import BaseLayout from "../../layouts/BaseLayout.astro";
import Link from "../../components/Link.astro";
import Webmentions from "../../components/Webmentions.svelte";
import { desc } from "astro:db";
import dayjs from "dayjs";
const { slug } = Astro.params;
const date = new Date(Number(slug));
console.log(date);
const notes = await db.select().from(Note);
const notes = await db.select().from(Note).where(eq(slug, Note.published))
const note = notes[0];
console.log(notes);
---

<BaseLayout
Expand All @@ -36,18 +32,9 @@ console.log(notes);
<p class="text-xl">{note.content}</p>
</div>
<div>
<time class="text-sm" datetime={note.published.toISOString()}>
<time class="text-sm" datetime={dayjs(note.published).toISOString()}>
{
note.published.toLocaleDateString("en-GB", {
weekday: "short",
day: "2-digit",
month: "short",
year: "numeric",
minute: "2-digit",
hour: "2-digit",
hour12: false,
era: "short",
})
dayjs(note.published).format("ddd, D MMM, YYYY, HH:mm")
}
</time>
</div>
Expand Down
19 changes: 7 additions & 12 deletions src/pages/notes/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const notes = await db.select().from(Note).orderBy(desc(Note.published));
import BaseLayout from "../../layouts/BaseLayout.astro";
import Link from "../../components/Link.astro";
import { desc } from "astro:db";
import dayjs from "dayjs";
---

<BaseLayout title="notes!" description="Yusuf's note hub">
Expand All @@ -23,19 +24,13 @@ import { desc } from "astro:db";
<div>
<a
class="text-amber-900 font-medium underline underline-offset-2 decoration-amber-800/30 hover:decoration-amber-800"
href={Astro.site + "notes/" + note.published.getTime()}
href={"/notes/" + note.published}
>
<time class="text-sm" datetime={note.published.toISOString()}>
{note.published.toLocaleDateString("en-GB", {
weekday: "short",
day: "2-digit",
month: "short",
year: "numeric",
minute: "2-digit",
hour: "2-digit",
hour12: false,
era: "short",
})}
<time
class="text-sm"
datetime={dayjs(note.published).toISOString()}
>
{dayjs(note.published).format("ddd, D MMM, YYYY, HH:mm")}
</time>
</a>
</div>
Expand Down

0 comments on commit 60314da

Please sign in to comment.