From c5bfcaeaf334b03d1e0bc89d8c3e22717e2f0079 Mon Sep 17 00:00:00 2001 From: bill Date: Sun, 29 Sep 2024 17:58:51 +0800 Subject: [PATCH] feat: Drop database #1841 --- gui/app/(dashboard)/actions.ts | 43 ++++++++++------ gui/app/(dashboard)/database-card.tsx | 70 +++++++++++++++++++++++++++ gui/app/(dashboard)/page.tsx | 15 ++---- gui/lib/constant/api.ts | 3 +- gui/lib/constant/common.ts | 5 ++ gui/lib/request.ts | 5 +- gui/public/delete.svg | 6 +++ gui/public/more.svg | 12 +++++ 8 files changed, 130 insertions(+), 29 deletions(-) create mode 100644 gui/app/(dashboard)/database-card.tsx create mode 100644 gui/public/delete.svg create mode 100644 gui/public/more.svg diff --git a/gui/app/(dashboard)/actions.ts b/gui/app/(dashboard)/actions.ts index 885411004d..fab27412e9 100644 --- a/gui/app/(dashboard)/actions.ts +++ b/gui/app/(dashboard)/actions.ts @@ -1,20 +1,18 @@ 'use server'; import { ApiUrl } from '@/lib/constant/api'; -import { CreateOption } from '@/lib/constant/common'; -import { get, post } from '@/lib/request'; +import { CreateOption, DropOption } from '@/lib/constant/common'; +import { drop, get, post } from '@/lib/request'; -export async function deleteProduct(formData: FormData) { - // let id = Number(formData.get('id')); - // await deleteProductById(id); - // revalidatePath('/'); -} - -export async function createDatabaseAction(formData: FormData) { - // let id = Number(formData.get('id')); - // await deleteProductById(id); - // revalidatePath('/'); -} +export const listDatabase = async () => { + try { + const x = await get(`${ApiUrl.databases}`); + console.log('🚀 ~ x:', x); + return x; + } catch (error) { + console.log('🚀 ~ error:', error); + } +}; export const createDatabase = async (params: { database_name: string; @@ -31,9 +29,24 @@ export const createDatabase = async (params: { } }; -export const listDatabase = async () => { +export const dropDatabase = async (params: { + database_name: string; + drop_option: DropOption; +}) => { try { - const x = await get(`${ApiUrl.databases}`); + const x = await drop(`${ApiUrl.databases}/${params.database_name}`, { + drop_option: params.drop_option + }); + console.log('🚀 ~ x:', x); + return x; + } catch (error) { + console.log('🚀 ~ error:', error); + } +}; + +export const showDatabase = async (params: { database_name: string }) => { + try { + const x = await get(`${ApiUrl.database}/${params.database_name}`); console.log('🚀 ~ x:', x); return x; } catch (error) { diff --git a/gui/app/(dashboard)/database-card.tsx b/gui/app/(dashboard)/database-card.tsx new file mode 100644 index 0000000000..d804ed77de --- /dev/null +++ b/gui/app/(dashboard)/database-card.tsx @@ -0,0 +1,70 @@ +'use client'; + +import { toast } from '@/components/hooks/use-toast'; +import { + Card, + CardDescription, + CardHeader, + CardTitle +} from '@/components/ui/card'; +import { + DropdownMenu, + DropdownMenuContent, + DropdownMenuTrigger +} from '@/components/ui/dropdown-menu'; +import { DropOption } from '@/lib/constant/common'; +import { DropdownMenuItem } from '@radix-ui/react-dropdown-menu'; +import { useRouter } from 'next/navigation'; +import { dropDatabase } from './actions'; +import DeleteIcon from '/public/delete.svg'; +import MoreIcon from '/public/more.svg'; + +export const DeleteDropdown = ({ + children, + deleteItem +}: React.PropsWithChildren<{ deleteItem: () => void }>) => { + return ( + + {children} + + +
+ Drop + +
+
+
+
+ ); +}; + +export function DatabaseCard({ data }: { data: string }) { + const router = useRouter(); + const handleDelete = async () => { + const ret = await dropDatabase({ + database_name: data, + drop_option: DropOption.IgnoreIfNotExists + }); + if (ret.error_code === 0) { + router.refresh(); + toast({ + title: 'Drop Success', + description: '' + }); + } + }; + + return ( + + +
+ + + +
+ {data} + db description +
+
+ ); +} diff --git a/gui/app/(dashboard)/page.tsx b/gui/app/(dashboard)/page.tsx index fd9dc7a376..67dc9e9bda 100644 --- a/gui/app/(dashboard)/page.tsx +++ b/gui/app/(dashboard)/page.tsx @@ -1,9 +1,4 @@ -import { - Card, - CardDescription, - CardHeader, - CardTitle -} from '@/components/ui/card'; +import { Card, CardHeader, CardTitle } from '@/components/ui/card'; import AddIcon from '/public/add.svg'; import { @@ -14,6 +9,7 @@ import { SelectValue } from '@radix-ui/react-select'; import { listDatabase } from './actions'; +import { DatabaseCard } from './database-card'; import { DatabaseCreatingDialog } from './database-creating-dialog'; interface IDatabaseSelectProps { @@ -51,12 +47,7 @@ export default async function HomePage({ return (
{ret.databases.map((x: string, idx: number) => ( - - - {x} - db description - - + ))} diff --git a/gui/lib/constant/api.ts b/gui/lib/constant/api.ts index c0206cde0d..e25b17212f 100644 --- a/gui/lib/constant/api.ts +++ b/gui/lib/constant/api.ts @@ -1,3 +1,4 @@ export const ApiUrl = { - databases: 'databases' + databases: 'databases', + database: 'database' }; diff --git a/gui/lib/constant/common.ts b/gui/lib/constant/common.ts index 49d47667e1..d6a76ace1a 100644 --- a/gui/lib/constant/common.ts +++ b/gui/lib/constant/common.ts @@ -2,3 +2,8 @@ export enum CreateOption { Error = 'error', IgnoreIfExists = 'ignore_if_exists' } + +export enum DropOption { + Error = 'error', + IgnoreIfNotExists = 'ignore_if_not_exists' +} diff --git a/gui/lib/request.ts b/gui/lib/request.ts index ac2110e68f..fc4cfdbca1 100644 --- a/gui/lib/request.ts +++ b/gui/lib/request.ts @@ -1,4 +1,4 @@ -const baseUrl = 'http://127.0.0.1:23820/'; +const baseUrl = 'http://127.0.0.1:3000/'; export const request = async ( url: string, @@ -34,3 +34,6 @@ export const get = (url: string, params?: Record) => export const post = (url: string, params: Record) => request(url, params, 'POST'); + +export const drop = (url: string, params: Record) => + request(url, params, 'DELETE'); diff --git a/gui/public/delete.svg b/gui/public/delete.svg new file mode 100644 index 0000000000..6f3744c9ab --- /dev/null +++ b/gui/public/delete.svg @@ -0,0 +1,6 @@ + + + \ No newline at end of file diff --git a/gui/public/more.svg b/gui/public/more.svg new file mode 100644 index 0000000000..d8f208dc83 --- /dev/null +++ b/gui/public/more.svg @@ -0,0 +1,12 @@ + + + + + \ No newline at end of file