Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update next generator #369

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions src/generators/NextGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default class NextGenerator extends BaseGenerator {
this.routeAddedtoServer = false;
this.registerTemplates(`next/`, [
// components
"components/common/Layout.tsx",
"components/common/Wrapper.tsx",
"components/common/Pagination.tsx",
"components/common/ReferenceLinks.tsx",
"components/foo/List.tsx",
Expand All @@ -25,12 +25,12 @@ export default class NextGenerator extends BaseGenerator {
"types/item.ts",

// pages

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can change this to app

"pages/foos/[id]/index.tsx",
"pages/foos/[id]/edit.tsx",
"pages/foos/page/[page].tsx",
"pages/foos/index.tsx",
"pages/foos/create.tsx",
"pages/_app.tsx",
"app/foos/[id]/page.tsx",
"app/foos/[id]/edit/page.tsx",
"app/foos/page/[page]/page.tsx",
"app/foos/page.tsx",
"app/foos/create/page.tsx",
"app/layout.tsx",

// utils
"utils/dataAccess.ts",
Expand Down Expand Up @@ -81,9 +81,12 @@ export default class NextGenerator extends BaseGenerator {

// Copy with patterned name
this.createDir(`${dir}/components/${context.lc}`);
this.createDir(`${dir}/pages/${context.lc}s`);
this.createDir(`${dir}/pages/${context.lc}s/[id]`);
this.createDir(`${dir}/pages/${context.lc}s/page`);
this.createDir(`${dir}/app/${context.lc}s`);
this.createDir(`${dir}/app/${context.lc}s/[id]`);
this.createDir(`${dir}/app/${context.lc}s/create`);
this.createDir(`${dir}/app/${context.lc}s/[id]/edit`);
this.createDir(`${dir}/app/${context.lc}s/page`);
this.createDir(`${dir}/app/${context.lc}s/page/[page]`);
[
// components
"components/%s/List.tsx",
Expand All @@ -92,11 +95,11 @@ export default class NextGenerator extends BaseGenerator {
"components/%s/Form.tsx",

// pages

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can change this to app

"pages/%ss/[id]/index.tsx",
"pages/%ss/[id]/edit.tsx",
"pages/%ss/page/[page].tsx",
"pages/%ss/index.tsx",
"pages/%ss/create.tsx",
"app/%ss/[id]/page.tsx",
"app/%ss/[id]/edit/page.tsx",
"app/%ss/page/[page]/page.tsx",
"app/%ss/page.tsx",
"app/%ss/create/page.tsx",
].forEach((pattern) =>
this.createFileFromPattern(pattern, dir, [context.lc], context)
);
Expand All @@ -107,7 +110,7 @@ export default class NextGenerator extends BaseGenerator {
// copy with regular name
[
// components
"components/common/Layout.tsx",
"components/common/Wrapper.tsx",
"components/common/Pagination.tsx",
"components/common/ReferenceLinks.tsx",

Expand All @@ -116,7 +119,7 @@ export default class NextGenerator extends BaseGenerator {
"types/item.ts",

// pages

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can change this to app

"pages/_app.tsx",
"app/layout.tsx",

// utils
"utils/dataAccess.ts",
Expand Down
14 changes: 7 additions & 7 deletions src/generators/NextGenerator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,18 @@ describe("generate", () => {
"/components/abc/PageList.tsx",
"/components/abc/Show.tsx",
"/components/abc/Form.tsx",
"/components/common/Layout.tsx",
"/components/common/Wrapper.tsx",
"/components/common/ReferenceLinks.tsx",
"/components/common/Pagination.tsx",
"/types/Abc.ts",
"/types/collection.ts",
"/types/item.ts",
"/pages/abcs/[id]/index.tsx",
"/pages/abcs/[id]/edit.tsx",
"/pages/abcs/page/[page].tsx",
"/pages/abcs/index.tsx",
"/pages/abcs/create.tsx",
"/pages/_app.tsx",
"/app/abcs/[id]/index.tsx",
"/app/abcs/[id]/edit.tsx",
"/app/abcs/page/[page].tsx",
"/app/abcs/index.tsx",
"/app/abcs/create.tsx",
"/app/_app.tsx",
"/utils/dataAccess.ts",
"/utils/mercure.ts",
].forEach((file) => expect(fs.existsSync(tmpobj.name + file)).toBe(true));
Expand Down
Empty file modified src/index.js
100755 → 100644
Empty file.
32 changes: 32 additions & 0 deletions templates/next/app/foos/[id]/edit/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"use client";

import type { Metadata } from "next";
import { useQuery } from "react-query";

import { Form } from "../../../../components/{{{lc}}}/Form";
import { {{{ucf}}} } from "../../../../types/{{{ucf}}}";
import { customFetch, FetchResponse } from "../../../../utils/dataAccess";


const get{{{ucf}}} = async (id: string|string[]|undefined) => id ? await customFetch<{{{ucf}}}>(`/{{{name}}}/${id}`) : Promise.resolve(undefined);

type Props = {
params: { id: string };
};

export default function Page({ params }: Props) {
const { id } = params;

const { data: { data: {{lc}} } = {} } = useQuery<FetchResponse<{{{ucf}}}> | undefined>(['{{{lc}}}', id], () => get{{{ucf}}}(id));

if (!{{{lc}}}) {
return null;
}

return (
<div>
<title>{`Edit {{{ucf}}} ${ {{{lc}}}["@id"]}`}</title>
<Form {{{lc}}}={ {{{lc}}} } />
</div>
);
};
34 changes: 34 additions & 0 deletions templates/next/app/foos/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"use client";

import { useQuery } from "react-query";

import { Show } from "../../../components/{{{lc}}}/Show";
import { {{{ucf}}} } from "../../../types/{{{ucf}}}";
import { customFetch, FetchResponse } from "../../../utils/dataAccess";
import { useMercure } from "../../../utils/mercure";

const get{{{ucf}}} = async (id: string|string[]|undefined) => id ? await customFetch<{{{ucf}}}>(`/{{{name}}}/${id}`) : Promise.resolve(undefined);

type Props = {
params: { id: string };
};

export default function Page({ params }: Props) {
const { id } = params;


const { data: { data: {{lc}}, hubURL } = { hubURL: null, text: '' } } =
useQuery<FetchResponse<{{{ucf}}}> | undefined>(['{{{lc}}}', id], () => get{{{ucf}}}(id));
const {{{lc}}}Data = useMercure({{lc}}, hubURL);

if (!{{{lc}}}Data) {
return null;
}

return (
<div>
<title>{`Show {{{ucf}}} ${ {{{lc}}}Data["@id"]}`}</title>
<Show {{{lc}}}={ {{{lc}}}Data } />
</div>
);
};
17 changes: 17 additions & 0 deletions templates/next/app/foos/create.tsx
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is not necessary.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { NextComponentType, NextPageContext } from "next";
import Head from "next/head";

import { Form } from "../../components/{{{lc}}}/Form";

const Page: NextComponentType<NextPageContext> = () => (
<div>
<div>
<Head>
<title>Create {{{ucf}}}</title>
</Head>
</div>
<Form />
</div>
);

export default Page;
14 changes: 14 additions & 0 deletions templates/next/app/foos/create/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { Metadata } from "next";
import { Form } from "../../../components/{{{lc}}}/Form";

export const metadata: Metadata = {
title: "Create {{{ucf}}}",
};

export default function Page() {
return (
<div>
<Form />
</div>
);
}
8 changes: 8 additions & 0 deletions templates/next/app/foos/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { Metadata } from "next";
import PageList from "../../components/{{{lc}}}/PageList";

export const metadata: Metadata = {
title: "{{{ucf}}} List",
};

export default PageList;
3 changes: 3 additions & 0 deletions templates/next/app/foos/page/[page]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import PageList from "../../../../components/{{{lc}}}/PageList";

export default PageList;
24 changes: 24 additions & 0 deletions templates/next/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from "react";
import type { Metadata } from "next";
import Wrapper from "../components/common/Wrapper";
import "../styles/style.css";

export const metadata: Metadata = {
title: "API Platform Create Client",
description:
"Create REST and GraphQL APIs, scaffold Jamstack webapps, stream changes in real-time.",
};

export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="en">
<body>
<Wrapper>{children}</Wrapper>
</body>
</html>
);
}
14 changes: 14 additions & 0 deletions templates/next/components/common/Wrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use client";

import { ReactNode, useState } from "react";
import { QueryClient, QueryClientProvider } from "react-query";

const Wrapper = ({ children }: { children: ReactNode }) => {
const [queryClient] = useState(() => new QueryClient());

return (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);
};

export default Wrapper;
Loading
Loading