-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathflight.html
More file actions
93 lines (76 loc) · 2.25 KB
/
Copy pathflight.html
File metadata and controls
93 lines (76 loc) · 2.25 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<form>
<select id="type">
<option value="oneway">One-way flight</option>
<option value="returning">Return flight</option>
</select>
<input type="text" id="startDate" />
<input type="text" id="endDate" />
<button type="submit" id="book" disabled>Book</button>
</form>
<script type="importmap">
{
"imports": {
"rescript/": "/node_modules/rescript/",
"@rescript/": "/node_modules/@rescript/"
}
}
</script>
<script type="module">
import { FlightBooker, Field } from './flight.res.js';
let $form = document.querySelector('form');
let $type = document.getElementById('type');
let $startDate = document.getElementById('startDate');
let $endDate = document.getElementById('endDate');
let $book = document.getElementById('book');
let bind = state => {
let type = FlightBooker.State.getType(state);
let startDate = FlightBooker.State.getStartDate(state);
let endDate = FlightBooker.State.getEndDate(state);
$form.onsubmit = e => {
e.preventDefault();
window.alert(`You have booked a ${FlightBooker.Util.formatType(type)} on ${startDate}`);
};
$type.value = type;
$startDate.value = startDate;
if (Field.validate(startDate)) {
$startDate.classList.remove('invalid');
} else {
$startDate.classList.add('invalid');
}
$endDate.value = endDate;
$endDate.disabled = (type === 'oneway');
if (Field.validate(endDate)) {
$endDate.classList.remove('invalid');
} else {
$endDate.classList.add('invalid');
}
$book.disabled = !(
Field.validate(startDate) &&
(Field.validate(endDate) || $endDate.disabled)
)
};
let program = FlightBooker.make(bind);
FlightBooker.init(program);
$type.onchange = e => {
let type = e.currentTarget.value;
program = FlightBooker.selectType(program, type);
};
$startDate.onchange = e => {
let startDate = e.currentTarget.value;
program = FlightBooker.changeStartDate(program, startDate);
};
$endDate.onchange = e => {
let endDate = e.currentTarget.value;
program = FlightBooker.changeEndDate(program, endDate);
};
</script>
<style>
form {
display: grid;
gap: 0.5rem;
max-width: 20rem;
}
.invalid:not(:disabled) {
border: 1px solid red;
}
</style>