Skip to content

Commit

Permalink
Merge pull request #1943 from ThemeFuse/24103-getDCGroupsHandler
Browse files Browse the repository at this point in the history
feat(dynamic_content): added handler to get dc groups
  • Loading branch information
maxval1 committed Feb 12, 2024
2 parents a19bb42 + b32c051 commit 28b59c7
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 3 deletions.
36 changes: 36 additions & 0 deletions public/editor-client/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getConfig } from "../config";
import { ConfigDCItem } from "../types/DynamicContent";
import { Page } from "../types/Page";
import { Rule } from "../types/PopupConditions";
import { Project } from "../types/Project";
Expand Down Expand Up @@ -851,3 +852,38 @@ export const updateBlockScreenshot = async ({
};

//#endregion

//#region Dynamic Content

export const getPlaceholders = (extraData: {
entityType: string;
groupType: string;
}): Promise<ConfigDCItem[]> => {
const config = getConfig();

if (!config) {
throw new Error(t("Invalid __BRZ_PLUGIN_ENV__"));
}

const { entityType, groupType } = extraData;
const { editorVersion, url: _url, hash, actions } = config;

const url = makeUrl(_url, {
post: entityType ?? "",
hash,
action: actions.getDynamicContentPlaceholders,
version: editorVersion
});

return request(url, { method: "GET" })
.then((r) => r.json())
.then(({ data }) => {
if (Array.isArray(data[groupType])) {
return data[groupType];
}

return [];
});
};

//#endregion
13 changes: 11 additions & 2 deletions public/editor-client/src/collectionTypes/loadCollectionTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,24 @@ import { t } from "../utils/i18n";
import { getCollectionTypes } from "./utils";

export const loadCollectionTypes = {
async handler(res: Response<ChoicesSync>, rej: Response<string>) {
async handler(
res: Response<ChoicesSync>,
rej: Response<string>,
extraData?: { defaultTitle?: string; defaultValue?: string }
) {
try {
const collectionTypes = getCollectionTypes();

if (!collectionTypes) {
return rej(t("Missing collectionTypes in config"));
}

const items = [{ title: t("None"), value: "" }, ...collectionTypes];
const { defaultTitle, defaultValue } = extraData ?? {};

const items = [
{ title: defaultTitle ?? t("None"), value: defaultValue ?? "" },
...collectionTypes
];

res(items);
} catch (e) {
Expand Down
6 changes: 6 additions & 0 deletions public/editor-client/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ interface Actions {

createBlockScreenshot: string;
updateBlockScreenshot: string;

getDynamicContentPlaceholders: string;
}

interface API {
Expand Down Expand Up @@ -194,6 +196,10 @@ const actionsReader = parseStrict<PLUGIN_ENV["actions"], Actions>({
updateBlockScreenshot: pipe(
mPipe(Obj.readKey("updateBlockScreenshot"), Str.read),
throwOnNullish("Invalid actions: updateBlockScreenshot")
),
getDynamicContentPlaceholders: pipe(
mPipe(Obj.readKey("getDynamicContentPlaceholders"), Str.read),
throwOnNullish("Invalid actions: getDynamicContentPlaceholders")
)
});

Expand Down
13 changes: 13 additions & 0 deletions public/editor-client/src/dynamicContent/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { getPlaceholders } from "../api";
import { DCHandler } from "../types/DynamicContent";
import { t } from "../utils/i18n";

export const placeholders: DCHandler = async (res, rej, extraData) => {
try {
const placeholders = await getPlaceholders(extraData);

res(placeholders);
} catch (e) {
rej(t("Failed to load dynamic content placeholders"));
}
};
8 changes: 7 additions & 1 deletion public/editor-client/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import set from "lodash/set";
import { handler as posts } from "./Elements/Posts";
import { doAiRequest } from "./aiText";
import { autoSave } from "./autoSave";
import { getCollectionItemsIds } from "./collectionItems/getCollectionItemsIds";
Expand All @@ -13,6 +12,8 @@ import {
defaultPopups,
defaultStories
} from "./defaultTemplates";
import { placeholders } from "./dynamicContent";
import { handler as posts } from "./Elements/Posts";
import { addMedia } from "./media/addMedia";
import { addMediaGallery } from "./media/addMediaGallery";
import { onChange } from "./onChange";
Expand Down Expand Up @@ -79,4 +80,9 @@ if (window.__VISUAL_CONFIG__) {
} else {
set(window.__VISUAL_CONFIG__, ["elements", "posts", "handler"], posts);
}

// Dynamic Content
if (window.__VISUAL_CONFIG__.dynamicContent) {
set(window.__VISUAL_CONFIG__.dynamicContent, ["handler"], placeholders);
}
}
32 changes: 32 additions & 0 deletions public/editor-client/src/types/DynamicContent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Literal } from "../utils/types";
import { Response } from "./Response";

export enum DCTypes {
image = "image",
link = "link",
richText = "richText",
reference = "reference",
multiReference = "multiReference"
}

interface BaseDCItem {
label: string;
placeholder: string;
alias?: string;
display?: "block" | "inline";
attr?: Record<string, Literal>;
}

export interface ConfigDCItem extends BaseDCItem {
optgroup?: ConfigDCItem[];
}

export type DCConfigItems = {
[k in DCTypes]: ConfigDCItem[];
};

export type DCHandler = (
res: Response<ConfigDCItem[]>,
rej: Response<string>,
extraData: { entityType: string; groupType: string }
) => void;
7 changes: 7 additions & 0 deletions public/editor-client/src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
PopupsWithThumbs,
StoriesWithThumbs
} from "./DefaultTemplate";
import { DCHandler } from "./DynamicContent";
import { AddFileData } from "./File";
import { AddMediaData, AddMediaGallery } from "./Media";
import { OnChange } from "./OnChange";
Expand Down Expand Up @@ -167,6 +168,12 @@ export interface VISUAL_CONFIG {
};

//#endregion

//#region Dynamic Content
dynamicContent?: {
handler?: DCHandler;
};
//#endregion
}

declare global {
Expand Down

0 comments on commit 28b59c7

Please sign in to comment.