Skip to content

Commit 6ca804a

Browse files
committed
fix: add more fixes
1 parent f188d95 commit 6ca804a

File tree

7 files changed

+65
-70
lines changed

7 files changed

+65
-70
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"eslint-check": "eslint-config-prettier src/index.js",
5454
"build": "babel src -d lib --ignore '*.test.js'",
5555
"watch": "babel --watch src -d lib --ignore '*.test.js'",
56-
"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",
56+
"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",
5757
"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",
5858
"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",
5959
"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"

templates/next/components/foo/Show.tsx

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { FunctionComponent, useState } from 'react';
2-
import Link from 'next/link';
1+
import { FunctionComponent, useState } from "react";
2+
import Link from "next/link";
33
import { useRouter } from "next/router";
4+
import Head from "next/head";
45
import { fetch } from "../../utils/dataAccess";
5-
import ReferenceLinks from '../common/ReferenceLinks';
6-
import { {{{ucf}}} } from '../../types/{{{ucf}}}';
7-
import Head from 'next/head'
6+
import ReferenceLinks from "../common/ReferenceLinks";
7+
import { {{{ucf}}} } from "../../types/{{{ucf}}}";
88

99
interface Props {
1010
{{{lc}}}: {{{ucf}}};
11-
text: String
11+
text: string;
1212
}
1313

