-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.tsx
333 lines (293 loc) · 12.4 KB
/
index.tsx
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import { component$, useContext, useSignal, useStore, useStylesScoped$, useTask$, useVisibleTask$ } from '@builder.io/qwik';
import styles from "./checkout.css?inline"
import { APP_STATE, DEFAULT_USER } from '~/utils/constants';
import type { AppState, CartItem, User } from '~/utils/types';
import { server$, useNavigate } from '@builder.io/qwik-city';
import cartDataAdapter from '~/utils/cartDataAdapter';
import { client } from '~/utils/turso-db';
const placeOrder = server$(async (appState: AppState, contactInformation: {firstName: string, lastName: string, email: string, phone: number}, shippingAddress: {zipCode: number, country: string}, authenticatedUser: User ) => {
// * After having checked that the payment was successfull using your payment client
// check for missing input
if(!contactInformation.firstName || !contactInformation.lastName || !contactInformation.email || !contactInformation.phone || !shippingAddress.country || !shippingAddress.zipCode){
return {
status: "error",
message: "Some fields are missing!",
data: null
}
}
const amount = appState.cart.items.reduce((accumulator, currentVal) => accumulator + (currentVal.count * currentVal.product.price), 0);
const calculatedShippingFees = 0;
const discountAmount = 0;
const finalAmount = amount + calculatedShippingFees - discountAmount;
try {
const transaction = await client.transaction();
const newOrder = await transaction.execute({
sql: "insert into orders(user_id, customer_name, amount, shipping_fees, discount_amt, final_amount, shipping_address, paid) values(?, ?, ?, ?, ?, ?, ?, ?)",
args: [authenticatedUser.id, `${authenticatedUser.first_name} ${authenticatedUser.last_name}`, amount, calculatedShippingFees, discountAmount, finalAmount, `${shippingAddress.zipCode} ${shippingAddress.country}`, true]
});
for(const item of appState.cart.items){
transaction.execute({
sql: "insert into order_items(order_id, product_id, count) values(?, ?, ?);",
args: [newOrder.lastInsertRowid as unknown as number, item.product.id, item.count]
});
transaction.execute({
sql: "delete from cart_items where id = ?;",
args: [item.id]
});
}
await transaction.commit();
return {
status: "success",
message: "Order placed!",
data: true
}
} catch (error) {
// TODO: Catch error and notify user
}
return {
status: "failure",
message: "Could not create an order!",
data: null
}
})
export default component$(() => {
useStylesScoped$(styles);
const nav = useNavigate();
const appState = useContext(APP_STATE);
const authenticatedUser = DEFAULT_USER;
const shippingAddress = useStore({
country: "",
zipCode: 0
})
const contactInformation = useStore({
firstName: "",
lastName: "",
email: "",
phone: 0
})
const submissionError = useSignal("");
useVisibleTask$(() => {
contactInformation.email = authenticatedUser.email;
contactInformation.lastName = authenticatedUser.last_name;
contactInformation.firstName = authenticatedUser.first_name;
shippingAddress.country = "England";
})
const totalPrice = appState.cart.items.reduce((accumulator, currentVal) => accumulator + (currentVal.count * currentVal.product.price), 0).toFixed(2);
// will run atleast once
useTask$(async () => {
const storedCartItems = await client.execute({
sql: "select cart_items.count, cart_items.id as cart_item_id, products.* from cart_items left join products on products.id = cart_items.product_id where user_id = ?",
args: [authenticatedUser.id]
});
appState.cart.items = cartDataAdapter(storedCartItems.rows as unknown as CartItem[]);
});
return (
<section>
<h1 class="sr-only">Checkout</h1>
<div class="mx-auto grid max-w-screen-2xl grid-cols-1 md:grid-cols-2">
<div class="bg-gray-50 py-12 md:py-24">
<div class="mx-auto max-w-lg space-y-8 px-4 lg:px-8">
<div class="flex items-center gap-4">
<span class="h-10 w-10 rounded-full bg-blue-700"></span>
<h2 class="font-medium text-gray-900">TurQw store</h2>
</div>
<div>
<p class="text-2xl font-medium tracking-tight text-gray-900">
${totalPrice}
</p>
<p class="mt-1 text-sm text-gray-600">For the purchase of</p>
</div>
<div>
<div class="flow-root">
<ul class="-my-4 divide-y divide-gray-100">
{ appState.cart.items?.map((item: CartItem) =>
<li key={item.id} class="flex items-center gap-4 py-4">
<img
src={item.product.image}
alt=""
class="h-16 w-16 rounded object-cover"
/>
<div>
<h3 class="text-sm text-gray-900">{item.product.name} X {item.count}</h3>
<dl class="mt-0.5 space-y-px text-[10px] text-gray-600">
<div>
<dt class="inline">${item.product.price}:</dt>
</div>
</dl>
</div>
</li>)
}
</ul>
</div>
</div>
</div>
</div>
<div class="bg-white py-12 md:py-24">
<div class="mx-auto max-w-lg px-4 lg:px-8">
{submissionError.value && <p class="text-red-600 font-semibold">{submissionError.value}</p>}
<form class="grid grid-cols-6 gap-4">
<legend class="col-span-6">Contact Information</legend>
<div class="col-span-3">
<label
for="FirstName"
class="block text-xs font-medium text-gray-700"
>
First Name
</label>
<input
type="text"
id="FirstName"
class="mt-1 w-full rounded-md border-gray-200 shadow-sm sm:text-sm"
value={contactInformation.firstName}
onInput$={(e) => {
contactInformation.firstName = (e.target as HTMLInputElement).value;
}}
/>
</div>
<div class="col-span-3">
<label
for="LastName"
class="block text-xs font-medium text-gray-700"
>
Last Name
</label>
<input
type="text"
id="LastName"
class="mt-1 w-full rounded-md border-gray-200 shadow-sm sm:text-sm"
value={contactInformation.lastName}
onInput$={(e) => {
contactInformation.lastName = (e.target as HTMLInputElement).value;
}}
/>
</div>
<div class="col-span-6">
<label for="Email" class="block text-xs font-medium text-gray-700">
Email
</label>
<input
type="email"
id="Email"
class="mt-1 w-full rounded-md border-gray-200 shadow-sm sm:text-sm"
value={contactInformation.email}
onInput$={(e) => {
contactInformation.email = (e.target as HTMLInputElement).value;
}}
/>
</div>
<div class="col-span-6">
<label for="Phone" class="block text-xs font-medium text-gray-700">
Phone
</label>
<input
type="tel"
id="Phone"
class="mt-1 w-full rounded-md border-gray-200 shadow-sm sm:text-sm"
value={contactInformation.phone}
onInput$={(e) => {
const typedVal = (e.target as HTMLInputElement).value;
if(!isNaN(parseInt(typedVal))){
contactInformation.phone = parseInt(typedVal);
}
}}
/>
</div>
<fieldset class="col-span-6">
<legend class="block text-sm font-medium text-gray-700">
Card Details
</legend>
<div class="mt-1 -space-y-px rounded-md bg-white shadow-sm">
<div>
<label for="CardNumber" class="sr-only"> Card Number </label>
<input
type="text"
id="CardNumber"
placeholder="Card Number"
class="relative mt-1 w-full rounded-t-md border-gray-200 focus:z-10 sm:text-sm"
/>
</div>
<div class="flex -space-x-px rtl:space-x-reverse">
<div class="flex-1">
<label for="CardExpiry" class="sr-only"> Card Expiry </label>
<input
type="text"
id="CardExpiry"
placeholder="Expiry Date"
class="relative w-full border-gray-200 focus:z-10 ltr:rounded-bl-md rtl:rounded-br-md sm:text-sm"
/>
</div>
<div class="flex-1">
<label for="CardCVC" class="sr-only"> Card CVC </label>
<input
type="text"
id="CardCVC"
placeholder="CVC"
class="relative w-full border-gray-200 focus:z-10 ltr:rounded-br-md rtl:rounded-bl-md sm:text-sm"
/>
</div>
</div>
</div>
</fieldset>
<fieldset class="col-span-6">
<legend class="block text-sm font-medium text-gray-700">
Billing Address
</legend>
<div class="mt-1 -space-y-px rounded-md bg-white shadow-sm">
<div>
<label for="Country" class="sr-only">Country</label>
<select
id="Country"
class="relative w-full rounded-t-md border-gray-200 focus:z-10 sm:text-sm"
onChange$={(e) => {
shippingAddress.country = (e.target as HTMLSelectElement).value;
}}
>
<option>England</option>
<option>Wales</option>
<option>Scotland</option>
<option>France</option>
<option>Belgium</option>
<option>Japan</option>
</select>
</div>
<div>
<label class="sr-only" for="PostalCode"> ZIP/Post Code </label>
<input
type="text"
id="PostalCode"
placeholder="ZIP/Post Code"
class="relative w-full rounded-b-md border-gray-200 focus:z-10 sm:text-sm"
onInput$={(e) => {
shippingAddress.zipCode = parseInt((e.target as HTMLInputElement).value);
}}
/>
</div>
</div>
</fieldset>
<div class="col-span-6">
<button
class="block w-full rounded-md bg-black p-2.5 text-sm text-white transition hover:shadow-lg"
preventdefault:submit
preventdefault:click
onClick$={async () => {
submissionError.value = "";
const response = await placeOrder(appState, contactInformation, shippingAddress, authenticatedUser);
if(response.status === "error"){
submissionError.value = response.message;
}
if(response.status === "success"){
alert("Order successfully placed!");
nav("/category/furniture");
}
}}
>
Pay Now
</button>
</div>
</form>
</div>
</div>
</div>
</section>
)
});