Skip to content

Commit

Permalink
connect to layouts to API
Browse files Browse the repository at this point in the history
  • Loading branch information
GunkaArtur committed Mar 26, 2024
1 parent daca336 commit 0e8026a
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 274 deletions.
87 changes: 63 additions & 24 deletions public/editor-client/src/defaultTemplates/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Json } from "@brizy/readers";
import { Config } from "../config";
import {
BlocksArray,
CustomTemplatePage,
DefaultBlock,
DefaultBlockWithID,
DefaultTemplate,
Expand All @@ -9,15 +11,18 @@ import {
KitItem,
Kits,
KitsWithThumbs,
Layouts,
LayoutsDefaultTemplate,
LayoutsPageAPI,
LayoutsPages,
LayoutsWithThumbs,
Popups,
PopupsWithThumbs,
Stories,
StoriesWithThumbs
} from "../types/DefaultTemplate";
import { t } from "../utils/i18n";
import { tempConverterKit } from "./tempComverters";
import { tempConverterKit } from "./tempConverters";
import { convertLayouts, convertToCategories, fetchAllLayouts1 } from "./utils";

const defaultKits = (
config: Config
Expand Down Expand Up @@ -243,47 +248,81 @@ const defaultStories = (

const defaultLayouts = (
config: Config
): DefaultTemplate<LayoutsWithThumbs, BlocksArray<DefaultBlockWithID>> => {
): LayoutsDefaultTemplate<
LayoutsWithThumbs,
BlocksArray<DefaultBlockWithID>,
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 apiLayoutsUrl1 =
"https://phplaravel-1109775-4184176.cloudwaysapps.com/api";
const apiLayoutsUrl = "https://j6dfq8pl41.b-cdn.net/api";

return {
async getMeta(res, rej) {
try {
const meta: Layouts = await fetch(`${layoutsUrl}/meta.json`).then((r) =>
r.json()
const meta = await fetchAllLayouts1(
`${apiLayoutsUrl1}/get-layouts-chunk`
);

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.templates, `${imageUrl}`),
categories: convertToCategories(meta.categories)
};

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.read(data[0].pageData) as {
items: DefaultBlockWithID[];
};

const result: BlocksArray<DefaultBlockWithID> = {
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}&per_page=20`
).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"));
}
}
};
};
Expand Down

0 comments on commit 0e8026a

Please sign in to comment.