From f188d9541f159f28fd1e7b162ecba40136f391e8 Mon Sep 17 00:00:00 2001 From: mlahfaoui Date: Mon, 14 Mar 2022 13:35:28 +0100 Subject: [PATCH 1/4] fix: FineTuning & Fixe Bugs --- templates/next/components/foo/Show.tsx | 3 +- templates/next/pages/foos/[id]/edit.tsx | 42 ++++++------------------ templates/next/pages/foos/[id]/index.tsx | 42 ++++-------------------- templates/next/utils/dataAccess.ts | 5 +-- templates/next/utils/helpers.ts | 25 ++++++++++++++ 5 files changed, 47 insertions(+), 70 deletions(-) create mode 100644 templates/next/utils/helpers.ts diff --git a/templates/next/components/foo/Show.tsx b/templates/next/components/foo/Show.tsx index ef89523b..09547ecc 100644 --- a/templates/next/components/foo/Show.tsx +++ b/templates/next/components/foo/Show.tsx @@ -8,9 +8,10 @@ import Head from 'next/head' interface Props { {{{lc}}}: {{{ucf}}}; + text: String } -export const Show: FunctionComponent = ({ {{{lc}}} }) => { +export const Show: FunctionComponent = ({ {{{lc}}}, text }) => { const [error, setError] = useState(null); const router = useRouter(); diff --git a/templates/next/pages/foos/[id]/edit.tsx b/templates/next/pages/foos/[id]/edit.tsx index f052a1a0..4ef57b97 100644 --- a/templates/next/pages/foos/[id]/edit.tsx +++ b/templates/next/pages/foos/[id]/edit.tsx @@ -4,6 +4,7 @@ import { {{{ucf}}} } from "../../../types/{{{ucf}}}"; import { fetch } from "../../../utils/dataAccess"; import Head from "next/head"; import DefaultErrorPage from "next/error"; +import { getPathsFromHydraResponse } from "../../../utils/helpers"; interface Props { {{{lc}}}: {{{ucf}}}; @@ -27,48 +28,25 @@ const Page: NextComponentType = ({ {{{lc}}} }) => }; export const getStaticProps: GetStaticProps = async ({ params }) => { + const response = await fetch(`/{{{name}}}/${params.id}`); + return { props: { - {{{lc}}}: await fetch(`/{{{name}}}/${params.id}`), + {{{lc}}}: response.data, }, revalidate: 1, }; } -export const getStaticPaths: GetStaticPaths = async () => { - try { - const response = await fetch("/{{{name}}}"); - } catch (e) { - console.error(e); - - return { - paths: [], - fallback: true, - }; - } - - const view = response.data['{{{hydraPrefix}}}view']; - const paths = response.data["{{{hydraPrefix}}}member"].map(({{{lc}}}) => `${ {{~lc}}['@id'] }/edit`); - if (view) { - try { - const { - '{{{hydraPrefix}}}last': last - } = view; - for (let page = 2; page <= parseInt(last.replace(/^\/{{{name}}}\?page=(\d+)/, '$1')); page++) { - paths.concat( - await fetch(`/{{{name}}}?page=${page}`).data["{{{hydraPrefix}}}member"].map(({{{lc}}}) => `${ {{~lc}}['@id'] }/edit`) - ); - } - } catch (e) { - console.error(e); - } +export const getStaticPaths: GetStaticPaths = async() => { + const response = await fetch("/{{{name}}}"); + const paths= await getPathsFromHydraResponse(response,true); + return { + paths, + fallback:true } - return { - paths, - fallback: true, - }; } export default Page; diff --git a/templates/next/pages/foos/[id]/index.tsx b/templates/next/pages/foos/[id]/index.tsx index 8af2d4f2..666feb0f 100644 --- a/templates/next/pages/foos/[id]/index.tsx +++ b/templates/next/pages/foos/[id]/index.tsx @@ -5,6 +5,7 @@ import { fetch } from "../../../utils/dataAccess"; import Head from "next/head"; import DefaultErrorPage from "next/error"; import { useMercure } from "../../../utils/mercure"; +import { getPathsFromHydraResponse } from "../../../utils/helpers"; interface Props { {{{lc}}}: {{{ucf}}}; @@ -25,7 +26,7 @@ const Page: NextComponentType = (props) => { {`Show {{{ucf}}} ${ {{~lc}}['@id'] }`} - + ); }; @@ -42,41 +43,12 @@ export const getStaticProps: GetStaticProps = async ({ params }) => { revalidate: 1, }; } - -export const getStaticPaths: GetStaticPaths = async () => { - try { - const response = await fetch("/{{{name}}}"); - } catch (e) { - console.error(e); - - return { - paths: [], - fallback: true, - }; - } - - const view = response.data['{{{hydraPrefix}}}view']; - const paths = response.data["{{{hydraPrefix}}}member"].map(({{{lc}}}) => `${ {{~lc}}['@id'] }`); - - if (view) { - try { - const { - '{{{hydraPrefix}}}last': last - } = view; - for (let page = 2; page <= parseInt(last.replace(/^\/{{{name}}}\?page=(\d+)/, '$1')); page++) { - paths.concat( - await fetch(`/{{{name}}}?page=${page}`).data["{{{hydraPrefix}}}member"].map(({{{lc}}}) => `${ {{~lc}}['@id'] }`) - ); - } - } catch (e) { - console.error(e); - } - } - +export const getStaticPaths: GetStaticPaths = async() => { + const response = await fetch("/{{{name}}}"); + const paths= await getPathsFromHydraResponse(response,false); return { paths, - fallback: true, - }; + fallback:true +} } - export default Page; diff --git a/templates/next/utils/dataAccess.ts b/templates/next/utils/dataAccess.ts index fa929028..8e9fb656 100644 --- a/templates/next/utils/dataAccess.ts +++ b/templates/next/utils/dataAccess.ts @@ -36,12 +36,13 @@ export const fetch = async (id: string, init: RequestInit = {}) => { const resp = await isomorphicFetch(ENTRYPOINT + id, init); if (resp.status === 204) return; - const json = await resp.json(); + const text = await resp.text(); + const json = JSON.parse(text); if (resp.ok) { return { hubURL: extractHubURL(resp)?.toString(), // URL cannot be serialized as JSON, must be sent as string data: normalize(json), - text: await resp.text(), + text, }; } diff --git a/templates/next/utils/helpers.ts b/templates/next/utils/helpers.ts new file mode 100644 index 00000000..0558b01b --- /dev/null +++ b/templates/next/utils/helpers.ts @@ -0,0 +1,25 @@ +import { fetch } from "./dataAccess"; + +export const getPathsFromHydraResponse = async (response,isEdit: boolean) => { +try { + const concatPath=isEdit?"/edit":""; + const view = response.data['{{{hydraPrefix}}}view']; + const paths = response.data["{{{hydraPrefix}}}member"].map(({{{lc}}}) => `${ {{~lc}}['@id'] }${concatPath}`); + + if (view) { + const { + '{{{hydraPrefix}}}last': last + } = view; + for (let page = 2; page <= parseInt(last.replace(/^\/{{{name}}}\?page=(\d+)/, '$1')); page++) { + paths.concat( + await fetch(`/{{{name}}}?page=${page}`).data["{{{hydraPrefix}}}member"].map(({{{lc}}}) => `${ {{~lc}}['@id'] }${concatPath}`) + ); + } + } + return paths + } catch (e) { + console.error(e); + + return [] + } +} \ No newline at end of file From 6ca804ae020ba1ff3451ede9b03a537e72327f8e Mon Sep 17 00:00:00 2001 From: Alan Poulain Date: Tue, 22 Mar 2022 19:10:41 +0100 Subject: [PATCH 2/4] fix: add more fixes --- package.json | 2 +- templates/next/components/foo/Show.tsx | 18 ++++++---------- templates/next/pages/foos/[id]/edit.tsx | 20 ++++++++---------- templates/next/pages/foos/[id]/index.tsx | 27 +++++++++++++----------- templates/next/utils/dataAccess.ts | 23 ++++++++++++++++++++ templates/next/utils/helpers.ts | 25 ---------------------- templates/next/utils/mercure.ts | 20 +++++++++--------- 7 files changed, 65 insertions(+), 70 deletions(-) delete mode 100644 templates/next/utils/helpers.ts diff --git a/package.json b/package.json index 9145f23a..0a7db16d 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,7 @@ "eslint-check": "eslint-config-prettier src/index.js", "build": "babel src -d lib --ignore '*.test.js'", "watch": "babel --watch src -d lib --ignore '*.test.js'", - "test-gen": "rm -rf ./tmp && yarn build && ./lib/index.js https://demo.api-platform.com ./tmp/react -g react && ./lib/index.js https://demo.api-platform.com ./tmp/react-native -g react-native && ./lib/index.js https://demo.api-platform.com ./tmp/vue -g vue", + "test-gen": "rm -rf ./tmp && yarn build && ./lib/index.js https://demo.api-platform.com ./tmp/react -g react && ./lib/index.js https://demo.api-platform.com ./tmp/react-native -g react-native && ./lib/index.js https://demo.api-platform.com ./tmp/next -g next && ./lib/index.js https://demo.api-platform.com ./tmp/vue -g vue", "test-gen-custom": "rm -rf ./tmp && yarn build && babel src/generators/ReactGenerator.js src/generators/BaseGenerator.js -d ./tmp/gens && cp -r ./templates/react ./templates/react-common ./templates/entrypoint.js ./tmp/gens && ./lib/index.js https://demo.api-platform.com ./tmp/react-custom -g \"$(pwd)/tmp/gens/ReactGenerator.js\" -t ./tmp/gens", "test-gen-swagger": "rm -rf ./tmp && yarn build && ./lib/index.js https://demo.api-platform.com/docs.json ./tmp/react -f swagger && ./lib/index.js https://demo.api-platform.com/docs.json ./tmp/react-native -g react-native -f swagger && ./lib/index.js https://demo.api-platform.com/docs.json ./tmp/vue -g vue -f swagger", "test-gen-env": "rm -rf ./tmp && yarn build && API_PLATFORM_CLIENT_GENERATOR_ENTRYPOINT=https://demo.api-platform.com API_PLATFORM_CLIENT_GENERATOR_OUTPUT=./tmp ./lib/index.js" diff --git a/templates/next/components/foo/Show.tsx b/templates/next/components/foo/Show.tsx index 09547ecc..6aa0d5db 100644 --- a/templates/next/components/foo/Show.tsx +++ b/templates/next/components/foo/Show.tsx @@ -1,14 +1,14 @@ -import { FunctionComponent, useState } from 'react'; -import Link from 'next/link'; +import { FunctionComponent, useState } from "react"; +import Link from "next/link"; import { useRouter } from "next/router"; +import Head from "next/head"; import { fetch } from "../../utils/dataAccess"; -import ReferenceLinks from '../common/ReferenceLinks'; -import { {{{ucf}}} } from '../../types/{{{ucf}}}'; -import Head from 'next/head' +import ReferenceLinks from "../common/ReferenceLinks"; +import { {{{ucf}}} } from "../../types/{{{ucf}}}"; interface Props { {{{lc}}}: {{{ucf}}}; - text: String + text: string; } export const Show: FunctionComponent = ({ {{{lc}}}, text }) => { @@ -31,11 +31,7 @@ export const Show: FunctionComponent = ({ {{{lc}}}, text }) => {
{`Show {{{ucf}}} ${ {{~lc}}['@id']}`} - +