add ability to move cards
This commit is contained in:
@@ -85,7 +85,8 @@ export const getDashboard = createServerFn({ method: "GET" })
|
||||
.select({ id: card.id, name: card.name })
|
||||
.from(card)
|
||||
.where(and(eq(card.userId, userId), eq(card.dashboardId, dashboardId)))
|
||||
.orderBy(asc(card.createdAt)),
|
||||
// cards that were never moved all have position 0: oldest first
|
||||
.orderBy(asc(card.position), asc(card.createdAt)),
|
||||
db
|
||||
.select({
|
||||
id: link.id,
|
||||
@@ -173,7 +174,13 @@ export const saveCard = createServerFn({ method: "POST" })
|
||||
.where(and(eq(card.id, id), eq(card.userId, user.id)))
|
||||
} else {
|
||||
await requireDashboard(user.id, dashboardId)
|
||||
await db.insert(card).values({ name, dashboardId, userId: user.id })
|
||||
await db.insert(card).values({
|
||||
name,
|
||||
dashboardId,
|
||||
userId: user.id,
|
||||
// after the last card of the dashboard
|
||||
position: sql`(select coalesce(max(${card.position}) + 1, 0) from ${card} where ${card.dashboardId} = ${dashboardId})`,
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
@@ -217,8 +224,8 @@ export const deleteCard = createServerFn({ method: "POST" })
|
||||
.where(and(eq(card.id, data.id), eq(card.userId, user.id)))
|
||||
})
|
||||
|
||||
/** Stores the result of a drag: for each card, its links in order. */
|
||||
export const moveLinks = createServerFn({ method: "POST" })
|
||||
/** Stores the result of a drag: the cards in order and, for each card, its links in order. */
|
||||
export const saveLayout = createServerFn({ method: "POST" })
|
||||
.validator(
|
||||
z.object({
|
||||
cards: z
|
||||
@@ -234,15 +241,20 @@ export const moveLinks = createServerFn({ method: "POST" })
|
||||
.from(card)
|
||||
.where(and(eq(card.userId, user.id), inArray(card.id, cardIds)))
|
||||
if (owned.length !== cardIds.length) throw new Error("Unknown card")
|
||||
// shortcut: one UPDATE per link, fine for a few hundred links; switch to a
|
||||
// single UPDATE ... FROM (VALUES ...) if a save gets slow
|
||||
// shortcut: one UPDATE per card and link, fine for a few hundred links; switch
|
||||
// to a single UPDATE ... FROM (VALUES ...) if a save gets slow
|
||||
await db.transaction(async (tx) => {
|
||||
for (const c of data.cards)
|
||||
for (const [at, c] of data.cards.entries()) {
|
||||
await tx
|
||||
.update(card)
|
||||
.set({ position: at })
|
||||
.where(and(eq(card.id, c.id), eq(card.userId, user.id)))
|
||||
for (const [position, id] of c.linkIds.entries())
|
||||
await tx
|
||||
.update(link)
|
||||
.set({ cardId: c.id, position })
|
||||
.where(and(eq(link.id, id), eq(link.userId, user.id)))
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
+13
-1
@@ -1,6 +1,6 @@
|
||||
import assert from "node:assert/strict"
|
||||
import { test } from "node:test"
|
||||
import { drop, moveOver, order } from "./layout.ts"
|
||||
import { drop, moveCard, moveOver, order } from "./layout.ts"
|
||||
|
||||
const cards = () => [
|
||||
{ id: "apps", links: [{ id: "a" }, { id: "b" }, { id: "c" }] },
|
||||
@@ -31,6 +31,18 @@ test("a link dropped on a card goes to its end", () => {
|
||||
assert.deepEqual(ids(drop(cards(), "a", "empty")), ["bc", "x", "a"])
|
||||
})
|
||||
|
||||
test("a card takes the place of the card it is over, with its links", () => {
|
||||
assert.deepEqual(ids(moveCard(cards(), "apps", "empty")), ["x", "", "abc"])
|
||||
assert.deepEqual(ids(moveCard(cards(), "empty", "apps")), ["", "abc", "x"])
|
||||
})
|
||||
|
||||
test("a card over itself, or a link over a card, moves no card", () => {
|
||||
const before = cards()
|
||||
assert.equal(moveCard(before, "apps", "apps"), before)
|
||||
assert.equal(moveCard(before, "a", "web"), before)
|
||||
assert.equal(drop(before, "apps", "web"), before)
|
||||
})
|
||||
|
||||
test("unknown ids change nothing", () => {
|
||||
const before = cards()
|
||||
assert.equal(drop(before, "nope", "x"), before)
|
||||
|
||||
+16
-2
@@ -1,4 +1,4 @@
|
||||
// Where links sit in their cards. Pure, so drag and drop can be tested without a browser.
|
||||
// Where the cards sit, and the links in them. Pure, so drag and drop can be tested without a browser.
|
||||
|
||||
type Card = { id: string; links: { id: string }[] }
|
||||
|
||||
@@ -28,6 +28,20 @@ export function moveOver<TCard extends Card>(
|
||||
)
|
||||
}
|
||||
|
||||
/** While dragging a card: it takes the place of the card it hovers over. */
|
||||
export function moveCard<TCard extends Card>(
|
||||
cards: TCard[],
|
||||
cardId: string,
|
||||
overId: string
|
||||
): TCard[] {
|
||||
const from = cards.findIndex((c) => c.id === cardId)
|
||||
const to = cards.findIndex((c) => c.id === overId)
|
||||
if (from < 0 || to < 0 || from === to) return cards
|
||||
const next = [...cards]
|
||||
next.splice(to, 0, ...next.splice(from, 1))
|
||||
return next
|
||||
}
|
||||
|
||||
/** On drop: the link takes the place of the link it was dropped on. */
|
||||
export function drop<TCard extends Card>(
|
||||
cards: TCard[],
|
||||
@@ -45,6 +59,6 @@ export function drop<TCard extends Card>(
|
||||
return cards.map((c) => (c === card ? { ...c, links } : c))
|
||||
}
|
||||
|
||||
/** What the server stores: per card, its link ids in order. */
|
||||
/** What the server stores: the cards in order and, per card, its link ids in order. */
|
||||
export const order = (cards: Card[]) =>
|
||||
cards.map((c) => ({ id: c.id, linkIds: c.links.map((l) => l.id) }))
|
||||
|
||||
Reference in New Issue
Block a user