mirror of
https://github.com/Fluffy-Bean/TastyBites.git
synced 2024-12-28 02:16:07 +00:00
Clean up Calendar code
Add checks for notBefore a date Rename variables for easier reading
This commit is contained in:
parent
3bf9b7b0ad
commit
475ac92de9
|
@ -28,42 +28,61 @@
|
||||||
"December",
|
"December",
|
||||||
];
|
];
|
||||||
|
|
||||||
const today = new Date();
|
export let notBefore: Date;
|
||||||
|
export let selectedDate: Date;
|
||||||
|
let todayDate = new Date();
|
||||||
|
|
||||||
let date = new Date();
|
let viewingDate = new Date();
|
||||||
let year = date.getFullYear();
|
let viewingYear = viewingDate.getFullYear();
|
||||||
let month = date.getMonth();
|
let viewingMonth = viewingDate.getMonth();
|
||||||
let selectedDate: Date;
|
|
||||||
|
|
||||||
$: firstDayOffset = new Date(year, month, 0).getDay();
|
$: firstDayOffset = new Date(viewingYear, viewingMonth, 0).getDay();
|
||||||
$: monthLength = new Date(year, month + 1, 0).getDate();
|
$: monthLength = new Date(viewingYear, viewingMonth + 1, 0).getDate();
|
||||||
$: lastDayOffset = new Date(year, month, monthLength).getDay();
|
|
||||||
|
|
||||||
function last() {
|
$: isToday = (day: number) => {
|
||||||
month -= 1;
|
return (
|
||||||
|
day === todayDate.getDate()
|
||||||
|
&& viewingMonth === todayDate.getMonth()
|
||||||
|
&& viewingYear === todayDate.getFullYear()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
$: isSelected = (day: number) => {
|
||||||
|
return (
|
||||||
|
selectedDate
|
||||||
|
&& day === selectedDate.getDate()
|
||||||
|
&& viewingMonth === selectedDate.getMonth()
|
||||||
|
&& viewingYear === selectedDate.getFullYear()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
$: isBefore = (day: number) => {
|
||||||
|
const thisDate = new Date(viewingYear, viewingMonth, day);
|
||||||
|
return notBefore && thisDate < notBefore;
|
||||||
|
}
|
||||||
|
|
||||||
|
function backMonth() {
|
||||||
|
viewingMonth -= 1;
|
||||||
updateDate();
|
updateDate();
|
||||||
}
|
}
|
||||||
|
function forwardsMonth() {
|
||||||
function next() {
|
viewingMonth += 1;
|
||||||
month += 1;
|
|
||||||
updateDate();
|
updateDate();
|
||||||
}
|
}
|
||||||
|
|
||||||
function selected(day: number) {
|
|
||||||
selectedDate = new Date(year, month, day);
|
|
||||||
dispatcher("selected", { date: selectedDate });
|
|
||||||
}
|
|
||||||
|
|
||||||
function updateDate() {
|
function updateDate() {
|
||||||
if (month < 0 || month > 11) {
|
if (viewingMonth < 0 || viewingMonth > 11) {
|
||||||
date = new Date(year, month, new Date().getDate());
|
viewingDate = new Date(viewingYear, viewingMonth, new Date().getDate());
|
||||||
year = date.getFullYear();
|
viewingYear = viewingDate.getFullYear();
|
||||||
month = date.getMonth();
|
viewingMonth = viewingDate.getMonth();
|
||||||
} else {
|
} else {
|
||||||
date = new Date();
|
viewingDate = new Date();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function dateSelected(event) {
|
||||||
|
const day: number = event.target.getAttribute("data-day");
|
||||||
|
selectedDate = new Date(viewingYear, viewingMonth, day);
|
||||||
|
dispatcher("selected", { date: selectedDate });
|
||||||
|
}
|
||||||
|
|
||||||
// Full reference code:
|
// Full reference code:
|
||||||
// https://www.geeksforgeeks.org/how-to-design-a-simple-calendar-using-javascript/
|
// https://www.geeksforgeeks.org/how-to-design-a-simple-calendar-using-javascript/
|
||||||
</script>
|
</script>
|
||||||
|
@ -71,9 +90,9 @@
|
||||||
|
|
||||||
<div class="calendar">
|
<div class="calendar">
|
||||||
<div class="calendar-header">
|
<div class="calendar-header">
|
||||||
<p>{monthLabels[month]} <span>{year}</span></p>
|
<p>{monthLabels[viewingMonth]} <span>{viewingYear}</span></p>
|
||||||
<button on:click={last}><ArrowArcLeft weight="fill" /></button>
|
<button on:click={backMonth}><ArrowArcLeft weight="fill" /></button>
|
||||||
<button on:click={next}><ArrowArcRight weight="fill" /></button>
|
<button on:click={forwardsMonth}><ArrowArcRight weight="fill" /></button>
|
||||||
</div>
|
</div>
|
||||||
<div class="calendar-weeks">
|
<div class="calendar-weeks">
|
||||||
{#each weekLabels as label}
|
{#each weekLabels as label}
|
||||||
|
@ -81,28 +100,11 @@
|
||||||
{/each}
|
{/each}
|
||||||
</div>
|
</div>
|
||||||
<div class="calendar-days">
|
<div class="calendar-days">
|
||||||
{#each {length:firstDayOffset} as _}
|
{#each {length:firstDayOffset} as _}<div />{/each}
|
||||||
<div />
|
{#each {length:monthLength} as _, day}
|
||||||
{/each}
|
<div class:today={isToday(day+1)} class:selected={isSelected(day+1)} class:before={isBefore(day+1)}>
|
||||||
{#each {length:monthLength} as _, dayNum}
|
<button on:click={dateSelected} data-day={day+1}>{day+1}</button>
|
||||||
<div
|
|
||||||
class:today={
|
|
||||||
dayNum+1 === today.getDate()
|
|
||||||
&& month === today.getMonth()
|
|
||||||
&& year === today.getFullYear()
|
|
||||||
}
|
|
||||||
class:selected={
|
|
||||||
selectedDate
|
|
||||||
&& dayNum+1 === selectedDate.getDate()
|
|
||||||
&& month === selectedDate.getMonth()
|
|
||||||
&& year === selectedDate.getFullYear()
|
|
||||||
}
|
|
||||||
>
|
|
||||||
<button on:click={() => { selected(dayNum+1) }}>{dayNum+1}</button>
|
|
||||||
</div>
|
</div>
|
||||||
{/each}
|
{/each}
|
||||||
{#each {length:lastDayOffset} as _}
|
|
||||||
<!-- Not really important to add these.... -->
|
|
||||||
{/each}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -3,32 +3,25 @@
|
||||||
import Calendar from "../components/Calendar.svelte";
|
import Calendar from "../components/Calendar.svelte";
|
||||||
|
|
||||||
const specialRequestsMax = 300;
|
const specialRequestsMax = 300;
|
||||||
|
const today = new Date();
|
||||||
|
|
||||||
let name = "";
|
let name = "";
|
||||||
let email = "";
|
let email = "";
|
||||||
let telephone = "";
|
let telephone = "";
|
||||||
|
let date: Date;
|
||||||
let specialRequests = "";
|
let specialRequests = "";
|
||||||
|
|
||||||
let nameValid = true;
|
let nameValid = true;
|
||||||
let emailValid = true;
|
let emailValid = true;
|
||||||
let telephoneValid = true;
|
let telephoneValid = true;
|
||||||
|
let dateValid = true;
|
||||||
let specialRequestsValid = true;
|
let specialRequestsValid = true;
|
||||||
|
|
||||||
function validateName() {
|
function validateName() { nameValid = name.length > 1}
|
||||||
nameValid = name.length > 1
|
function validateEmail() { emailValid = email.length > 1}
|
||||||
}
|
function validateTelephone() { telephoneValid = telephone.length == 11}
|
||||||
|
function validateDate() { dateValid = date > today;}
|
||||||
function validateEmail() {
|
function validateSpecialRequests() { specialRequestsValid = specialRequests.length < 301 }
|
||||||
emailValid = email.length > 1
|
|
||||||
}
|
|
||||||
|
|
||||||
function validateTelephone() {
|
|
||||||
telephoneValid = telephone.length == 11
|
|
||||||
}
|
|
||||||
|
|
||||||
function validateSpecialRequests() {
|
|
||||||
specialRequestsValid = specialRequests.length < 301
|
|
||||||
}
|
|
||||||
|
|
||||||
function onSubmit(event) {
|
function onSubmit(event) {
|
||||||
|
|
||||||
|
@ -91,11 +84,17 @@
|
||||||
|
|
||||||
<div class="spacer" />
|
<div class="spacer" />
|
||||||
|
|
||||||
<Calendar
|
<div class="form-element">
|
||||||
on:selected={(event) => {
|
<p class="form-label">Booking Date</p>
|
||||||
console.log(event.detail.date)
|
<Calendar
|
||||||
}}
|
bind:selectedDate={date}
|
||||||
/>
|
on:selected={validateDate}
|
||||||
|
notBefore={today}
|
||||||
|
/>
|
||||||
|
{#if !dateValid}
|
||||||
|
<span class="form-notice error">Date not valid! Must chose date tomorrow or later</span>
|
||||||
|
{/if}
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="spacer" />
|
<div class="spacer" />
|
||||||
|
|
||||||
|
|
|
@ -30,6 +30,7 @@
|
||||||
padding-left: $spacing-normal;
|
padding-left: $spacing-normal;
|
||||||
flex-grow: 1;
|
flex-grow: 1;
|
||||||
font-size: $font-size-h4;
|
font-size: $font-size-h4;
|
||||||
|
font-weight: $font-weight-thin;
|
||||||
|
|
||||||
> span {
|
> span {
|
||||||
font-weight: $font-weight-bolder;
|
font-weight: $font-weight-bolder;
|
||||||
|
@ -47,8 +48,7 @@
|
||||||
|
|
||||||
font-size: $font-size-p;
|
font-size: $font-size-p;
|
||||||
|
|
||||||
border-radius: $border-radius-normal;
|
border: 0 solid transparent;
|
||||||
border: 1px solid transparent;
|
|
||||||
background-color: transparent;
|
background-color: transparent;
|
||||||
color: $color-on-dark;
|
color: $color-on-dark;
|
||||||
|
|
||||||
|
@ -95,19 +95,28 @@
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
|
||||||
border-radius: $border-radius-normal;
|
border-radius: $border-radius-circle;
|
||||||
border: 1px solid transparent;
|
border: 1px solid transparent;
|
||||||
background-color: transparent;
|
background-color: transparent;
|
||||||
|
color: $color-on-light;
|
||||||
|
|
||||||
&:hover {
|
&:hover {
|
||||||
border: 1px solid rgba($color-dark, 0.3);
|
border: 1px solid rgba($color-dark, 0.3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
&:nth-child(7n-1),
|
||||||
|
&:nth-child(7n){
|
||||||
|
> button {
|
||||||
|
color: rgba($color-on-light, 0.5);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
&.today {
|
&.today {
|
||||||
> button {
|
> button {
|
||||||
font-weight: $font-weight-black;
|
font-weight: $font-weight-black;
|
||||||
text-decoration: underline;
|
text-decoration: underline;
|
||||||
|
color: $color-on-light;
|
||||||
//background-color: $color-primary;
|
//background-color: $color-primary;
|
||||||
//color: $color-on-primary;
|
//color: $color-on-primary;
|
||||||
}
|
}
|
||||||
|
@ -120,6 +129,13 @@
|
||||||
background-color: $color-primary;
|
background-color: $color-primary;
|
||||||
color: $color-on-primary;
|
color: $color-on-primary;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
&.before {
|
||||||
|
> button {
|
||||||
|
background-color: $color-error;
|
||||||
|
color: $color-on-error;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue