I have a reservation form which contains the time of the beginning of the reservation and the period of reservation.
The period will not be sent to the backend only the time of the beginning and the end will be sent, so the end of the reservation will be calculated at the frontend and then it's gonna be sent with the time of beginning.
I'm having a problem with how to add the period to the beginning of reservation with react js.
my code:
function ReservePlace(props) {
const initialInputValues = {
period: '',
beginReservation: ''
}
const [values, setValues] = useState(initialInputValues)
const handleInputChange = (e) => {
const name = e.target.name
const value = e.target.value
setValues({
...values,
[name]: value,
})
}
const sendUserReservation = async (e) => {
e.preventDefault()
await axios.post('/users/reservation', {
...values,
endReservation: addHours(values.beginReservation, values.period),
})
}
return (
<form >
<label>Enter the reservation start time</label>
<input name="beginReservation" value={values.beginReservation} onChange={handleInputChange} type="time"/>
<label>Enter the period</label>
<input name="period" value={values.period} onChange={handleInputChange} type="number" min={1} max={6} />
<button className='btn-reserve' onClick={sendUserReservation} type="submit" >
Reserve
</button>
</form>
);
}
export default ReservePlace;