-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ministry brands): handler in editor-client
- Loading branch information
1 parent
e45ebb2
commit b4deb60
Showing
5 changed files
with
215 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |