mirror of https://github.com/sveltejs/svelte
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.3 KiB
59 lines
1.3 KiB
4 years ago
|
<!-- https://eugenkiss.github.io/7guis/tasks/#flight -->
|
||
6 years ago
|
<script>
|
||
|
const tomorrow = new Date(Date.now() + 86400000);
|
||
|
|
||
6 years ago
|
let start = [
|
||
6 years ago
|
tomorrow.getFullYear(),
|
||
|
pad(tomorrow.getMonth() + 1, 2),
|
||
|
pad(tomorrow.getDate(), 2)
|
||
|
].join('-');
|
||
|
|
||
6 years ago
|
let end = start;
|
||
6 years ago
|
let isReturn = false;
|
||
|
|
||
6 years ago
|
$: startDate = convertToDate(start);
|
||
|
$: endDate = convertToDate(end);
|
||
6 years ago
|
|
||
|
function bookFlight() {
|
||
|
const type = isReturn ? 'return' : 'one-way';
|
||
|
|
||
6 years ago
|
let message = `You have booked a ${type} flight, leaving ${startDate.toDateString()}`;
|
||
6 years ago
|
if (type === 'return') {
|
||
6 years ago
|
message += ` and returning ${endDate.toDateString()}`;
|
||
6 years ago
|
}
|
||
|
|
||
|
alert(message);
|
||
|
}
|
||
|
|
||
|
function convertToDate(str) {
|
||
6 years ago
|
const split = str.split('-');
|
||
6 years ago
|
return new Date(+split[0], +split[1] - 1, +split[2]);
|
||
|
}
|
||
|
|
||
|
function pad(x, len) {
|
||
|
x = String(x);
|
||
|
while (x.length < len) x = `0${x}`;
|
||
|
return x;
|
||
|
}
|
||
|
</script>
|
||
|
|
||
|
<select bind:value={isReturn}>
|
||
|
<option value={false}>one-way flight</option>
|
||
|
<option value={true}>return flight</option>
|
||
|
</select>
|
||
|
|
||
2 years ago
|
<input type="date" bind:value={start} />
|
||
|
<input type="date" bind:value={end} disabled={!isReturn} />
|
||
6 years ago
|
|
||
2 years ago
|
<button on:click={bookFlight} disabled={isReturn && startDate >= endDate}>book</button>
|
||
4 years ago
|
|
||
|
<style>
|
||
2 years ago
|
select,
|
||
|
input,
|
||
|
button {
|
||
4 years ago
|
display: block;
|
||
|
margin: 0.5em 0;
|
||
|
font-size: inherit;
|
||
|
}
|
||
2 years ago
|
</style>
|