taler-typescript-core

Wallet core logic and WebUIs for various components
Log | Files | Refs | Submodules | README | LICENSE

commit 23bb82f00cc83541527402bdd2d4309c530df674
parent 62713c7e71e96e1f6875adb691927c68dc37dea7
Author: Sebastian <sebasjm@gmail.com>
Date:   Thu, 18 Aug 2022 12:48:05 -0300

disable button when wating for response

Diffstat:
Mpackages/taler-wallet-webextension/src/mui/Button.stories.tsx | 27+++++++++++++++++++++++++++
Mpackages/taler-wallet-webextension/src/mui/Button.tsx | 17+++++++++++++++--
2 files changed, 42 insertions(+), 2 deletions(-)

diff --git a/packages/taler-wallet-webextension/src/mui/Button.stories.tsx b/packages/taler-wallet-webextension/src/mui/Button.stories.tsx @@ -39,6 +39,33 @@ const Stack = styled.div` background-color: white; `; +export const WithDelay = (): VNode => ( + <Stack> + <Button + size="small" + variant="contained" + onClick={() => + new Promise((resolve) => { + setTimeout(resolve, 2000); + }) + } + > + Returns after 2 seconds + </Button> + <Button + size="small" + variant="contained" + onClick={() => + new Promise((_, reject) => { + setTimeout(reject, 2000); + }) + } + > + Fails after 2 seconds + </Button> + </Stack> +); + export const BasicExample = (): VNode => ( <Fragment> <Stack> diff --git a/packages/taler-wallet-webextension/src/mui/Button.tsx b/packages/taler-wallet-webextension/src/mui/Button.tsx @@ -19,6 +19,7 @@ import { css } from "@linaria/core"; import { theme, Colors, rippleEnabled, rippleEnabledOutlined } from "./style"; // eslint-disable-next-line import/extensions import { alpha } from "./colors/manipulation"; +import { useState } from "preact/hooks"; export const buttonBaseStyle = css` display: inline-flex; @@ -219,7 +220,7 @@ export function Button({ size = "medium", style: parentStyle, color = "primary", - onClick, + onClick: doClick, }: Props): VNode { const style = css` user-select: none; @@ -275,9 +276,21 @@ export function Button({ }} /> ); + const [running, setRunning] = useState(false); + + async function onClick(): Promise<void> { + if (!doClick || disabled || running) return; + setRunning(true); + try { + await doClick(); + } finally { + setRunning(false); + } + } + return ( <ButtonBase - disabled={disabled} + disabled={disabled || running} class={[ theme.typography.button, theme.shape.roundBorder,