diff options
author | Sebastian <sebasjm@gmail.com> | 2022-12-07 09:29:36 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2022-12-07 16:08:14 -0300 |
commit | 54c8d0db31851e9dd0667da77b119994dee38770 (patch) | |
tree | b42f8428e06144b448b3bb99adc343888f64acbf /packages/demobank-ui/src/hooks/backend.ts | |
parent | bb0955d89c32471fb228326e96a721fcd346d0d1 (diff) | |
download | wallet-core-54c8d0db31851e9dd0667da77b119994dee38770.tar.gz wallet-core-54c8d0db31851e9dd0667da77b119994dee38770.tar.bz2 wallet-core-54c8d0db31851e9dd0667da77b119994dee38770.zip |
no-fix: move out useBackend hook
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]; +} |