Skip to content

Commit

Permalink
pages API
Browse files Browse the repository at this point in the history
  • Loading branch information
GunkaArtur committed Mar 28, 2024
1 parent daca336 commit cf0edb5
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 382 deletions.
36 changes: 35 additions & 1 deletion public/editor-client/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Config, getConfig } from "@/config";
import { DefaultBlock, PagesAPI } 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,6 +16,7 @@ import {
} from "@/types/SavedBlocks";
import { ScreenshotData } from "@/types/Screenshots";
import { t } from "@/utils/i18n";
import { Json } from "@brizy/readers";
import { Literal } from "../utils/types";
import {
GetCollections,
Expand Down Expand Up @@ -1206,3 +1208,35 @@ export const updateGlobalBlocks = async (
};

//#endregion

// region Default Pages
export const getDefaultPages = async (): Promise<{
blocks: PagesAPI[];
categories: Record<string, string>;
}> => {
const res = await fetch(
// temporary solution, wait until new API will come via config
"https://phplaravel-1109775-4184176.cloudwaysapps.com/api/get-pages-chunk"
).then((r) => r.json());

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

export const getDefaultPage = async (
slug: string
): Promise<{
items: DefaultBlock[];
}> => {
const data = await fetch(
// temporary solution, wait until new API will come via config
`https://j6dfq8pl41.b-cdn.net/api/get-page-data?page_slug=${slug}`
).then((r) => r.json());

return Json.read(data[0].pageData) as {
items: DefaultBlock[];
};
};
// 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;
pagesUrl: string;
}

interface Actions {
Expand Down Expand Up @@ -104,6 +105,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
46 changes: 45 additions & 1 deletion public/editor-client/src/defaultTemplates/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getDefaultPage, getDefaultPages } from "@/api";
import { Config } from "../config";
import {
BlocksArray,
Expand All @@ -11,13 +12,16 @@ import {
KitsWithThumbs,
Layouts,
LayoutsWithThumbs,
Pages,
PagesDefaultTemplate,
Popups,
PopupsWithThumbs,
Stories,
StoriesWithThumbs
} from "../types/DefaultTemplate";
import { t } from "../utils/i18n";
import { tempConverterKit } from "./tempComverters";
import { convertPages, convertToCategories } from "./utils";

const defaultKits = (
config: Config
Expand Down Expand Up @@ -288,4 +292,44 @@ 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/";

return {
async getMeta(res, rej) {
try {
const { blocks, categories } = await getDefaultPages();

res({
blocks: convertPages(blocks, imageUrl),
categories: convertToCategories(categories)
});
} catch (e) {
rej(t("Failed to load meta.json"));
}
},
async getData(res, rej, { slug }) {
try {
const pageData = await getDefaultPage(slug);

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

export {
defaultKits,
defaultStories,
defaultLayouts,
defaultPopups,
defaultPages
};
Loading

0 comments on commit cf0edb5

Please sign in to comment.