Skip to content

Commit

Permalink
WP stories
Browse files Browse the repository at this point in the history
  • Loading branch information
GunkaArtur committed Mar 13, 2024
1 parent f10764f commit b28625c
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 280 deletions.
85 changes: 58 additions & 27 deletions public/editor-client/src/defaultTemplates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
CustomTemplatePage,
DefaultBlock,
DefaultBlockWithID,
DefaultTemplate,
DefaultTemplateKits,
DefaultTemplatePopup,
KitItem,
Expand All @@ -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";
Expand Down Expand Up @@ -207,46 +208,76 @@ const defaultPopups = (

const defaultStories = (
config: Config
): DefaultTemplate<StoriesWithThumbs, BlocksArray<DefaultBlock>> => {
): LayoutsDefaultTemplate<
StoriesWithThumbs,
BlocksArray<DefaultBlock>,
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<StoriesAPI>(
`${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"));
}
}
};
};
Expand Down

0 comments on commit b28625c

Please sign in to comment.