-
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
45df3c3
commit f1d557f
Showing
5 changed files
with
201 additions
and
0 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,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")); | ||
} | ||
} | ||
}); |
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>; | ||
} |
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