Skip to content

Commit

Permalink
fix: broken clickoutside & example custom page
Browse files Browse the repository at this point in the history
  • Loading branch information
foyarash committed Dec 13, 2024
1 parent aba7c4d commit 2ee9186
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 46 deletions.
5 changes: 5 additions & 0 deletions .changeset/beige-meals-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@premieroctet/next-admin": patch
---

fix: broken clickoutside & example custom page
39 changes: 14 additions & 25 deletions apps/example/app/[locale]/admin/custom/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,25 @@ const CustomPage = async () => {
}}
>
<div className="p-10">
<h1 className="mb-4 text-xl font-bold leading-7 text-gray-900 dark:text-gray-300 sm:truncate sm:text-3xl sm:tracking-tight">
<h1 className="mb-4 text-xl font-bold leading-7 text-gray-900 sm:truncate sm:text-3xl sm:tracking-tight dark:text-gray-300">
Dashboard
</h1>
<div className="mt-2">
<div>
<dl className="mt-5 grid grid-cols-1 gap-5 sm:grid-cols-3">
<div className="overflow-hidden rounded-lg bg-white px-4 py-5 shadow dark:bg-gray-800 sm:p-6">
<dt className="truncate text-sm font-medium text-gray-500 dark:text-gray-400">
Total Users
</dt>
<dd className="mt-1 text-3xl font-semibold tracking-tight text-gray-900 dark:text-gray-200">
8
</dd>
</div>
<div className="overflow-hidden rounded-lg bg-white px-4 py-5 shadow dark:bg-gray-800 sm:p-6">
<dt className="truncate text-sm font-medium text-gray-500 dark:text-gray-400">
Total Posts
</dt>
<dd className="mt-1 text-3xl font-semibold tracking-tight text-gray-900 dark:text-gray-200">
59
</dd>
</div>
<div className="overflow-hidden rounded-lg bg-white px-4 py-5 shadow dark:bg-gray-800 sm:p-6">
<dt className="truncate text-sm font-medium text-gray-500 dark:text-gray-400">
Total Categories
</dt>
<dd className="mt-1 text-3xl font-semibold tracking-tight text-gray-900 dark:text-gray-200">
1
</dd>
</div>
{stats.map((item) => (
<div
key={item.name}
className="overflow-hidden rounded-lg bg-white px-4 py-5 shadow sm:p-6 dark:bg-gray-800"
>
<dt className="truncate text-sm font-medium text-gray-500 dark:text-gray-400">
{item.name}
</dt>
<dd className="mt-1 text-3xl font-semibold tracking-tight text-gray-900 dark:text-gray-200">
{item.stat}
</dd>
</div>
))}
</dl>
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions apps/example/pages/pagerouter/admin/custom/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ const CustomPage = ({
{stats.map((item) => (
<div
key={item.name}
className="overflow-hidden rounded-lg bg-white px-4 py-5 shadow sm:p-6"
className="overflow-hidden rounded-lg bg-white px-4 py-5 shadow sm:p-6 dark:bg-gray-800"
>
<dt className="truncate text-sm font-medium text-gray-500">
<dt className="truncate text-sm font-medium text-gray-500 dark:text-gray-400">
{item.name}
</dt>
<dd className="mt-1 text-3xl font-semibold tracking-tight text-gray-900">
<dd className="mt-1 text-3xl font-semibold tracking-tight text-gray-900 dark:text-gray-200">
{item.stat}
</dd>
</div>
Expand Down
2 changes: 1 addition & 1 deletion packages/next-admin/src/components/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const ColorSchemeSwitch = dynamic(() => import("./ColorSchemeSwitch"), {
});

export type MenuProps = {
resource?: ModelName;
resource?: ModelName | null;
resources?: ModelName[];
resourcesTitles?: Record<ModelName, string | undefined>;
customPages?: AdminComponentProps["customPages"];
Expand Down
4 changes: 2 additions & 2 deletions packages/next-admin/src/context/ConfigContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type ConfigContextType = {
basePath: string;
isAppDir?: boolean;
apiBasePath: string;
resource?: ModelName;
resource?: ModelName | null;
resourcesIdProperty: Record<ModelName, string> | null;
schema: Schema;
nextAdminContext?: NextAdminContext;
Expand All @@ -28,7 +28,7 @@ type ProviderProps = {
options?: NextAdminOptions;
children: React.ReactNode;
isAppDir?: boolean;
resource?: ModelName;
resource?: ModelName | null;
resourcesIdProperty: Record<ModelName, string> | null;
schema: Schema;
nextAdminContext?: NextAdminContext;
Expand Down
12 changes: 2 additions & 10 deletions packages/next-admin/src/hooks/useCloseOnOutsideClick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,9 @@ const useClickOutside = <E extends HTMLElement = HTMLElement>(
}
};

setTimeout(() => {
/**
* There is a weird behavior on Page Router where this triggers
* just when a selector is opened. I suppose this is because the click event is not finished
* dispatching when we click on a trigger, but this is strange that
* this does not occur in App Router
*/
document.addEventListener("click", handleClickOutside);
});
document.addEventListener("mousedown", handleClickOutside);
return () => {
document.removeEventListener("click", handleClickOutside);
document.removeEventListener("mousedown", handleClickOutside);
};
}, [callback]);

Expand Down
2 changes: 1 addition & 1 deletion packages/next-admin/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ export type AdminComponentProps = {
apiBasePath: string;
schema: Schema;
data?: ListData<ModelName>;
resource?: ModelName;
resource?: ModelName | null;
slug?: string;
/**
* Page router only
Expand Down
10 changes: 6 additions & 4 deletions packages/next-admin/src/utils/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1161,10 +1161,12 @@ export const getResourceFromParams = (
params: string[],
resources: Prisma.ModelName[]
) => {
return resources.find((r) => {
const slugifiedResource = r.toLowerCase();
return params.some((param) => param.toLowerCase() === slugifiedResource);
});
return (
resources.find((r) => {
const slugifiedResource = r.toLowerCase();
return params.some((param) => param.toLowerCase() === slugifiedResource);
}) ?? null
);
};

/**
Expand Down

0 comments on commit 2ee9186

Please sign in to comment.