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
de rotation désiré.
</p>
<form class="settings" onsubmit="setEndpoint()">
<form class="settings" id="settings">
<label>
<span>Adresse IP du robot</span>
<input
type="text"
name="ip-address"
title="Adresse IP du robot (ex: 192.168.0.1)"
pattern="\d+\.\d+\.\d+\.\d+"
placeholder="192.168.0.0"
@ -84,10 +85,46 @@
console.log(text)
}
document.addEventListener('keydown', (event) => {
const key = event.keyCode
const { command, value } = keyMap[key]
sendCommand(endpoint, command, value)
/**
* Get robot endpoint address.
*
* @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() {