Skip to content

Commit

Permalink
pages API
Browse files Browse the repository at this point in the history
  • Loading branch information
GunkaArtur committed Apr 26, 2024
1 parent dfd55f6 commit fc6ef12
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 384 deletions.
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

0 comments on commit fc6ef12

Please sign in to comment.