Skip to content

Commit 516d7f0

Browse files
authored
Merge pull request #9 from database-playground/pan93412/dbp-21-implement-new-auth-in-admin
DBP-21: Implement new auth in admin
2 parents 25779ce + 37ff4a0 commit 516d7f0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+2479
-988
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
```env
66
NEXT_PUBLIC_BACKEND_BASE_URL=
7-
APP_REDIRECT_URL=
7+
AUTH_SECRET=
88
```
99

1010
## Development

app/(admin)/(question-management)/database/_components/create.tsx

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use client";
22

33
import { buttonVariants } from "@/components/ui/button";
4+
import { ConfirmationDialog } from "@/components/ui/confirmation-dialog";
45
import {
56
Dialog,
67
DialogContent,
@@ -9,35 +10,66 @@ import {
910
DialogTitle,
1011
DialogTrigger,
1112
} from "@/components/ui/dialog";
13+
import { useDialogCloseConfirmation } from "@/hooks/use-dialog-close-confirmation";
1214
import { useMutation } from "@apollo/client/react";
13-
import { useRouter } from "next/navigation";
1415
import { useState } from "react";
1516
import { toast } from "sonner";
1617
import { DATABASE_CREATE_MUTATION } from "./mutation";
1718
import { DATABASES_TABLE_QUERY } from "./query";
18-
import { UpdateDatabaseForm, type UpdateDatabaseFormData } from "./update-form";
19+
import { UpdateDatabaseForm } from "./update-form";
1920

2021
export function CreateDatabaseTrigger() {
21-
const router = useRouter();
2222
const [open, setOpen] = useState(false);
23+
const [isFormDirty, setIsFormDirty] = useState(false);
24+
25+
const {
26+
showConfirmation,
27+
handleDialogOpenChange,
28+
handleConfirmClose,
29+
handleCancelClose,
30+
} = useDialogCloseConfirmation({
31+
isDirty: isFormDirty,
32+
setOpen,
33+
onConfirmedClose: () => {
34+
setIsFormDirty(false);
35+
},
36+
});
37+
38+
const handleFormStateChange = (isDirty: boolean) => {
39+
setIsFormDirty(isDirty);
40+
};
41+
42+
const handleCompleted = () => {
43+
setIsFormDirty(false);
44+
setOpen(false);
45+
};
2346

2447
return (
25-
<Dialog open={open} onOpenChange={setOpen}>
26-
<DialogTrigger className={buttonVariants()}>新增資料庫</DialogTrigger>
27-
<CreateDatabaseDialogContent
28-
onCompleted={() => {
29-
setOpen(false);
30-
router.refresh();
31-
}}
48+
<>
49+
<Dialog open={open} onOpenChange={handleDialogOpenChange}>
50+
<DialogTrigger className={buttonVariants()}>新增資料庫</DialogTrigger>
51+
<CreateDatabaseDialogContent
52+
onCompleted={handleCompleted}
53+
onFormStateChange={handleFormStateChange}
54+
/>
55+
</Dialog>
56+
57+
<ConfirmationDialog
58+
open={showConfirmation}
59+
onOpenChange={() => {}}
60+
onConfirm={handleConfirmClose}
61+
onCancel={handleCancelClose}
3262
/>
33-
</Dialog>
63+
</>
3464
);
3565
}
3666

3767
function CreateDatabaseDialogContent({
3868
onCompleted,
69+
onFormStateChange,
3970
}: {
4071
onCompleted: () => void;
72+
onFormStateChange: (isDirty: boolean) => void;
4173
}) {
4274
const [createDatabase] = useMutation(DATABASE_CREATE_MUTATION, {
4375
refetchQueries: [DATABASES_TABLE_QUERY],
@@ -54,25 +86,6 @@ function CreateDatabaseDialogContent({
5486
},
5587
});
5688

57-
const onSubmit = (data: UpdateDatabaseFormData) => {
58-
try {
59-
createDatabase({
60-
variables: {
61-
input: {
62-
slug: data.slug!,
63-
description: data.description,
64-
schema: data.schema!,
65-
relationFigure: data.relationFigure!,
66-
},
67-
},
68-
});
69-
} catch (error) {
70-
toast.error("資料庫建立失敗", {
71-
description: error instanceof Error ? error.message : "未知錯誤",
72-
});
73-
}
74-
};
75-
7689
return (
7790
<DialogContent className="max-h-[85vh] max-w-3xl overflow-y-auto">
7891
<DialogHeader>
@@ -88,8 +101,20 @@ function CreateDatabaseDialogContent({
88101
schema: "",
89102
relationFigure: "",
90103
}}
91-
onSubmit={onSubmit}
104+
onSubmit={(data) => {
105+
createDatabase({
106+
variables: {
107+
input: {
108+
slug: data.slug,
109+
description: data.description,
110+
schema: data.schema,
111+
relationFigure: data.relationFigure,
112+
},
113+
},
114+
});
115+
}}
92116
action="create"
117+
onFormStateChange={onFormStateChange}
93118
/>
94119
</DialogContent>
95120
);

app/(admin)/(question-management)/database/_components/delete.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from "@/components/ui/alert-dialog";
1414
import { buttonVariants } from "@/components/ui/button";
1515
import { DropdownMenuItem } from "@/components/ui/dropdown-menu";
16-
import { useMutation, useSuspenseQuery } from "@apollo/client/react";
16+
import { skipToken, useMutation, useSuspenseQuery } from "@apollo/client/react";
1717
import { Trash } from "lucide-react";
1818
import { useRouter } from "next/navigation";
1919
import { Suspense, useState } from "react";
@@ -42,6 +42,7 @@ export function DeleteDatabaseDropdownTrigger({ id }: { id: string }) {
4242
<Suspense>
4343
<DeleteDatabaseAlertDialogContent
4444
id={id}
45+
open={open}
4546
onCompleted={() => {
4647
setOpen(false);
4748
router.refresh();
@@ -66,6 +67,7 @@ export function DeleteDatabaseButtonTrigger({ id }: { id: string }) {
6667
<Suspense>
6768
<DeleteDatabaseAlertDialogContent
6869
id={id}
70+
open={open}
6971
onCompleted={() => {
7072
setOpen(false);
7173
router.push("/database");
@@ -78,14 +80,21 @@ export function DeleteDatabaseButtonTrigger({ id }: { id: string }) {
7880

7981
function DeleteDatabaseAlertDialogContent({
8082
id,
83+
open,
8184
onCompleted,
8285
}: {
8386
id: string;
87+
open: boolean;
8488
onCompleted: () => void;
8589
}) {
86-
const { data } = useSuspenseQuery(DATABASE_BY_ID_QUERY, {
87-
variables: { id },
88-
});
90+
const { data } = useSuspenseQuery(
91+
DATABASE_BY_ID_QUERY,
92+
open
93+
? {
94+
variables: { id },
95+
}
96+
: skipToken,
97+
);
8998

9099
const [deleteDatabase] = useMutation(DATABASE_DELETE_MUTATION, {
91100
refetchQueries: [DATABASES_TABLE_QUERY],
@@ -106,7 +115,7 @@ function DeleteDatabaseAlertDialogContent({
106115
<AlertDialogContent>
107116
<AlertDialogHeader>
108117
<AlertDialogTitle>
109-
確定要刪除「{data.database.slug}」資料庫嗎?
118+
確定要刪除「{data?.database.slug}」資料庫嗎?
110119
</AlertDialogTitle>
111120
<AlertDialogDescription>
112121
刪除後將無法復原此資料庫,且相關的題目可能會受到影響。請謹慎操作。

0 commit comments

Comments
 (0)