Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,20 @@ export default function EnableEditor(props: BuilderEditorProps) {
'initpreviewingbldr',
() => {
const searchParams = new URL(location.href).searchParams;
const builderPreviewSearchParams = searchParams.get('builder.preview');
if (builderPreviewSearchParams === 'BUILDER_STUDIO') {
searchParams.set('builder.preview', props.model || '');
}

const searchParamPreviewModel = searchParams.get('builder.preview');

if (builderPreviewSearchParams === 'BUILDER_STUDIO') {
searchParams.set(
`builder.overrides.${searchParamPreviewModel}`,
props.content?.id || ''
);
}

const searchParamPreviewId = searchParams.get(
`builder.overrides.${searchParamPreviewModel}`
);
Expand All @@ -305,7 +318,8 @@ export default function EnableEditor(props: BuilderEditorProps) {
* TO-DO: should we only update the state when there is a change?
**/
if (
searchParamPreviewModel === props.model &&
(searchParamPreviewModel === props.model ||
searchParamPreviewModel === 'BUILDER_STUDIO') &&
previewApiKey === props.apiKey &&
(!props.content || searchParamPreviewId === props.content.id)
) {
Expand Down
26 changes: 18 additions & 8 deletions packages/sdks/src/functions/get-builder-search-params/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,39 @@ const BUILDER_OPTIONS_PREFIX = 'options.';
* @returns
*/
export const getBuilderSearchParams = (
_options: QueryObject | URLSearchParams | undefined
_options: QueryObject | URLSearchParams | undefined,
extraArgs?: { model: string }
) => {
if (!_options) {
return {};
}
const options = normalizeSearchParams(_options);

const newOptions: QueryObject = {};
Object.keys(options).forEach((key) => {
if (key.startsWith(BUILDER_SEARCHPARAMS_PREFIX)) {
const trimmedKey = key
.replace(BUILDER_SEARCHPARAMS_PREFIX, '')
.replace(BUILDER_OPTIONS_PREFIX, '');
newOptions[trimmedKey] = options[key];
if (key === 'builder.preview' && options[key] === 'BUILDER_STUDIO') {
newOptions['preview'] = extraArgs?.model || '';
} else if (key === 'builder.userAttributes.date') {
const date = new Date(options[key] as string);
newOptions['query.startDate.$lte'] = `${date.getTime()}`;
newOptions['query.endDate.$gte'] = `${date.getTime()}`;
} else {
const trimmedKey = key
.replace(BUILDER_SEARCHPARAMS_PREFIX, '')
.replace(BUILDER_OPTIONS_PREFIX, '');
newOptions[trimmedKey] = options[key];
}
}
});
return newOptions;
};

export const getBuilderSearchParamsFromWindow = () => {
export const getBuilderSearchParamsFromWindow = (extraArgs?: {
model: string;
}) => {
if (!isBrowser()) {
return {};
}
const searchParams = new URLSearchParams(window.location.search);
return getBuilderSearchParams(searchParams);
return getBuilderSearchParams(searchParams, extraArgs);
};
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ export const generateContentUrl = (options: GetContentOptions): URL => {
// TODO: how to express 'offset' in the url - as direct queryparam or as flattened in options[key] ?

const queryOptions = {
...getBuilderSearchParamsFromWindow(),
...getBuilderSearchParamsFromWindow({
model: options.model,
}),
...normalizeSearchParams(options.options || {}),
};

Expand Down