All files / packages/app/features/receipt receipt-date-input.tsx

62.5% Statements 5/8
50% Branches 2/4
50% Functions 1/2
62.5% Lines 5/8

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                              301x       442x 442x           442x                         442x                    
import React from "react";
 
import { useMutation } from "@tanstack/react-query";
 
import { useTrpcMutationOptions } from "~app/hooks/use-trpc-mutation-options";
import type { Receipt } from "~app/trpc-types";
import { useTRPC } from "~app/utils/trpc";
import { DateInput } from "~components/date-input";
import { options as receiptsUpdateOptions } from "~mutations/receipts/update";
 
type Props = {
	receipt: Receipt;
	isLoading: boolean;
};
 
export const ReceiptDateInput: React.FC<Props> = ({
	receipt,
	isLoading: isDisabled,
}) => {
	const trpc = useTRPC();
	const updateReceiptMutation = useMutation(
		trpc.receipts.update.mutationOptions(
			useTrpcMutationOptions(receiptsUpdateOptions),
		),
	);
 
	const saveDate = React.useCallback(
		(nextDate: Temporal.PlainDate) => {
			if (Temporal.PlainDate.compare(nextDate, receipt.issued) === 0) {
				return;
			}
			updateReceiptMutation.mutate({
				id: receipt.id,
				update: { type: "issued", issued: nextDate },
			});
		},
		[updateReceiptMutation, receipt.id, receipt.issued],
	);
 
	return (
		<DateInput
			value={receipt.issued}
			onValueChange={saveDate}
			mutation={updateReceiptMutation}
			labelPlacement="outside-left"
			isDisabled={receipt.ownerPeerId !== receipt.selfPeerId || isDisabled}
		/>
	);
};