feat: select position when creating a new card

This commit is contained in:
Henry
2024-07-26 09:59:48 +01:00
parent d334607a9c
commit 9931a585a5
5 changed files with 69 additions and 4 deletions

View File

@@ -15,6 +15,7 @@ export const cardRouter = createTRPCRouter({
listPublicId: z.string().min(12),
labelPublicIds: z.array(z.string().min(12)),
memberPublicIds: z.array(z.string().min(12)),
position: z.enum(["start", "end"]),
}),
)
.mutation(async ({ ctx, input }) => {
@@ -29,13 +30,26 @@ export const cardRouter = createTRPCRouter({
if (!list?.id) return;
const latestCard = list.cards.length && list.cards[0];
const lastCard = list.cards.length && list.cards[0];
let index = 0;
if (list.cards.length) {
if (input.position === "end" && lastCard) index = lastCard.index + 1;
if (input.position === "start") {
await cardRepo.pushIndex(ctx.db, {
listId: list.id,
cardIndex: 0,
});
}
}
const newCard = await cardRepo.create(ctx.db, {
title: input.title,
createdBy: userId,
listId: list.id,
index: latestCard ? latestCard.index + 1 : 0,
index,
});
const newCardId = newCard?.id;