Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WP stories #1973

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 109 additions & 3 deletions public/editor-client/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { Arr, Obj, Str } from "@brizy/readers";
import { Config, getConfig } from "@/config";
import {
DefaultBlock,
DefaultBlockWithID,
LayoutsAPI,
LayoutsPageAPI,
StoriesAPI,
StoryPages,
Style
} 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,
Expand All @@ -15,8 +23,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,
Expand Down Expand Up @@ -1262,3 +1271,100 @@ export const updateGlobalBlocks = async (
};

//#endregion

//#region Default Templates
export const getDefaultLayouts = async (
url: string
): Promise<{
templates: LayoutsAPI[];
categories: { slug: string; title: string }[];
}> => {
const res = await fetch(`${url}/get-layouts-chunk`).then((r) => r.json());

return { templates: res.collections, categories: res.categories };
};

export const getDefaultLayoutsPages = async (
url: string,
id: string
): Promise<{
collections: LayoutsPageAPI[];
paginationInfo: {
itemsPerPage: number;
lastPage: number;
totalCount: number;
};
styles: Style;
}> => {
const data = await fetch(
`${url}/get-layouts-pages?project_id=${id}&per_page=20`
).then((r) => r.json());

return data;
};

export const getDefaultLayoutData = async (
url: string,
layoutId: Literal,
id: string
): Promise<{
items: DefaultBlockWithID[];
}> => {
const data = await fetch(
`${url}/get-layouts-page?project_id=${layoutId}&page_slug=${id}`
).then((r) => r.json());

return Json.read(data[0].pageData) as {
items: DefaultBlockWithID[];
};
Comment on lines +1317 to +1319
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove casting, because of casting we don't handle some errors

as example here Json.read can return undefined
image

but you cast is as object with items inside

};

export const getDefaultStories = async (
url: string
): Promise<{
templates: StoriesAPI[];
categories: { slug: string; title: string }[];
}> => {
const res = await fetch(`${url}/get-story-chunk`).then((r) => r.json());

return {
templates: res.collections,
categories: res.categories
};
};

export const getDefaultStory = async (
url: string,
layoutId: Literal,
id: string
): Promise<{
blocks: DefaultBlock[];
}> => {
const data = await fetch(
`${url}/get-story-page-data?project_id=${layoutId}&page_slug=${id}`
).then((r) => r.json());

const pageData = JSON.parse(data.collection);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use Json.read or wrap in try/catch json parsing


return pageData as {
blocks: DefaultBlock[];
};
Comment on lines +1349 to +1351
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove casting

if without casting is not possible then validate it, as example if pageData will be undefined/null then return empty array

};

export const getDefaultStoryPages = async (
url: string,
id: string
): Promise<{
collections: StoryPages[];
paginationInfo: {
itemsPerPage: number;
lastPage: number;
totalCount: number;
};
styles: Style;
}> => {
return await fetch(`${url}/get-story-page?project_id=${id}&per_page=20`).then(
(r) => r.json()
);
};
//#endregion
5 changes: 5 additions & 0 deletions public/editor-client/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface DefaultTemplates {
popupsUrl: string;
storiesUrl: string;
layoutsUrl: string;
templatesUrl: string;
}

interface Actions {
Expand Down Expand Up @@ -104,6 +105,10 @@ const templatesReader = parseStrict<Record<string, unknown>, 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")
)
});

Expand Down
157 changes: 109 additions & 48 deletions public/editor-client/src/defaultTemplates/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
import {
getDefaultLayoutData,
getDefaultLayouts,
getDefaultLayoutsPages,
getDefaultStories,
getDefaultStory,
getDefaultStoryPages
} from "@/api";
import { Config } from "../config";
import {
BlocksArray,
CustomTemplatePage,
DefaultBlock,
DefaultBlockWithID,
DefaultTemplate,
DefaultTemplateKits,
DefaultTemplatePopup,
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, convertStories, convertToCategories } from "./utils";

const defaultKits = (
config: Config
Expand Down Expand Up @@ -197,93 +207,144 @@ 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 getDefaultStories(apiLayoutsUrl);

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`
};
})
};
})
stories: convertStories(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, { layoutId, id }) {
try {
const data = await fetch(`${storiesUrl}/resolves/${id}.json`).then(
(r) => r.json()
);
res(data);
const { blocks } = await getDefaultStory(apiLayoutsUrl, layoutId, id);

res({ blocks });
} catch (e) {
rej(t("Failed to load resolves for selected DefaultTemplate"));
}
},
async getPages(res, rej, id) {
try {
const { collections, styles } = await getDefaultStoryPages(
apiLayoutsUrl,
id
);

const parsedData: CustomTemplatePage[] = collections.map(
({ slug, thumbnailHeight, thumbnailWidth, thumbnail }) => ({
id: slug,
title: slug,
thumbnailSrc: `${imageUrl}${thumbnail}`,
thumbnailHeight,
thumbnailWidth,
layoutId: id
})
);

res({ pages: parsedData, styles: [styles] });
} catch (e) {
rej(t("Failed to load pages for selected Stories"));
}
}
};
};

const defaultLayouts = (
config: Config
): DefaultTemplate<LayoutsWithThumbs, BlocksArray<DefaultBlockWithID>> => {
const { layoutsUrl } = config.api.templates;
): LayoutsDefaultTemplate<
LayoutsWithThumbs,
BlocksArray<DefaultBlockWithID>,
LayoutsPages
> => {
const { templatesUrl } = config.api.templates;
console.log("TESTING:FIX THIS BEFORE MERGE", templatesUrl);
const imageUrl = "https://cloud-1de12d.b-cdn.net/media/iW=1024&iH=1024/";
const apiLayoutsUrl1 =
"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 { templates, categories } = await getDefaultLayouts(
apiLayoutsUrl1
);

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(templates, `${imageUrl}`),
categories: convertToCategories(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 getDefaultLayoutData(
apiLayoutsUrl1,
// `${templatesUrl}/api`,
layoutId,
id
);

res(data);
const result: BlocksArray<DefaultBlockWithID> = {
blocks: [...data.items]
};

res(result);
} catch (e) {
rej(t("Failed to load resolves for selected DefaultTemplate"));
}
},
async getPages(res, rej, id) {
try {
const { collections, styles } = await getDefaultLayoutsPages(
apiLayoutsUrl1,
// `${templatesUrl}/api`,
id
);

const parsedData: CustomTemplatePage[] = collections.map(
({
slug,
title,
thumbnailHeight,
thumbnailWidth,
thumbs
}: LayoutsPageAPI) => ({
id: slug,
title,
thumbnailWidth,
thumbnailHeight,
thumbnailSrc: `${imageUrl}${thumbs}`,
layoutId: id
})
);

res({ pages: parsedData, styles: [styles] });
} catch (e) {
rej(t("Failed to load pages for selected Layout"));
}
}
};
};
Expand Down
Loading