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

7.14% Statements 1/14
0% Branches 0/8
0% Functions 0/6
7.14% Lines 1/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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88                                        6x                                                                                                                                      
import type React from "react";
 
import { useMutation } from "@tanstack/react-query";
import { useTranslation } from "react-i18next";
import { z } from "zod";
 
import { useTrpcMutationOptions } from "~app/hooks/use-trpc-mutation-options";
import type { Receipt } from "~app/trpc-types";
import { useAppForm } from "~app/utils/forms";
import { useTRPC } from "~app/utils/trpc";
import { receiptNameSchema } from "~app/utils/validation";
import { SaveButton } from "~components/save-button";
import { options as receiptsUpdateOptions } from "~mutations/receipts/update";
 
type Props = {
	receipt: Receipt;
	isLoading: boolean;
	unsetEditing: () => void;
};
 
export const ReceiptNameInput: React.FC<Props> = ({
	receipt,
	isLoading,
	unsetEditing,
}) => {
	const { t } = useTranslation("receipts");
	const trpc = useTRPC();
	const updateReceiptMutation = useMutation(
		trpc.receipts.update.mutationOptions(
			useTrpcMutationOptions(receiptsUpdateOptions, {
				onSuccess: unsetEditing,
			}),
		),
	);
	const form = useAppForm({
		defaultValues: { value: receipt.name },
		validators: { onChange: z.object({ value: receiptNameSchema }) },
		onSubmit: ({ value }) => {
			if (value.value === receipt.name) {
				unsetEditing();
				return;
			}
			updateReceiptMutation.mutate({
				id: receipt.id,
				update: { type: "name", name: value.value },
			});
		},
	});
 
	return (
		<form.AppField name="value">
			{(field) => (
				<field.TextField
					value={field.state.value}
					onValueChange={field.setValue}
					name={field.name}
					onBlur={field.handleBlur}
					fieldError={
						field.state.meta.isDirty ? field.state.meta.errors : undefined
					}
					aria-label={t("receipt.form.name.label")}
					mutation={updateReceiptMutation}
					labelPlacement="outside-left"
					className="basis-36"
					isDisabled={isLoading}
					isReadOnly={receipt.ownerPeerId !== receipt.selfPeerId}
					endContent={
						receipt.ownerPeerId === receipt.selfPeerId ? (
							<form.Subscribe selector={(state) => state.canSubmit}>
								{(canSubmit) => (
									<SaveButton
										title={t("receipt.form.name.saveButton")}
										onPress={() => {
											void field.form.handleSubmit();
										}}
										isLoading={updateReceiptMutation.isPending}
										isDisabled={isLoading || !canSubmit}
									/>
								)}
							</form.Subscribe>
						) : null
					}
				/>
			)}
		</form.AppField>
	);
};