diff options
Diffstat (limited to 'packages/demobank-ui/src/hooks/backend.ts')
-rw-r--r-- | packages/demobank-ui/src/hooks/backend.ts | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/packages/demobank-ui/src/hooks/backend.ts b/packages/demobank-ui/src/hooks/backend.ts new file mode 100644 index 000000000..fa4211f13 --- /dev/null +++ b/packages/demobank-ui/src/hooks/backend.ts @@ -0,0 +1,34 @@ +import { hooks } from "@gnu-taler/web-util/lib/index.browser"; +import { StateUpdater } from "preact/hooks"; + + +/** + * Has the information to reach and + * authenticate at the bank's backend. + */ +export interface BackendStateType { + url?: string; + username?: string; + password?: string; +} + +/** + * Return getters and setters for + * login credentials and backend's + * base URL. + */ +type BackendStateTypeOpt = BackendStateType | undefined; +export function useBackendState( + state?: BackendStateType, +): [BackendStateTypeOpt, StateUpdater<BackendStateTypeOpt>] { + const ret = hooks.useLocalStorage("backend-state", JSON.stringify(state)); + const retObj: BackendStateTypeOpt = ret[0] ? JSON.parse(ret[0]) : ret[0]; + const retSetter: StateUpdater<BackendStateTypeOpt> = function (val) { + const newVal = + val instanceof Function + ? JSON.stringify(val(retObj)) + : JSON.stringify(val); + ret[1](newVal); + }; + return [retObj, retSetter]; +} |