feat(client): add form settings handler

This commit is contained in:
Julien Oculi 2024-06-25 12:46:11 +02:00
parent 8d561f14c3
commit 75959df9c2

View file

@ -18,11 +18,12 @@
adresse IP ainsi que lui indiquer la distance à parcourir ou l'angle adresse IP ainsi que lui indiquer la distance à parcourir ou l'angle
de rotation désiré. de rotation désiré.
</p> </p>
<form class="settings" onsubmit="setEndpoint()"> <form class="settings" id="settings">
<label> <label>
<span>Adresse IP du robot</span> <span>Adresse IP du robot</span>
<input <input
type="text" type="text"
name="ip-address"
title="Adresse IP du robot (ex: 192.168.0.1)" title="Adresse IP du robot (ex: 192.168.0.1)"
pattern="\d+\.\d+\.\d+\.\d+" pattern="\d+\.\d+\.\d+\.\d+"
placeholder="192.168.0.0" placeholder="192.168.0.0"
@ -84,11 +85,47 @@
console.log(text) console.log(text)
} }
document.addEventListener('keydown', (event) => { /**
const key = event.keyCode * Get robot endpoint address.
const { command, value } = keyMap[key] *
sendCommand(endpoint, command, value) * @returns {string}
}) */
function getEndpoint() {
const endpoint = sessionStorage.getItem('robot-endpoint')
if (endpoint === null) {
alert("Aucune adresse IP n'a été renseignée !")
throw new Error('no given ip address')
}
return endpoint
}
/**
* Set robot endpoint address.
*
* @param {string} ip Robot ip.
*
* @returns void
*/
function setEndpoint(ip) {
sessionStorage.setItem('robot-endpoint')
}
document
.querySelector('#settings')
.addEventListener('submit', (event) => {
// Don't interrupt event if not form submitting
if (!(event instanceof SubmitEvent)) return true
if (event.target === null) return true
// Disable form sending
event.preventDefault()
const form = new FormData(event.target)
const ipAddress = form.get('ip-address')
if (ipAddress === null) return
setEndpoint(`http://${ipAddress}`)
})
function storeInput() { function storeInput() {
ipInput = document.getElementById('ip').value ipInput = document.getElementById('ip').value