diff options
Diffstat (limited to 'packages/anastasis-webui/src/utils/index.tsx')
-rw-r--r-- | packages/anastasis-webui/src/utils/index.tsx | 270 |
1 files changed, 195 insertions, 75 deletions
diff --git a/packages/anastasis-webui/src/utils/index.tsx b/packages/anastasis-webui/src/utils/index.tsx index d1d861469..88bcac551 100644 --- a/packages/anastasis-webui/src/utils/index.tsx +++ b/packages/anastasis-webui/src/utils/index.tsx @@ -1,45 +1,49 @@ -/* eslint-disable @typescript-eslint/camelcase */ -import { BackupStates, RecoveryStates, ReducerState } from 'anastasis-core'; -import { FunctionalComponent, h, VNode } from 'preact'; -import { AnastasisProvider } from '../context/anastasis'; +/* + This file is part of GNU Anastasis + (C) 2021-2022 Anastasis SARL -export function createExample<Props>(Component: FunctionalComponent<Props>, currentReducerState?: ReducerState, props?: Partial<Props>): { (args: Props): VNode } { - const r = (args: Props): VNode => { - return <AnastasisProvider value={{ - currentReducerState, - currentError: undefined, - back: () => { null }, - dismissError: () => { null }, - reset: () => { null }, - runTransaction: () => { null }, - startBackup: () => { null }, - startRecover: () => { null }, - transition: () => { null }, - }}> - <Component {...args} /> - </AnastasisProvider> - } - r.args = props - return r -} + GNU Anastasis is free software; you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License along with + GNU Anastasis; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ +import { + AuthenticationProviderStatusError, + AuthenticationProviderStatusOk, + BackupStates, + RecoveryStates, + ReducerState, + ReducerStateRecovery, +} from "@gnu-taler/anastasis-core"; +import { VNode } from "preact"; + +const noop = async (): Promise<void> => { + return; +}; const base = { continents: [ { - name: "Europe" + name: "Europe", }, { - name: "India" + name: "India", }, { - name: "Asia" + name: "Asia", }, { - name: "North America" + name: "North America", }, { - name: "Testcontinent" - } + name: "Testcontinent", + }, ], countries: [ { @@ -47,115 +51,231 @@ const base = { name: "Testland", continent: "Testcontinent", continent_i18n: { - de_DE: "Testkontinent" + de_DE: "Testkontinent", }, name_i18n: { de_DE: "Testlandt", de_CH: "Testlandi", fr_FR: "Testpais", - en_UK: "Testland" + en_UK: "Testland", }, currency: "TESTKUDOS", - call_code: "+00" + call_code: "+00", }, { code: "xy", name: "Demoland", continent: "Testcontinent", continent_i18n: { - de_DE: "Testkontinent" + de_DE: "Testkontinent", }, name_i18n: { de_DE: "Demolandt", de_CH: "Demolandi", fr_FR: "Demopais", - en_UK: "Demoland" + en_UK: "Demoland", }, currency: "KUDOS", - call_code: "+01" - } + call_code: "+01", + }, ], authentication_providers: { "http://localhost:8086/": { + status: "ok", http_status: 200, annual_fee: "COL:0", - business_name: "ana", + business_name: "Anastasis Local", currency: "COL", liability_limit: "COL:10", methods: [ { type: "question", - usage_fee: "COL:0" - } + usage_fee: "COL:0", + }, + { + type: "sms", + usage_fee: "COL:0", + }, + { + type: "email", + usage_fee: "COL:0", + }, ], - salt: "WBMDD76BR1E90YQ5AHBMKPH7GW", + provider_salt: "WBMDD76BR1E90YQ5AHBMKPH7GW", storage_limit_in_megabytes: 16, - truth_upload_fee: "COL:0" - }, + truth_upload_fee: "COL:0", + } as AuthenticationProviderStatusOk, + "https://kudos.demo.anastasis.lu/": { + status: "ok", + http_status: 200, + annual_fee: "COL:0", + business_name: "Anastasis Kudo", + currency: "COL", + liability_limit: "COL:10", + methods: [ + { + type: "question", + usage_fee: "COL:0", + }, + { + type: "email", + usage_fee: "COL:0", + }, + ], + provider_salt: "WBMDD76BR1E90YQ5AHBMKPH7GW", + storage_limit_in_megabytes: 16, + truth_upload_fee: "COL:0", + } as AuthenticationProviderStatusOk, + "https://anastasis.demo.taler.net/": { + status: "ok", + http_status: 200, + annual_fee: "COL:0", + business_name: "Anastasis Demo", + currency: "COL", + liability_limit: "COL:10", + methods: [ + { + type: "question", + usage_fee: "COL:0", + }, + { + type: "sms", + usage_fee: "COL:0", + }, + { + type: "totp", + usage_fee: "COL:0", + }, + ], + provider_salt: "WBMDD76BR1E90YQ5AHBMKPH7GW", + storage_limit_in_megabytes: 16, + truth_upload_fee: "COL:0", + } as AuthenticationProviderStatusOk, + "http://localhost:8087/": { + status: "error", code: 8414, - hint: "request to provider failed" - }, + hint: "request to provider failed", + } as AuthenticationProviderStatusError, "http://localhost:8088/": { + status: "error", code: 8414, - hint: "request to provider failed" - }, + hint: "request to provider failed", + } as AuthenticationProviderStatusError, "http://localhost:8089/": { + status: "error", code: 8414, - hint: "request to provider failed" - } + hint: "request to provider failed", + } as AuthenticationProviderStatusError, }, - // expiration: { - // d_ms: 1792525051855 // check t_ms - // }, -} as Partial<ReducerState> +} as Partial<ReducerState>; export const reducerStatesExample = { initial: undefined, - recoverySelectCountry: {...base, - recovery_state: RecoveryStates.CountrySelecting - } as ReducerState, - backupSelectCountry: {...base, - backup_state: BackupStates.CountrySelecting + recoverySelectCountry: { + ...base, + reducer_type: "recovery", + recovery_state: RecoveryStates.CountrySelecting, } as ReducerState, - recoverySelectContinent: {...base, + recoverySelectContinent: { + ...base, + reducer_type: "recovery", recovery_state: RecoveryStates.ContinentSelecting, } as ReducerState, - backupSelectContinent: {...base, - backup_state: BackupStates.ContinentSelecting, - } as ReducerState, - secretSelection: {...base, + secretSelection: { + ...base, + reducer_type: "recovery", recovery_state: RecoveryStates.SecretSelecting, } as ReducerState, - recoveryFinished: {...base, + recoveryFinished: { + ...base, + reducer_type: "recovery", recovery_state: RecoveryStates.RecoveryFinished, } as ReducerState, - challengeSelecting: {...base, + challengeSelecting: { + ...base, + reducer_type: "recovery", recovery_state: RecoveryStates.ChallengeSelecting, } as ReducerState, - challengeSolving: {...base, + challengeSolving: { + ...base, + reducer_type: "recovery", recovery_state: RecoveryStates.ChallengeSolving, + } as ReducerStateRecovery, + challengePaying: { + ...base, + reducer_type: "recovery", + recovery_state: RecoveryStates.ChallengePaying, + } as ReducerState, + recoveryAttributeEditing: { + ...base, + reducer_type: "recovery", + recovery_state: RecoveryStates.UserAttributesCollecting, } as ReducerState, - secretEdition: {...base, + backupSelectCountry: { + ...base, + reducer_type: "backup", + backup_state: BackupStates.CountrySelecting, + } as ReducerState, + backupSelectContinent: { + ...base, + reducer_type: "backup", + backup_state: BackupStates.ContinentSelecting, + } as ReducerState, + secretEdition: { + ...base, + reducer_type: "backup", backup_state: BackupStates.SecretEditing, } as ReducerState, - policyReview: {...base, + policyReview: { + ...base, + reducer_type: "backup", backup_state: BackupStates.PoliciesReviewing, } as ReducerState, - policyPay: {...base, + policyPay: { + ...base, + reducer_type: "backup", backup_state: BackupStates.PoliciesPaying, } as ReducerState, - backupFinished: {...base, + backupFinished: { + ...base, + reducer_type: "backup", backup_state: BackupStates.BackupFinished, } as ReducerState, - authEditing: {...base, - backup_state: BackupStates.AuthenticationsEditing + authEditing: { + ...base, + backup_state: BackupStates.AuthenticationsEditing, + reducer_type: "backup", } as ReducerState, - attributeEditing: {...base, - backup_state: BackupStates.UserAttributesCollecting + backupAttributeEditing: { + ...base, + reducer_type: "backup", + backup_state: BackupStates.UserAttributesCollecting, } as ReducerState, - truthsPaying: {...base, - backup_state: BackupStates.TruthsPaying + truthsPaying: { + ...base, + reducer_type: "backup", + backup_state: BackupStates.TruthsPaying, } as ReducerState, +}; + +export type StateFunc<S> = (p: S) => VNode; + +export type StateViewMap<StateType extends { status: string }> = { + [S in StateType as S["status"]]: StateFunc<S>; +}; +export function compose<SType extends { status: string }, PType>( + name: string, + hook: (p: PType) => SType, + vs: StateViewMap<SType>, +): (p: PType) => VNode { + const Component = (p: PType): VNode => { + const state = hook(p); + const s = state.status as unknown as SType["status"]; + const c = vs[s] as unknown as StateFunc<SType>; + return c(state); + }; + // Component.name = `${name}`; + return Component; } |