diff options
author | Liang Yi <[email protected]> | 2022-05-31 23:49:04 +0800 |
---|---|---|
committer | GitHub <[email protected]> | 2022-05-31 23:49:04 +0800 |
commit | 2cecb4c5b5bb38ceaf1f17e8d7d30142c3dbaea0 (patch) | |
tree | 09a83aca741c74d25e1a3671e37b9d1353ecd278 /frontend/src/components/async/MutateButton.tsx | |
parent | 6515c42f265c34086c5155505b03fc0576fce039 (diff) | |
download | bazarr-2cecb4c5b5bb38ceaf1f17e8d7d30142c3dbaea0.tar.gz bazarr-2cecb4c5b5bb38ceaf1f17e8d7d30142c3dbaea0.zip |
Replace Bootstrap with Mantine (#1795)
Diffstat (limited to 'frontend/src/components/async/MutateButton.tsx')
-rw-r--r-- | frontend/src/components/async/MutateButton.tsx | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/frontend/src/components/async/MutateButton.tsx b/frontend/src/components/async/MutateButton.tsx new file mode 100644 index 000000000..af301773b --- /dev/null +++ b/frontend/src/components/async/MutateButton.tsx @@ -0,0 +1,47 @@ +import { Button, ButtonProps } from "@mantine/core"; +import { useCallback, useState } from "react"; +import { UseMutationResult } from "react-query"; + +type MutateButtonProps<DATA, VAR> = Omit< + ButtonProps<"button">, + "onClick" | "loading" | "color" +> & { + mutation: UseMutationResult<DATA, unknown, VAR>; + args: () => VAR | null; + onSuccess?: (args: DATA) => void; + onError?: () => void; + noReset?: boolean; +}; + +function MutateButton<DATA, VAR>({ + mutation, + noReset, + onSuccess, + onError, + args, + ...props +}: MutateButtonProps<DATA, VAR>) { + const { mutateAsync } = mutation; + + const [isLoading, setLoading] = useState(false); + + const onClick = useCallback(async () => { + setLoading(true); + try { + const argument = args(); + if (argument !== null) { + const data = await mutateAsync(argument); + onSuccess?.(data); + } else { + onError?.(); + } + } catch (error) { + onError?.(); + } + setLoading(false); + }, [args, mutateAsync, onError, onSuccess]); + + return <Button {...props} loading={isLoading} onClick={onClick}></Button>; +} + +export default MutateButton; |