refactor: move reorder lists to update list
This commit is contained in:
@@ -92,7 +92,7 @@ export default function BoardPage() {
|
|||||||
|
|
||||||
const isLoading = isInitialLoading || isQueryLoading;
|
const isLoading = isInitialLoading || isQueryLoading;
|
||||||
|
|
||||||
const updateListMutation = api.list.reorder.useMutation({
|
const updateListMutation = api.list.update.useMutation({
|
||||||
onMutate: async (args) => {
|
onMutate: async (args) => {
|
||||||
await utils.board.byId.cancel();
|
await utils.board.byId.cancel();
|
||||||
|
|
||||||
@@ -102,10 +102,19 @@ export default function BoardPage() {
|
|||||||
if (!oldBoard) return oldBoard;
|
if (!oldBoard) return oldBoard;
|
||||||
|
|
||||||
const updatedLists = Array.from(oldBoard.lists);
|
const updatedLists = Array.from(oldBoard.lists);
|
||||||
const removedList = updatedLists.splice(args.currentIndex, 1)[0];
|
|
||||||
|
|
||||||
if (removedList) {
|
const sourceList = updatedLists.find(
|
||||||
updatedLists.splice(args.newIndex, 0, removedList);
|
(list) => list.publicId === args.listPublicId,
|
||||||
|
);
|
||||||
|
|
||||||
|
const currentIndex = sourceList?.index;
|
||||||
|
|
||||||
|
if (currentIndex === undefined) return oldBoard;
|
||||||
|
|
||||||
|
const removedList = updatedLists.splice(currentIndex, 1)[0];
|
||||||
|
|
||||||
|
if (removedList && args.index !== undefined) {
|
||||||
|
updatedLists.splice(args.index, 0, removedList);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...oldBoard,
|
...oldBoard,
|
||||||
@@ -201,8 +210,7 @@ export default function BoardPage() {
|
|||||||
if (type === "LIST") {
|
if (type === "LIST") {
|
||||||
updateListMutation.mutate({
|
updateListMutation.mutate({
|
||||||
listPublicId: draggableId,
|
listPublicId: draggableId,
|
||||||
currentIndex: source.index,
|
index: destination.index,
|
||||||
newIndex: destination.index,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -254,7 +262,7 @@ export default function BoardPage() {
|
|||||||
/>
|
/>
|
||||||
<Filters
|
<Filters
|
||||||
labels={boardData?.labels ?? []}
|
labels={boardData?.labels ?? []}
|
||||||
members={boardData?.workspace?.members ?? []}
|
members={boardData?.workspace.members ?? []}
|
||||||
position="left"
|
position="left"
|
||||||
isLoading={isLoading}
|
isLoading={isLoading}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -63,49 +63,6 @@ export const listRouter = createTRPCRouter({
|
|||||||
code: "INTERNAL_SERVER_ERROR",
|
code: "INTERNAL_SERVER_ERROR",
|
||||||
});
|
});
|
||||||
|
|
||||||
return result;
|
|
||||||
}),
|
|
||||||
reorder: protectedProcedure
|
|
||||||
.meta({
|
|
||||||
openapi: {
|
|
||||||
summary: "Reorder a list",
|
|
||||||
method: "POST",
|
|
||||||
path: "/lists/{listPublicId}/reorder",
|
|
||||||
description: "Reorders the position of a list",
|
|
||||||
tags: ["Lists"],
|
|
||||||
protect: true,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.input(
|
|
||||||
z.object({
|
|
||||||
listPublicId: z.string().min(12),
|
|
||||||
currentIndex: z.number(),
|
|
||||||
newIndex: z.number(),
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
.output(z.custom<Awaited<ReturnType<typeof listRepo.reorder>>>())
|
|
||||||
.mutation(async ({ ctx, input }) => {
|
|
||||||
const list = await listRepo.getByPublicId(ctx.db, input.listPublicId);
|
|
||||||
|
|
||||||
if (!list)
|
|
||||||
throw new TRPCError({
|
|
||||||
message: `List with public ID ${input.listPublicId} not found`,
|
|
||||||
code: "NOT_FOUND",
|
|
||||||
});
|
|
||||||
|
|
||||||
const result = await listRepo.reorder(ctx.supabaseClient, {
|
|
||||||
boardPublicId: list.boardId,
|
|
||||||
listPublicId: list.id,
|
|
||||||
currentIndex: input.currentIndex,
|
|
||||||
newIndex: input.newIndex,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!result)
|
|
||||||
throw new TRPCError({
|
|
||||||
message: `Failed to reorder list`,
|
|
||||||
code: "INTERNAL_SERVER_ERROR",
|
|
||||||
});
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}),
|
}),
|
||||||
delete: protectedProcedure
|
delete: protectedProcedure
|
||||||
@@ -197,16 +154,35 @@ export const listRouter = createTRPCRouter({
|
|||||||
.input(
|
.input(
|
||||||
z.object({
|
z.object({
|
||||||
listPublicId: z.string().min(12),
|
listPublicId: z.string().min(12),
|
||||||
name: z.string().min(1),
|
name: z.string().min(1).optional(),
|
||||||
|
index: z.number().optional(),
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.output(z.custom<Awaited<ReturnType<typeof listRepo.update>>>())
|
.output(
|
||||||
|
z.custom<
|
||||||
|
| Awaited<ReturnType<typeof listRepo.update>>
|
||||||
|
| Awaited<ReturnType<typeof listRepo.reorder>>
|
||||||
|
>(),
|
||||||
|
)
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(async ({ ctx, input }) => {
|
||||||
const result = await listRepo.update(
|
let result: { name: string; publicId: string } | undefined;
|
||||||
ctx.supabaseClient,
|
|
||||||
{ name: input.name },
|
console.log({ input });
|
||||||
{ listPublicId: input.listPublicId },
|
|
||||||
);
|
if (input.name) {
|
||||||
|
result = await listRepo.update(
|
||||||
|
ctx.db,
|
||||||
|
{ name: input.name },
|
||||||
|
{ listPublicId: input.listPublicId },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (input.index !== undefined) {
|
||||||
|
result = await listRepo.reorder(ctx.db, {
|
||||||
|
listPublicId: input.listPublicId,
|
||||||
|
newIndex: input.index,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (!result)
|
if (!result)
|
||||||
throw new TRPCError({
|
throw new TRPCError({
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import type { RouterInputs, RouterOutputs } from "../index";
|
|||||||
|
|
||||||
export type GetBoardByIdOutput = RouterOutputs["board"]["byId"];
|
export type GetBoardByIdOutput = RouterOutputs["board"]["byId"];
|
||||||
export type GetCardByIdOutput = RouterOutputs["card"]["byId"];
|
export type GetCardByIdOutput = RouterOutputs["card"]["byId"];
|
||||||
export type ReorderListInput = RouterInputs["list"]["reorder"];
|
|
||||||
export type ReorderCardInput = RouterInputs["card"]["reorder"];
|
export type ReorderCardInput = RouterInputs["card"]["reorder"];
|
||||||
export type UpdateBoardInput = RouterInputs["board"]["update"];
|
export type UpdateBoardInput = RouterInputs["board"]["update"];
|
||||||
export type NewLabelInput = RouterInputs["label"]["create"];
|
export type NewLabelInput = RouterInputs["label"]["create"];
|
||||||
|
|||||||
@@ -1,33 +1,4 @@
|
|||||||
CREATE OR REPLACE FUNCTION reorder_lists(board_id BIGINT, list_id BIGINT, current_index INT, new_index INT)
|
|
||||||
RETURNS BOOLEAN
|
|
||||||
LANGUAGE PLPGSQL
|
|
||||||
AS $$
|
|
||||||
BEGIN
|
|
||||||
UPDATE list
|
|
||||||
SET index =
|
|
||||||
CASE
|
|
||||||
WHEN index = current_index AND id = list_id THEN new_index
|
|
||||||
WHEN current_index < new_index AND index > current_index AND index <= new_index THEN index - 1
|
|
||||||
WHEN current_index > new_index AND index >= new_index AND index < current_index THEN index + 1
|
|
||||||
ELSE index
|
|
||||||
END
|
|
||||||
WHERE "boardId" = board_id;
|
|
||||||
|
|
||||||
-- Check for duplicate indices after the update
|
|
||||||
IF EXISTS (
|
|
||||||
SELECT index, COUNT(*)
|
|
||||||
FROM list
|
|
||||||
WHERE "boardId" = board_id
|
|
||||||
AND "deletedAt" IS NULL
|
|
||||||
GROUP BY index
|
|
||||||
HAVING COUNT(*) > 1
|
|
||||||
) THEN
|
|
||||||
RAISE EXCEPTION 'Duplicate indices found after reordering in board %', board_id;
|
|
||||||
END IF;
|
|
||||||
|
|
||||||
RETURN TRUE;
|
|
||||||
END;
|
|
||||||
$$;
|
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION reorder_cards(card_id BIGINT, current_list_id BIGINT, new_list_id BIGINT, current_index INT, new_index INT)
|
CREATE OR REPLACE FUNCTION reorder_cards(card_id BIGINT, current_list_id BIGINT, new_list_id BIGINT, current_index INT, new_index INT)
|
||||||
RETURNS BOOLEAN
|
RETURNS BOOLEAN
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import type { SupabaseClient } from "@supabase/supabase-js";
|
import type { SupabaseClient } from "@supabase/supabase-js";
|
||||||
import { and, desc, eq, isNull } from "drizzle-orm";
|
import { and, desc, eq, gt, isNull, sql } from "drizzle-orm";
|
||||||
|
|
||||||
import type { dbClient } from "@kan/db/client";
|
import type { dbClient } from "@kan/db/client";
|
||||||
import type { Database } from "@kan/db/types/database.types";
|
import type { Database } from "@kan/db/types/database.types";
|
||||||
@@ -68,7 +68,7 @@ export const getWithCardsByPublicId = async (
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const update = async (
|
export const update = async (
|
||||||
db: SupabaseClient<Database>,
|
db: dbClient,
|
||||||
listInput: {
|
listInput: {
|
||||||
name: string;
|
name: string;
|
||||||
},
|
},
|
||||||
@@ -76,33 +76,78 @@ export const update = async (
|
|||||||
listPublicId: string;
|
listPublicId: string;
|
||||||
},
|
},
|
||||||
) => {
|
) => {
|
||||||
const { data } = await db
|
const [result] = await db
|
||||||
.from("list")
|
.update(lists)
|
||||||
.update({ name: listInput.name })
|
.set({ name: listInput.name })
|
||||||
.eq("publicId", args.listPublicId)
|
.where(and(eq(lists.publicId, args.listPublicId), isNull(lists.deletedAt)))
|
||||||
.is("deletedAt", null)
|
.returning({
|
||||||
.select(`publicId, name`);
|
publicId: lists.publicId,
|
||||||
|
name: lists.name,
|
||||||
|
});
|
||||||
|
|
||||||
return data;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const reorder = async (
|
export const reorder = async (
|
||||||
db: SupabaseClient<Database>,
|
db: dbClient,
|
||||||
args: {
|
args: {
|
||||||
boardPublicId: number;
|
listPublicId: string;
|
||||||
listPublicId: number;
|
|
||||||
currentIndex: number;
|
|
||||||
newIndex: number;
|
newIndex: number;
|
||||||
},
|
},
|
||||||
) => {
|
) => {
|
||||||
const { data } = await db.rpc("reorder_lists", {
|
return db.transaction(async (tx) => {
|
||||||
board_id: args.boardPublicId,
|
const list = await tx.query.lists.findFirst({
|
||||||
list_id: args.listPublicId,
|
columns: {
|
||||||
current_index: args.currentIndex,
|
id: true,
|
||||||
new_index: args.newIndex,
|
boardId: true,
|
||||||
});
|
index: true,
|
||||||
|
},
|
||||||
|
where: eq(lists.publicId, args.listPublicId),
|
||||||
|
});
|
||||||
|
|
||||||
return data;
|
if (!list)
|
||||||
|
throw new Error(`List not found for public ID ${args.listPublicId}`);
|
||||||
|
|
||||||
|
await tx.execute(sql`
|
||||||
|
UPDATE list
|
||||||
|
SET index =
|
||||||
|
CASE
|
||||||
|
WHEN index = ${list.index} AND id = ${list.id} THEN ${args.newIndex}
|
||||||
|
WHEN ${list.index} < ${args.newIndex} AND index > ${list.index} AND index <= ${args.newIndex} THEN index - 1
|
||||||
|
WHEN ${list.index} > ${args.newIndex} AND index >= ${args.newIndex} AND index < ${list.index} THEN index + 1
|
||||||
|
ELSE index
|
||||||
|
END
|
||||||
|
WHERE "boardId" = ${list.boardId};
|
||||||
|
`);
|
||||||
|
|
||||||
|
const countExpr = sql<number>`COUNT(*)`.mapWith(Number);
|
||||||
|
|
||||||
|
const duplicateIndices = await db
|
||||||
|
.select({
|
||||||
|
index: lists.index,
|
||||||
|
count: countExpr,
|
||||||
|
})
|
||||||
|
.from(lists)
|
||||||
|
.where(and(eq(lists.boardId, list.boardId), isNull(lists.deletedAt)))
|
||||||
|
.groupBy(lists.index)
|
||||||
|
.having(gt(countExpr, 1));
|
||||||
|
|
||||||
|
if (duplicateIndices.length > 0) {
|
||||||
|
throw new Error(
|
||||||
|
`Duplicate indices found after reordering in board ${list.boardId}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const updatedList = await tx.query.lists.findFirst({
|
||||||
|
columns: {
|
||||||
|
publicId: true,
|
||||||
|
name: true,
|
||||||
|
},
|
||||||
|
where: eq(lists.publicId, args.listPublicId),
|
||||||
|
});
|
||||||
|
|
||||||
|
return updatedList;
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const shiftIndex = async (
|
export const shiftIndex = async (
|
||||||
|
|||||||
Reference in New Issue
Block a user