mirror of
https://github.com/ahmadk953/tasko.git
synced 2025-05-04 04:33:10 +00:00
Initial Commit
This commit is contained in:
commit
f3e2f01bd7
150 changed files with 13612 additions and 0 deletions
49
actions/update-card-order/index.ts
Normal file
49
actions/update-card-order/index.ts
Normal file
|
@ -0,0 +1,49 @@
|
|||
"use server";
|
||||
|
||||
import { auth } from "@clerk/nextjs";
|
||||
import { revalidatePath } from "next/cache";
|
||||
|
||||
import { db } from "@/lib/db";
|
||||
import { createSafeAction } from "@/lib/create-safe-action";
|
||||
|
||||
import { InputType, ReturnType } from "./types";
|
||||
import { UpdateCardOrder } from "./schema";
|
||||
|
||||
const handler = async (data: InputType): Promise<ReturnType> => {
|
||||
const { userId, orgId } = auth();
|
||||
|
||||
if (!userId || !orgId) return { error: "Unauthorized" };
|
||||
|
||||
const { items, boardId } = data;
|
||||
let updatedCards;
|
||||
|
||||
try {
|
||||
const transaction = items.map((card) =>
|
||||
db.card.update({
|
||||
where: {
|
||||
id: card.id,
|
||||
list: {
|
||||
board: {
|
||||
orgId,
|
||||
},
|
||||
},
|
||||
},
|
||||
data: {
|
||||
order: card.order,
|
||||
listId: card.listId,
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
updatedCards = await db.$transaction(transaction);
|
||||
} catch (error) {
|
||||
return {
|
||||
error: "Failed to reorder list",
|
||||
};
|
||||
}
|
||||
|
||||
revalidatePath(`/board/${boardId}`);
|
||||
return { data: updatedCards };
|
||||
};
|
||||
|
||||
export const updateCardOrder = createSafeAction(UpdateCardOrder, handler);
|
15
actions/update-card-order/schema.ts
Normal file
15
actions/update-card-order/schema.ts
Normal file
|
@ -0,0 +1,15 @@
|
|||
import { z } from "zod";
|
||||
|
||||
export const UpdateCardOrder = z.object({
|
||||
items: z.array(
|
||||
z.object({
|
||||
id: z.string(),
|
||||
title: z.string(),
|
||||
order: z.number(),
|
||||
listId: z.string(),
|
||||
createdAt: z.date(),
|
||||
updatedAt: z.date(),
|
||||
})
|
||||
),
|
||||
boardId: z.string(),
|
||||
});
|
9
actions/update-card-order/types.ts
Normal file
9
actions/update-card-order/types.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
import { z } from "zod";
|
||||
import { Card } from "@prisma/client";
|
||||
|
||||
import { ActionState } from "@/lib/create-safe-action";
|
||||
|
||||
import { UpdateCardOrder } from "./schema";
|
||||
|
||||
export type InputType = z.infer<typeof UpdateCardOrder>;
|
||||
export type ReturnType = ActionState<InputType, Card[]>;
|
Loading…
Add table
Add a link
Reference in a new issue