-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracking.js
209 lines (151 loc) · 7.85 KB
/
tracking.js
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
document.addEventListener('DOMContentLoaded', function() {
// event listener for when the DOM content is loaded
// see all this code??? this is a ticking time bomb waiting to explode, dont know how it works but it does
const isLoggedIn = localStorage.getItem('isLoggedIn');
// retrieves the value of 'isLoggedIn' from the local storage
const signInButton = document.getElementById('signin');
// retrieves the element with the id 'signin'
const authStatus = document.getElementById('auth-status');
// retrieves the element with the id 'auth-status'
const exportButton = document.getElementById('export-button');
// retrieves the element with the id 'export-button'
if (isLoggedIn === 'true') {
// checks if the user is logged in
const username = localStorage.getItem('username');
// retrieves the value of 'username' from the local storage
signInButton.textContent = 'Sign Out';
// sets the text content of the signInButton to 'Sign Out'
signInButton.href = 'logout.html';
// sets the href attribute of the signInButton to 'logout.html'
const usernameDisplay = document.createElement('span');
// creates a new span element
usernameDisplay.id = 'username-display';
// sets the id attribute of the usernameDisplay span element to 'username-display'
usernameDisplay.textContent = `logged in as ${username}`;
// sets the text content of the usernameDisplay span element to 'logged in as {username}'
authStatus.appendChild(usernameDisplay);
// appends the usernameDisplay span element to the authStatus element
fetchInventoryItems(username);
// calls the fetchInventoryItems function with the username as an argument
}
function fetchInventoryItems(username) {
// function to fetch inventory items for a given username
fetch(`data/${username}.txt`)
// fetches the inventory data for the given username
.then(response => response.text())
// converts the response to text
.then(data => {
// handles the fetched data
const inventoryList = document.getElementById('inventory-list');
// retrieves the element with the id 'inventory-list'
const items = data.trim().split('\n');
// trims and splits the fetched data into an array of items
items.forEach(item => {
// iterates over each item in the items array
const [name, imagePath, note] = item.split('|');
// destructures the item into name, imagePath, and note using the '|' delimiter
if (name && imagePath && note) {
// checks if all the required fields are present
const itemElement = document.createElement('div');
// creates a new div element
itemElement.className = 'inventory-item';
// sets the class name of the itemElement to 'inventory-item'
itemElement.innerHTML = `
<h3>${name}</h3>
<img src="${imagePath}" alt="${name}" class="thumbnail" />
<p>${note}</p>
<button class="delete-button">Delete</button>
`;
// sets the inner HTML of the itemElement with the item details
itemElement.querySelector('.thumbnail').addEventListener('click', () => {
// adds a click event listener to the thumbnail image
openModal(imagePath);
// calls the openModal function with the imagePath as an argument
});
itemElement.querySelector('.delete-button').addEventListener('click', () => {
// adds a click event listener to the delete button
deleteInventoryItem(username, name, imagePath);
// calls the deleteInventoryItem function with the username, name, and imagePath as arguments
itemElement.remove();
// removes the itemElement from the DOM
});
inventoryList.appendChild(itemElement);
// appends the itemElement to the inventoryList element
}
});
exportButton.disabled = false;
// enables the exportButton
exportButton.addEventListener('click', () => exportInventory(items));
// adds a click event listener to the exportButton that calls the exportInventory function with the items array as an argument
})
.catch(error => {
// handles any errors during fetching
console.error('error fetching inventory items:', error);
// logs the error message to the console
});
}
function deleteInventoryItem(username, name, imagePath) {
// function to delete an inventory item
fetch('delete.php', {
// sends a POST request to 'delete.php'
method: 'POST',
// specifies the HTTP method as POST
headers: {
'Content-Type': 'application/json'
},
// sets the request headers
body: JSON.stringify({ username, name, imagePath })
// converts the data to JSON and sets it as the request body
})
.then(response => response.text())
// converts the response to text
.then(data => {
// handles the response data
console.log(data);
// logs the response data to the console
})
.catch(error => {
// handles any errors during deletion
console.error('error deleting inventory item:', error);
// logs the error message to the console
});
}
function openModal(imagePath) {
// function to open a modal with an image
const modal = document.createElement('div');
// creates a new div element
modal.className = 'modal';
// sets the class name of the modal div element to 'modal'
modal.innerHTML = `
<span class="close-button">×</span>
<img class="modal-content" src="${imagePath}" />
`;
// sets the inner HTML of the modal div element with the image details
document.body.appendChild(modal);
// appends the modal div element to the body of the document
modal.querySelector('.close-button').addEventListener('click', () => {
// adds a click event listener to the close button
modal.remove();
// removes the modal div element from the DOM
});
}
function exportInventory(items) {
// function to export the inventory items
const content = items.join('\n');
// joins the items array into a string with newline separators
const blob = new Blob([content], { type: 'text/plain' });
// creates a new Blob object with the content string and MIME type 'text/plain'
const url = URL.createObjectURL(blob);
// creates a URL for the blob object
const a = document.createElement('a');
// creates a new anchor element
a.href = url;
// sets the href attribute of the anchor element to the URL
a.download = 'inventory.txt';
// sets the download attribute of the anchor element to 'inventory.txt'
a.click();
// programmatically clicks the anchor element to trigger the download
URL.revokeObjectURL(url);
// releases the URL object
}
});