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

fix[gen2][builderStudio] ENG-7646 add Builder Studio support for Gen-2 SDK #3794

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
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 @@ -313,15 +313,27 @@ export default function EnableEditor(props: BuilderEditorProps) {
*
* TO-DO: should we only update the state when there is a change?
**/

console.log('DEBUG: searchParamPreviewModel', searchParamPreviewModel);
console.log('DEBUG: props.model', props.model);
console.log('DEBUG: previewApiKey', previewApiKey);
console.log('DEBUG: props.apiKey', props.apiKey);
console.log('DEBUG: props.content', props.content);
console.log('DEBUG: searchParamPreviewId', searchParamPreviewId);
console.log('DEBUG: props.content.id', props.content?.id);
if (
searchParamPreviewModel === props.model &&
previewApiKey === props.apiKey &&
(!props.content || searchParamPreviewId === props.content.id)
searchParamPreviewModel === 'BUILDER_STUDIO' ||
(searchParamPreviewModel === props.model &&
previewApiKey === props.apiKey &&
(!props.content || searchParamPreviewId === props.content.id))
) {
fetchOneEntry({
model: props.model,
apiKey: props.apiKey,
apiVersion: props.builderContextSignal.value.apiVersion,
...(searchParamPreviewModel === 'BUILDER_STUDIO' && props.content
? { query: { id: props.content?.id } }
: {}),
}).then((content) => {
if (content) {
state.mergeNewContent(content);
Expand Down Expand Up @@ -440,6 +452,8 @@ export default function EnableEditor(props: BuilderEditorProps) {
}
}, [props.locale]);

console.log('DEBUG enable-editor.lite.tsx props.model', props.model);

return (
<Show
when={
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { flatten, flattenMongoQuery } from '../../helpers/flatten.js';
import {
flatten,
flattenMongoQuery,
unflatten,
} from '../../helpers/flatten.js';
import { normalizeSearchParams } from '../../helpers/search/search.js';
import { DEFAULT_API_VERSION } from '../../types/api-version.js';
import { getBuilderSearchParamsFromWindow } from '../get-builder-search-params/index.js';
import { isBrowser } from '../is-browser.js';
import type { GetContentOptions } from './types.js';

const isPositiveNumber = (thing: unknown) =>
typeof thing === 'number' && !isNaN(thing) && thing >= 0;

export const generateContentUrl = (options: GetContentOptions): URL => {
console.log('DEBUG: options', options);
const {
limit = 30,
userAttributes,
Expand Down Expand Up @@ -96,6 +102,13 @@ export const generateContentUrl = (options: GetContentOptions): URL => {
...normalizeSearchParams(options.options || {}),
};

console.log('DEBUG: queryOptions', queryOptions);

finalUserAttributes = {
...finalUserAttributes,
...getUserAttributesAsJSON(queryOptions),
};

const flattened = flatten(queryOptions);
for (const key in flattened) {
url.searchParams.set(key, String(flattened[key]));
Expand All @@ -112,3 +125,24 @@ export const generateContentUrl = (options: GetContentOptions): URL => {
}
return url;
};

const getUserAttributesFromQueryOptions = (queryOptions: any) => {
const newUserAttributes: any = {};
for (const key in queryOptions) {
if (key.startsWith('userAttributes.')) {
newUserAttributes[key] = queryOptions[key];
delete queryOptions[key];
}
}
return newUserAttributes;
};

const getUserAttributesAsJSON = (queryOptions: any) => {
if (isBrowser()) {
queryOptions['userAttributes.urlPath'] = window.location.pathname;
}
const queryOptionsForUserAttributes =
getUserAttributesFromQueryOptions(queryOptions);
const { userAttributes } = unflatten(queryOptionsForUserAttributes);
return userAttributes;
};
1 change: 1 addition & 0 deletions packages/sdks/src/functions/get-content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type ContentResponse =

const _fetchContent = async (options: GetContentOptions) => {
const url = generateContentUrl(options);
console.log('DEBUG: _fetchContent url', url);
const _fetch = options.fetch ?? fetch;

const fetchOptions = {
Expand Down
23 changes: 23 additions & 0 deletions packages/sdks/src/helpers/flatten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,26 @@ export function flattenMongoQuery(
}
return _res;
}

/**
* Unflatten a flat object with dot-separated keys back into a nested object.
*
* { 'foo.bar': 'baz' } -> { foo: { bar: 'baz' }}
*/
export function unflatten(obj: any): any {
const result: any = {};
for (const key in obj) {
const parts = key.split('.');
let current = result;
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
if (i === parts.length - 1) {
current[part] = obj[key];
} else {
current[part] = current[part] || {};
current = current[part];
}
}
}
return result;
}
Loading