Skip to content

Commit f188d95

Browse files
fix: FineTuning & Fixe Bugs
1 parent 7d12fe1 commit f188d95

File tree

5 files changed

+47
-70
lines changed

5 files changed

+47
-70
lines changed

templates/next/components/foo/Show.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ import Head from 'next/head'
88

99
interface Props {
1010
{{{lc}}}: {{{ucf}}};
11+
text: String
1112
}
1213

13-
export const Show: FunctionComponent<Props> = ({ {{{lc}}} }) => {
14+
export const Show: FunctionComponent<Props> = ({ {{{lc}}}, text }) => {
1415
const [error, setError] = useState(null);
1516
const router = useRouter();
1617

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

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { {{{ucf}}} } from "../../../types/{{{ucf}}}";
44
import { fetch } from "../../../utils/dataAccess";
55
import Head from "next/head";
66
import DefaultErrorPage from "next/error";
7+
import { getPathsFromHydraResponse } from "../../../utils/helpers";
78

89
interface Props {
910
{{{lc}}}: {{{ucf}}};
@@ -27,48 +28,25 @@ const Page: NextComponentType<NextPageContext, Props, Props> = ({ {{{lc}}} }) =>
2728
};
2829

2930
export const getStaticProps: GetStaticProps = async ({ params }) => {
31+
const response = await fetch(`/{{{name}}}/${params.id}`);
32+
3033
return {
3134
props: {
32-
{{{lc}}}: await fetch(`/{{{name}}}/${params.id}`),
35+
{{{lc}}}: response.data,
3336
},
3437
revalidate: 1,
3538
};
3639
}
3740

38-
export const getStaticPaths: GetStaticPaths = async () => {
39-
try {
40-
const response = await fetch("/{{{name}}}");
41-
} catch (e) {
42-
console.error(e);
43-
44-
return {
45-
paths: [],
46-
fallback: true,
47-
};
48-
}
49-
50-
const view = response.data['{{{hydraPrefix}}}view'];
51-
const paths = response.data["{{{hydraPrefix}}}member"].map(({{{lc}}}) => `${ {{~lc}}['@id'] }/edit`);
5241

53-
if (view) {
54-
try {
55-
const {
56-
'{{{hydraPrefix}}}last': last
57-
} = view;
58-
for (let page = 2; page <= parseInt(last.replace(/^\/{{{name}}}\?page=(\d+)/, '$1')); page++) {
59-
paths.concat(
60-
await fetch(`/{{{name}}}?page=${page}`).data["{{{hydraPrefix}}}member"].map(({{{lc}}}) => `${ {{~lc}}['@id'] }/edit`)
61-
);
62-
}
63-
} catch (e) {
64-
console.error(e);
65-
}
42+
export const getStaticPaths: GetStaticPaths = async() => {
43+
const response = await fetch("/{{{name}}}");
44+
const paths= await getPathsFromHydraResponse(response,true);
45+
return {
46+
paths,
47+
fallback:true
6648
}
6749

68-
return {
69-
paths,
70-
fallback: true,
71-
};
7250
}
7351

7452
export default Page;

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

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { fetch } from "../../../utils/dataAccess";
55
import Head from "next/head";
66
import DefaultErrorPage from "next/error";
77
import { useMercure } from "../../../utils/mercure";
8+
import { getPathsFromHydraResponse } from "../../../utils/helpers";
89

910
interface Props {
1011
{{{lc}}}: {{{ucf}}};
@@ -25,7 +26,7 @@ const Page: NextComponentType<NextPageContext, Props, Props> = (props) => {
2526
<title>{`Show {{{ucf}}} ${ {{~lc}}['@id'] }`}</title>
2627
</Head>
2728
</div>
28-
<Show {{{lc}}}={ {{{lc}}} } text={ props.text } />
29+
<Show {{{lc}}}={ {{{lc}}} } text={ data.text } />
2930
</div>
3031
);
3132
};
@@ -42,41 +43,12 @@ export const getStaticProps: GetStaticProps = async ({ params }) => {
4243
revalidate: 1,
4344
};
4445
}
45-
46-
export const getStaticPaths: GetStaticPaths = async () => {
47-
try {
48-
const response = await fetch("/{{{name}}}");
49-
} catch (e) {
50-
console.error(e);
51-
52-
return {
53-
paths: [],
54-
fallback: true,
55-
};
56-
}
57-
58-
const view = response.data['{{{hydraPrefix}}}view'];
59-
const paths = response.data["{{{hydraPrefix}}}member"].map(({{{lc}}}) => `${ {{~lc}}['@id'] }`);
60-
61-
if (view) {
62-
try {
63-
const {
64-
'{{{hydraPrefix}}}last': last
65-
} = view;
66-
for (let page = 2; page <= parseInt(last.replace(/^\/{{{name}}}\?page=(\d+)/, '$1')); page++) {
67-
paths.concat(
68-
await fetch(`/{{{name}}}?page=${page}`).data["{{{hydraPrefix}}}member"].map(({{{lc}}}) => `${ {{~lc}}['@id'] }`)
69-
);
70-
}
71-
} catch (e) {
72-
console.error(e);
73-
}
74-
}
75-
46+
export const getStaticPaths: GetStaticPaths = async() => {
47+
const response = await fetch("/{{{name}}}");
48+
const paths= await getPathsFromHydraResponse(response,false);
7649
return {
7750
paths,
78-
fallback: true,
79-
};
51+
fallback:true
52+
}
8053
}
81-
8254
export default Page;

templates/next/utils/dataAccess.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,13 @@ export const fetch = async (id: string, init: RequestInit = {}) => {
3636
const resp = await isomorphicFetch(ENTRYPOINT + id, init);
3737
if (resp.status === 204) return;
3838

39-
const json = await resp.json();
39+
const text = await resp.text();
40+
const json = JSON.parse(text);
4041
if (resp.ok) {
4142
return {
4243
hubURL: extractHubURL(resp)?.toString(), // URL cannot be serialized as JSON, must be sent as string
4344
data: normalize(json),
44-
text: await resp.text(),
45+
text,
4546
};
4647
}
4748

templates/next/utils/helpers.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { fetch } from "./dataAccess";
2+
3+
export const getPathsFromHydraResponse = async (response,isEdit: boolean) => {
4+
try {
5+
const concatPath=isEdit?"/edit":"";
6+
const view = response.data['{{{hydraPrefix}}}view'];
7+
const paths = response.data["{{{hydraPrefix}}}member"].map(({{{lc}}}) => `${ {{~lc}}['@id'] }${concatPath}`);
8+
9+
if (view) {
10+
const {
11+
'{{{hydraPrefix}}}last': last
12+
} = view;
13+
for (let page = 2; page <= parseInt(last.replace(/^\/{{{name}}}\?page=(\d+)/, '$1')); page++) {
14+
paths.concat(
15+
await fetch(`/{{{name}}}?page=${page}`).data["{{{hydraPrefix}}}member"].map(({{{lc}}}) => `${ {{~lc}}['@id'] }${concatPath}`)
16+
);
17+
}
18+
}
19+
return paths
20+
} catch (e) {
21+
console.error(e);
22+
23+
return []
24+
}
25+
}

0 commit comments

Comments
 (0)