-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransactionForm.jsx
More file actions
178 lines (167 loc) · 5.64 KB
/
TransactionForm.jsx
File metadata and controls
178 lines (167 loc) · 5.64 KB
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
import { useMutation } from "@apollo/client";
import { CREATE_TRANSACTION } from "../graphql/mutations/transcation.mutation";
import toast from "react-hot-toast";
const TransactionForm = () => {
// TODO => WHEN RELATIONSHIPS ARE ADDED, CHANGE THE REFETCH QUERY A BIT
const [createTransaction, { loading }] = useMutation(CREATE_TRANSACTION, {
refetchQueries: ["GetTransactions", "GetTransactionStatistics"],
});
const handleSubmit = async (e) => {
e.preventDefault();
const form = e.target;
const formData = new FormData(form);
const transactionData = {
description: formData.get("description"),
paymentType: formData.get("paymentType"),
category: formData.get("category"),
amount: parseFloat(formData.get("amount")),
location: formData.get("location"),
date: formData.get("date"),
};
try {
await createTransaction({ variables: { input: transactionData } });
form.reset();
toast.success("Transaction created successfully");
} catch (error) {
toast.error(error.message);
}
};
return (
<form className='w-full max-w-lg flex flex-col gap-5 px-3' onSubmit={handleSubmit}>
{/* TRANSACTION */}
<div className='flex flex-wrap'>
<div className='w-full'>
<label
className='block uppercase tracking-wide text-white text-xs font-bold mb-2'
htmlFor='description'
>
Transaction
</label>
<input
className='appearance-none block w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500'
id='description'
name='description'
type='text'
required
placeholder='Rent, Groceries, Salary, etc.'
/>
</div>
</div>
{/* PAYMENT TYPE */}
<div className='flex flex-wrap gap-3'>
<div className='w-full flex-1 mb-6 md:mb-0'>
<label
className='block uppercase tracking-wide text-white text-xs font-bold mb-2'
htmlFor='paymentType'
>
Payment Type
</label>
<div className='relative'>
<select
className='block appearance-none w-full bg-gray-200 border border-gray-200 text-gray-700 py-3 px-4 pr-8 rounded leading-tight focus:outline-none focus:bg-white focus:border-gray-500'
id='paymentType'
name='paymentType'
>
<option value={"card"}>Card</option>
<option value={"cash"}>Cash</option>
</select>
<div className='pointer-events-none absolute inset-y-0 right-0 flex items-center px-2 text-gray-700'>
<svg
className='fill-current h-4 w-4'
xmlns='http://www.w3.org/2000/svg'
viewBox='0 0 20 20'
>
<path d='M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z' />
</svg>
</div>
</div>
</div>
{/* CATEGORY */}
<div className='w-full flex-1 mb-6 md:mb-0'>
<label
className='block uppercase tracking-wide text-white text-xs font-bold mb-2'
htmlFor='category'
>
Category
</label>
<div className='relative'>
<select
className='block appearance-none w-full bg-gray-200 border border-gray-200 text-gray-700 py-3 px-4 pr-8 rounded leading-tight focus:outline-none focus:bg-white focus:border-gray-500'
id='category'
name='category'
>
<option value={"saving"}>Saving</option>
<option value={"expense"}>Expense</option>
<option value={"investment"}>Investment</option>
</select>
<div className='pointer-events-none absolute inset-y-0 right-0 flex items-center px-2 text-gray-700'>
<svg
className='fill-current h-4 w-4'
xmlns='http://www.w3.org/2000/svg'
viewBox='0 0 20 20'
>
<path d='M9.293 12.95l.707.707L15.657 8l-1.414-1.414L10 10.828 5.757 6.586 4.343 8z' />
</svg>
</div>
</div>
</div>
{/* AMOUNT */}
<div className='w-full flex-1 mb-6 md:mb-0'>
<label className='block uppercase text-white text-xs font-bold mb-2' htmlFor='amount'>
Amount($)
</label>
<input
className='appearance-none block w-full bg-gray-200 text-gray-700 border border-gray-200 rounded py-3 px-4 leading-tight focus:outline-none focus:bg-white focus:border-gray-500'
id='amount'
name='amount'
type='number'
placeholder='150'
/>
</div>
</div>
{/* LOCATION */}
<div className='flex flex-wrap gap-3'>
<div className='w-full flex-1 mb-6 md:mb-0'>
<label
className='block uppercase tracking-wide text-white text-xs font-bold mb-2'
htmlFor='location'
>
Location
</label>
<input
className='appearance-none block w-full bg-gray-200 text-gray-700 border rounded py-3 px-4 mb-3 leading-tight focus:outline-none focus:bg-white'
id='location'
name='location'
type='text'
placeholder='New York'
/>
</div>
{/* DATE */}
<div className='w-full flex-1'>
<label className='block uppercase tracking-wide text-white text-xs font-bold mb-2' htmlFor='date'>
Date
</label>
<input
type='date'
name='date'
id='date'
className='appearance-none block w-full bg-gray-200 text-gray-700 border rounded py-[11px] px-4 mb-3 leading-tight focus:outline-none
focus:bg-white'
placeholder='Select date'
/>
</div>
</div>
{/* SUBMIT BUTTON */}
<button
className='text-white font-bold w-full rounded px-4 py-2 bg-gradient-to-br
from-pink-500 to-pink-500 hover:from-pink-600 hover:to-pink-600
disabled:opacity-70 disabled:cursor-not-allowed'
type='submit'
disabled={loading}
>
{loading ? "Loading..." : "Add Transaction"}
</button>
</form>
);
};
export default TransactionForm;