From 475ac92de9f5ec358ec1ccc015d72d204f772659 Mon Sep 17 00:00:00 2001 From: Fluffy-Bean Date: Sat, 11 May 2024 13:50:47 +0100 Subject: [PATCH] Clean up Calendar code Add checks for notBefore a date Rename variables for easier reading --- front/src/components/Calendar.svelte | 98 ++++++++++++++-------------- front/src/pages/PageBooking.svelte | 39 ++++++----- front/src/styles/_calendar.scss | 22 ++++++- 3 files changed, 88 insertions(+), 71 deletions(-) diff --git a/front/src/components/Calendar.svelte b/front/src/components/Calendar.svelte index 4cee1c8..39ff3fa 100644 --- a/front/src/components/Calendar.svelte +++ b/front/src/components/Calendar.svelte @@ -28,42 +28,61 @@ "December", ]; - const today = new Date(); + export let notBefore: Date; + export let selectedDate: Date; + let todayDate = new Date(); - let date = new Date(); - let year = date.getFullYear(); - let month = date.getMonth(); - let selectedDate: Date; + let viewingDate = new Date(); + let viewingYear = viewingDate.getFullYear(); + let viewingMonth = viewingDate.getMonth(); - $: firstDayOffset = new Date(year, month, 0).getDay(); - $: monthLength = new Date(year, month + 1, 0).getDate(); - $: lastDayOffset = new Date(year, month, monthLength).getDay(); + $: firstDayOffset = new Date(viewingYear, viewingMonth, 0).getDay(); + $: monthLength = new Date(viewingYear, viewingMonth + 1, 0).getDate(); - function last() { - month -= 1; + $: isToday = (day: number) => { + 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(); } - - function next() { - month += 1; + function forwardsMonth() { + viewingMonth += 1; updateDate(); } - - function selected(day: number) { - selectedDate = new Date(year, month, day); - dispatcher("selected", { date: selectedDate }); - } - function updateDate() { - if (month < 0 || month > 11) { - date = new Date(year, month, new Date().getDate()); - year = date.getFullYear(); - month = date.getMonth(); + if (viewingMonth < 0 || viewingMonth > 11) { + viewingDate = new Date(viewingYear, viewingMonth, new Date().getDate()); + viewingYear = viewingDate.getFullYear(); + viewingMonth = viewingDate.getMonth(); } 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: // https://www.geeksforgeeks.org/how-to-design-a-simple-calendar-using-javascript/ @@ -71,9 +90,9 @@
-

{monthLabels[month]} {year}

- - +

{monthLabels[viewingMonth]} {viewingYear}

+ +
{#each weekLabels as label} @@ -81,28 +100,11 @@ {/each}
- {#each {length:firstDayOffset} as _} -
- {/each} - {#each {length:monthLength} as _, dayNum} -
- + {#each {length:firstDayOffset} as _}
{/each} + {#each {length:monthLength} as _, day} +
+
{/each} - {#each {length:lastDayOffset} as _} - - {/each}
diff --git a/front/src/pages/PageBooking.svelte b/front/src/pages/PageBooking.svelte index 0f2bd9e..8edc242 100644 --- a/front/src/pages/PageBooking.svelte +++ b/front/src/pages/PageBooking.svelte @@ -3,32 +3,25 @@ import Calendar from "../components/Calendar.svelte"; const specialRequestsMax = 300; + const today = new Date(); let name = ""; let email = ""; let telephone = ""; + let date: Date; let specialRequests = ""; let nameValid = true; let emailValid = true; let telephoneValid = true; + let dateValid = true; let specialRequestsValid = true; - function validateName() { - nameValid = name.length > 1 - } - - function validateEmail() { - emailValid = email.length > 1 - } - - function validateTelephone() { - telephoneValid = telephone.length == 11 - } - - function validateSpecialRequests() { - specialRequestsValid = specialRequests.length < 301 - } + function validateName() { nameValid = name.length > 1} + function validateEmail() { emailValid = email.length > 1} + function validateTelephone() { telephoneValid = telephone.length == 11} + function validateDate() { dateValid = date > today;} + function validateSpecialRequests() { specialRequestsValid = specialRequests.length < 301 } function onSubmit(event) { @@ -91,11 +84,17 @@
- { - console.log(event.detail.date) - }} - /> +
+

Booking Date

+ + {#if !dateValid} + Date not valid! Must chose date tomorrow or later + {/if} +
diff --git a/front/src/styles/_calendar.scss b/front/src/styles/_calendar.scss index 55243fa..ab2ea9a 100644 --- a/front/src/styles/_calendar.scss +++ b/front/src/styles/_calendar.scss @@ -30,6 +30,7 @@ padding-left: $spacing-normal; flex-grow: 1; font-size: $font-size-h4; + font-weight: $font-weight-thin; > span { font-weight: $font-weight-bolder; @@ -47,8 +48,7 @@ font-size: $font-size-p; - border-radius: $border-radius-normal; - border: 1px solid transparent; + border: 0 solid transparent; background-color: transparent; color: $color-on-dark; @@ -95,19 +95,28 @@ justify-content: center; align-items: center; - border-radius: $border-radius-normal; + border-radius: $border-radius-circle; border: 1px solid transparent; background-color: transparent; + color: $color-on-light; &:hover { border: 1px solid rgba($color-dark, 0.3); } } + &:nth-child(7n-1), + &:nth-child(7n){ + > button { + color: rgba($color-on-light, 0.5); + } + } + &.today { > button { font-weight: $font-weight-black; text-decoration: underline; + color: $color-on-light; //background-color: $color-primary; //color: $color-on-primary; } @@ -120,6 +129,13 @@ background-color: $color-primary; color: $color-on-primary; } + + &.before { + > button { + background-color: $color-error; + color: $color-on-error; + } + } } } }