Skip to content

Commit

Permalink
pages API
Browse files Browse the repository at this point in the history
  • Loading branch information
GunkaArtur committed Mar 12, 2024
1 parent c831750 commit b539713
Show file tree
Hide file tree
Showing 7 changed files with 235 additions and 3 deletions.
2 changes: 1 addition & 1 deletion brizy.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
$_SERVER['HTTPS'] = 'on';
}

define('BRIZY_DEVELOPMENT', false );
define('BRIZY_DEVELOPMENT', true );
define('BRIZY_LOG', false );
define('BRIZY_VERSION', '2.4.41');
define('BRIZY_MINIMUM_PRO_VERSION', '2.4.15');
Expand Down
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;
pagesUrl: string;
}

interface Actions {
Expand Down Expand Up @@ -99,6 +100,10 @@ const templatesReader = parseStrict<Record<string, unknown>, DefaultTemplates>({
mPipe(Obj.readKey("storiesUrl"), Str.read),
throwOnNullish("Invalid API Config: stories")
)
// pagesUrl: pipe(
// mPipe(Obj.readKey("pagesUrl"), Str.read),
// throwOnNullish("Invalid API Config: stories")
// )
});

const collectionTypesReader = (arr: Array<unknown>): Array<CollectionType> => {
Expand Down
59 changes: 58 additions & 1 deletion public/editor-client/src/defaultTemplates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@ import {
KitsWithThumbs,
Layouts,
LayoutsWithThumbs,
Pages,
PagesAPI,
PagesDefaultTemplate,
Popups,
PopupsWithThumbs,
Stories,
StoriesWithThumbs
} from "../types/DefaultTemplate";
import { t } from "../utils/i18n";
import { read } from "../utils/reader/json";
import { tempConverterKit } from "./tempComverters";
import {
convertPages,
fetchAllLayouts,
getUniqueLayoutsCategories
} from "./utils";

const defaultKits = (
config: Config
Expand Down Expand Up @@ -288,4 +297,52 @@ const defaultLayouts = (
};
};

export { defaultKits, defaultStories, defaultLayouts, defaultPopups };
const defaultPages = (
config: Config
): PagesDefaultTemplate<Pages, BlocksArray<DefaultBlock>> => {
// @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 apiPagesUrl = "https://j6dfq8pl41.b-cdn.net/api";

return {
async getMeta(res, rej) {
try {
const meta = await fetchAllLayouts<PagesAPI>(
`${apiPagesUrl}/get-pages`,
50
);

res({
blocks: convertPages(meta, imageUrl),
categories: getUniqueLayoutsCategories(meta)
});
} catch (e) {
rej(t("Failed to load meta.json"));
}
},
async getData(res, rej, { slug }) {
try {
const data = await fetch(
`${apiPagesUrl}/get-page-data?page_slug=${slug}`
).then((r) => r.json());

const pageData = read(data[0].pageData) as { items: DefaultBlock[] };

res({
blocks: [...pageData.items]
});
} catch (e) {
rej(t("Failed to load resolves for selected DefaultTemplate"));
}
}
};
};

export {
defaultKits,
defaultStories,
defaultLayouts,
defaultPopups,
defaultPages
};
74 changes: 73 additions & 1 deletion public/editor-client/src/defaultTemplates/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { flatten, uniq, upperFirst } from "lodash";
import { flatten, trim, uniq, upperFirst } from "lodash";
import {
APIPopup,
BlockWithThumbs,
Categories,
Kit,
KitType,
LayoutsAPI,
PagesAPI,
PagesTemplate,
Style
} from "../types/DefaultTemplate";
import { pipe } from "../utils/fp/pipe";
Expand All @@ -25,6 +28,26 @@ export const getUniqueKitCategories = (collections: CatTypes[]): Categories[] =>
}))
)(collections);

export const getUniqueLayoutsCategories = (
collections: LayoutsAPI[] | PagesAPI[]
): Categories[] =>
pipe(
(collections: LayoutsAPI[] | PagesAPI[]) =>
collections.map(
(collection: LayoutsAPI | PagesAPI) => collection.categories
),
(cats: string[]) => cats.map((it) => it.split(",")),
flatten,
(cats: string[]) => cats.map(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),
Expand Down Expand Up @@ -121,6 +144,30 @@ export const converterPopup = (
};
};

export const convertPages = (
collections: PagesAPI[],
thumbUrl: string
): PagesTemplate[] =>
collections.map(
({
title,
categories,
thumbnailWidth,
thumbnailHeight,
thumbnail,
slug
}) => {
return {
title: title,
slug: slug,
cat: categories.split(",").map(trim),
thumbnailWidth,
thumbnailHeight,
thumbnailSrc: `${thumbUrl}${thumbnail}`
};
}
);

