Skip to content

Commit

Permalink
Restructure for dependency insertion and separation between data fetc…
Browse files Browse the repository at this point in the history
…hing and rendering (#49). Foundation for later extraction of the data fetching to a separate (NPM?) render-agnostic module.
  • Loading branch information
espen42 committed Sep 8, 2021
1 parent 991968b commit 5c63d8a
Show file tree
Hide file tree
Showing 16 changed files with 599 additions and 460 deletions.
419 changes: 309 additions & 110 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lint": "next/node_modules/.bin/next lint"
},
"dependencies": {
"next": "11.0.1",
"next": "^11.1.2",
"react": "17.0.2",
"react-dom": "17.0.2"
},
Expand Down
75 changes: 20 additions & 55 deletions src/components/BasePage.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import React, {useEffect, useState} from "react";
import {useRouter} from "next/router";

import {fetchContent} from "../shared/data/fetchContent";
import {getTemplate} from "./templateSelector";

import Custom500 from './errors/500';
import Custom404 from './errors/404';
import CustomError from './errors/Error';
import {g} from "../enonic-connection-config";

const BasePage = ({error, content, fetching}) => {
import DefaultPage from "../components/templates/Default";
import { pageSelector} from "../shared/pageSelector";

const selectPage = (contentType) => pageSelector[contentType] || DefaultPage;

export type BasePageProps = {
error?: {
code: string,
message: string
},
data?: {
type: string
}
fetching?: boolean
}

const BasePage = ({error, data, fetching}: BasePageProps) => {
if (error) {
switch (error.code) {
case 404:
Expand All @@ -24,56 +33,12 @@ const BasePage = ({error, content, fetching}) => {
return <p className="spinner">Fetching data...</p>
}

if (content) {
const TemplatePage = getTemplate(content.type);
return <TemplatePage {...content} />
if (data) {
const SelectedPage = selectPage(data.type);
return <SelectedPage {...data} />
}

return null;
};

export default BasePage;




////////////////////////////////////////////////////////////////////////

export const buildClientSideBasePage = (branch: string) => {
const ClientSideFetchingBasePage = () => {

const [props, setProps] = useState({error: null, content: null, fetching: false});

const router = useRouter();
const contentPath = router.query.contentPath;

useEffect(
() => {

const refresh = async (contentPath: string|string[]) => {
setProps(props => ({...props, fetching: true}));

const contentResult = await fetchContent(contentPath, branch);

// @ts-ignore
setProps(() => contentResult);

// Simulate server delay: comment out the setProps line above, and activate this:
/*setTimeout(() => {
// @ts-ignore
setProps(() => contentResult);
},
750);*/
};

if (contentPath !== undefined) {
refresh(contentPath);
}
},
[contentPath]
);

return <BasePage error={props.error} content={props.content} fetching={props.fetching}/>;
};
return ClientSideFetchingBasePage;
};
56 changes: 0 additions & 56 deletions src/components/blocks/Seo.tsx

This file was deleted.

49 changes: 0 additions & 49 deletions src/components/blocks/header.tsx

This file was deleted.

41 changes: 0 additions & 41 deletions src/components/blocks/layout.tsx

This file was deleted.

21 changes: 0 additions & 21 deletions src/components/templateSelector.ts

This file was deleted.

10 changes: 1 addition & 9 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
import '../styles/globals.css'
import type {AppProps} from 'next/app'
import Layout from '../components/blocks/layout'
import SEO from '../components/blocks/Seo'

function MyApp({Component, pageProps}: AppProps) {
return (
<>
<Layout siteTitle="Enonic ❤ Next.js">
<SEO title={pageProps.displayName} siteTitle="NextXP Poc"/>
<Component {...pageProps} />
</Layout>
</>
<Component {...pageProps} />
);
}

Expand Down
15 changes: 12 additions & 3 deletions src/pages/_draft/[[...contentPath]].tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import React from 'react';

import {fetchContent} from "../../shared/data/fetchContent";
import buildContentFetcher from "../../shared/data/fetchContent";
import { querySelector, variablesGetterSelector} from "../../shared/querySelector";

import BasePage, {buildClientSideBasePage} from "../../components/BasePage";
const fetchContent = buildContentFetcher({
querySelector,
variablesGetterSelector,
firstMethodKey: true
});


import BasePage from "../../components/BasePage";
import {buildClientSideBasePage} from "../../shared/clientSideBasePage";

const BRANCH = 'draft';

Expand Down Expand Up @@ -30,6 +39,6 @@ export default BasePage;

////////////////////////////////////////////////////////////////////////////////////////////// CLIENT: uncomment this instead of SSR above

const ClientSideFetchBasePage = buildClientSideBasePage(BRANCH);
const ClientSideFetchBasePage = buildClientSideBasePage(BRANCH, fetchContent);

export default ClientSideFetchBasePage;
15 changes: 12 additions & 3 deletions src/pages/_master/[[...contentPath]].tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import React from 'react';

import {fetchContent} from "../../shared/data/fetchContent";
import buildContentFetcher from "../../shared/data/fetchContent";
import { querySelector, variablesGetterSelector} from "../../shared/querySelector";

import BasePage, { buildClientSideBasePage } from "../../components/BasePage";
const fetchContent = buildContentFetcher({
querySelector,
variablesGetterSelector,
firstMethodKey: true
});


import BasePage from "../../components/BasePage";
import {buildClientSideBasePage} from "../../shared/clientSideBasePage";

const BRANCH = 'master';

Expand All @@ -26,7 +35,7 @@ export default BasePage;
////////////////////////////////////////////////////////////////////////////////////////////// CLIENT: uncomment this instead of SSR above
/*
const ClientSideFetchBasePage = buildClientSideBasePage(BRANCH);
const ClientSideFetchBasePage = buildClientSideBasePage(BRANCH, fetchContent);
export default ClientSideFetchBasePage;
*/
44 changes: 44 additions & 0 deletions src/shared/clientSideBasePage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React, {useEffect, useState} from "react";
import {useRouter} from "next/router";

import {Branch} from "./data/fetchContent";
import BasePage, {BasePageProps} from "../components/BasePage";

export const buildClientSideBasePage = (branch: Branch, fetchContent) => {
const ClientSideFetchingBasePage = () => {

const [props, setProps] = useState({error: null, content: null, fetching: false});

const router = useRouter();
const contentPath = router.query.contentPath;

useEffect(
() => {

const refresh = async (contentPath: string | string[]) => {
setProps(props => ({...props, fetching: true}));

const contentResult = await fetchContent(contentPath, branch);

// @ts-ignore
setProps(() => contentResult);

// Simulate server delay: comment out the setProps line above, and activate this:
/*setTimeout(() => {
// @ts-ignore
setProps(() => contentResult);
},
750);*/
};

if (contentPath !== undefined) {
refresh(contentPath);
}
},
[contentPath]
);

return <BasePage error={props.error} content = {props.content} fetching = {props.fetching} />;
};
return ClientSideFetchingBasePage;
};
Loading

0 comments on commit 5c63d8a

Please sign in to comment.