Skip to content

Commit 317fac4

Browse files
authored
Adds API functionality (#18)
* Adds more hooks * Enum hook reorder & extension * PR comments
1 parent 1c004b4 commit 317fac4

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

hooks/enums/useEnumOptions.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { enumToArray, enumToArrayOptions } from '@/submodules/javascript-functions/general';
2+
import { useMemo } from 'react';
3+
4+
5+
export default function useEnumOptions<T>(enumObj: T, options?: enumToArrayOptions): { name: string, value: T }[] {
6+
return useMemo(() => enumToArray(enumObj, options), []);
7+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { enumToArray } from '@/submodules/javascript-functions/general';
2+
import { useMemo } from 'react';
3+
import { useTranslation } from 'react-i18next';
4+
5+
6+
const cache = {};
7+
8+
function getEnumOptionsForLanguage<T>(enumObj: T, lookupKey: string, t: any, language: string): { name: string, value: T }[] {
9+
if (!(lookupKey in cache)) cache[lookupKey] = {};
10+
if (cache[lookupKey][language]) return cache[lookupKey][language];
11+
const finalArray = enumToArray(enumObj, { nameFunction: (s) => { return t(`${lookupKey}.${s}`) } });
12+
cache[lookupKey][language] = finalArray;
13+
return cache[lookupKey][language];
14+
}
15+
16+
// unfortunately we need to provide the lookupKey ourselves which is the Enum name itself.
17+
// there is no way to get the name of the enum at runtime since they are compiled away
18+
// https://stackoverflow.com/a/59824941/19801189
19+
export default function useEnumOptionsTranslated<T>(enumObj: T, lookupKey: string, translationScope: string): { name: string, value: T }[] {
20+
const { t, i18n } = useTranslation(translationScope);
21+
const values = useMemo(() => getEnumOptionsForLanguage(enumObj, lookupKey, t, i18n.language), [i18n.language]);
22+
return values;
23+
}

hooks/enums/useEnumState.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Dispatch, SetStateAction, useEffect } from "react";
2+
import useEnumOptionsTranslated from "./useEnumOptionsTranslated";
3+
import useRefState from "../useRefState";
4+
import useEnumOptions from "./useEnumOptions";
5+
import { enumToArrayOptions } from "@/submodules/javascript-functions/general";
6+
7+
export default function useEnumState<T>(enumObj: T, conversionOptions?: enumToArrayOptions): [
8+
{ name: string, value: T },
9+
Dispatch<SetStateAction<{ name: string; value: T; }>>,
10+
{ name: string, value: T }[]
11+
] {
12+
const options = useEnumOptions(enumObj, conversionOptions);
13+
const { state, setState, ref } = useRefState(options[0]);
14+
useEffect(() => {
15+
if (!ref.current) return;
16+
const newOption = options.find(e => e.value == ref.current.value);
17+
if (newOption) setState(newOption);
18+
}, [options]);
19+
20+
return [state, setState, options]
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Dispatch, SetStateAction, useEffect } from "react";
2+
import useEnumOptionsTranslated from "./useEnumOptionsTranslated";
3+
import useRefState from "../useRefState";
4+
//TODO: build one for without translation so we can use it outside of cognition as well
5+
6+
// only works if the corresponding enum is translated in the i18n file with the lookupKey
7+
// example: cognition-ui> PATExpiresAt
8+
export default function useEnumStateTranslated<T>(enumObj: T, lookupKey: string, translationScope?: string): [
9+
{ name: string, value: T },
10+
Dispatch<SetStateAction<{ name: string; value: T; }>>,
11+
{ name: string, value: T }[]
12+
] {
13+
const _translationScope = translationScope || 'enums';
14+
const options = useEnumOptionsTranslated(enumObj, lookupKey, _translationScope);
15+
const { state, setState, ref } = useRefState(options[0]);
16+
useEffect(() => {
17+
if (!ref.current) return;
18+
const newOption = options.find(e => e.value == ref.current.value);
19+
if (newOption) setState(newOption);
20+
}, [options]);
21+
22+
return [state, setState, options]
23+
}

0 commit comments

Comments
 (0)