-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.js
66 lines (54 loc) · 1.98 KB
/
Main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import React, { useReducer, useEffect } from "react";
import { Navigate, Route, Routes, useNavigate } from "react-router-dom";
import Booking from "./Booking";
import ConfirmedBooking from "./ConfirmedBooking";
import Header from "./Header";
const Main = () => {
// const [availableTimes, setAvailableTimes] = useState(["17:00", "18:00", "19:00", "20:00", "21:00", "22:00"])
//Chrome was blocking running the script on the index page so I added it here. "https://chromestatus.com/feature/5629709824032768"
const seededRandom = function (seed) {
var m = 2**35 - 31;
var a = 185852;
var s = seed % m;
return function () {
return (s = s * a % m) / m;
};
}
const fetchAPI = function(date) {
let result = [];
let random = seededRandom(date.getDate());
for(let i = 17; i <= 23; i++) {
if(random() < 0.5) {
result.push(i + ':00');
}
if(random() < 0.5) {
result.push(i + ':30');
}
}
return result;
};
const submitAPI = function(formData) {
return true;
};
const initialState = {availableTimes: fetchAPI(new Date())}
const [state, dispatch] = useReducer(updateTimes, initialState);
function updateTimes(state, date) {
return {availableTimes: fetchAPI(new Date(date))}
}
const navigate = useNavigate();
function submitForm (formData) {
if (submitAPI(formData)) {
navigate("/confirmed")
}
}
return(
<main className="main">
<Routes>
<Route path="/" element={<Header />} />
<Route path="/booking" element={<Booking availableTimes={state} dispatch={dispatch} submitForm={submitForm}/>} />
<Route path="/confirmed" element={<ConfirmedBooking/> } />
</Routes>
</main>
)
}
export default Main;