Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
pmelab committed Dec 21, 2023
1 parent 21e71f9 commit c1498fa
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 39 deletions.
6 changes: 3 additions & 3 deletions apps/website/src/layouts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ export default function Layout({
// TODO: Remove duplication of queries here.
const data = useStaticQuery<FrameQuery>(graphql`
query Frame {
mainNavigation {
mainNavigation: mainNavigations {
...Navigation
}
footerNavigation {
footerNavigation: footerNavigations {
...Navigation
}
}
`);
registerOperator(() => data, FrameQuery);
registerOperator(data, FrameQuery);
return (
<IntlProvider locale={locale || 'en'}>
<Frame>{children}</Frame>
Expand Down
2 changes: 1 addition & 1 deletion apps/website/src/templates/decap-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default function DecapPageTemplate({
data,
pageContext,
}: PageProps<ViewPageQuery, SilverbackPageContext>) {
registerOperator(() => data, ViewPageQuery, {
registerOperator(data, ViewPageQuery, {
id: pageContext.id,
locale: pageContext.locale,
});
Expand Down
2 changes: 1 addition & 1 deletion apps/website/src/templates/drupal-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default function DrupalPageTemplate({
data,
pageContext,
}: PageProps<PageTemplateQuery, SilverbackPageContext>) {
registerOperator(() => data, ViewPageQuery, {
registerOperator(data, ViewPageQuery, {
id: pageContext.id,
locale: pageContext.locale,
});
Expand Down
4 changes: 2 additions & 2 deletions packages/schema/src/operations/Frame.gql
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
query Frame {
mainNavigation {
mainNavigation: mainNavigations {
...Navigation
}
footerNavigation {
footerNavigation: mainNavigations {
...Navigation
}
}
7 changes: 5 additions & 2 deletions packages/schema/src/operators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type VariablesMatcher<OperationId extends AnyOperationId> =
| ((vars: OperationVariables<OperationId>) => boolean);

type RegistryEntry = {
operator: Operator<AnyOperationId>;
operator: Operator<AnyOperationId> | OperationResult<AnyOperationId>;
id?: AnyOperationId;
variables?: VariablesMatcher<OperationVariables<AnyOperationId>>;
};
Expand Down Expand Up @@ -51,7 +51,7 @@ export function drupalOperator(endpoint: string) {
}

export function registerOperator<OperationId extends AnyOperationId>(
operator: Operator<OperationId>,
operator: Operator<OperationId> | OperationResult<OperationId>,
id?: OperationId,
variables?: VariablesMatcher<OperationVariables<OperationId>>,
) {
Expand Down Expand Up @@ -124,6 +124,9 @@ export function createOperator<OperationId extends AnyOperationId>(
.pop();

if (op) {
if (typeof op.operator !== 'function') {
return op.operator;
}
return () => op.operator(id, variables);
}
throw new OperatorRegistryError(id, variables);
Expand Down
7 changes: 3 additions & 4 deletions packages/schema/src/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ type DrupalPage implements ContentHubResultItem & Page
@type(id: "page") {
id: ID! @resolveEntityUuid
locale: Locale! @resolveEntityLanguage
translations: [Page] @resolveEntityTranslations
translations: [DrupalPage] @resolveEntityTranslations
path: Url! @resolveEntityPath @isPath
title: String! @resolveProperty(path: "title.value")
teaserImage: MediaImage
Expand Down Expand Up @@ -167,9 +167,8 @@ input PaginationInput {
type Query {
previewDecapPage: Page
previewDrupalPage(id: ID!, rid: ID, locale: String!): Page
mainNavigation: [MainNavigation] @gatsbyNodes(type: "MainNavigation")

footerNavigation: [FooterNavigation] @gatsbyNodes(type: "FooterNavigation")
mainNavigations: [MainNavigation] @gatsbyNodes(type: "MainNavigation")
footerNavigations: [FooterNavigation] @gatsbyNodes(type: "FooterNavigation")

allPages: [Page] @gatsbyNodes(type: "Page")
websiteSettings: WebsiteSettings
Expand Down
15 changes: 8 additions & 7 deletions packages/ui/src/components/Organisms/Footer.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ import { FrameQuery, Locale, Navigation, Url } from '@custom/schema';
import { Meta, StoryObj } from '@storybook/react';

import { buildOperator } from '../../helpers/operators';
import { Footer } from './Footer';
import { Footer as Component } from './Footer';

export default {
component: Footer,
component: Component,
parameters: {
layout: 'fullscreen',
},
} satisfies Meta<typeof Footer>;
excludeStories: /^Fixture/,
} satisfies Meta<typeof Component>;

export const FooterNavigationExample: Navigation = {
export const FixtureFooterNavigation: Navigation = {
locale: Locale.En,
items: [
{ title: 'About', target: '/about' as Url },
Expand All @@ -27,12 +28,12 @@ export const FooterNavigationExample: Navigation = {
})),
};

export const Default = {
export const Footer = {
parameters: {
operators: [
buildOperator(FrameQuery, () => ({
footerNavigation: [FooterNavigationExample],
footerNavigation: [FixtureFooterNavigation],
})),
],
},
} satisfies StoryObj<typeof Footer>;
} satisfies StoryObj<typeof Component>;
2 changes: 1 addition & 1 deletion packages/ui/src/components/Organisms/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useOperation } from '../../utils/operation';
function useFooterNavigation(lang: string = 'en') {
return (
useOperation(FrameQuery)
.data?.mainNavigation?.filter((nav) => nav?.locale === lang)
.data?.footerNavigation?.filter((nav) => nav?.locale === lang)
.pop()
?.items.filter(isTruthy) || []
);
Expand Down
11 changes: 6 additions & 5 deletions packages/ui/src/components/Organisms/Header.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ import { Meta, StoryObj } from '@storybook/react';
import { userEvent, within } from '@storybook/testing-library';

import { buildOperator } from '../../helpers/operators';
import Header from './Header';
import { Header } from './Header';

export default {
component: Header,
parameters: {
layout: 'fullscreen',
},
excludeStories: /^Fixture/,
} satisfies Meta<typeof Header>;

export const MainNavigationExample: Navigation = {
export const FixtureMainNavigation: Navigation = {
locale: Locale.En,
items: [
{
Expand Down Expand Up @@ -50,18 +51,18 @@ export const MainNavigationExample: Navigation = {
],
};

export const Default = {
export const Idle = {
parameters: {
operators: [
buildOperator(FrameQuery, () => ({
mainNavigation: [MainNavigationExample],
mainNavigation: [FixtureMainNavigation],
})),
],
},
} satisfies StoryObj<typeof Header>;

export const Expanded: StoryObj<typeof Header> = {
...Default,
...Idle,
play: async ({ canvasElement }) => {
const canvas = within(canvasElement);
const navigation = within(
Expand Down
8 changes: 4 additions & 4 deletions packages/ui/src/components/Organisms/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ import {
MobileMenuProvider,
} from '../Client/MobileMenu';

function useFooterNavigation(lang: string = 'en') {
function useHeaderNavigation(lang: string = 'en') {
return (
useOperation(FrameQuery)
.data?.footerNavigation?.filter((nav) => nav?.locale === lang)
.data?.mainNavigation?.filter((nav) => nav?.locale === lang)
.pop()
?.items.filter(isTruthy) || []
);
}

export default function Header() {
export function Header() {
const intl = useIntl();
const items = buildNavigationTree(useFooterNavigation(intl.locale));
const items = buildNavigationTree(useHeaderNavigation(intl.locale));

return (
<MobileMenuProvider>
Expand Down
8 changes: 4 additions & 4 deletions packages/ui/src/components/Routes/Frame.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { FrameQuery } from '@custom/schema';
import { Meta, StoryObj } from '@storybook/react';

import { buildOperator } from '../../helpers/operators';
import { FooterNavigationExample } from '../Organisms/Footer.stories';
import { MainNavigationExample } from '../Organisms/Header.stories';
import { FixtureFooterNavigation } from '../Organisms/Footer.stories';
import { FixtureMainNavigation } from '../Organisms/Header.stories';
import { Frame } from './Frame';

export default {
Expand All @@ -17,8 +17,8 @@ export const Default = {
parameters: {
operators: [
buildOperator(FrameQuery, () => ({
mainNavigation: [MainNavigationExample],
footerNavigation: [FooterNavigationExample],
mainNavigation: [FixtureMainNavigation],
footerNavigation: [FixtureFooterNavigation],
})),
],
},
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/Routes/Frame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AnimatePresence, useReducedMotion } from 'framer-motion';
import React, { PropsWithChildren } from 'react';

import { Footer } from '../Organisms/Footer';
import Header from '../Organisms/Header';
import { Header } from '../Organisms/Header';

export function Frame(props: PropsWithChildren<{}>) {
return (
Expand Down
18 changes: 14 additions & 4 deletions packages/ui/src/utils/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,27 @@ import {
OperationResult,
OperationVariables,
} from '@custom/schema';
import useSwr from 'swr';
import useSwr, { SWRResponse } from 'swr';

export function useOperation<TOperation extends AnyOperationId>(
operation: TOperation,
variables?: OperationVariables<TOperation>,
) {
return useSwr<OperationResult<TOperation>>(
): SWRResponse<OperationResult<TOperation>> {
const operator = createOperator(operation, variables);
const result = useSwr<OperationResult<TOperation>>(
[operation, variables],
createOperator(operation, variables),
typeof operator === 'function' ? operator : null,
{
suspense: false,
},
);
return typeof operator === 'function'
? result
: ({
data: operator,
error: undefined,
isValidating: false,
isLoading: false,
mutate: () => undefined as any,
} satisfies SWRResponse<OperationResult<TOperation>>);
}

0 comments on commit c1498fa

Please sign in to comment.