feat: select position when creating a new card
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -83,7 +83,7 @@ export const destroy = async (
|
||||
const result = await db
|
||||
.from("card")
|
||||
.update({ deletedAt: args.deletedAt, deletedBy: args.deletedBy })
|
||||
.eq("publicId", args.cardId);
|
||||
.eq("id", args.cardId);
|
||||
|
||||
return result;
|
||||
};
|
||||
@@ -327,3 +327,18 @@ export const shiftIndex = async (
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
export const pushIndex = async (
|
||||
db: SupabaseClient<Database>,
|
||||
args: {
|
||||
listId: number;
|
||||
cardIndex: number;
|
||||
},
|
||||
) => {
|
||||
const { data } = await db.rpc("push_card_index", {
|
||||
list_id: args.listId,
|
||||
card_index: args.cardIndex,
|
||||
});
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user