handle bookmark delete loading state
This commit is contained in:
@@ -18,7 +18,13 @@ async function listUserBookmarks(request: Bun.BunRequest<"/api/bookmarks">, user
|
|||||||
}
|
}
|
||||||
|
|
||||||
const listBookmarksQuery = db.query(
|
const listBookmarksQuery = db.query(
|
||||||
"SELECT id, kind, title, url FROM bookmarks WHERE user_id = $userId ORDER BY id LIMIT $limit OFFSET $skip",
|
`
|
||||||
|
SELECT bookmarks.id, bookmarks.kind, bookmarks.title, bookmarks.url, tags.name as tag FROM bookmarks
|
||||||
|
LEFT JOIN tags
|
||||||
|
ON bookmarks.id = tags.bookmark_id
|
||||||
|
WHERE bookmarks.user_id = $userId
|
||||||
|
ORDER BY bookmarks.id LIMIT $limit OFFSET $skip
|
||||||
|
`,
|
||||||
)
|
)
|
||||||
|
|
||||||
const results = listBookmarksQuery.all({
|
const results = listBookmarksQuery.all({
|
||||||
|
@@ -28,6 +28,12 @@ CREATE TABLE IF NOT EXISTS bookmarks(
|
|||||||
url TEXT NOT NULL
|
url TEXT NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS tags(
|
||||||
|
id TEXT PRIMARY KEY,
|
||||||
|
name TEXT NOT NULL,
|
||||||
|
bookmark_id TEXT NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS sessions(
|
CREATE TABLE IF NOT EXISTS sessions(
|
||||||
session_id TEXT NOT NULL,
|
session_id TEXT NOT NULL,
|
||||||
user_id TEXT NOT NULL,
|
user_id TEXT NOT NULL,
|
||||||
@@ -54,23 +60,26 @@ function migrateDatabase() {
|
|||||||
createMetadataTableQuery.run()
|
createMetadataTableQuery.run()
|
||||||
|
|
||||||
const schemaVersionQuery = db.query("SELECT value FROM metadata WHERE key = 'schema_version'")
|
const schemaVersionQuery = db.query("SELECT value FROM metadata WHERE key = 'schema_version'")
|
||||||
const setSchemaVersionQuery = db.query("UPDATE metadata SET value = $schemaVersion WHERE key = 'schema_version'")
|
const setSchemaVersionQuery = db.query("INSERT OR REPLACE INTO metadata VALUES ('schema_version', $schemaVersion)")
|
||||||
|
|
||||||
const row = schemaVersionQuery.get()
|
const row = schemaVersionQuery.get()
|
||||||
let currentVersion: number
|
let currentVersion: number
|
||||||
if (row) {
|
if (row) {
|
||||||
currentVersion = (row as { value: number }).value
|
currentVersion = (row as { value: number }).value
|
||||||
console.log(`Migrating database from version ${currentVersion} to version ${SCHEMA_VERSION}...`)
|
|
||||||
} else {
|
} else {
|
||||||
currentVersion = -1
|
currentVersion = -1
|
||||||
console.log("Initializing database...")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (currentVersion < SCHEMA_VERSION) {
|
if (currentVersion < SCHEMA_VERSION) {
|
||||||
|
if (currentVersion < 0) {
|
||||||
|
console.log("Initializing database...")
|
||||||
|
} else {
|
||||||
|
console.log(`Migrating database from version ${currentVersion} to version ${SCHEMA_VERSION}...`)
|
||||||
|
}
|
||||||
executeMigrations(migrations.slice(currentVersion + 1, SCHEMA_VERSION + 1))
|
executeMigrations(migrations.slice(currentVersion + 1, SCHEMA_VERSION + 1))
|
||||||
setSchemaVersionQuery.run({ schemaVersion: SCHEMA_VERSION })
|
setSchemaVersionQuery.run({ schemaVersion: SCHEMA_VERSION })
|
||||||
console.log("Database successfully migrated!")
|
console.log("Database successfully migrated!")
|
||||||
} else {
|
} else if (currentVersion > SCHEMA_VERSION) {
|
||||||
console.error("Rolling back database to a previous version is unsupported. Are you trying to downgrade MARKONE?")
|
console.error("Rolling back database to a previous version is unsupported. Are you trying to downgrade MARKONE?")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -6,6 +6,7 @@ import { create } from "zustand"
|
|||||||
import { fetchApi, useAuthenticatedQuery } from "~/api"
|
import { fetchApi, useAuthenticatedQuery } from "~/api"
|
||||||
import { Button } from "~/components/button"
|
import { Button } from "~/components/button"
|
||||||
import { useDeleteBookmark } from "~/bookmark/api"
|
import { useDeleteBookmark } from "~/bookmark/api"
|
||||||
|
import { LoadingSpinner } from "~/components/loading-spinner"
|
||||||
import { useMnemonics } from "~/hooks/use-mnemonics"
|
import { useMnemonics } from "~/hooks/use-mnemonics"
|
||||||
import { Dialog, DialogActionRow, DialogBody, DialogTitle } from "~/components/dialog"
|
import { Dialog, DialogActionRow, DialogBody, DialogTitle } from "~/components/dialog"
|
||||||
|
|
||||||
@@ -171,8 +172,8 @@ function DeleteBookmarkDialog() {
|
|||||||
|
|
||||||
useMnemonics(
|
useMnemonics(
|
||||||
{
|
{
|
||||||
p: proceed,
|
y: proceed,
|
||||||
c: cancel,
|
n: cancel,
|
||||||
},
|
},
|
||||||
{ active: true },
|
{ active: true },
|
||||||
)
|
)
|
||||||
@@ -192,22 +193,50 @@ function DeleteBookmarkDialog() {
|
|||||||
markBookmarkForDeletion(null)
|
markBookmarkForDeletion(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function body() {
|
||||||
|
switch (deleteBookmarkMutation.status) {
|
||||||
|
case "pending":
|
||||||
|
return (
|
||||||
|
<p>
|
||||||
|
Deleting <LoadingSpinner />
|
||||||
|
</p>
|
||||||
|
)
|
||||||
|
case "idle":
|
||||||
|
return (
|
||||||
|
<p>
|
||||||
|
The bookmark titled{" "}
|
||||||
|
<strong>
|
||||||
|
<em>"{bookmark.title}"</em>
|
||||||
|
</strong>{" "}
|
||||||
|
will be deleted. Proceed?
|
||||||
|
</p>
|
||||||
|
)
|
||||||
|
case "error":
|
||||||
|
return <p className="text-red-500">Failed to delete the bookmark!</p>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function title() {
|
||||||
|
switch (deleteBookmarkMutation.status) {
|
||||||
|
case "pending":
|
||||||
|
return "PLEASE WAIT"
|
||||||
|
case "idle":
|
||||||
|
return "CONFIRM"
|
||||||
|
case "error":
|
||||||
|
return "ERROR OCCURRED"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Dialog>
|
<Dialog>
|
||||||
<DialogTitle>CONFIRM</DialogTitle>
|
<DialogTitle>{title()}</DialogTitle>
|
||||||
<DialogBody>
|
<DialogBody>{body()}</DialogBody>
|
||||||
The bookmark titled{" "}
|
|
||||||
<strong>
|
|
||||||
<em>"{bookmark.title}"</em>
|
|
||||||
</strong>{" "}
|
|
||||||
will be deleted. Proceed?
|
|
||||||
</DialogBody>
|
|
||||||
<DialogActionRow>
|
<DialogActionRow>
|
||||||
<Button disabled={deleteBookmarkMutation.isPending} onClick={proceed}>
|
<Button disabled={deleteBookmarkMutation.isPending} onClick={proceed}>
|
||||||
<span className="underline">P</span>roceed
|
{deleteBookmarkMutation.isError ? "Retry" : "Proceed"} (y)
|
||||||
</Button>
|
</Button>
|
||||||
<Button disabled={deleteBookmarkMutation.isPending} onClick={cancel}>
|
<Button disabled={deleteBookmarkMutation.isPending} onClick={cancel}>
|
||||||
<span className="underline">C</span>ancel
|
Cancel (n)
|
||||||
</Button>
|
</Button>
|
||||||
</DialogActionRow>
|
</DialogActionRow>
|
||||||
</Dialog>
|
</Dialog>
|
||||||
@@ -215,17 +244,23 @@ function DeleteBookmarkDialog() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function BookmarkListSection() {
|
function BookmarkListSection() {
|
||||||
const {
|
const { data: bookmarks, status } = useAuthenticatedQuery(["bookmarks"], () =>
|
||||||
data: bookmarks,
|
fetchApi<LinkBookmark[]>("/bookmarks").then(([data]) => data),
|
||||||
error,
|
)
|
||||||
status,
|
|
||||||
} = useAuthenticatedQuery(["bookmarks"], () => fetchApi<LinkBookmark[]>("/bookmarks").then(([data]) => data))
|
|
||||||
|
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case "pending":
|
case "pending":
|
||||||
return <p>Loading...</p>
|
return (
|
||||||
|
<p className="container max-w-2xl">
|
||||||
|
Loading
|
||||||
|
<LoadingSpinner />
|
||||||
|
</p>
|
||||||
|
)
|
||||||
case "success":
|
case "success":
|
||||||
return <BookmarkList bookmarks={bookmarks} />
|
if (bookmarks) {
|
||||||
|
return <BookmarkList bookmarks={bookmarks} />
|
||||||
|
}
|
||||||
|
return null
|
||||||
default:
|
default:
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
@@ -66,7 +66,7 @@ function Page() {
|
|||||||
<form onSubmit={submitLoginForm}>
|
<form onSubmit={submitLoginForm}>
|
||||||
<FormField type="text" name="username" label="USERNAME" className="mb-2" />
|
<FormField type="text" name="username" label="USERNAME" className="mb-2" />
|
||||||
<FormField type="password" name="password" label="PASSWORD" className="mb-8" />
|
<FormField type="password" name="password" label="PASSWORD" className="mb-8" />
|
||||||
<Button className="w-full py-2" type="submit">
|
<Button className="w-full py-2 md:py-2" type="submit">
|
||||||
Log in
|
Log in
|
||||||
</Button>
|
</Button>
|
||||||
</form>
|
</form>
|
||||||
|
@@ -9,9 +9,11 @@ function useDeleteBookmark() {
|
|||||||
|
|
||||||
return useMutation({
|
return useMutation({
|
||||||
mutationFn: ({ bookmark }: { bookmark: Bookmark }) =>
|
mutationFn: ({ bookmark }: { bookmark: Bookmark }) =>
|
||||||
fetchApi(`/bookmark/${bookmark.id}`, {
|
new Promise((resolve) => setTimeout(resolve, 5000)).then(() =>
|
||||||
method: "DELETE",
|
fetchApi(`/bookmark/${bookmark.id}`, {
|
||||||
}),
|
method: "DELETE",
|
||||||
|
}),
|
||||||
|
),
|
||||||
onError: (error) => {
|
onError: (error) => {
|
||||||
if (error instanceof UnauthenticatedError) {
|
if (error instanceof UnauthenticatedError) {
|
||||||
navigate({ to: "/login", replace: true })
|
navigate({ to: "/login", replace: true })
|
||||||
|
24
packages/web/src/components/loading-spinner.tsx
Normal file
24
packages/web/src/components/loading-spinner.tsx
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
import { useState, useEffect, useRef } from "react"
|
||||||
|
|
||||||
|
// courtesy of https://github.com/sindresorhus/cli-spinners
|
||||||
|
const FRAMES = ["-", "\\", "|", "/"]
|
||||||
|
|
||||||
|
function LoadingSpinner() {
|
||||||
|
const [frame, setFrame] = useState(0)
|
||||||
|
const timer = useRef<ReturnType<typeof setInterval>>(null)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
timer.current = setInterval(() => {
|
||||||
|
setFrame((frame) => (frame === 3 ? 0 : frame + 1))
|
||||||
|
}, 100)
|
||||||
|
return () => {
|
||||||
|
if (timer.current) {
|
||||||
|
clearInterval(timer.current)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
return <span className="whitespace-pre">{FRAMES[frame]}</span>
|
||||||
|
}
|
||||||
|
|
||||||
|
export { LoadingSpinner }
|
@@ -12,8 +12,8 @@ body {
|
|||||||
background: repeating-linear-gradient(
|
background: repeating-linear-gradient(
|
||||||
-45deg,
|
-45deg,
|
||||||
var(--color-stone-400),
|
var(--color-stone-400),
|
||||||
var(--color-stone-400) 4px,
|
var(--color-stone-400) 2px,
|
||||||
var(--color-stone-300) 4px,
|
var(--color-stone-300) 2px,
|
||||||
var(--color-stone-300) 12px
|
var(--color-stone-300) 6px
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user