gimm285-api-project/insert.html

303 lines
9.9 KiB
HTML
Raw Normal View History

2023-04-18 14:41:16 -06:00
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Kaj Forney -- GIMM 285 CRUD API Project</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="manifest" href="site.webmanifest">
<link rel="apple-touch-icon" href="icon.png">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/main.css">
<meta name="theme-color" content="#488ea7">
</head>
<body>
<div id="header" class="container">
<h1>Add New Game</h1>
<hr/>
</div>
<div id="form">
<div class="container">
<div class="row mb-3">
<div class="col">
<label>Title</label>
</div>
<div class="col">
<input id="title" type="text"/>
</div>
<div class="col"></div>
</div>
<div class="row mb-3">
<div class="col">
2023-04-29 20:48:03 -06:00
<label>
Developer
<br/>
(<a href="insertDeveloper.html">Add New Developer</a>)
</label>
2023-04-18 14:41:16 -06:00
</div>
<div class="col">
<select id="developer">
2023-04-29 20:48:03 -06:00
2023-04-18 14:41:16 -06:00
</select>
</div>
<div class="col"></div>
</div>
<div class="row mb-3">
<div class="col">
2023-04-29 20:48:03 -06:00
<label>
Publisher
<br/>
(<a href="insertPublisher.html">Add New Publisher</a>)
</label>
2023-04-18 14:41:16 -06:00
</div>
<div class="col">
<select id="publisher">
2023-04-29 20:48:03 -06:00
2023-04-18 14:41:16 -06:00
</select>
</div>
<div class="col"></div>
</div>
<div class="row mb-3">
<div class="col">
<label>Release Date</label>
</div>
<div class="col">
<input id="releaseDate" type="date"/>
</div>
<div class="col"></div>
</div>
<div class="row mb-3">
<div class="col">
<label>Supported OSes</label>
</div>
<div class="col">
<div class="row">
<div class="col">
<span>Windows</span>
<input id="win" type="checkbox"/>
</div>
<div class="col">
<span>MacOS</span>
<input id="mac" type="checkbox"/>
</div>
<div class="col">
<span>Linux</span>
<input id="linux" type="checkbox"/>
</div>
</div>
</div>
<div class="col"></div>
</div>
2023-04-29 20:48:03 -06:00
<div class="row mb-3">
<div class="col">
<label>Cover Art</label>
</div>
<div class="col">
<input id="coverArt" type="file"
accept="image/jpeg, image/png"/>
</div>
<div class="col"></div>
</div>
2023-04-18 14:41:16 -06:00
<div class="row mb-3">
<div class="col">
<label>Short Description</label>
2023-04-29 20:48:03 -06:00
<p>(maximum 300 characters)</p>
2023-04-18 14:41:16 -06:00
</div>
<div class="col">
2023-04-29 20:48:03 -06:00
<textarea id="shortDescription" rows="5" cols="40" maxlength="300"></textarea>
2023-04-18 14:41:16 -06:00
</div>
<div class="col"></div>
</div>
<div class="row mb-3">
<div class="col">
<label>Long Description</label>
2023-04-29 20:48:03 -06:00
<p>(supports <a href="https://www.markdownguide.org/">Markdown</a>) </p>
2023-04-18 14:41:16 -06:00
</div>
<div class="col">
<textarea id="longDescription" rows="15" cols="40"></textarea>
</div>
<div class="col"></div>
</div>
<div class="row mb-3">
<div class="col align-content-center">
<button id="submitButton" type="submit">Submit</button>
</div>
</div>
<hr/>
</div>
2023-04-29 20:48:03 -06:00
<div class="row mb-3">
<div class="col"></div>
<div class="col card" id="outputMessage"></div>
<div class="col"></div>
</div>
2023-04-18 14:41:16 -06:00
</div>
<footer id="footer">
<p>Page created by <a href="https://lunarpenguin.net/">Kaj Forney</a>, as work for class (GIMM 285).</p>
2023-04-29 20:48:03 -06:00
<p>All example data used is originally from <a href="https://store.steampowered.com/">Steam</a>.</p>
2023-04-18 14:41:16 -06:00
</footer>
<script src="js/vendor/modernizr-3.11.2.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4"
crossorigin="anonymous" defer></script>
<script>
const isEmpty = (obj) => Object.keys(obj).length === 0;
2023-04-29 20:48:03 -06:00
document.getElementById("outputMessage").hidden = true;
fetch("http://localhost:8787/developers", { method: 'GET' })
.then((response) => {
return new Promise((resolve) => response.json()
.then((json) => resolve({
status: response.status,
json,
})
));
})
.then(({ status, json }) => {
if (200 === status) {
const developerSelect = document.getElementById('developer');
for (dev of json.data) {
let option = document.createElement("option");
option.value = dev.id;
option.text = dev.name;
developerSelect.add(option);
}
}
})
.catch(error => {
console.error('Error:', error);
});
fetch("http://localhost:8787/publishers", { method: 'GET' })
.then((response) => {
return new Promise((resolve) => response.json()
.then((json) => resolve({
status: response.status,
json,
})
));
})
.then(({ status, json }) => {
if (200 === status) {
const publisherSelect = document.getElementById('publisher');
for (publisher of json.data) {
let option = document.createElement("option");
option.value = publisher.id;
option.text = publisher.name;
publisherSelect.add(option);
}
}
})
.catch(error => {
console.error('Error:', error);
});
2023-04-18 14:41:16 -06:00
document.getElementById('submitButton').addEventListener('click', (event) => {
//const formData = new FormData();
2023-04-29 20:48:03 -06:00
const parameters = {};
2023-04-18 14:41:16 -06:00
if (document.getElementById('title').value.length !== 0) {
//formData.append('title', document.getElementById('title').value);
2023-04-29 20:48:03 -06:00
parameters.title = document.getElementById('title').value;
2023-04-18 14:41:16 -06:00
}
if (document.getElementById('developer').value.length !== 0) {
//formData.append('developer', document.getElementById('developer').value);
2023-04-29 20:48:03 -06:00
parameters.developerID = document.getElementById('developer').value;
2023-04-18 14:41:16 -06:00
}
if (document.getElementById('publisher').value.length !== 0) {
//formData.append('publisher', document.getElementById('publisher').value);
2023-04-29 20:48:03 -06:00
parameters.publisherID = document.getElementById('publisher').value;
2023-04-18 14:41:16 -06:00
}
if (document.getElementById('releaseDate').value.length !== 0) {
//formData.append('releaseDate', document.getElementById('releaseDate').value);
2023-04-29 20:48:03 -06:00
parameters.releaseDate = document.getElementById('releaseDate').value;
}
if (document.getElementById('win').checked) {
parameters.win = 1;
}
else {
parameters.win = 0;
}
if (document.getElementById('mac').checked) {
parameters.mac = 1;
2023-04-18 14:41:16 -06:00
}
2023-04-29 20:48:03 -06:00
else {
parameters.mac = 0;
2023-04-18 14:41:16 -06:00
}
2023-04-29 20:48:03 -06:00
if (document.getElementById('linux').checked) {
parameters.linux = 1;
2023-04-18 14:41:16 -06:00
}
2023-04-29 20:48:03 -06:00
else {
parameters.linux = 0;
2023-04-18 14:41:16 -06:00
}
2023-04-29 20:48:03 -06:00
if (document.getElementById('shortDescription').value.length !== 0) {
parameters.shortDescription = document.getElementById('shortDescription').value;
2023-04-18 14:41:16 -06:00
}
2023-04-29 20:48:03 -06:00
if (document.getElementById('longDescription').value.length !== 0) {
parameters.longDescription = document.getElementById('longDescription').value;
2023-04-18 14:41:16 -06:00
}
2023-04-29 20:48:03 -06:00
const formData = new FormData();
const coverField = document.querySelector('input[type="file"]');
formData.append("coverArt", coverField.files[0]);
2023-04-18 14:41:16 -06:00
//Settings for FETCH API request
let fetchSettings = {
2023-04-29 20:48:03 -06:00
method: 'POST',
body: formData
2023-04-18 14:41:16 -06:00
};
//Send FETCH API request
2023-04-29 20:48:03 -06:00
fetch("http://localhost:8787/games" + (!isEmpty(parameters) ? '?' + new URLSearchParams(parameters) : ''), fetchSettings)
2023-04-18 14:41:16 -06:00
.then((response) => {
return new Promise((resolve) => response.json()
.then((json) => resolve({
status: response.status,
json,
})
));
})
.then(({status, json}) => {
if (status === 200) {
2023-04-29 20:48:03 -06:00
let message = "Game submission successful!"
document.getElementById('outputMessage').hidden = false;
document.getElementById('outputMessage').innerHTML = message;
2023-04-18 14:41:16 -06:00
}
})
.catch(error => {
console.error('Error:', error);
});
return;
});
</script>
</body>
</html>