-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
200 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
export default class inputValidator{ | ||
constructor(responseCode, message) { | ||
this.responseCode = responseCode; | ||
this.message = message; | ||
} | ||
validate(xVal, yVal, rVal){ | ||
if (![-3, -2, -1, 0, 1, 2, 3, 4, 5].includes(xVal)) { | ||
this.message = "The X value should be something from this array: [-3, -2, -1, 0, 1, 2, 3, 4, 5]"; | ||
this.responseCode = 0; | ||
} else if (!(-5 < yVal && yVal < 5)){ | ||
this.message = "The Y value should be in (-5, 5) interval"; | ||
this.responseCode = 0; | ||
} else if (![1, 1.5, 2, 2.5, 3].includes(rVal)){ | ||
this.message = "The R value should be something from this array: [1, 1.5, 2, 2.5, 3]"; | ||
this.responseCode = 0; | ||
} else { | ||
this.responseCode = 1; | ||
} | ||
} | ||
|
||
getResponseCode(){ | ||
return this.responseCode; | ||
} | ||
getMessage(){ | ||
return this.message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import inputValidator from "./inputValidator.js"; | ||
|
||
"use strict"; | ||
|
||
document.addEventListener("DOMContentLoaded", () => { | ||
// Update Date and Time Function | ||
function updateDateTime() { | ||
const now = new Date(); | ||
document.getElementById("date").innerText = now.toDateString(); | ||
document.getElementById("time").innerText = now.toTimeString().substring(0, 8); | ||
} | ||
|
||
// Initial Date Time Update | ||
updateDateTime(); | ||
setInterval(updateDateTime, 1000); | ||
}); | ||
|
||
window.onload = function () { | ||
// const savedData = JSON.parse(localStorage.getItem('tableData')) || []; | ||
// savedData.forEach(data => { | ||
// addToTable(data.x, data.y, data.r, data.result, data.curr_time, data.exec_time); | ||
// }) | ||
console.log(localStorage.getItem("session")); | ||
document.getElementById("output").innerHTML = localStorage.getItem("session"); | ||
} | ||
|
||
const mainForm = document.querySelector('#input-form'); | ||
mainForm.addEventListener('submit', function (e) { | ||
// default action is to send the form data to the server and reload the page | ||
// by calling .preventDefault() i am stopping the browser from doing this, | ||
// which allows me to handle the form submission programmatically in your JavaScript code instead. | ||
e.preventDefault(); | ||
|
||
const xElement = document.querySelector('input[name="x"]:checked'); | ||
const yElement = document.querySelector('#y'); | ||
const rElement = document.querySelector('#r'); | ||
|
||
if (xElement && yElement && rElement) { | ||
const xVal = parseFloat(xElement.value); | ||
const yVal = parseFloat(yElement.value.substring(0, 8)); | ||
const rVal = parseFloat(rElement.value); | ||
console.log(`X: ${xVal}, Y: ${yVal}, R: ${rVal}`); | ||
|
||
const validator = new inputValidator(); | ||
validator.validate(xVal, yVal, rVal); | ||
|
||
if (validator.getResponseCode() === 1) { | ||
console.log(`everything is ok`); | ||
|
||
fetch(`./script.php?xVal=${xVal}&yVal=${yVal}&rVal=${rVal}`, { | ||
method: 'GET', | ||
}) | ||
.then(response => { | ||
if (!response.ok) { | ||
throw new Error(`Server responded with bad getaway status: ${response.status}`); | ||
} | ||
return response.text(); | ||
}) | ||
.then(function (serverAnswer) { | ||
localStorage.setItem("session", serverAnswer); | ||
document.getElementById("output").innerHTML = serverAnswer; | ||
// addToTable(xVal, yVal, rVal, responseData.result, responseData.curr_time, responseData.exec_time); | ||
// saveToLocalStorage(xVal, yVal, rVal, responseData.result, responseData.curr_time, responseData.exec_time); | ||
}) | ||
.catch(error => { | ||
alert(`There was an error processing your request: ${error.message}`) | ||
}) | ||
} else { | ||
Toastify({ | ||
text: validator.getMessage(), | ||
className: "info", | ||
style: { | ||
background: "linear-gradient(to right, #00b09b, #96c93d)", | ||
border: "1px solid white" | ||
}, | ||
offset: { | ||
x: 240, | ||
y: 60 | ||
}, | ||
position: "right", | ||
}).showToast(); | ||
} | ||
} else { | ||
Toastify({ | ||
text: "You should fill the form before submitting it :)", | ||
className: "info", | ||
style: { | ||
background: "linear-gradient(to right, #00b09b, #96c93d)", | ||
border: "1px solid white" | ||
}, | ||
offset: { | ||
x: 240, | ||
y: 60 | ||
}, | ||
position: "right", | ||
}).showToast(); | ||
} | ||
}); | ||
|
||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.