mirror of
https://github.com/ahmadk953/tasko.git
synced 2025-01-31 00:53:37 +00:00
Added Copy Board Feature
This commit is contained in:
parent
b15a0fbac5
commit
993552226b
8 changed files with 125 additions and 17 deletions
69
actions/copy-board/index.ts
Normal file
69
actions/copy-board/index.ts
Normal 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);
|
5
actions/copy-board/schema.ts
Normal file
5
actions/copy-board/schema.ts
Normal file
|
@ -0,0 +1,5 @@
|
|||
import { z } from 'zod';
|
||||
|
||||
export const CopyBoard = z.object({
|
||||
id: z.string(),
|
||||
});
|
8
actions/copy-board/types.ts
Normal file
8
actions/copy-board/types.ts
Normal 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>;
|
|
@ -75,6 +75,7 @@ const handler = async (data: InputType): Promise<ReturnType> => {
|
|||
imageFullUrl,
|
||||
imageUserName,
|
||||
imageLinkHTML,
|
||||
imageDownloadUrl,
|
||||
},
|
||||
});
|
||||
|
||||
|
|
|
@ -12,20 +12,37 @@ import {
|
|||
PopoverContent,
|
||||
PopoverTrigger,
|
||||
} from '@/components/ui/popover';
|
||||
import { copyBoard } from '@/actions/copy-board';
|
||||
|
||||
interface BoardOptionsProps {
|
||||
id: string;
|
||||
}
|
||||
|
||||
export const BoardOptions = ({ id }: BoardOptionsProps) => {
|
||||
const { execute, isLoading } = useAction(deleteBoard, {
|
||||
const { execute: executeDelete, isLoading: isLoadingDelete } = useAction(
|
||||
deleteBoard,
|
||||
{
|
||||
onError: (error) => {
|
||||
toast.error(error);
|
||||
},
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
const { execute: executeCopy, isLoading: isLoadingCopy } = useAction(
|
||||
copyBoard,
|
||||
{
|
||||
onError: (error) => {
|
||||
toast.error(error);
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
const onDelete = () => {
|
||||
execute({ id });
|
||||
executeDelete({ id });
|
||||
};
|
||||
|
||||
const onCopy = () => {
|
||||
executeCopy({ id });
|
||||
};
|
||||
|
||||
return (
|
||||
|
@ -47,10 +64,18 @@ export const BoardOptions = ({ id }: BoardOptionsProps) => {
|
|||
<X className='h-4 w-4' />
|
||||
</Button>
|
||||
</PopoverClose>
|
||||
<Button
|
||||
variant='ghost'
|
||||
onClick={onCopy}
|
||||
disabled={isLoadingCopy}
|
||||
className='h-auto w-full justify-start rounded-none p-2 px-5 text-sm font-normal text-neutral-600'
|
||||
>
|
||||
Copy this board
|
||||
</Button>
|
||||
<Button
|
||||
variant='ghost'
|
||||
onClick={onDelete}
|
||||
disabled={isLoading}
|
||||
disabled={isLoadingDelete}
|
||||
className='h-auto w-full justify-start rounded-none p-2 px-5 text-sm font-normal text-destructive hover:text-destructive'
|
||||
>
|
||||
Delete this board
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -3,7 +3,7 @@ import Link from 'next/link';
|
|||
|
||||
export const Logo = () => {
|
||||
return (
|
||||
// TODO: Make this go back to teh organization page is you are logged in
|
||||
// TODO: Make this go back to the organization page if you are logged in
|
||||
<Link href='/'>
|
||||
<div className='hidden items-center gap-x-2 transition hover:opacity-75 md:flex'>
|
||||
<Image
|
||||
|
|
|
@ -18,6 +18,7 @@ model Board {
|
|||
imageFullUrl String @db.Text
|
||||
imageUserName String @db.Text
|
||||
imageLinkHTML String @db.Text
|
||||
imageDownloadUrl String @db.Text
|
||||
|
||||
lists List[]
|
||||
|
||||
|
|
Loading…
Reference in a new issue