From 20feb9d55e5c6419f967d93c868953ae7fb4a913 Mon Sep 17 00:00:00 2001 From: Artur Gunka Date: Mon, 15 Jan 2024 16:55:29 +0200 Subject: [PATCH] connect to layouts to API --- brizy.php | 2 +- .../src/defaultTemplates/index.ts | 90 +++++++++++++------ .../{tempComverters.ts => tempConverters.ts} | 0 .../src/defaultTemplates/utils.ts | 77 ++++++++++++++++ .../src/types/DefaultTemplate.ts | 78 ++++++++++++++-- public/editor-client/src/types/global.d.ts | 7 +- 6 files changed, 217 insertions(+), 37 deletions(-) rename public/editor-client/src/defaultTemplates/{tempComverters.ts => tempConverters.ts} (100%) diff --git a/brizy.php b/brizy.php index fd3c4a1ef8..4e9caabf48 100755 --- a/brizy.php +++ b/brizy.php @@ -17,7 +17,7 @@ $_SERVER['HTTPS'] = 'on'; } -define('BRIZY_DEVELOPMENT', false ); +define('BRIZY_DEVELOPMENT', true ); define('BRIZY_LOG', false ); define('BRIZY_VERSION', '2.4.40'); define('BRIZY_MINIMUM_PRO_VERSION', '2.4.15'); diff --git a/public/editor-client/src/defaultTemplates/index.ts b/public/editor-client/src/defaultTemplates/index.ts index 5090642a89..97780c251c 100644 --- a/public/editor-client/src/defaultTemplates/index.ts +++ b/public/editor-client/src/defaultTemplates/index.ts @@ -1,6 +1,7 @@ import { Config } from "../config"; import { BlocksArray, + CustomTemplatePage, DefaultBlock, DefaultBlockWithID, DefaultTemplate, @@ -9,7 +10,10 @@ import { KitItem, Kits, KitsWithThumbs, - Layouts, + LayoutsAPI, + LayoutsDefaultTemplate, + LayoutsPageAPI, + LayoutsPages, LayoutsWithThumbs, Popups, PopupsWithThumbs, @@ -17,7 +21,12 @@ import { StoriesWithThumbs } from "../types/DefaultTemplate"; import { t } from "../utils/i18n"; -import { tempConverterKit } from "./tempComverters"; +import { tempConverterKit } from "./tempConverters"; +import { + convertLayouts, + fetchAllLayouts, + getUniqueLayoutsCategories +} from "./utils"; const defaultKits = ( config: Config @@ -243,47 +252,78 @@ const defaultStories = ( const defaultLayouts = ( config: Config -): DefaultTemplate> => { +): LayoutsDefaultTemplate< + LayoutsWithThumbs, + BlocksArray, + LayoutsPages +> => { + // @ts-expect-error: temporary solution, wait until new API will come via config const { layoutsUrl } = config.api.templates; + const imageUrl = "https://cloud-1de12d.b-cdn.net/media/iW=1024&iH=1024/"; + const apiLayoutsUrl = + "https://phplaravel-1109775-4184176.cloudwaysapps.com/api"; return { async getMeta(res, rej) { try { - const meta: Layouts = await fetch(`${layoutsUrl}/meta.json`).then((r) => - r.json() + const meta = await fetchAllLayouts( + `${apiLayoutsUrl}/get-layouts`, + 50 ); - const data = { - ...meta, - templates: meta.templates.map((item) => { - return { - ...item, - thumbnailSrc: `${layoutsUrl}/thumbs/${item.pages[0].id}.jpg`, - pages: item.pages.map((page) => { - return { - ...page, - thumbnailSrc: `${layoutsUrl}/thumbs/${page.id}.jpg` - }; - }) - }; - }) + const data: LayoutsWithThumbs = { + templates: convertLayouts(meta, imageUrl), + categories: getUniqueLayoutsCategories(meta) }; - res(data); } catch (e) { rej(t("Failed to load meta.json")); } }, - async getData(res, rej, id) { + async getData(res, rej, { id, layoutId }) { try { - const data = await fetch(`${layoutsUrl}/resolves/${id}.json`).then( - (r) => r.json() - ); + const data = await fetch( + `${apiLayoutsUrl}/get-layouts-page?project_id=${layoutId}&page_slug=${id}` + ).then((r) => r.json()); - res(data); + const pageData = JSON.parse(data[0].pageData); + + const result: BlocksArray = { + blocks: [...pageData.items] + }; + + res(result); } catch (e) { rej(t("Failed to load resolves for selected DefaultTemplate")); } + }, + async getPages(res, rej, id) { + try { + const data = await fetch( + `${apiLayoutsUrl}/get-layouts-pages?project_id=${id}` + ).then((r) => r.json()); + + const parsedData: CustomTemplatePage[] = data.collections.map( + ({ + slug, + title, + thumbnailHeight, + thumbnailWidth, + thumbs + }: LayoutsPageAPI) => ({ + id: slug, + title, + thumbnailWidth, + thumbnailHeight, + thumbnailSrc: `${imageUrl}${thumbs}`, + layoutId: id + }) + ); + + res({ pages: parsedData, styles: [data.styles] }); + } catch (e) { + rej(t("Failed to load pages for selected Layout")); + } } }; }; diff --git a/public/editor-client/src/defaultTemplates/tempComverters.ts b/public/editor-client/src/defaultTemplates/tempConverters.ts similarity index 100% rename from public/editor-client/src/defaultTemplates/tempComverters.ts rename to public/editor-client/src/defaultTemplates/tempConverters.ts diff --git a/public/editor-client/src/defaultTemplates/utils.ts b/public/editor-client/src/defaultTemplates/utils.ts index 2cfe123df0..213b9fe11b 100644 --- a/public/editor-client/src/defaultTemplates/utils.ts +++ b/public/editor-client/src/defaultTemplates/utils.ts @@ -5,6 +5,8 @@ import { Categories, Kit, KitType, + LayoutsAPI, + LayoutTemplateWithThumbs, Style } from "../types/DefaultTemplate"; import { pipe } from "../utils/fp/pipe"; @@ -25,6 +27,24 @@ export const getUniqueKitCategories = (collections: CatTypes[]): Categories[] => })) )(collections); +export const getUniqueLayoutsCategories = ( + collections: LayoutsAPI[] +): Categories[] => + pipe( + (collections: LayoutsAPI[]) => + collections.map((collection: LayoutsAPI) => collection.categories), + (cats: string[]) => cats.map((it) => it.split(",")), + flatten, + (cats: string[]) => cats.map((cat) => cat.trim()), + uniq, + (cats) => + cats.map((cat) => ({ + title: cat, + slug: cat, + id: cat + })) + )(collections); + export const getUniqueKitTypes = (collections: Kit[]): KitType[] => pipe( (collections: Kit[]) => collections.map((collection) => collection.theme), @@ -121,6 +141,38 @@ export const converterPopup = ( }; }; +export const convertLayouts = ( + collections: LayoutsAPI[], + thumbUrl: string +): LayoutTemplateWithThumbs[] => + collections.map( + ({ + title, + categories, + color, + pagesCount, + pro, + keywords, + thumbnailWidth, + thumbnailHeight, + thumbnail, + slug + }) => { + return { + name: title, + cat: categories.split(",").map((item) => item.trim()), + color, + pagesCount: Number(pagesCount), + pro: pro === "True", + keywords, + thumbnailWidth, + thumbnailHeight, + thumbnailSrc: `${thumbUrl}${thumbnail}`, + layoutId: slug + }; + } + ); + export const getStyles = (): Array