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

pages API #1986

Open
wants to merge 1 commit into
base: develop
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
38 changes: 35 additions & 3 deletions public/editor-client/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Arr, Obj, Str } from "@brizy/readers";
import { Config, getConfig } from "@/config";
import { DefaultBlock, PagesAPI } from "@/types/DefaultTemplate";
import { ConfigDCItem } from "@/types/DynamicContent";
import { GlobalBlock } from "@/types/GlobalBlocks";
import { Page } from "@/types/Page";
import { Rule } from "@/types/PopupConditions";
import { Project } from "@/types/Project";
import { ResponseWithBody } from "@/types/Response";
import { ConfigDCItem } from "@/types/DynamicContent";
import {
CreateSavedBlock,
CreateSavedLayout,
Expand All @@ -15,8 +15,9 @@ import {
SavedLayoutMeta
} from "@/types/SavedBlocks";
import { ScreenshotData } from "@/types/Screenshots";
import { Dictionary } from "../types/utils";
import { t } from "@/utils/i18n";
import { Arr, Json, Obj, Str } from "@brizy/readers";
import { Dictionary } from "../types/utils";
import { Literal } from "../utils/types";
import {
GetCollections,
Expand Down Expand Up @@ -1262,3 +1263,34 @@ export const updateGlobalBlocks = async (
};

//#endregion

// region Default Pages
export const getDefaultPages = async (
url: string
): Promise<{
blocks: PagesAPI[];
categories: Record<string, string>;
}> => {
const res = await fetch(`${url}/api/get-pages-chunk`).then((r) => r.json());

return {
blocks: res.collections,
categories: res.categories
};
};

export const getDefaultPage = async (
url: string,
slug: string
): Promise<{
items: DefaultBlock[];
}> => {
const data = await fetch(`${url}/api/get-page-data?page_slug=${slug}`).then(
(r) => r.json()
);

return Json.read(data[0].pageData) as {
items: DefaultBlock[];
};
};
// endregion
10 changes: 10 additions & 0 deletions public/editor-client/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface DefaultTemplates {
popupsUrl: string;
storiesUrl: string;
layoutsUrl: string;
templatesUrl: string;
}

interface Actions {
Expand Down Expand Up @@ -74,6 +75,7 @@ interface API {
fileUrl: string;
templates: DefaultTemplates;
openAIUrl?: string;
templatesImageUrl: string;
imagePatterns: ImagePatterns;
}
export interface Config {
Expand Down Expand Up @@ -104,6 +106,10 @@ const templatesReader = parseStrict<Record<string, unknown>, DefaultTemplates>({
storiesUrl: pipe(
mPipe(Obj.readKey("storiesUrl"), Str.read),
throwOnNullish("Invalid API Config: stories")
),
templatesUrl: pipe(
mPipe(Obj.readKey("templatesUrl"), Str.read),
throwOnNullish("Invalid API Config: templates")
)
});

Expand Down Expand Up @@ -137,6 +143,10 @@ const apiReader = parseStrict<PLUGIN_ENV["api"], API>({
throwOnNullish("Invalid API: templates")
),
openAIUrl: optional(pipe(mPipe(Obj.readKey("openAIUrl"), Str.read))),
templatesImageUrl: pipe(
mPipe(Obj.readKey("templatesImageUrl"), Str.read),
throwOnNullish("Invalid API: Image templates")
),
imagePatterns: pipe(
mPipe(
Obj.readKey("media"),
Expand Down
45 changes: 44 additions & 1 deletion public/editor-client/src/defaultTemplates/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getDefaultPage, getDefaultPages } from "@/api";
import { Config } from "../config";
import {
BlocksArray,
Expand All @@ -11,13 +12,16 @@ import {
KitsWithThumbs,
Layouts,
LayoutsWithThumbs,
Pages,
PagesDefaultTemplate,
Popups,
PopupsWithThumbs,
Stories,
StoriesWithThumbs
} from "../types/DefaultTemplate";
import { t } from "../utils/i18n";
import { tempConverterKit } from "./tempComverters";
import { convertPages, convertToCategories } from "./utils";

const defaultKits = (
config: Config
Expand Down Expand Up @@ -288,4 +292,43 @@ const defaultLayouts = (
};
};

export { defaultKits, defaultStories, defaultLayouts, defaultPopups };
const defaultPages = (
config: Config
): PagesDefaultTemplate<Pages, BlocksArray<DefaultBlock>> => {
const { templatesUrl } = config.api.templates;
const { templatesImageUrl } = config.api;

return {
async getMeta(res, rej) {
try {
const { blocks, categories } = await getDefaultPages(templatesUrl);

res({
blocks: convertPages(blocks, templatesImageUrl),
categories: convertToCategories(categories)
});
} catch (e) {
rej(t("Failed to load meta.json"));
}
},
async getData(res, rej, { slug }) {
try {
const pageData = await getDefaultPage(templatesUrl, slug);

res({
blocks: [...pageData.items]
});
} catch (e) {
rej(t("Failed to load resolves for selected DefaultTemplate"));
}
}
};
};

export {
defaultKits,
defaultStories,
defaultLayouts,
defaultPopups,
defaultPages
};
Loading