Skip to content

Commit

Permalink
#34: adapt rendering for relative-href <a> links and pure <img> tags
Browse files Browse the repository at this point in the history
  • Loading branch information
espen42 committed Sep 22, 2021
1 parent 2cdc91a commit 8f22ca1
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 53 deletions.
19 changes: 10 additions & 9 deletions src/components/BasePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import CustomError from './errors/Error';
import DefaultPage from "../components/pagetypes/_Default";
import {pageSelector} from "../selectors/pageSelector";

import {ResultMeta} from "../shared/data/fetchContent";


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

Expand All @@ -26,7 +26,7 @@ const errorPageSelector = {
'500': Custom500
}

const BasePage = ({type, data, error}: BasePageProps) => {
const BasePage = ({meta, data, error}: BasePageProps) => {
if (error) {
// @ts-ignore
const ErrorPage = errorPageSelector[error.code] || CustomError;
Expand All @@ -38,16 +38,17 @@ const BasePage = ({type, data, error}: BasePageProps) => {
return null;
}

if (!type) {
console.warn("BasePage props are missing 'type'. Falling back to default page type.");
if (!meta || !meta.type) {
console.warn("BasePage props are missing 'meta.type'. Falling back to default page type.");

} else if (type.startsWith('media:')) {
} else if (meta.type.startsWith('media:')) {
return null;
}

// @ts-ignore
const SelectedPage = pageSelector[type] || DefaultPage;
const SelectedPage = pageSelector[meta.type] || DefaultPage;
return <SelectedPage {...data} />;
};

export default BasePage;

9 changes: 4 additions & 5 deletions src/enonic-connection-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@ const getContentApiUrl = (branch, contentPathArray) => {

/**
*
* @param contentPathArray
* @param contentPathString {string}
* @return {string}
*/
const getFullContentPath = (contentPathArray) => {
console.log(contentPathArray)
const getFullContentPath = (contentPathString) => {
return (CONNECTION_CONFIG.siteName)
? `/${CONNECTION_CONFIG.siteName}/${contentPathArray.join("/")}`
: `/${contentPathArray.join("/")}`
? `/${CONNECTION_CONFIG.siteName}/${contentPathString}`
: `/${contentPathString}`
};

module.exports = {
Expand Down
16 changes: 10 additions & 6 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import '../styles/globals.css'
import type {AppProps} from 'next/app'
import Seo from '../components/blocks/seo'
import Layout from '../components/blocks/layout'
import Head from 'next/head';


function MyApp({Component, pageProps}: AppProps) {
//console.log("pageProps: ", pageProps);
console.log("pageProps: ", pageProps);
return (
<Layout siteTitle="Next.js PoC" random={pageProps.staticRandom}>
<Seo title="Poc" siteTitle="NextXP" />
<>
{
pageProps.meta &&
<Head>
<base href={`/_${pageProps.meta.branch}/${pageProps.meta.path}/`} />
</Head>
}
<Component {...pageProps} />
</Layout>
</>
);
}

Expand Down
1 change: 0 additions & 1 deletion src/pages/_draft/[[...contentPath]].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export const getServerSideProps = async (context: Context) => {
return {
props: {
...await fetchContent(context.params.contentPath, BRANCH),
staticRandom: await fetchRandom()
}
}
};
Expand Down
75 changes: 43 additions & 32 deletions src/shared/data/fetchContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import { LOW_PERFORMING_DEFAULT_QUERY } from "./queries/_getDefaultData";
import {QuerySelector, VariablesGetterFunc, VariablesGetterSelector} from "../../selectors/querySelector";


export type Branch = 'master' | 'draft';

export type ResultMeta = Meta & {
path: string,
branch: Branch
}

type Result = {
error?: {
Expand All @@ -17,14 +23,40 @@ type Result = {
}
}
export type ContentResult = Result & {
type?: string,
data?: any
data?: any,
meta: ResultMeta
};
type MetaResult = Result & {
meta?: Meta
};

export type Branch = 'master' | 'draft';


type FetcherConfig = {
querySelector?: QuerySelector,
variablesGetterSelector?: VariablesGetterSelector,
firstMethodKey?: boolean,
/*
apiConfig?: {
getGuillotineUrlDraft?: ApiGetterFunc,
getGuillotineUrlMaster?: ApiGetterFunc
},
*/
}
// type ApiGetterFunc = (appName:string) => string;


/**
* Sends one query to the guillotine API and asks for content type, then uses the type to select a second query and variables, which is sent to the API and fetches content data.
* @param contentPath string or string array: pre-split or slash-delimited _path to a content available on the API
* @param branch 'draft' or 'master'
* @returns ContentResult object: {data?: T, error?: {code, message}}
*/
export type ContentFetcher = (
contentPath: string | string[],
branch: Branch
) => Promise<ContentResult>




Expand Down Expand Up @@ -111,31 +143,6 @@ const verifyBranchOrThrow400 = (branch: Branch) => {

///////////////////////////////////////////////////////////////////////////////// Entry:

type FetcherConfig = {
querySelector?: QuerySelector,
variablesGetterSelector?: VariablesGetterSelector,
firstMethodKey?: boolean,
/*
apiConfig?: {
getGuillotineUrlDraft?: ApiGetterFunc,
getGuillotineUrlMaster?: ApiGetterFunc
},
*/
}
// type ApiGetterFunc = (appName:string) => string;


/**
* Sends one query to the guillotine API and asks for content type, then uses the type to select a second query and variables, which is sent to the API and fetches content data.
* @param contentPath string or string array: pre-split or slash-delimited _path to a content available on the API
* @param branch 'draft' or 'master'
* @returns ContentResult object: {data?: T, error?: {code, message}}
*/
export type ContentFetcher = (
contentPath: string | string[],
branch: Branch
) => Promise<ContentResult>



/**
Expand Down Expand Up @@ -188,15 +195,15 @@ const buildContentFetcher = ({querySelector, variablesGetterSelector, firstMetho
verifyBranchOrThrow400(branch);

const contentPathArray = getCleanContentPathArrayOrThrow400(contentPath);

const contentApiUrl = getContentApiUrl(branch, contentPathArray);
const fullContentPath = getFullContentPath(contentPathArray);
const contentPathString = contentPathArray.join("/");
const fullContentPath = getFullContentPath(contentPathString);


const metaResult = await fetchMetaData(contentApiUrl, fullContentPath);

if (metaResult.error) {
return {
return await {
error: metaResult.error
};
}
Expand Down Expand Up @@ -230,7 +237,11 @@ const buildContentFetcher = ({querySelector, variablesGetterSelector, firstMetho

return await {
...await fetchContentFull(contentApiUrl, fullContentPath, query, methodKeyFromQuery, variables),
type
meta: {
path: contentPathString,
type,
branch
}
};

} catch (e) {
Expand Down

0 comments on commit 8f22ca1

Please sign in to comment.