From f1d557f464fd82cbb78ddf9b64c797d74fbb912a Mon Sep 17 00:00:00 2001 From: DanielGherjavca Date: Fri, 27 Oct 2023 00:13:21 +0300 Subject: [PATCH] feat(ministry brands): handler in editor-client --- public/editor-client/src/config.ts | 6 + public/editor-client/src/ekklesia/index.s.ts | 130 +++++++++++++++++++ public/editor-client/src/index.ts | 7 + public/editor-client/src/types/Ekklesia.ts | 57 ++++++++ public/editor-client/src/types/global.d.ts | 1 + 5 files changed, 201 insertions(+) create mode 100644 public/editor-client/src/ekklesia/index.s.ts create mode 100644 public/editor-client/src/types/Ekklesia.ts diff --git a/public/editor-client/src/config.ts b/public/editor-client/src/config.ts index 5de91a106b..bf5459f81d 100644 --- a/public/editor-client/src/config.ts +++ b/public/editor-client/src/config.ts @@ -57,6 +57,7 @@ export interface Config { actions: Actions; api: API; l10n?: Record; + ekklesiaApiUrl?: string; collectionTypes: CollectionType[]; } @@ -219,6 +220,7 @@ const reader = parseStrict({ throwOnNullish("Invalid: api") ), l10n: optional(pipe(Obj.readKey("l10n"), Obj.read, onNullish({}))), + ekklesiaApiUrl: optional(pipe(Obj.readKey("ekklesiaApiUrl"), Str.read)), collectionTypes: pipe( mPipe(Obj.readKey("collectionTypes"), Arr.read, collectionTypesReader), throwOnNullish("Invalid: collectionTypes") @@ -227,6 +229,10 @@ const reader = parseStrict({ export const getConfig = (): MValue => { const __BRZ_PLUGIN_ENV__ = window.__BRZ_PLUGIN_ENV__ ?? {}; + if (window.__BRZ_PLUGIN_ENV__?.api) { + window.__BRZ_PLUGIN_ENV__.ekklesiaApiUrl = + "http://brizy.local/wp-admin/admin-ajax.php?action=brizy_get_ekklesia_data"; + } const parsed = reader(__BRZ_PLUGIN_ENV__); if (parsed === undefined) { diff --git a/public/editor-client/src/ekklesia/index.s.ts b/public/editor-client/src/ekklesia/index.s.ts new file mode 100644 index 0000000000..d1b765e0df --- /dev/null +++ b/public/editor-client/src/ekklesia/index.s.ts @@ -0,0 +1,130 @@ +import { Config } from "config"; +import { ChoicesAsync, ChoicesSync } from "types/Choices"; +import { + EkklesiaChoiceParamsWithSubKey, + EkklesiaFieldMap, + EkklesiaFields, + EkklesiaKeys, + EkklesiaParams, + EkklesiaParentsChilds, + EkklesiaResponse +} from "types/Ekklesia"; +import { Literal } from "utils/types"; +import { Response } from "../types/Response"; +import { t } from "../utils/i18n"; + +export const request = (url: string): Promise => { + return fetch(url).then((res) => res.json()); +}; + +export const fieldHaveParentsChildsKeys = ( + key: Record | EkklesiaParentsChilds +): key is EkklesiaParentsChilds => { + return "childs" in key && "parents" in key; +}; + +export const keysHaveSubkey = ( + keys: EkklesiaParams +): keys is EkklesiaChoiceParamsWithSubKey => { + return "subKey" in keys; +}; + +export const getOption = ( + obj: Record | undefined +): ChoicesSync => { + return obj + ? [ + { title: t("None"), value: "" }, + ...Object.entries(obj).map(([key, value]) => { + return { + title: String(value), + value: key + }; + }) + ] + : []; +}; + +export const getFields = async < + T extends keyof EkklesiaFields = keyof EkklesiaFields +>( + url: string, + keys: EkklesiaParams +): Promise => { + const { key } = keys; + + const { data = {} } = await request(url.concat("?module=", key)); + const field = data[key]; + + if (field && fieldHaveParentsChildsKeys(field)) { + if (keysHaveSubkey(keys)) { + return getOption(field[keys.subKey]); + } + return []; + } else { + return getOption(field); + } +}; + +export const getEkklesiaFields = (config: Config) => ({ + async handler( + res: Response, + rej: Response, + keys: EkklesiaParams + ): Promise { + const { ekklesiaApiUrl } = config; + + if (!ekklesiaApiUrl) { + rej(t("Missing Ekklesia api url!")); + return; + } + try { + const fields = await getFields(ekklesiaApiUrl, keys); + res(fields); + } catch (error) { + rej(t("Failed to load ekklesia fields")); + } + } +}); + +export const updateEkklesiaFields = (config: Config) => ({ + async handler( + res: Response, + rej: Response, + keys: { + fields: Array; + } + ): Promise { + const { ekklesiaApiUrl } = config; + + if (!ekklesiaApiUrl) { + rej(t("Missing Ekklesia api url!")); + return; + } + + const dataToChange: EkklesiaKeys = {}; + try { + for (const field of keys.fields) { + const choiches = await getFields(ekklesiaApiUrl, field.module); + + const updatedField = Object.keys(field.value).reduce((acc, key) => { + const value = field.value[key]; + const currentField = choiches.find( + (choice) => choice.value === value + ); + + if (!currentField) { + acc[key] = ""; + } + + return acc; + }, {} as EkklesiaKeys); + + Object.assign(dataToChange, updatedField); + } + res(Object.keys(dataToChange).length ? dataToChange : undefined); + } catch (error) { + rej(t("Failed to load ekklesia fields")); + } + } +}); diff --git a/public/editor-client/src/index.ts b/public/editor-client/src/index.ts index d2a4b7f26f..b99909dd49 100644 --- a/public/editor-client/src/index.ts +++ b/public/editor-client/src/index.ts @@ -1,3 +1,4 @@ +import { getEkklesiaFields, updateEkklesiaFields } from "ekklesia/index.s"; import set from "lodash/set"; import { autoSave } from "./autoSave"; import { getCollectionItemsIds } from "./collectionItems/getCollectionItemsIds"; @@ -50,6 +51,12 @@ const api = { searchCollectionItems, getCollectionItemsIds }, + modules: { + ekklesia: { + getEkklesiaFields: getEkklesiaFields(config), + updateEkklesiaFields: updateEkklesiaFields(config) + } + }, collectionTypes: { loadCollectionTypes }, diff --git a/public/editor-client/src/types/Ekklesia.ts b/public/editor-client/src/types/Ekklesia.ts new file mode 100644 index 0000000000..c45a23f9ef --- /dev/null +++ b/public/editor-client/src/types/Ekklesia.ts @@ -0,0 +1,57 @@ +import { Literal } from "utils/types"; + +export interface EkklesiaResponse { + data: Partial; +} + +export interface EkklesiaFields { + groups: Record; + events: Record; + series: Record; + recentSermons: Record; + smallgroups: Record; + forms: Record; + sermon: Record; + event: Record; + smallgroup: Record; + eventsLvl: EkklesiaParentsChilds; + smallgroupsLvl: EkklesiaParentsChilds; +} + +export interface EkklesiaParentsChilds { + parents: Record; + childs: Record; +} + +export interface EkklesiaChoiceParams< + T extends keyof EkklesiaFields = keyof EkklesiaFields +> { + key: T; +} + +export interface EkklesiaChoiceParamsWithSubKey< + T extends keyof EkklesiaFields = keyof EkklesiaFields +> extends EkklesiaChoiceParams { + subKey: keyof EkklesiaFields[T]; +} + +export type EkklesiaParams< + T extends keyof EkklesiaFields = keyof EkklesiaFields +> = EkklesiaFields[T] extends EkklesiaParentsChilds + ? EkklesiaChoiceParamsWithSubKey + : EkklesiaChoiceParams; + +export interface EkklesiaKeys { + [key: string]: string; +} + +export type EkklesiaFieldMap = { + [K in keyof EkklesiaFields]: EkklesiaModuleFields; +}; + +export interface EkklesiaModuleFields< + T extends keyof EkklesiaFields = keyof EkklesiaFields +> { + value: Record; + module: EkklesiaParams; +} diff --git a/public/editor-client/src/types/global.d.ts b/public/editor-client/src/types/global.d.ts index 47faf54060..dbaff2ad70 100644 --- a/public/editor-client/src/types/global.d.ts +++ b/public/editor-client/src/types/global.d.ts @@ -37,6 +37,7 @@ export interface PLUGIN_ENV { fileUrl?: string; }; l10n?: Record; + ekklesiaApiUrl?: string; collectionTypes?: CollectionType[]; }