export const getStyles = (): Array<Style> => [
{
colorPalette: [
Expand Down Expand Up @@ -385,3 +432,28 @@ export const fetchAllElements = async <T>(

return allElements;
};

export const fetchAllLayouts = async <T>(
url: string,
itemsPerPage: number
): Promise<T[]> => {
let allElements: T[] = [];

const firstPageResponse = await fetch(`${url}?per_page=${itemsPerPage}`).then(
(r) => r.json()
);

const lastPage = firstPageResponse.paginationInfo.lastPage;

allElements = allElements.concat(firstPageResponse.collections);

for (let currentPage = 2; currentPage <= lastPage; currentPage++) {
const nextPageResponse = await fetch(
`${url}?per_page=${itemsPerPage}&page_number=${currentPage}`
).then((r) => r.json());

allElements = allElements.concat(nextPageResponse.collections);
}

return allElements;
};
2 changes: 2 additions & 0 deletions public/editor-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { addFile } from "./customFile/addFile";
import {
defaultKits,
defaultLayouts,
defaultPages,
defaultPopups,
defaultStories
} from "./defaultTemplates";
Expand Down Expand Up @@ -52,6 +53,7 @@ const api = {
defaultPopups: defaultPopups(config),
defaultStories: defaultStories(config),
defaultLayouts: defaultLayouts(config),
defaultPages: defaultPages(config),
collectionItems: {
searchCollectionItems,
getCollectionItemsIds
Expand Down
94 changes: 94 additions & 0 deletions public/editor-client/src/types/DefaultTemplate.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Literal } from "../utils/types";
import { Response } from "./Response";

export interface DefaultBlock {
Expand Down Expand Up @@ -131,6 +132,10 @@ interface FontStyle {
tabletLineHeight: number;
}

export type CustomTemplatePage = TemplatePageWithThumbs & {
[key: string]: string;
};

// region Kits
export interface KitCategories {
id: number;
Expand Down Expand Up @@ -185,6 +190,61 @@ export type Kit = {
};
// endregion

// region Layouts Type
export interface LayoutTemplate {
blank?: boolean;
name: string;
cat: Array<Literal>;
color: string;
pagesCount: number;
pro: boolean;
keywords: string;
}

export interface LayoutTemplateWithThumbs extends LayoutTemplate {
thumbnailSrc: string;
thumbnailWidth: number;
thumbnailHeight: number;
}

export interface LayoutsDefaultTemplate<T1, T2, T3> {
label?: string;
getMeta: (res: Response<T1>, rej: Response<string>) => void;
getData: (
res: Response<T2>,
rej: Response<string>,
page: CustomTemplatePage
) => void;
getPages: (res: Response<T3>, rej: Response<string>, id: string) => void;
}

export interface LayoutsPages {
pages: CustomTemplatePage[];
styles: Style[];
}

export interface LayoutsPageAPI {
title: string;
slug: string;
thumbs: string;
thumbnailWidth: number;
thumbnailHeight: number;
}

export interface LayoutsAPI {
title: string;
pro: string;
categories: string;
pagesCount: string;
slug: string;
thumbnail: string;
thumbnailWidth: number;
thumbnailHeight: number;
keywords: string;
color: string;
}
// endregion

// region Popups
export interface DefaultTemplatePopup<T1, T2> {
label?: string;
Expand Down Expand Up @@ -215,3 +275,37 @@ export interface PopupsWithThumbs extends Omit<Popups, "blocks"> {
blocks: Array<BlockWithThumbs>;
}
// endregion

// region Pages
export interface PagesAPI {
title: string;
slug: string;
categories: string;
thumbnail: string;
thumbnailWidth: number;
thumbnailHeight: number;
}
export interface PagesDefaultTemplate<T1, T2> {
label?: string;
getMeta: (res: Response<T1>, rej: Response<string>) => void;
getData: (
res: Response<T2>,
rej: Response<string>,
page: CustomTemplatePage
) => void;
}

export interface Pages {
blocks: Array<PagesTemplate>;
categories: Pick<Categories, "id" | "title">[];
}

export interface PagesTemplate {
title: string;
slug: string;
cat: Array<Literal>;
thumbnailSrc: string;
thumbnailWidth: number;
thumbnailHeight: number;
}
// endregion
2 changes: 2 additions & 0 deletions public/editor-client/src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
KitItem,
KitsWithThumbs,
LayoutsWithThumbs,
Pages,
PopupsWithThumbs,
StoriesWithThumbs
} from "./DefaultTemplate";
Expand Down Expand Up @@ -151,6 +152,7 @@ export interface VISUAL_CONFIG {
StoriesWithThumbs,
BlocksArray<DefaultBlock> | DefaultBlock
>;
defaultPages?: DefaultTemplate<Pages, BlocksArray<DefaultBlock>>;

//Collection Items
collectionItems?: {
Expand Down

0 comments on commit b539713

Please sign in to comment.