diff options
Diffstat (limited to 'packages/challenger-ui/src/hooks/session.ts')
-rw-r--r-- | packages/challenger-ui/src/hooks/session.ts | 107 |
1 files changed, 50 insertions, 57 deletions
diff --git a/packages/challenger-ui/src/hooks/session.ts b/packages/challenger-ui/src/hooks/session.ts index 4dc7e0dc1..86dfff94e 100644 --- a/packages/challenger-ui/src/hooks/session.ts +++ b/packages/challenger-ui/src/hooks/session.ts @@ -20,21 +20,20 @@ import { Codec, buildCodecForObject, codecForAbsoluteTime, - codecForBoolean, - codecForChallengeStatus, - codecForNumber, + codecForAny, + codecForList, codecForString, codecForStringURL, codecOptional, } from "@gnu-taler/taler-util"; import { buildStorageKey, useLocalStorage } from "@gnu-taler/web-util/browser"; -import { mutate } from "swr"; /** * Has the information to reach and * authenticate at the bank's backend. */ export type SessionId = { + nonce: string; clientId: string; redirectURL: string; state: string; @@ -48,36 +47,37 @@ export type LastChallengeResponse = { transmitted: boolean; }; -export type SessionState = SessionId & { - lastTry: LastChallengeResponse | undefined; - lastStatus: ChallengerApi.ChallengeStatus | undefined; +interface LastAddress { + address: Record<string, string>; + type: string; + savedAt: AbsoluteTime; +} + +export type SessionState = { completedURL: string | undefined; + lastAddress: Array<LastAddress> | undefined; }; -export const codecForLastChallengeResponse = (): Codec<LastChallengeResponse> => - buildCodecForObject<LastChallengeResponse>() - .property("sendCodeLeft", codecForNumber()) - .property("changeTargetLeft", codecForNumber()) - .property("checkPinLeft", codecForNumber()) - .property("nextSend", codecForAbsoluteTime) - .property("transmitted", codecForBoolean()) - .build("LastChallengeResponse"); +export const codecForLastAddress = (): Codec<LastAddress> => + buildCodecForObject<LastAddress>() + .property("address", codecForAny()) + .property("type", codecForString()) + .property("savedAt", codecForAbsoluteTime) + .build("LastAddress"); export const codecForSessionState = (): Codec<SessionState> => buildCodecForObject<SessionState>() - .property("clientId", codecForString()) - .property("redirectURL", codecForStringURL()) - .property("state", codecForString()) .property("completedURL", codecOptional(codecForStringURL())) - .property("lastStatus", codecOptional(codecForChallengeStatus())) - .property("lastTry", codecOptional(codecForLastChallengeResponse())) + .property("lastAddress", codecOptional(codecForList(codecForLastAddress()))) .build("SessionState"); export interface SessionStateHandler { state: SessionState | undefined; start(s: SessionId): void; - accepted(l: LastChallengeResponse): void; - completed(e: URL): void; - updateStatus(s: ChallengerApi.ChallengeStatus): void; + saveAddress(type: string, address: Record<string, string>): void; + removeAddress(index: number): void; + sent(info: ChallengerApi.ChallengeCreateResponse): void; + failed(info: ChallengerApi.InvalidPinResponse): void; + completed(info: ChallengerApi.ChallengeRedirect): void; } const SESSION_STATE_KEY = buildStorageKey( @@ -95,55 +95,48 @@ export function useSessionState(): SessionStateHandler { return { state, - start(info) { + start() { update({ - ...info, - lastTry: undefined, completedURL: undefined, - lastStatus: undefined, + lastAddress: state?.lastAddress ?? [], }); - cleanAllCache(); }, - accepted(lastTry) { - if (!state) return; + removeAddress(index) { + const lastAddr = [...(state?.lastAddress ?? [])]; + lastAddr.splice(index, 1); update({ - ...state, - lastTry, + completedURL: undefined, + lastAddress: lastAddr, }); }, - completed(url) { - if (!state) return; + saveAddress(type, address) { + const lastAddr = [...(state?.lastAddress ?? [])]; + lastAddr.push({ + type, + address: address ?? {}, + savedAt: AbsoluteTime.now(), + }); update({ - ...state, - completedURL: url.href, + completedURL: undefined, + lastAddress: lastAddr, }); }, - updateStatus(st: ChallengerApi.ChallengeStatus) { - if (!state) return; - if (!state.lastStatus) { + sent(info) { + }, + failed(info) { + }, + completed(info) { + if (!state) { update({ - ...state, - lastStatus: st, + completedURL: info.redirect_url, + lastAddress: [], }); - return; - } - // current status, FIXME: better check to know if the state changed - const ls = state.lastStatus; - if ( - ls.changes_left !== st.changes_left || - ls.fix_address !== st.fix_address || - ls.last_address !== st.last_address - ) { + } else { update({ - ...state, - lastStatus: st, + completedURL: info.redirect_url, + lastAddress: state.lastAddress, }); - return; } }, }; } - -function cleanAllCache(): void { - mutate(() => true, undefined, { revalidate: false }); -} |