Added Copy Board Feature

This commit is contained in:
Ahmad 2024-03-29 23:15:34 -04:00
parent b15a0fbac5
commit 993552226b
No known key found for this signature in database
GPG key ID: 8FD8A93530D182BF
8 changed files with 125 additions and 17 deletions

View file

@ -0,0 +1,69 @@
'use server';
import { auth } from '@clerk/nextjs/server';
import { db } from '@/lib/db';
import { createSafeAction } from '@/lib/create-safe-action';
import { InputType, ReturnType } from './types';
import { CopyBoard } from './schema';
import { redirect } from 'next/navigation';
const handler = async (data: InputType): Promise<ReturnType> => {
const { userId, orgId } = auth();
if (!userId || !orgId) {
return {
error: 'Unauthorized',
};
}
const { id } = data;
let board;
try {
const boardToCopy = await db.board.findUnique({
where: {
id,
orgId,
},
include: {
lists: true,
},
});
if (!boardToCopy) return { error: 'Board not found' };
board = await db.board.create({
data: {
title: `${boardToCopy.title} - Copy`,
orgId,
imageId: boardToCopy.imageId,
imageThumbUrl: boardToCopy.imageThumbUrl,
imageFullUrl: boardToCopy.imageFullUrl,
imageLinkHTML: boardToCopy.imageLinkHTML,
imageUserName: boardToCopy.imageUserName,
imageDownloadUrl: boardToCopy.imageDownloadUrl,
lists: {
createMany: {
data: boardToCopy.lists.map((list) => ({
title: list.title,
order: list.order,
})),
},
},
},
include: {
lists: true,
},
});
} catch (error) {
return {
error: 'Failed to create board',
};
}
redirect(`/board/${board.id}`);
};
export const copyBoard = createSafeAction(CopyBoard, handler);

View file

@ -0,0 +1,5 @@
import { z } from 'zod';
export const CopyBoard = z.object({
id: z.string(),
});

View file

@ -0,0 +1,8 @@
import { z } from 'zod';
import { Board } from '@prisma/client';
import { ActionState } from '@/lib/create-safe-action';
import { CopyBoard } from './schema';
export type InputType = z.infer<typeof CopyBoard>;
export type ReturnType = ActionState<InputType, Board>;

View file

@ -75,6 +75,7 @@ const handler = async (data: InputType): Promise<ReturnType> => {
imageFullUrl,
imageUserName,
imageLinkHTML,
imageDownloadUrl,
},
});