File size: 6,946 Bytes
2dddd1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import * as z from "zod";
import { Button } from "../../../components/ui/button";
import { Input } from "../../../components/ui/input";
import { AmountInput } from "../../../components/ui/AmountInput";
import { Select } from "../../../components/ui/select";
import { Label } from "../../../components/ui/label";
import { cn } from "../../../lib/utils";
import type { Wallet } from "../../../types";
import { useEffect } from "react";

export const transactionSchema = z.object({
  type: z.enum(['expense', 'income', 'transfer']),
  amount: z.coerce.number().positive("Amount must be positive"),
  currency: z.string().min(1, "Currency is required"),
  wallet_id: z.coerce.number().min(1, "Wallet is required"),
  to_wallet_id: z.coerce.number().optional().nullable(),
  category: z.string().optional(),
  note: z.string().optional()
}).refine(data => {
  if (data.type === 'transfer' && !data.to_wallet_id) return false;
  return true;
}, {
  message: "Destination wallet is required",
  path: ["to_wallet_id"]
}).refine(data => {
  if (data.type === 'transfer' && data.wallet_id === data.to_wallet_id) return false;
  return true;
}, {
  message: "Cannot transfer to same wallet",
  path: ["to_wallet_id"]
});

export type TransactionFormValues = z.infer<typeof transactionSchema>;

const CATEGORIES = ['Food', 'Transport', 'Utilities', 'Shopping', 'Entertainment', 'Health', 'Salary', 'Investment', 'Other'];

export function TransactionForm({ 
    wallets, 
    onSubmit, 
    isSubmitting,
    submitError 
}: { 
    wallets: Wallet[], 
    onSubmit: (values: TransactionFormValues) => void,
    isSubmitting: boolean,
    submitError: string
}) {
    const form = useForm<TransactionFormValues>({
        resolver: zodResolver(transactionSchema),
        defaultValues: {
            type: 'expense',
            amount: '' as any,
            currency: wallets[0]?.currency || 'USD',
            wallet_id: wallets[0]?.id || 0,
            category: 'Other',
            note: ''
        }
    });

    const type = form.watch('type');
    const walletId = form.watch('wallet_id');

    useEffect(() => {
        const w = wallets.find(w => w.id === Number(walletId));
        if (w) {
            form.setValue('currency', w.currency);
        }
    }, [walletId, wallets, form]);

    return (
        <div className="glass-panel p-6 mb-8 border border-blue-500/30 shadow-blue-500/5 relative overflow-hidden">
            <div className="absolute top-0 left-0 w-full h-1 bg-gradient-to-r from-blue-500 to-indigo-500" />
            <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
                <div className="flex flex-wrap gap-2 md:gap-4 mb-4 md:mb-6">
                    {['expense', 'income', 'transfer'].map(t => (
                        <button
                            key={t} type="button"
                            onClick={() => form.setValue('type', t as any)}
                            className={cn(
                                "flex-1 min-w-[30%] py-2 rounded-lg font-medium capitalize border transition-all text-sm md:text-base",
                                type === t
                                    ? "bg-blue-500/20 border-blue-500/50 text-blue-400"
                                    : "bg-white/5 border-white/10 text-neutral-400 hover:text-neutral-200"
                            )}
                        >
                            {t}
                        </button>
                    ))}
                </div>

                <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div>
                        <Label>Amount</Label>
                        <AmountInput {...form.register('amount')} />
                        {form.formState.errors.amount && <p className="text-rose-400 text-xs mt-1">{form.formState.errors.amount.message}</p>}
                    </div>
                    <div>
                        <Label>Currency</Label>
                        <Select {...form.register('currency')}>
                            <option value="USD">USD</option>
                            <option value="IQD">IQD</option>
                            <option value="RMB">RMB</option>
                        </Select>
                    </div>

                    <div>
                        <Label>Wallet</Label>
                        <Select {...form.register('wallet_id')}>
                            {wallets.map(w => <option key={w.id} value={w.id}>{w.name} ({w.currency})</option>)}
                        </Select>
                    </div>

                    {type === 'transfer' && (
                        <div>
                            <Label>To Wallet</Label>
                            <Select {...form.register('to_wallet_id')}>
                                <option value="">Select Destination</option>
                                {wallets.filter(w => w.id.toString() !== walletId.toString()).map(w => <option key={w.id} value={w.id}>{w.name}</option>)}
                            </Select>
                            {form.formState.errors.to_wallet_id && <p className="text-rose-400 text-xs mt-1">{form.formState.errors.to_wallet_id.message}</p>}
                        </div>
                    )}

                    {type !== 'transfer' && (
                        <div>
                            <Label>Category</Label>
                            <Select {...form.register('category')}>
                                {CATEGORIES.map(cat => <option key={cat} value={cat}>{cat}</option>)}
                            </Select>
                        </div>
                    )}

                    <div className={cn("col-span-2", type === 'transfer' && "md:col-span-2")}>
                        <Label>Note (Optional)</Label>
                        <Input type="text" {...form.register('note')} placeholder="e.g. Paid university fee" />
                    </div>
                </div>

                {submitError && (
                    <div className="bg-rose-500/10 border border-rose-500/20 text-rose-400 p-3 rounded-xl text-sm mb-4">
                        {submitError}
                    </div>
                )}

                <div className="flex justify-end pt-2 md:pt-4">
                    <Button type="submit" disabled={isSubmitting} className="w-full md:w-auto">
                        {isSubmitting ? (
                            <span className="flex items-center gap-2">
                                <div className="w-4 h-4 border-2 border-white/30 border-t-white rounded-full animate-spin" /> Saving...
                            </span>
                        ) : (
                            <>Save {type}</>
                        )}
                    </Button>
                </div>
            </form>
        </div>
    );
}