Skip to content

Commit

Permalink
feat(ministry brands): handler in editor-client
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielGherjavca committed Oct 26, 2023
1 parent 45df3c3 commit f1d557f
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 0 deletions.
6 changes: 6 additions & 0 deletions public/editor-client/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface Config {
actions: Actions;
api: API;
l10n?: Record<string, string>;
ekklesiaApiUrl?: string;
collectionTypes: CollectionType[];
}

Expand Down Expand Up @@ -219,6 +220,7 @@ const reader = parseStrict<PLUGIN_ENV, Config>({
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")
Expand All @@ -227,6 +229,10 @@ const reader = parseStrict<PLUGIN_ENV, Config>({

export const getConfig = (): MValue<Config> => {
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) {
Expand Down
130 changes: 130 additions & 0 deletions public/editor-client/src/ekklesia/index.s.ts
Original file line number Diff line number Diff line change
@@ -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<EkklesiaResponse> => {
return fetch(url).then((res) => res.json());
};

export const fieldHaveParentsChildsKeys = (
key: Record<string, Literal> | 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<string, Literal> | 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<T>
): Promise<ChoicesSync> => {
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<T extends keyof EkklesiaFields = keyof EkklesiaFields>(
res: Response<ChoicesAsync | ChoicesSync>,
rej: Response<string>,
keys: EkklesiaParams<T>
): Promise<void> {
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<T extends keyof EkklesiaFields = keyof EkklesiaFields>(
res: Response<EkklesiaKeys | undefined>,
rej: Response<string>,
keys: {
fields: Array<EkklesiaFieldMap[T]>;
}
): Promise<void> {
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<T>(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"));
}
}
});
7 changes: 7 additions & 0 deletions public/editor-client/src/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -50,6 +51,12 @@ const api = {
searchCollectionItems,
getCollectionItemsIds
},
modules: {
ekklesia: {
getEkklesiaFields: getEkklesiaFields(config),
updateEkklesiaFields: updateEkklesiaFields(config)
}
},
collectionTypes: {
loadCollectionTypes
},
Expand Down
57 changes: 57 additions & 0 deletions public/editor-client/src/types/Ekklesia.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Literal } from "utils/types";

export interface EkklesiaResponse {
data: Partial<EkklesiaFields>;
}

export interface EkklesiaFields {
groups: Record<string, Literal>;
events: Record<string, Literal>;
series: Record<string, Literal>;
recentSermons: Record<string, Literal>;
smallgroups: Record<string, Literal>;
forms: Record<string, string>;
sermon: Record<string, Literal>;
event: Record<string, Literal>;
smallgroup: Record<string, Literal>;
eventsLvl: EkklesiaParentsChilds;
smallgroupsLvl: EkklesiaParentsChilds;
}

export interface EkklesiaParentsChilds {
parents: Record<string, Literal>;
childs: Record<string, Literal>;
}

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<T>
: EkklesiaChoiceParams<T>;

export interface EkklesiaKeys {
[key: string]: string;
}

export type EkklesiaFieldMap = {
[K in keyof EkklesiaFields]: EkklesiaModuleFields<K>;
};

export interface EkklesiaModuleFields<
T extends keyof EkklesiaFields = keyof EkklesiaFields
> {
value: Record<string, string>;
module: EkklesiaParams<T>;
}
1 change: 1 addition & 0 deletions public/editor-client/src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface PLUGIN_ENV {
fileUrl?: string;
};
l10n?: Record<string, string>;
ekklesiaApiUrl?: string;
collectionTypes?: CollectionType[];
}

Expand Down

0 comments on commit f1d557f

Please sign in to comment.