Skip to content

Commit

Permalink
Setup config getter/setter (wpengine#945)
Browse files Browse the repository at this point in the history
  • Loading branch information
blakewilson authored Jul 22, 2022
1 parent ea72b90 commit 6de8204
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 10 deletions.
9 changes: 9 additions & 0 deletions examples/next/faust-nx/faustnx.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { setConfig } from 'faust-nx';
import templates from './wp-templates';

/**
* @type {import('faust-nx').FaustNXConfig}
**/
export default setConfig({
templates,
});
6 changes: 3 additions & 3 deletions examples/next/faust-nx/pages/[...wordpressNode].tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import 'faustnx.config';
import { getWordPressProps, WordPressTemplate } from 'faust-nx';
import { GetStaticPropsContext } from 'next';
import templates from 'wp-templates';
import client from 'client';

export default function Page(props: any) {
return <WordPressTemplate templates={templates} {...props} />;
return <WordPressTemplate {...props} />;
}

export function getStaticProps(ctx: GetStaticPropsContext) {
return getWordPressProps({ client, templates, ctx });
return getWordPressProps({ client, ctx });
}

export async function getStaticPaths() {
Expand Down
17 changes: 14 additions & 3 deletions packages/faust-nx/components/WordPressTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@ import { PropsWithChildren } from 'react';
import { getTemplate } from '../getTemplate';
import { WordPressTemplate } from '../getWordPressProps';
import { SeedNode } from '../queries/seedQuery';
import { getConfig } from '../config';

export type WordPressTemplateProps = PropsWithChildren<{
__SEED_NODE__: SeedNode;
templates: { [key: string]: WordPressTemplate };
}>;

export function WordPressTemplate(props: WordPressTemplateProps) {
const { __SEED_NODE__: seedNode, templates } = props;
const { templates } = getConfig();

if (!templates) {
throw new Error('Templates are required. Please add them to your config.');
}

const { __SEED_NODE__: seedNode } = props;
const template = getTemplate(seedNode, templates);

if (!template) {
Expand All @@ -25,10 +32,14 @@ export function WordPressTemplate(props: WordPressTemplateProps) {
res = useQuery(query, {
variables: variables ? variables(seedNode) : undefined,
ssr: true,
skip: !query
skip: !query,
});

const { data, error, loading } = res ?? {};

return React.cloneElement(<Component />, { ...props, data, error, loading }, null);
return React.cloneElement(
<Component />,
{ ...props, data, error, loading },
null,
);
}
15 changes: 15 additions & 0 deletions packages/faust-nx/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { WordPressTemplate } from '../getWordPressProps.js';

export interface FaustNXConfig {
templates: { [key: string]: WordPressTemplate };
}

let config = {};

export function setConfig(_config: FaustNXConfig) {
config = _config;
}

export function getConfig(): Partial<FaustNXConfig> {
return config;
}
15 changes: 13 additions & 2 deletions packages/faust-nx/getWordPressProps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { DocumentNode } from 'graphql'
import { SeedNode, SEED_QUERY } from './queries/seedQuery';
import { getTemplate } from './getTemplate';
import { addApolloState } from './client';
import { getConfig } from './config';

function isSSR(
ctx: GetServerSidePropsContext | GetStaticPropsContext,
Expand All @@ -19,12 +20,22 @@ export interface WordPressTemplate {

export interface getWordPressPropsConfig {
client: ApolloClient<NormalizedCacheObject>;
templates: {[key: string]: WordPressTemplate};
ctx: GetServerSidePropsContext | GetStaticPropsContext
}

export async function getWordPressProps(options: getWordPressPropsConfig) {
const { client, templates, ctx } = options;
const {templates} = getConfig()

if (!templates) {
throw new Error('Templates are required. Please add them to your config.');
}

const { client, ctx } = options;

if(!client) {
throw new Error('getWordPressProps: client is required. Please add it as a prop.')
}

let resolvedUrl = null;

if (!isSSR(ctx)) {
Expand Down
10 changes: 9 additions & 1 deletion packages/faust-nx/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { FaustNXProvider } from './components/FaustNXProvider';
import { WordPressTemplate } from './components/WordPressTemplate';
import { getWordPressProps } from './getWordPressProps';
import { getConfig, setConfig, FaustNXConfig } from './config/index.js';

export { FaustNXProvider, WordPressTemplate, getWordPressProps };
export {
FaustNXProvider,
WordPressTemplate,
getWordPressProps,
getConfig,
setConfig,
FaustNXConfig,
};
3 changes: 2 additions & 1 deletion packages/faust-nx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
},
"scripts": {
"test": "npm test",
"build": "tsc -p tsconfig.json"
"build": "tsc -p tsconfig.json",
"dev": "tsc -p tsconfig.json --watch"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 6de8204

Please sign in to comment.