diff --git a/public/editor-client/src/api/index.ts b/public/editor-client/src/api/index.ts index b53628f339..36117423e8 100644 --- a/public/editor-client/src/api/index.ts +++ b/public/editor-client/src/api/index.ts @@ -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, @@ -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, @@ -1262,3 +1263,34 @@ export const updateGlobalBlocks = async ( }; //#endregion + +// region Default Pages +export const getDefaultPages = async ( + url: string +): Promise<{ + blocks: PagesAPI[]; + categories: Record; +}> => { + 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 diff --git a/public/editor-client/src/config.ts b/public/editor-client/src/config.ts index 9176f937e6..e0c9a9b8ae 100644 --- a/public/editor-client/src/config.ts +++ b/public/editor-client/src/config.ts @@ -12,6 +12,7 @@ interface DefaultTemplates { popupsUrl: string; storiesUrl: string; layoutsUrl: string; + templatesUrl: string; } interface Actions { @@ -74,6 +75,7 @@ interface API { fileUrl: string; templates: DefaultTemplates; openAIUrl?: string; + templatesImageUrl: string; imagePatterns: ImagePatterns; } export interface Config { @@ -104,6 +106,10 @@ const templatesReader = parseStrict, 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") ) }); @@ -137,6 +143,10 @@ const apiReader = parseStrict({ 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"), diff --git a/public/editor-client/src/defaultTemplates/index.ts b/public/editor-client/src/defaultTemplates/index.ts index 5090642a89..a257467d14 100644 --- a/public/editor-client/src/defaultTemplates/index.ts +++ b/public/editor-client/src/defaultTemplates/index.ts @@ -1,3 +1,4 @@ +import { getDefaultPage, getDefaultPages } from "@/api"; import { Config } from "../config"; import { BlocksArray, @@ -11,6 +12,8 @@ import { KitsWithThumbs, Layouts, LayoutsWithThumbs, + Pages, + PagesDefaultTemplate, Popups, PopupsWithThumbs, Stories, @@ -18,6 +21,7 @@ import { } from "../types/DefaultTemplate"; import { t } from "../utils/i18n"; import { tempConverterKit } from "./tempComverters"; +import { convertPages, convertToCategories } from "./utils"; const defaultKits = ( config: Config @@ -288,4 +292,43 @@ const defaultLayouts = ( }; }; -export { defaultKits, defaultStories, defaultLayouts, defaultPopups }; +const defaultPages = ( + config: Config +): PagesDefaultTemplate> => { + 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 +}; diff --git a/public/editor-client/src/defaultTemplates/utils.ts b/public/editor-client/src/defaultTemplates/utils.ts index 2cfe123df0..aff9c498b5 100644 --- a/public/editor-client/src/defaultTemplates/utils.ts +++ b/public/editor-client/src/defaultTemplates/utils.ts @@ -1,387 +1,34 @@ -import { flatten, uniq, upperFirst } from "lodash"; -import { - APIPopup, - BlockWithThumbs, - Categories, - Kit, - KitType, - Style -} from "../types/DefaultTemplate"; -import { pipe } from "../utils/fp/pipe"; - -type CatTypes = Kit | APIPopup; - -export const getUniqueKitCategories = (collections: CatTypes[]): Categories[] => - pipe( - (collections: CatTypes[]) => - collections.map((collection: CatTypes) => collection.categories), - flatten, - uniq, - (cats) => - cats.map((cat) => ({ - title: upperFirst(cat), - slug: cat, - id: cat - })) - )(collections); - -export const getUniqueKitTypes = (collections: Kit[]): KitType[] => - pipe( - (collections: Kit[]) => collections.map((collection) => collection.theme), - flatten, - uniq, - (uni) => - uni.map((u) => ({ - title: upperFirst(u), - id: u, - name: u, - icon: "nc-light" - })) - )(collections); - -export const converterKit = ( - kit: Kit[], - url: string, - kitId: string -): { - blocks: BlockWithThumbs[]; - categories: Categories[]; - types: KitType[]; -} => { - const categories = getUniqueKitCategories(kit); - const types = getUniqueKitTypes(kit); - - const blocks: BlockWithThumbs[] = kit.map( - ({ - slug, - categories, - pro, - thumbnail, - keywords, - thumbnailWidth, - thumbnailHeight - }) => ({ - id: slug, - cat: [categories], - title: slug, - type: types[0].name, - keywords: keywords ?? "", - thumbnailHeight, - thumbnailWidth, - thumbnailSrc: `${url}${thumbnail}`, - pro: pro === "yes", - kitId - }) - ); - - return { - blocks, - categories, - types - }; -}; - -export const converterPopup = ( - kit: APIPopup[], - url: string, - kitId: string -): { - blocks: BlockWithThumbs[]; - categories: Categories[]; -} => { - const categories = getUniqueKitCategories(kit); - - const blocks: BlockWithThumbs[] = kit.map( +import { trim, upperFirst } from "lodash"; +import { Categories, PagesAPI, PagesTemplate } from "../types/DefaultTemplate"; + +export const convertPages = ( + collections: PagesAPI[], + thumbUrl: string +): PagesTemplate[] => + collections.map( ({ - id, title, categories, - pro, - thumbnail, thumbnailWidth, - thumbnailHeight - }) => ({ - id: id, - cat: [categories], - title: title, thumbnailHeight, - thumbnailWidth, - thumbnailSrc: `${url}${thumbnail}`, - pro: pro === "true", - keywords: "popup2", - position: 1, - type: 0, - kitId - }) + thumbnail, + slug + }) => { + return { + title: title, + slug: slug, + cat: categories.split(",").map(trim), + thumbnailWidth, + thumbnailHeight, + thumbnailSrc: `${thumbUrl}${thumbnail}` + }; + } ); - return { - blocks, - categories - }; -}; - -export const getStyles = (): Array