Skip to content

Commit

Permalink
Merge pull request #79 from premieroctet/feature/url-slug
Browse files Browse the repository at this point in the history
feat: allow slug version of model in url
  • Loading branch information
tlenclos authored Dec 5, 2023
2 parents ab45bde + c103c33 commit dddb9fe
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-penguins-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@premieroctet/next-admin": patch
---

feat: allow slug version of model in url
3 changes: 2 additions & 1 deletion .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: E2E tests
name: E2E & Unit tests
on:
push:
branches:
Expand Down Expand Up @@ -34,6 +34,7 @@ jobs:
run: yarn start:example &
- name: Run tests
run: |
yarn test
yarn turbo test:e2e
BASE_URL=http://localhost:3000/pagerouter/admin yarn test:e2e
- uses: actions/upload-artifact@v3
Expand Down
4 changes: 3 additions & 1 deletion apps/example/e2e/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ export const deleteItem = async (model: ModelName, page: Page, id: string) => {
page.on("dialog", async (dialog) => dialog.accept());
await page.goto(`${process.env.BASE_URL}/${model}/${id}`);
await page.click('button:has-text("Delete")');
await page.waitForURL((url) => url.pathname.endsWith(`/${model}`));
await page.waitForURL((url) =>
url.pathname.endsWith(`/${model.toLowerCase()}`)
);
};

export const readItem = async (model: ModelName, page: Page, id: string) => {
Expand Down
6 changes: 4 additions & 2 deletions packages/next-admin/src/components/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,10 @@ export function DataTable({ columns, data, resource }: DataTableProps) {
className="cursor-pointer hover:bg-indigo-50"
onClick={() => {
router.push({
// @ts-expect-error
pathname: `${basePath}/${resource}/${row.original.id.value}`,
pathname: `${basePath}/${resource.toLowerCase()}/${
// @ts-expect-error
row.original.id.value
}`,
});
}}
>
Expand Down
6 changes: 3 additions & 3 deletions packages/next-admin/src/components/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from "@rjsf/utils";
import validator from "@rjsf/validator-ajv8";
import clsx from "clsx";
import { ChangeEvent, useEffect, useState } from "react";
import { ChangeEvent, useState } from "react";
import { PropertyValidationError } from "../exceptions/ValidationError";
import { ModelName, SubmitFormResult } from "../types";
import { Schemas, getSchemas } from "../utils/jsonSchema";
Expand Down Expand Up @@ -192,7 +192,7 @@ const Form = ({

if (result?.deleted) {
return router.replace({
pathname: `${basePath}/${resource}`,
pathname: `${basePath}/${resource.toLowerCase()}`,
query: {
message: JSON.stringify({
type: "success",
Expand All @@ -204,7 +204,7 @@ const Form = ({

if (result?.created) {
return router.replace({
pathname: `${basePath}/${resource}/${result.createdId}`,
pathname: `${basePath}/${resource.toLowerCase()}/${result.createdId}`,
query: {
message: JSON.stringify({
type: "success",
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 @@ -27,7 +27,7 @@ export default function Menu({
}> =
resources?.map((resource) => ({
name: resource,
href: `${basePath}/${resource}`,
href: `${basePath}/${resource.toLowerCase()}`,
current: resource === currentResource,
})) || [];

Expand Down
4 changes: 2 additions & 2 deletions packages/next-admin/src/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export const nextAdminRouter = async (

return {
redirect: {
destination: `${options.basePath}/${resource}`,
destination: `${options.basePath}/${resource.toLowerCase()}`,
permanent: false,
},
};
Expand Down Expand Up @@ -188,7 +188,7 @@ export const nextAdminRouter = async (

return {
redirect: {
destination: `${options.basePath}/${resource}/${
destination: `${options.basePath}/${resource.toLowerCase()}/${
createdData.id
}?message=${JSON.stringify({
type: "success",
Expand Down
3 changes: 2 additions & 1 deletion packages/next-admin/src/tests/prismaUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ describe("getMappedDataList", () => {
prismaMock,
"Post",
options,
new URLSearchParams()
new URLSearchParams(),
{},
);
expect(result).toEqual({
data: postData,
Expand Down
4 changes: 4 additions & 0 deletions packages/next-admin/src/utils/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ describe("Server utils", () => {
expect(getResourceFromParams(["User", "1"], ["User"])).toEqual("User");
});

it("should return a resource with /admin/user/1", () => {
expect(getResourceFromParams(["user", "1"], ["User"])).toEqual("User");
});

it("should not return a resource with /admin/Post", () => {
expect(getResourceFromParams(["Post"], ["User"])).toEqual(undefined);
});
Expand Down
7 changes: 4 additions & 3 deletions packages/next-admin/src/utils/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { Prisma, PrismaClient } from "@prisma/client";
import bodyParser from "body-parser";
import util from "util";
import {
EditFieldsOptions,
EditOptions,
Expand Down Expand Up @@ -493,7 +491,10 @@ export const getResourceFromParams = (
params: string[],
resources: Prisma.ModelName[]
) => {
return resources.find((r) => params.includes(r));
return resources.find((r) => {
const slugifiedResource = r.toLowerCase();
return params.some(param => param.toLowerCase() === slugifiedResource)
});
};

/**
Expand Down

0 comments on commit dddb9fe

Please sign in to comment.