Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support passing rootProps to resolveData #711

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/demo/config/blocks/Hero/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export const Hero: ComponentConfig<HeroProps> = {
*
* For example, requesting a third-party API for the latest content.
*/
resolveData: async ({ props }, { changed }) => {
resolveData: async ({ props }, { changed,rootProps }) => {
if (!props.quote)
return { props, readOnly: { title: false, description: false } };

Expand Down
6 changes: 4 additions & 2 deletions packages/core/lib/resolve-all-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export async function resolveAllData<
zones[zoneKey],
config,
onResolveStart,
onResolveEnd
onResolveEnd,
dynamicRoot.props
)) as Content<Props>;
}

Expand All @@ -45,7 +46,8 @@ export async function resolveAllData<
defaultedData.content,
config,
onResolveStart,
onResolveEnd
onResolveEnd,
dynamicRoot.props
)) as Content<Props>,
zones: resolvedZones,
} as Data<Props, RootProps>;
Expand Down
16 changes: 7 additions & 9 deletions packages/core/lib/resolve-component-data.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ComponentData, Config, MappedItem } from "../types";
import { AsFieldProps, ComponentData, Config, DefaultRootFieldProps, MappedItem } from "../types";
import { getChanged } from "./get-changed";

export const cache: {
Expand All @@ -9,26 +9,24 @@ export const resolveAllComponentData = async (
content: MappedItem[],
config: Config,
onResolveStart?: (item: MappedItem) => void,
onResolveEnd?: (item: MappedItem) => void
onResolveEnd?: (item: MappedItem) => void,
rootProps?: Record<string, any>
) => {
return await Promise.all(
content.map(async (item) => {
return await resolveComponentData(
item,
config,
onResolveStart,
onResolveEnd
onResolveEnd,
rootProps
);
})
);
};

export const resolveComponentData = async (
item: ComponentData,
config: Config,
onResolveStart?: (item: MappedItem) => void,
onResolveEnd?: (item: MappedItem) => void
) => {
item: ComponentData, config: Config, onResolveStart?: (item: MappedItem) => void, onResolveEnd?: (item: MappedItem) => void, rootProps?: Record<string, any>) => {
const configForItem = config.components[item.type];
if (configForItem.resolveData) {
const { item: oldItem = null, resolved = {} } =
Expand All @@ -45,7 +43,7 @@ export const resolveComponentData = async (
}

const { props: resolvedProps, readOnly = {} } =
await configForItem.resolveData(item, { changed, lastData: oldItem });
await configForItem.resolveData(item, { changed, lastData: oldItem ,rootProps});

const { readOnly: existingReadOnly = {} } = item || {};

Expand Down
5 changes: 3 additions & 2 deletions packages/core/lib/use-resolved-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const useResolvedData = (
const runResolvers = async () => {
// Flatten zones
const newData = newAppState.data;

const rootProps = newData.root?.props || {};
const flatContent = flattenData(newData).filter(
(item) => !!config.components[item.type]?.resolveData
);
Expand Down Expand Up @@ -111,7 +111,8 @@ export const useResolvedData = (
deferredSetStates[item.props.id];

_setComponentLoading(item.props.id, false);
}
},
rootProps
);

const dynamicDataMap = { [item.props.id]: dynamicData };
Expand Down
1 change: 1 addition & 0 deletions packages/core/types/Config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export type ComponentConfig<
params: {
changed: Partial<Record<keyof FieldProps, boolean>>;
lastData: DataShape | null;
rootProps?: Record<string, any>;
}
) =>
| Promise<{
Expand Down