Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
pmelab committed Dec 25, 2023
1 parent 19e2937 commit 746b776
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 426 deletions.
4 changes: 2 additions & 2 deletions apps/decap/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PreviewDecapPageQuery } from '@custom/schema';
import { PreviewDecapPageQuery, ViewPageQuery } from '@custom/schema';
import { registerExecutor } from '@custom/ui';
import { Page } from '@custom/ui/routes/Page';
import CMS from 'decap-cms-app';
Expand Down Expand Up @@ -67,7 +67,7 @@ CMS.registerPreviewTemplate(
PreviewDecapPageQuery,
pageSchema,
(data) => {
registerExecutor(PreviewDecapPageQuery, data);
registerExecutor(ViewPageQuery, { page: data.preview });
return <Page id="preview" locale="en" />;
},
'previewDecapPage',
Expand Down
31 changes: 12 additions & 19 deletions apps/website/src/preview/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,21 @@ import { usePreviewParameters } from '../utils/preview';

const previewExecutor = drupalExecutor(
`${process.env.GATSBY_DRUPAL_URL}/graphql`,
false,
);

export default function PagePreview() {
const { nid, rid, lang } = usePreviewParameters();

registerExecutor(ViewPageQuery, () => {
return new Promise<ViewPageQuery>((resolve, reject) => {
if (nid && rid && lang) {
previewExecutor(PreviewDrupalPageQuery, {
id: nid,
locale: lang,
rid,
})
.then((result) => {
if (result.preview) {
resolve({ page: result.preview });
}
return;
})
.catch((error) => reject(error));
}
if (nid && rid && lang) {
registerExecutor(ViewPageQuery, async () => {
const data = await previewExecutor(PreviewDrupalPageQuery, {
id: nid,
locale: lang,
rid,
});
return { page: data.preview };
});
});
return <Page id="preview" locale="en" />;
return <Page id="preview" locale="en" />;
}
return null;
}
15 changes: 8 additions & 7 deletions apps/website/src/utils/drupal-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ export type OperationVariables<TQueryID extends OperationId<any, any>> =

/**
* Create an executor that operates against a Drupal endpoint.
* TODO: Fix typing after moving Operation types into executors package.
*/
export function drupalExecutor(endpoint: string) {
export function drupalExecutor(endpoint: string, forward: boolean = true) {
return async function <OperationId extends AnyOperationId>(
id: OperationId,
variables?: OperationVariables<OperationId>,
Expand All @@ -30,11 +29,13 @@ export function drupalExecutor(endpoint: string) {
const { data, errors } = await (
await fetch(url, {
credentials: 'include',
headers: {
'SLB-Forwarded-Proto': window.location.protocol.slice(0, -1),
'SLB-Forwarded-Host': window.location.hostname,
'SLB-Forwarded-Port': window.location.port,
},
headers: forward
? {
'SLB-Forwarded-Proto': window.location.protocol.slice(0, -1),
'SLB-Forwarded-Host': window.location.hostname,
'SLB-Forwarded-Port': window.location.port,
}
: {},
})
).json();
if (errors) {
Expand Down
5 changes: 3 additions & 2 deletions packages/schema/src/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,9 @@ input PaginationInput {
}

type Query {
previewDecapPage: Page
previewDrupalPage(id: ID!, rid: ID, locale: String!): Page
previewDecapPage: DecapPage
previewDrupalPage(id: ID!, rid: ID, locale: String!): DrupalPage
@fetchEntity(type: "node", id: "$id", rid: "$rid", language: "$locale")
mainNavigations: [MainNavigation] @gatsbyNodes(type: "MainNavigation")
footerNavigations: [FooterNavigation] @gatsbyNodes(type: "FooterNavigation")

Expand Down
Loading

0 comments on commit 746b776

Please sign in to comment.