forked from himanshu60/concerned-toothpaste-1616
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkout.html
204 lines (180 loc) · 6.99 KB
/
checkout.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Food App-Like</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" integrity="sha512-KfkfwYDsLkIlwQp6LFnl8zNdLGxu9YAA1QvwINks4PhcElQSvqcyVLLD9aMhXd13uQjoXtEKNosOWaZqXgel0g==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="checkout.css">
</head>
<style>
a{
color: black;
}
a:hover{
color: orangered;
}
</style>
<body>
<!-- <div id="header" ></div>
<div class="container">
<div class="title">
<h2>Food Details Form</h2>
</div> -->
<div class="d-flex">
<form action="" method="" id="form">
<label>
<span class="fname">First Name <span class="required">*</span></span>
<input type="text" id="name" name="fname">
</label>
<label>
<span class="lname">Last Name <span class="required">*</span></span>
<input type="text" id="lastname" name="lname">
</label>
<label>
<span>Street Address <span class="required">*</span></span>
<input type="text" id="adress" name="houseadd" placeholder="House number and street name" required>
</label>
<label>
<!-- <span> </span> -->
<!-- <input type="text" name="apartment" placeholder="Apartment, suite, unit etc. (optional)"> -->
</label>
<label>
<span>Town / City <span class="required">*</span></span>
<input type="text" id="city" name="city">
</label>
<label>
<span>State / County <span class="required">*</span></span>
<input type="text" id="country" name="country">
</label>
<label>
<span>Postcode / ZIP <span class="required">*</span></span>
<input type="text" id="pincode" name="ZIP">
</label>
<label>
<span>Phone <span class="required">*</span></span>
<input type="tel" id="number" name="number">
</label>
<label>
<span>Email Address <span class="required">*</span></span>
<input type="email" id="email" name="email">
</label>
</form>
<div class="Yorder">
<table>
<tr>
<th colspan="2">Your order</th>
</tr>
<tr>
<td>Product Name</td>
<td id="showName">Name</td>
</tr>
<tr>
<td>Subtotal</td>
<td><i class="fa-solid fa-indian-rupee-sign"></i><span id="showtotalprice"></span></td>
</tr>
<tr>
<td>Shipping</td>
<td>Free shipping</td>
</tr>
</table><br>
<div>
<input type="radio" name="dbt" value="dbt" id="walletCheck" checked> Wallet
</div>
<p>
This payment will be deducted from your wallet balance.
</p>
<div>
<input type="radio" name="dbt" id="dbt" value="cd"> Cash on Delivery
</div>
<button type="button" id="placeOrder">Place Order</button>
</div><!-- Yorder -->
</div>
</body>
</html>
<script type="module">
import navbar from './components/navbar.js';
let header = document.getElementById('header');
header.innerHTML = navbar();
</script>
<script>
var getTotal = localStorage.getItem('total');
let data = JSON.parse(localStorage.getItem('cardData'))
let showName = document.getElementById('showName');
if(data.length>1){
showName.innerHTML = data[0].name + " "+ "etc.";
}else{
showName.innerHTML = data[0].name;
}
let showTotal = document.getElementById('showtotalprice');
showTotal.innerHTML = getTotal;
// --------------------
// input value
let order = document.getElementById('placeOrder');
let detailsArr = JSON.parse(localStorage.getItem('details')) || [];
order.addEventListener("click",function(){
var form = document.getElementById('form');
var name = document.getElementById('name').value;
var lastname = document.getElementById('lastname').value;
var adress = document.getElementById('adress').value;
var city = document.getElementById('city').value;
var country = document.getElementById('country').value;
var pincode = document.getElementById('pincode').value;
var number = document.getElementById('number').value;
var email = document.getElementById('email').value;
if(name == "" || lastname == "" || adress == "" || city == "" || country == "" || pincode == "" || number == "" || email == ""){
alert("Please fill all the fields");
}else{
detailsObj={
name:name,
lastname:lastname,
adress:adress,
city:city,
country:country,
pincode:pincode,
number:number,
email:email
}
detailsArr.push(detailsObj);
// console.log(detailsArr);
localStorage.setItem('details',JSON.stringify(detailsArr));
form.reset();
// check wallet checked or not and then send data to server
let walletCheck = document.getElementById('walletCheck');
let dbt = document.getElementById('dbt');
if(walletCheck.checked){
console.log("wallet checked");
let getTotal = localStorage.getItem('total');
let wallet = JSON.parse(localStorage.getItem('wallet'))
let total =Number(wallet)-Number(getTotal);
localStorage.setItem('wallet',total);
}
alert("Order Successfully");
// PUSH YOUR DATA IN DATABASE
let id = localStorage.getItem("id")
console.log(id)
fetch(`https://63ca391fd0ab64be2b4e8c76.mockapi.io/LoginCredientials/${id}/`).then((res)=>res.json()).then((a)=>{
console.log(a);
let cartelem = a.AddtoCart;
console.log("carelem is", cartelem)
let orders= a.OrderDetails;
// orders.push(234)
cartelem.forEach((ele)=>{
orders.push(ele)
})
console.log("orders is ",orders)
fetch(`https://63ca391fd0ab64be2b4e8c76.mockapi.io/LoginCredientials/${id}/`,{ method: 'PUT',
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify({OrderDetails:orders, AddtoCart:[]})
}).then(()=>{
let a = [];
localStorage.setItem("cardData",JSON.stringify(a))
window.location.href = "thank.html"
}) })
}
})
</script>