File size: 8,765 Bytes
24d40b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232

import { useState, useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { 
  Coffee, ShoppingCart, Zap, Home as HomeIcon, 
  Car, Utensils, Briefcase, CreditCard, Calendar, MessageSquare
} from 'lucide-react';
import { Button } from '@/components/ui/button';
import { cn } from '@/lib/utils';
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
import { Calendar as CalendarComponent } from '@/components/ui/calendar';
import { toast } from 'sonner';
import { v4 as uuidv4 } from 'uuid';
import database from '@/services/database';
import { Textarea } from '@/components/ui/textarea';

const CATEGORIES = [
  { id: 'food', name: 'Food', icon: Utensils, color: 'bg-amber-100 text-amber-600' },
  { id: 'coffee', name: 'Coffee', icon: Coffee, color: 'bg-brown-100 text-brown-600' },
  { id: 'shopping', name: 'Shopping', icon: ShoppingCart, color: 'bg-indigo-100 text-indigo-600' },
  { id: 'utilities', name: 'Utilities', icon: Zap, color: 'bg-yellow-100 text-yellow-600' },
  { id: 'housing', name: 'Housing', icon: HomeIcon, color: 'bg-blue-100 text-blue-600' },
  { id: 'transport', name: 'Transport', icon: Car, color: 'bg-green-100 text-green-600' },
  { id: 'salary', name: 'Salary', icon: Briefcase, color: 'bg-emerald-100 text-emerald-600' },
  { id: 'other', name: 'Other', icon: CreditCard, color: 'bg-gray-100 text-gray-600' },
];

const TransactionForm = () => {
  const navigate = useNavigate();
  const [type, setType] = useState<'expense' | 'income'>('expense');
  const [title, setTitle] = useState('');
  const [amount, setAmount] = useState('');
  const [category, setCategory] = useState('');
  const [date, setDate] = useState<Date>(new Date());
  const [note, setNote] = useState('');
  const [message, setMessage] = useState('');
  const [isDbInitialized, setIsDbInitialized] = useState(false);

  useEffect(() => {
    const initDb = async () => {
      try {
        await database.initialize();
        setIsDbInitialized(true);
      } catch (error) {
        console.error('Failed to initialize database:', error);
        toast.error('Failed to initialize database');
      }
    };
    
    initDb();
  }, []);

  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault();
    
    if (!title || !amount || !category) {
      toast.error('Please fill in all required fields');
      return;
    }

    if (!isDbInitialized) {
      toast.error('Database not initialized yet, please try again');
      return;
    }
    
    try {
      const formattedDate = date.toISOString().split('T')[0];
      
      const id = uuidv4();
      
      database.exec(
        'INSERT INTO transactions (id, title, amount, type, category, date, note, message) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
        [id, title, parseFloat(amount), type, category, formattedDate, note || null, message || null]
      );
      
      toast.success('Transaction saved successfully');
      navigate('/transactions');
    } catch (error) {
      console.error('Error saving transaction:', error);
      toast.error('Failed to save transaction');
    }
  };

  return (
    <form onSubmit={handleSubmit} className="space-y-6 p-4 animate-fade-in">

      <div className="flex rounded-lg border border-border overflow-hidden p-1 w-full">

        <button

          type="button"

          onClick={() => setType('expense')}

          className={cn(

            "flex-1 py-2.5 text-center text-sm font-medium rounded-md transition-all",

            type === 'expense' 

              ? "bg-primary text-white" 

              : "bg-transparent text-muted-foreground hover:bg-secondary"

          )}

        >

          Expense

        </button>

        <button

          type="button"

          onClick={() => setType('income')}

          className={cn(

            "flex-1 py-2.5 text-center text-sm font-medium rounded-md transition-all",

            type === 'income' 

              ? "bg-primary text-white" 

              : "bg-transparent text-muted-foreground hover:bg-secondary"

          )}

        >

          Income

        </button>

      </div>



      <div className="space-y-2">

        <label className="text-sm font-medium">Title</label>

        <input

          type="text"

          value={title}

          onChange={(e) => setTitle(e.target.value)}

          placeholder="What was this for?"

          className="w-full p-3 rounded-lg border border-border focus:border-primary/50 focus:ring-2 focus:ring-primary/20 transition-all bg-card"

        />

      </div>



      <div className="space-y-2">

        <label className="text-sm font-medium">Amount</label>

        <div className="relative">

          <span className="absolute left-3 top-3 text-muted-foreground">$</span>

          <input

            type="number"

            value={amount}

            onChange={(e) => setAmount(e.target.value)}

            placeholder="0.00"

            className="w-full p-3 pl-8 rounded-lg border border-border focus:border-primary/50 focus:ring-2 focus:ring-primary/20 transition-all bg-card"

          />

        </div>

      </div>



      <div className="space-y-3">

        <label className="text-sm font-medium">Category</label>

        <div className="grid grid-cols-4 gap-2">

          {CATEGORIES.map((cat) => (

            <button

              key={cat.id}

              type="button"

              onClick={() => setCategory(cat.id)}

              className={cn(

                "flex flex-col items-center justify-center p-3 rounded-lg transition-all",

                category === cat.id 

                  ? "border-2 border-primary scale-in" 

                  : "border border-border hover:border-primary/50",

              )}

            >

              <div className={cn(

                "w-10 h-10 rounded-full flex items-center justify-center mb-1",

                cat.color

              )}>

                <cat.icon size={18} />

              </div>

              <span className="text-xs font-medium">{cat.name}</span>

            </button>

          ))}

        </div>

      </div>



      <div className="space-y-2">

        <label className="text-sm font-medium">Date</label>

        <Popover>

          <PopoverTrigger asChild>

            <button 

              type="button"

              className="w-full flex items-center justify-between p-3 rounded-lg border border-border focus:border-primary/50 focus:ring-2 focus:ring-primary/20 transition-all bg-card"

            >

              <span>

                {date ? date.toLocaleDateString('en-US', {

                  month: 'short',

                  day: 'numeric',

                  year: 'numeric'

                }) : 'Select date'}

              </span>

              <Calendar size={18} className="text-muted-foreground" />

            </button>

          </PopoverTrigger>

          <PopoverContent className="w-auto p-0" align="center">

            <CalendarComponent

              mode="single"

              selected={date}

              onSelect={(newDate) => newDate && setDate(newDate)}

              initialFocus

            />

          </PopoverContent>

        </Popover>

      </div>



      <div className="space-y-2">

        <label className="text-sm font-medium">Note (Optional)</label>

        <textarea

          value={note}

          onChange={(e) => setNote(e.target.value)}

          placeholder="Add a note..."

          rows={3}

          className="w-full p-3 rounded-lg border border-border focus:border-primary/50 focus:ring-2 focus:ring-primary/20 transition-all bg-card resize-none"

        />

      </div>



      <div className="space-y-2">

        <div className="flex items-center gap-2">

          <MessageSquare size={16} className="text-primary" />

          <label className="text-sm font-medium">Message (Optional)</label>

        </div>

        <Textarea

          value={message}

          onChange={(e) => setMessage(e.target.value)}

          placeholder="Add a message about this transaction..."

          className="min-h-[100px] border-border focus:border-primary/50 focus:ring-2 focus:ring-primary/20 transition-all bg-card"

        />

        <p className="text-xs text-muted-foreground">

          Add any additional information or messages related to this transaction.

        </p>

      </div>



      <Button 

        type="submit" 

        className="w-full py-6 text-base shadow-[0_2px_10px_rgba(0,122,255,0.3)]"

      >

        Save Transaction

      </Button>

    </form>
  );
};

export default TransactionForm;