1414
export const Show: FunctionComponent<Props> = ({ {{{lc}}}, text }) => {
@@ -31,11 +31,7 @@ export const Show: FunctionComponent<Props> = ({ {{{lc}}}, text }) => {
3131
<div>
3232
<Head>
3333
<title>{`Show {{{ucf}}} ${ {{~lc}}['@id']}`}</title>
34-
<script
35-
type="application/ld+json"
36-
>
37-
{text}
38-
</script>
34+
<script type="application/ld+json" dangerouslySetInnerHTML={ { __html: text } } />
3935
</Head>
4036
<h1>{`Show {{{ucf}}} ${ {{~lc}}['@id']}`}</h1>
4137
<table className="table table-responsive table-striped table-hover">

templates/next/pages/foos/[id]/edit.tsx

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { GetStaticPaths, GetStaticProps, NextComponentType, NextPageContext } from "next";
2-
import { Form } from "../../../components/{{{lc}}}/Form";
3-
import { {{{ucf}}} } from "../../../types/{{{ucf}}}";
4-
import { fetch } from "../../../utils/dataAccess";
52
import Head from "next/head";
63
import DefaultErrorPage from "next/error";
7-
import { getPathsFromHydraResponse } from "../../../utils/helpers";
4+
import { Form } from "../../../components/{{{lc}}}/Form";
5+
import { {{{ucf}}} } from "../../../types/{{{ucf}}}";
6+
import { fetch, getPaths } from "../../../utils/dataAccess";
87

98
interface Props {
109
{{{lc}}}: {{{ucf}}};
@@ -38,15 +37,14 @@ export const getStaticProps: GetStaticProps = async ({ params }) => {
3837
};
3938
}
4039

41-
42-
export const getStaticPaths: GetStaticPaths = async() => {
40+
export const getStaticPaths: GetStaticPaths = async () => {
4341
const response = await fetch("/{{{name}}}");
44-
const paths= await getPathsFromHydraResponse(response,true);
45-
return {
46-
paths,
47-
fallback:true
48-
}
42+
const paths = await getPaths(response, "{{{name}}}", true);
4943

44+
return {
45+
paths,
46+
fallback: true,
47+
};
5048
}
5149

5250
export default Page;
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import { GetStaticPaths, GetStaticProps, NextComponentType, NextPageContext } from "next";
2-
import { Show } from "../../../components/{{{lc}}}/Show";
3-
import { {{{ucf}}} } from "../../../types/{{{ucf}}}";
4-
import { fetch } from "../../../utils/dataAccess";
52
import Head from "next/head";
63
import DefaultErrorPage from "next/error";
4+
import { Show } from "../../../components/{{{lc}}}/Show";
5+
import { {{{ucf}}} } from "../../../types/{{{ucf}}}";
6+
import { fetch, getPaths } from "../../../utils/dataAccess";
77
import { useMercure } from "../../../utils/mercure";
8-
import { getPathsFromHydraResponse } from "../../../utils/helpers";
98

109
interface Props {
1110
{{{lc}}}: {{{ucf}}};
1211
hubURL: null | string;
12+
text: string;
1313
};
1414

15-
const Page: NextComponentType<NextPageContext, Props, Props> = (props) => {
16-
const {{{lc}}} = props.hubURL === null ? props.{{{lc}}} : useMercure(props.{{{lc}}}, props.hubURL);
15+
const Page: NextComponentType<NextPageContext, Props, Props> = ({ {{{lc}}}, hubURL, text }) => {
16+
const {{{lc}}}Data = useMercure({{{lc}}}, hubURL);
1717

18-
if (!{{{lc}}}) {
18+
if (!{{{lc}}}Data) {
1919
return <DefaultErrorPage statusCode={404} />;
2020
}
2121

@@ -26,7 +26,7 @@ const Page: NextComponentType<NextPageContext, Props, Props> = (props) => {
2626
<title>{`Show {{{ucf}}} ${ {{~lc}}['@id'] }`}</title>
2727
</Head>
2828
</div>
29-
<Show {{{lc}}}={ {{{lc}}} } text={ data.text } />
29+
<Show {{{lc}}}={ {{{lc}}}Data } text={text} />
3030
</div>
3131
);
3232
};
@@ -43,12 +43,15 @@ export const getStaticProps: GetStaticProps = async ({ params }) => {
4343
revalidate: 1,
4444
};
4545
}
46-
export const getStaticPaths: GetStaticPaths = async() => {
46+
47+
export const getStaticPaths: GetStaticPaths = async () => {
4748
const response = await fetch("/{{{name}}}");
48-
const paths= await getPathsFromHydraResponse(response,false);
49+
const paths = await getPaths(response, "{{{name}}}", false);
50+
4951
return {
5052
paths,
51-
fallback:true
52-
}
53+
fallback: true,
54+
};
5355
}
56+
5457
export default Page;

templates/next/utils/dataAccess.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,26 @@ export const normalize = (data: unknown) => {
7575
: get(value, "@id", value)
7676
);
7777
};
78+
79+
export const getPaths = async (response, resourceName: string, isEdit: boolean) => {
80+
try {
81+
const pathSuffix = isEdit ? "/edit" : "";
82+
const view = response.data["{{{hydraPrefix}}}view"];
83+
const paths = response.data["{{{hydraPrefix}}}member"].map((resourceData) => `${resourceData['@id']}${pathSuffix}`);
84+
85+
if (view) {
86+
const { '{{{hydraPrefix}}}last': last } = view;
87+
for (let page = 2; page <= parseInt(last.replace(new RegExp(`^\/${resourceName}\?page=(\d+)`), "$1")); page++) {
88+
paths.concat(
89+
(await fetch(`/${resourceName}?page=${page}`)).data["{{{hydraPrefix}}}member"].map((resourceData) => `${ resourceData['@id'] }${pathSuffix}`)
90+
);
91+
}
92+
}
93+
94+
return paths;
95+
} catch (e) {
96+
console.error(e);
97+
98+
return [];
99+
}
100+
};

templates/next/utils/helpers.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

templates/next/utils/mercure.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import has from "lodash/has";
21
import { useEffect, useState } from "react";
2+
import has from "lodash/has";
33
import { PagedCollection } from "../types/Collection";
44
import { normalize } from "./dataAccess";
55

@@ -12,24 +12,24 @@ const mercureSubscribe = (hubURL: string, data: unknown | PagedCollection<unknow
1212
return eventSource;
1313
}
1414

15-
export const useMercure = (deps: unknown | PagedCollection<unknown>, hubURL: string) => {
15+
export const useMercure = (deps: unknown | PagedCollection<unknown>, hubURL: string | null) => {
1616
const [data, setData] = useState(deps);
1717

1818
useEffect(() => {
1919
setData(deps);
2020
}, [deps]);
2121

22-
if (!data) {
23-
return data;
24-
}
22+
useEffect(() => {
23+
if (!hubURL || !data) {
24+
return;
25+
}
2526

26-
if (!has(data, "{{{hydraPrefix}}}member") && !has(data, "@id")) {
27-
console.error("Object sent is not in JSON-LD format.");
27+
if (!has(data, "{{{hydraPrefix}}}member") && !has(data, "@id")) {
28+
console.error("Object sent is not in JSON-LD format.");
2829

29-
return data;
30-
}
30+
return;
31+
}
3132

32-
useEffect(() => {
3333
if (has(data, "{{{hydraPrefix}}}member") && Array.isArray(data["{{{hydraPrefix}}}member"]) && data["{{{hydraPrefix}}}member"].length !== 0) {
3434
// It's a PagedCollection
3535
data["{{{hydraPrefix}}}member"].forEach((obj, pos) => mercureSubscribe(hubURL, obj, (datum) => {

0 commit comments

Comments
 (0)