-
Notifications
You must be signed in to change notification settings - Fork 0
/
createcustomer.html
114 lines (102 loc) · 3.75 KB
/
createcustomer.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Create Customer</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<nav class="navbar">
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<a href="userhome.html">Home</a>
<a href="managecustomer.html">Customer Management</a>
<a href="transaction.html">Transaction</a>
<a href="manageitem.html">Stock Management</a>
<a href="updatepassword.html">Update Password</a>
</div>
<span class="open" onclick="openNav()">  ☰  
<div class="logo">
<a href="">
<img src="assets/sale-sign.png" alt="SDS_logo">
<span>  SALES PILOT  </span>
<img src="assets/cashier-machine.png" alt="SDS_logo">
</a>
</div>
</span>
<a class="logout" href="#" onclick="logout()">Log Out  </a>
</nav>
<form id="customerForm">
<table class="createCust">
<caption>Create Customer</caption>
<tr>
<td><label for="customer_name">Customer Name:</label></td>
<td><input type="text" id="customer_name" name="customer_name" required></td>
</tr>
<tr>
<td><label for="customer_contact">Customer Contact:</label></td>
<td><input type="text" id="customer_contact" name="customer_contact" required></td>
</tr>
<tr>
<td><label for="customer_email">Customer Email:</label></td>
<td><input type="email" id="customer_email" name="customer_email" required></td>
</tr>
<tr>
<td></td>
<td><button type="submit">Create Customer</button><br></td>
</tr>
</table>
</form>
<div id="result"></div>
<script>
// Open Side Navigation Bar
function openNav() {
document.getElementById("mySidenav").style.width = "280px";
}
// Close Side Navigation Bar
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
}
const token = localStorage.getItem("token");
if (!token) {
window.location.href = "login.html";
}
document.getElementById('customerForm').addEventListener('submit', createCustomer);
async function createCustomer(event) {
event.preventDefault();
const form = event.target;
const customer_name = form.elements.customer_name.value;
const customer_contact = form.elements.customer_contact.value;
const customer_email = form.elements.customer_email.value;
const payload = { customer_name, customer_contact, customer_email };
try {
const response = await fetch('http://localhost:3000/api/customer', {
method: 'POST',
headers: {
"Authorization": `Bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
});
if (response.ok) {
const responseData = await response.json();
alert("Customer created successfully");
location.reload();
window.location.href='managecustomer.html';
} else {
const errorData = await response.json();
const errorMessage = errorData.errors[0].msg;
alert(`Error creating customer: ${errorMessage}`);
location.reload();
window.location.href='managecustomer.html';
}
} catch (error) {
console.error(error);
alert('An error occurred while creating the customer.');
location.reload();
window.location.href='managecustomer.html';
}
}
</script>
</body>
</html>