All files / packages/app/features/settings manual-accept-debts-option.tsx

100% Statements 9/9
100% Branches 4/4
100% Functions 3/3
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59                          22x   309x 309x     309x         309x   2x           309x 269x     309x                                            
import React from "react";
 
import { useMutation, useSuspenseQuery } from "@tanstack/react-query";
 
import { ErrorMessage } from "~app/components/error-message";
import { suspendedFallback } from "~app/components/suspense-wrapper";
import { useTrpcMutationOptions } from "~app/hooks/use-trpc-mutation-options";
import { useTRPC } from "~app/utils/trpc";
import { SkeletonSwitch } from "~components/skeleton-switch";
import { Spinner } from "~components/spinner";
import { Switch } from "~components/switch";
import { options as userSettingsUpdateOptions } from "~mutations/user-settings/update";
 
export const ManualAcceptDebtsOption = suspendedFallback(
	() => {
		const trpc = useTRPC();
		const { data: settings } = useSuspenseQuery(
			trpc.userSettings.get.queryOptions(),
		);
		const updateSettingsMutation = useMutation(
			trpc.userSettings.update.mutationOptions(
				useTrpcMutationOptions(userSettingsUpdateOptions),
			),
		);
		const onChange = React.useCallback(
			(nextAutoAccept: boolean) =>
				updateSettingsMutation.mutate({
					type: "manualAcceptDebts",
					value: nextAutoAccept,
				}),
			[updateSettingsMutation],
		);
		const errorButton = React.useMemo(
			() => ({ text: "Reset", onPress: updateSettingsMutation.reset }),
			[updateSettingsMutation],
		);
		return (
			<>
				<Switch
					testID="manual-accept-debts-switch"
					isSelected={settings.manualAcceptDebts}
					onValueChange={onChange}
					thumbIcon={
						updateSettingsMutation.isPending ? <Spinner size="sm" /> : undefined
					}
					isDisabled={updateSettingsMutation.isPending}
				/>
				{updateSettingsMutation.status === "error" ? (
					<ErrorMessage
						message={updateSettingsMutation.error.message}
						button={errorButton}
					/>
				) : null}
			</>
		);
	},
	<SkeletonSwitch />,
);