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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | 27x 27x 83x 83x 83x 83x 83x 83x 83x 2x 83x 83x 3x 83x 83x 467x 83x 386x 385x 26x 1x 8x 381x 7x 7x 341x 398x 740x 356x | import React from "react";
import { useMutation } from "@tanstack/react-query";
import { useTranslation } from "react-i18next";
import { z } from "zod";
import { CurrencyInput } from "~app/components/app/currency-input";
import { PeersSuggest } from "~app/components/app/peers-suggest";
import { SignButtonGroup } from "~app/components/app/sign-button-group";
import type { Direction } from "~app/components/app/sign-button-group";
import { PageHeader } from "~app/components/page-header";
import { NavigationContext } from "~app/contexts/navigation-context";
import { EmailVerificationCard } from "~app/features/email-verification/email-verification-card";
import { useTrpcMutationOptions } from "~app/hooks/use-trpc-mutation-options";
import { useAppForm } from "~app/utils/forms";
import { getPathHooks } from "~app/utils/navigation";
import { useTRPC } from "~app/utils/trpc";
import {
currencyCodeSchema,
debtAmountSchema,
debtAmountSchemaDecimal,
debtNoteSchema,
peerIdSchema,
} from "~app/utils/validation";
import { BackLink } from "~components/back-link";
import { Button } from "~components/button";
import { DateInput } from "~components/date-input";
import { options as debtsAddOptions } from "~mutations/debts/add";
import { temporalSchemas } from "~utils/temporal";
const formSchema = z.object({
amount: debtAmountSchema,
direction: z.literal(["-", "+"]),
currencyCode: currencyCodeSchema,
peerId: peerIdSchema,
note: debtNoteSchema,
timestamp: temporalSchemas.plainDate,
});
type Form = z.infer<typeof formSchema>;
export const AddDebtScreen = () => {
const { useQueryState } = getPathHooks("/_protected/debts/add");
const [peerId, setPeerId] = useQueryState("peerId");
const { t } = useTranslation("debts");
const trpc = useTRPC();
const { useNavigate } = React.use(NavigationContext);
const navigate = useNavigate();
const addMutation = useMutation(
trpc.debts.add.mutationOptions(
useTrpcMutationOptions(debtsAddOptions, {
onSuccess: ({ id }) =>
navigate({ to: "/debts/$id", params: { id }, replace: true }),
}),
),
);
const defaultValues: Partial<Form> = {
note: "",
direction: "+",
timestamp: Temporal.Now.plainDateISO(),
peerId: undefined,
};
const form = useAppForm({
defaultValues: defaultValues as Form,
validators: {
onMount: formSchema,
onChange: formSchema,
onSubmit: formSchema,
},
onSubmit: ({ value }) => {
addMutation.mutate({
note: value.note,
currencyCode: value.currencyCode,
peerId: value.peerId,
amount: value.amount * (value.direction === "+" ? 1 : -1),
timestamp: value.timestamp,
});
},
});
const onDirectionUpdate = React.useCallback(
(direction: Direction) => form.setFieldValue("direction", direction),
[form],
);
return (
<>
<PageHeader startContent={<BackLink to="/debts" />}>
{t("add.title")}
</PageHeader>
<EmailVerificationCard />
<form.AppForm>
<form.Form className="flex flex-col gap-4">
<form.Subscribe selector={(state) => state.values.direction}>
{(direction) => (
<SignButtonGroup
isLoading={addMutation.isPending}
onUpdate={onDirectionUpdate}
direction={direction}
/>
)}
</form.Subscribe>
<form.AppField name="amount">
{(field) => (
<field.NumberField
value={field.state.value}
onValueChange={field.setValue}
name={field.name}
onBlur={field.handleBlur}
isRequired
minValue={0}
label={t("add.form.amount.label")}
isDisabled={addMutation.isPending}
fieldError={
field.state.meta.isDirty ? field.state.meta.errors : undefined
}
fractionDigits={debtAmountSchemaDecimal}
/>
)}
</form.AppField>
<form.AppField name="currencyCode">
{(field) => (
<CurrencyInput
mutation={addMutation}
topQueryOptions={{ type: "debts" }}
value={field.state.value}
onValueChange={field.setValue}
/>
)}
</form.AppField>
<form.AppField
name="peerId"
listeners={{
onMount: ({ fieldApi }) => {
if (peerId) {
fieldApi.setValue(peerId);
}
},
onChange: ({ value }) => setPeerId(value),
}}
>
{(field) => (
<PeersSuggest
selected={field.state.value}
isDisabled={addMutation.isPending}
onPeerClick={(nextPeerId) => {
Iif (nextPeerId === field.state.value) {
form.resetField("peerId");
// Unfortunately, resetting field doesn't emit a listener event
setPeerId(undefined);
} else {
field.setValue(nextPeerId);
}
}}
closeOnSelect
/>
)}
</form.AppField>
<form.AppField name="timestamp">
{(field) => (
<DateInput
value={field.state.value}
onValueChange={field.setValue}
mutation={addMutation}
/>
)}
</form.AppField>
<form.AppField name="note">
{(field) => (
<field.TextField
label={t("add.form.note.label")}
value={field.state.value}
onValueChange={field.setValue}
name={field.name}
onBlur={field.handleBlur}
isRequired
mutation={addMutation}
fieldError={
field.state.meta.isDirty ? field.state.meta.errors : undefined
}
/>
)}
</form.AppField>
<form.Subscribe selector={(state) => state.canSubmit}>
{(canSubmit) => (
<Button
className="mt-4"
color="primary"
isDisabled={!canSubmit || addMutation.isPending}
isLoading={addMutation.isPending}
type="submit"
>
{t("add.form.submitButton")}
</Button>
)}
</form.Subscribe>
</form.Form>
</form.AppForm>
</>
);
};
|