All files / packages/app/features/debts-intentions accept-all-intentions-button.tsx

100% Statements 14/14
100% Branches 0/0
100% Functions 5/5
100% Lines 14/14

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 60 61 62 63 64                                13x       16x 16x 16x 16x   16x     55x                 16x 2x 2x     8x     1x           16x       2x                
import React from "react";
 
import { useMutation } from "@tanstack/react-query";
import { useTranslation } from "react-i18next";
 
import { NavigationContext } from "~app/contexts/navigation-context";
import { useTrpcMutationOptions } from "~app/hooks/use-trpc-mutation-options";
import type { DebtIntention } from "~app/trpc-types";
import { useTRPC } from "~app/utils/trpc";
import { Button } from "~components/button";
import { options as acceptDebtIntentionOptions } from "~mutations/debt-intentions/accept";
 
type Props = {
	intentions: DebtIntention[];
} & React.ComponentProps<typeof Button>;
 
export const AcceptAllIntentionsButton: React.FC<Props> = ({
	intentions,
	...props
}) => {
	const { t } = useTranslation("debts");
	const trpc = useTRPC();
	const { useNavigate } = React.use(NavigationContext);
	const navigate = useNavigate();
 
	const acceptMutations = intentions.map((intention) =>
		// Intentions are stable due to `key` based on intention id in the upper component
		// oxlint-disable-next-line react-hooks/rules-of-hooks
		useMutation(
			trpc.debtIntentions.accept.mutationOptions(
				// oxlint-disable-next-line react-hooks/rules-of-hooks
				useTrpcMutationOptions(acceptDebtIntentionOptions, {
					context: { intention },
				}),
			),
		),
	);
	const acceptAllIntentions = React.useCallback(async () => {
		try {
			await Promise.all(
				acceptMutations.map((mutation, index) =>
					// oxlint-disable-next-line typescript/no-non-null-assertion
					mutation.mutateAsync({ id: intentions[index]!.id }),
				),
			);
			navigate({ to: "/debts" });
		} catch {
			/* empty */
		}
	}, [acceptMutations, intentions, navigate]);
 
	return (
		<Button
			color="primary"
			onPress={() => {
				void acceptAllIntentions();
			}}
			{...props}
		>
			{t("intentions.acceptAllButton")}
		</Button>
	);
};