From 89b38df0cd59ec86d52aa83d7364cdb1a58c2a02 Mon Sep 17 00:00:00 2001 From: Regourd Colin Date: Mon, 30 Sep 2024 16:57:42 +0200 Subject: [PATCH] Add refresh on dialog client and add options on example (#448) --- .changeset/seven-ties-mate.md | 5 ++ apps/example/actions/addTag.ts | 19 ++++++ .../components/PostAddTagDialogContent.tsx | 65 +++++++++++++++++++ .../components/UserDetailsDialogContent.tsx | 44 ++++++++++--- apps/example/messages/en.json | 4 ++ apps/example/messages/fr.json | 4 ++ apps/example/options.tsx | 8 +++ .../src/context/ClientActionDialogContext.tsx | 3 + 8 files changed, 143 insertions(+), 9 deletions(-) create mode 100644 .changeset/seven-ties-mate.md create mode 100644 apps/example/actions/addTag.ts create mode 100644 apps/example/components/PostAddTagDialogContent.tsx diff --git a/.changeset/seven-ties-mate.md b/.changeset/seven-ties-mate.md new file mode 100644 index 00000000..e5371787 --- /dev/null +++ b/.changeset/seven-ties-mate.md @@ -0,0 +1,5 @@ +--- +"@premieroctet/next-admin": patch +--- + +add refresh on dialog action diff --git a/apps/example/actions/addTag.ts b/apps/example/actions/addTag.ts new file mode 100644 index 00000000..aecbd9ed --- /dev/null +++ b/apps/example/actions/addTag.ts @@ -0,0 +1,19 @@ +"use server"; +import { prisma } from "./../prisma"; + +const addTag = async (tag: string, selectedIds?: number[]) => { + await prisma.post.updateMany({ + where: { + id: { + in: selectedIds, + }, + }, + data: { + tags: { + push: tag, + }, + }, + }); +}; + +export default addTag; diff --git a/apps/example/components/PostAddTagDialogContent.tsx b/apps/example/components/PostAddTagDialogContent.tsx new file mode 100644 index 00000000..eee7ed7a --- /dev/null +++ b/apps/example/components/PostAddTagDialogContent.tsx @@ -0,0 +1,65 @@ +"use client"; +import { ClientActionDialogContentProps } from "@premieroctet/next-admin"; +import { BaseInput, Button } from "@premieroctet/next-admin/components"; +import { useState } from "react"; +import addTag from "../actions/addTag"; + +type Props = ClientActionDialogContentProps<"User">; + +const AddTagDialog = ({ data, onClose }: Props) => { + const [tag, setTag] = useState(""); + const [isPending, setIsPending] = useState(false); + + const handleSubmit = () => { + setIsPending(true); + addTag( + tag, + data?.map((d) => d.id) + ) + .then(() => { + onClose?.({ + type: "success", + message: "Tag added successfully", + }); + + }) + .catch((e) => { + console.error(e); + onClose?.({ + type: "error", + message: "An error occured", + }); + }) + .finally(() => { + setIsPending(false); + }); + }; + + return ( +
+
+
+ Add a new tag +
+ setTag(e.target.value)} + /> + + +
+ + +
+
+ ); +}; + +export default AddTagDialog; diff --git a/apps/example/components/UserDetailsDialogContent.tsx b/apps/example/components/UserDetailsDialogContent.tsx index 0ada4732..6bdd1d00 100644 --- a/apps/example/components/UserDetailsDialogContent.tsx +++ b/apps/example/components/UserDetailsDialogContent.tsx @@ -9,17 +9,43 @@ const UserDetailsDialog = ({ data, onClose }: Props) => {
{data?.map((user) => (
-

- {user.email as string} -

-

- {user.name as string} -

-

- {user.role as string} -

+
+
+ User Information +
+
+ Details about the current user. +
+
+
+
+ + Name: + + + {user.name as string} + +
+
+ + Email: + + + {user.email as string} + +
+
+ + Role: + + + {user.role as string} + +
+
))} +