-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
243 lines (225 loc) · 7.38 KB
/
client.js
File metadata and controls
243 lines (225 loc) · 7.38 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
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
const addAlbumForm = document.querySelector('#add-album-form');
const fetchStartPage = async () => {
try {
const response = await fetch('/', {
method: 'GET',
headers: { 'Content-Type': 'application/json' }
});
const data = await response.json();
console.log(data);
} catch (err) {
console.log('frontend / route is kaputt')
console.log(err);
}
};
// get all albums - works
async function fetchAllAlbums() {
try {
const response = await fetch('http://localhost:3000/api/albums', {
method: 'GET',
headers: { 'Content-Type': 'application/json' }
});
if (response.ok) {
const data = await response.json();
return data;
} else {
throw new Error('Could not fetch album data');
}
} catch (err) {
console.log('frontend /albums GET route is kaputt')
console.log(err);
}
}
// get album by title - works
async function fetchAlbum(title) {
try {
const response = await fetch(`http://localhost:3000/api/albums/${title}`, {
method: 'GET',
headers: { 'Content-Type': 'application/json' }
});
if (response.ok) {
const data = await response.json();
return data;
} else {
throw new Error('Could not fetch album data');
}
} catch (err) {
console.log(`Could not fetch album with title ${title}`);
console.log(err);
}
}
// add new album - works
async function addAlbum(album) {
try {
const response = await fetch('http://localhost:3000/api/albums', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(album)
});
if (response.ok) {
const data = await response.json();
return data;
} else {
throw new Error('Could not add album');
}
} catch (err) {
console.log('frontend /albums POST route is kaputt')
console.log(err);
}
}
// edit an album - works
async function updateAlbum(id, updatedAlbum) {
try {
const response = await fetch(`http://localhost:3000/api/albums/${id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(updatedAlbum)
});
if (response.ok) {
const data = await response.json();
return data;
} else {
throw new Error('Could not update album');
}
} catch (err) {
console.log('frontend /albums PUT route is kaputt')
console.log(err);
}
}
// delete an album - works
async function deleteAlbum(id) {
const confirmDelete = window.confirm('Are you sure you want to delete this album?');
if (!confirmDelete) return;
try {
const response = await fetch(`http://localhost:3000/api/albums/${id}`, {
method: 'DELETE',
headers: { 'Content-Type': 'application/json' },
});
if (response.ok) {
const data = await response.json();
return data;
} else {
throw new Error('Could not delete album');
}
} catch (err) {
console.log('frontend /albums DELETE route is kaputt')
console.log(err);
}
}
// on page load, render all albums from the database to index.html where container is the div with id="album-list"
const renderAlbums = async () => {
const albums = await fetchAllAlbums();
const container = document.querySelector('#album-list');
container.innerHTML = '';
albums.forEach((album) => {
const div = document.createElement('div');
div.innerHTML = `
<div class="card" style="width: 18rem;">
<div class="card-body">
<h3 class="card-title">${album.title}</h3>
<p class="card-text">${album.artist}</p>
<p class="card-text">${album.year}</p>
<button type="button" class="details-btn" data-id="${album._id}">Details</button>
<button type="button" class="delete-btn" data-id="${album._id}">Delete</button>
</div>
<div id="edit-modal" class="modal">
<div class="modal-content">
<h2>Edit Album</h2>
<form id="edit-form">
<label for="title">Title:</label>
<input type="text" id="title" name="title" required><br><br>
<label for="artist">Artist:</label>
<input type="text" id="artist" name="artist" required><br><br>
<label for="year">Year:</label>
<input type="number" id="year" name="year" required><br><br>
<button type="submit" class="edit-btn" data-id="${album._id}">Edit</button>
</form>
</div>
</div>
</div>
`;
container.appendChild(div);
});
// add event listeners to buttons - unsure what the purpose of this is, but it's there
const detailsBtns = document.querySelectorAll('.details-btn');
detailsBtns.forEach((btn) => {
btn.addEventListener('click', async (e) => {
const album = await fetchAlbum(e.target.dataset.id);
});
});
const editForm = document.querySelectorAll('#edit-form');
editForm.forEach((form) => {
form.addEventListener('submit', async (e) => {
e.preventDefault();
const title = form.title.value;
const artist = form.artist.value;
const year = form.year.value;
const albumId = form.querySelector('.edit-btn').dataset.id;
const updatedAlbum = {
_id: albumId,
title: title,
artist: artist,
year: year
};
const album = await updateAlbum(albumId, updatedAlbum);
console.log(album);
location.reload();
});
});
const deleteBtns = document.querySelectorAll('.delete-btn');
deleteBtns.forEach((btn) => {
btn.addEventListener('click', async (e) => {
const album = await deleteAlbum(e.target.dataset.id);
console.log(album);
location.reload();
});
});
};
// when the page loads, render all albums
window.addEventListener('load', renderAlbums);
// event listener for finding an album by title, I wrapped it in a DOMContentLoaded event listener to avoid issues
document.addEventListener('DOMContentLoaded', () => {
const searchResultContainer = document.querySelector('#search-result-container');
const searchForm = document.querySelector('#search-form');
// add an event listener to the form for when it is submitted
searchForm.addEventListener('submit', async (e) => {
e.preventDefault();
// get the value of the input field
const title = searchForm.name.value;
// call the fetchAlbum function to fetch the album data
const album = await fetchAlbum(title);
// clear any previous results from the container
searchResultContainer.innerHTML = '';
// check if an album was found
if (album === null) {
searchResultContainer.innerHTML = '<p>No matching album found.</p>';
} else {
// display the album data
const div = document.createElement('div');
div.classList.add('card');
div.innerHTML = `
<div class="card-body">
<h3 class="card-title">${album.title}</h3>
<p class="card-text">${album.artist}</p>
<p class="card-text">${album.year}</p>
</div>
`;
searchResultContainer.appendChild(div);
}
});
});
// submit to add new album
addAlbumForm.addEventListener('submit', async (e) => {
e.preventDefault();
const title = addAlbumForm.title.value;
const artist = addAlbumForm.artist.value;
const year = addAlbumForm.year.value;
const album = { title, artist, year };
try {
const addedAlbum = await addAlbum(album);
console.log(addedAlbum);
} catch (err) {
console.log(err);
}
location.reload();
});