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 Feb 28, 2024
1 parent e45ebb2 commit b4deb60
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 3 deletions.
6 changes: 3 additions & 3 deletions public/editor-client/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ interface API {
fileUrl: string;
templates: DefaultTemplates;
openAIUrl?: string;
ekklesiaApiUrl?: string;
}
export interface Config {
hash: string;
Expand Down Expand Up @@ -111,9 +112,8 @@ const apiReader = parseStrict<PLUGIN_ENV["api"], API>({
mPipe(Obj.readKey("templates"), Obj.read, templatesReader),
throwOnNullish("Invalid API: templates")
),
openAIUrl: optional(pipe(
mPipe(Obj.readKey("openAIUrl"), Str.read),
))
openAIUrl: optional(pipe(mPipe(Obj.readKey("openAIUrl"), Str.read))),
ekklesiaApiUrl: optional(mPipe(Obj.readKey("ekklesiaApiUrl"), Str.read))
});

const actionsReader = parseStrict<PLUGIN_ENV["actions"], Actions>({
Expand Down
84 changes: 84 additions & 0 deletions public/editor-client/src/ekklesia/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { Config } from "config";
import { ChoicesAsync, ChoicesSync } from "types/Choices";
import {
EkklesiaFieldMap,
EkklesiaFields,
EkklesiaKeys,
EkklesiaParams
} from "types/Ekklesia";
import { Response } from "types/Response";
import { t } from "utils/i18n";
import { getFields } from "./utils";

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.api;
if (!ekklesiaApiUrl) {
if (process.env.NODE_ENV === "development") {
console.error("Missing Ekklesia api url!");
}
res([{ value: "", title: t("None") }]);
return;
}
try {
const fields = await getFields(ekklesiaApiUrl, keys);
res(fields);
} catch (error) {
if (process.env.NODE_ENV === "development") {
console.error("Failed to load ekklesia fields 2");
}
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.api;

if (!ekklesiaApiUrl) {
if (process.env.NODE_ENV === "development") {
console.error("Missing Ekklesia api url!");
}
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);

if (!choiches.length) continue;

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"));
}
}
});
64 changes: 64 additions & 0 deletions public/editor-client/src/ekklesia/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { request } from "api/index";
import { makeUrl } from "api/utils";
import { ChoicesSync } from "types/Choices";
import {
EkklesiaChoiceParamsWithSubKey,
EkklesiaFields,
EkklesiaParams,
EkklesiaParentsChilds,
EkklesiaResponse
} from "types/Ekklesia";
import { t } from "utils/i18n";
import { isObject } from "utils/reader/object";
import { read as readString } from "utils/reader/string";
import { Literal } from "utils/types";

export const requestFields = (url: string): Promise<EkklesiaResponse> => {
return request(url).then((res) => res.json());
};

export const fieldHaveParentsChildsKeys = (
keys: Record<string, Literal> | EkklesiaParentsChilds
): keys is EkklesiaParentsChilds => "childs" in keys && "parents" in keys;

export const keysHaveSubkey = (
keys: EkklesiaParams
): keys is EkklesiaChoiceParamsWithSubKey => "subKey" in keys;

export const getOption = (
obj: Record<string, Literal> | undefined
): ChoicesSync =>
isObject(obj)
? [
{ title: t("None"), value: "" },
...Object.entries(obj).map(([key, value]) => {
return {
title: readString(value) ?? "",
value: key
};
})
]
: [];

export const getFields = async <
T extends keyof EkklesiaFields = keyof EkklesiaFields
>(
_url: string,
keys: EkklesiaParams<T>
): Promise<ChoicesSync> => {
const { key } = keys;

const url = makeUrl(_url, { module: key });

const { data = {} } = await requestFields(url);
const field = data[key];

if (field && fieldHaveParentsChildsKeys(field)) {
if (keysHaveSubkey(keys)) {
return getOption(field[keys.subKey]);
}
return [{ value: "", title: t("None") }];
} else {
return getOption(field);
}
};
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";
import set from "lodash/set";
import {doAiRequest} from "./aiText";
import {autoSave} from "./autoSave";
Expand Down Expand Up @@ -54,6 +55,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>;
}

0 comments on commit b4deb60

Please sign in to comment.