diff --git a/public/editor-client/src/defaultTemplates/index.ts b/public/editor-client/src/defaultTemplates/index.ts index 405432219..de7d4f576 100644 --- a/public/editor-client/src/defaultTemplates/index.ts +++ b/public/editor-client/src/defaultTemplates/index.ts @@ -5,7 +5,6 @@ import { CustomTemplatePage, DefaultBlock, DefaultBlockWithID, - DefaultTemplate, DefaultTemplateKits, DefaultTemplatePopup, KitItem, @@ -18,13 +17,15 @@ import { LayoutsWithThumbs, Popups, PopupsWithThumbs, - Stories, - StoriesWithThumbs + StoriesAPI, + StoriesWithThumbs, + StoryPages } from "../types/DefaultTemplate"; import { t } from "../utils/i18n"; import { tempConverterKit } from "./tempConverters"; import { convertLayouts, + convertStories, fetchAllLayouts, getUniqueLayoutsCategories } from "./utils"; @@ -207,46 +208,76 @@ const defaultPopups = ( const defaultStories = ( config: Config -): DefaultTemplate> => { +): LayoutsDefaultTemplate< + StoriesWithThumbs, + BlocksArray, + LayoutsPages +> => { + // @ts-expect-error: temporary solution, wait until new API will come via config const { storiesUrl } = config.api.templates; + const apiLayoutsUrl = + "https://phplaravel-1109775-4184176.cloudwaysapps.com/api"; + const imageUrl = "https://cloud-1de12d.b-cdn.net/media/iW=1024&iH=1024/"; return { async getMeta(res, rej) { try { - const meta: Stories = await fetch(`${storiesUrl}/meta.json`).then((r) => - r.json() + const meta = await fetchAllLayouts( + `${apiLayoutsUrl}/get-story`, + 50 ); - const data = { - ...meta, - stories: meta.stories.map((story) => { - return { - ...story, - thumbnailSrc: `${storiesUrl}/thumbs/${story.pages[0].id}.jpg`, - pages: story.pages.map((page) => { - return { - ...page, - thumbnailSrc: `${storiesUrl}/thumbs/${page.id}.jpg` - }; - }) - }; - }) - }; + const converted = convertStories(meta, imageUrl); + const cats = getUniqueLayoutsCategories(meta); - res(data); + res({ + stories: converted, + categories: cats + }); } catch (e) { rej(t("Failed to load meta.json")); } }, - async getData(res, rej, id) { + async getData(res, rej, kit) { try { - const data = await fetch(`${storiesUrl}/resolves/${id}.json`).then( - (r) => r.json() - ); - res(data); + const data = await fetch( + `${apiLayoutsUrl}/get-story-page-data?project_id=${kit.layoutId}&page_slug=${kit.id}` + ).then((r) => r.json()); + + const pageData = Json.read(data.collection) as { + blocks: DefaultBlock[]; + }; + + res({ blocks: pageData.blocks }); } catch (e) { rej(t("Failed to load resolves for selected DefaultTemplate")); } + }, + async getPages(res, rej, id) { + try { + const data = await fetch( + `${apiLayoutsUrl}/get-story-page?project_id=${id}}&per_page=20` + ).then((r) => r.json()); + const parsedData: CustomTemplatePage[] = data.collections.map( + ({ + slug, + thumbnailHeight, + thumbnailWidth, + thumbnail + }: StoryPages) => ({ + id: slug, + title: slug, + thumbnailWidth, + thumbnailHeight, + thumbnailSrc: `${imageUrl}${thumbnail}`, + layoutId: id + }) + ); + + res({ pages: parsedData, styles: [data.styles] }); + } catch (e) { + rej(t("Failed to load pages for selected Stories")); + } } }; }; diff --git a/public/editor-client/src/defaultTemplates/utils.ts b/public/editor-client/src/defaultTemplates/utils.ts index 213b9fe11..1622611bd 100644 --- a/public/editor-client/src/defaultTemplates/utils.ts +++ b/public/editor-client/src/defaultTemplates/utils.ts @@ -7,11 +7,15 @@ import { KitType, LayoutsAPI, LayoutTemplateWithThumbs, - Style + StoriesAPI, + StoriesTemplateWithThumbs } from "../types/DefaultTemplate"; import { pipe } from "../utils/fp/pipe"; type CatTypes = Kit | APIPopup; +type LayStorType = LayoutsAPI | StoriesAPI; + +const BLANK_PAGE = "Blank"; export const getUniqueKitCategories = (collections: CatTypes[]): Categories[] => pipe( @@ -28,11 +32,11 @@ export const getUniqueKitCategories = (collections: CatTypes[]): Categories[] => )(collections); export const getUniqueLayoutsCategories = ( - collections: LayoutsAPI[] + collections: LayStorType[] ): Categories[] => pipe( - (collections: LayoutsAPI[]) => - collections.map((collection: LayoutsAPI) => collection.categories), + (collections: LayStorType[]) => + collections.map((collection: LayStorType) => collection.categories), (cats: string[]) => cats.map((it) => it.split(",")), flatten, (cats: string[]) => cats.map((cat) => cat.trim()), @@ -173,244 +177,36 @@ export const convertLayouts = ( } ); -export const getStyles = (): Array