|
| 1 | +import { component$ } from "@builder.io/qwik"; |
| 2 | + |
| 3 | +import css from "./days.module.css"; |
| 4 | + |
| 5 | +const Day = component$((props) => { |
| 6 | + const thisDate = new Date(props.dateObj); |
| 7 | + const today = new Date(); |
| 8 | + |
| 9 | + let textColor = thisDate.getDay() === 0 || thisDate.getDay() === 6 ? "#d10000" : "#000000"; |
| 10 | + |
| 11 | + let bgColor = Math.round(props.dateObj / 8.64e7) === Math.round(today.valueOf() / 8.64e7) ? "#FFFF76" : "transparent" |
| 12 | + |
| 13 | + return ( |
| 14 | + <> |
| 15 | + <button class={css.day} style={{color: textColor, backgroundColor: bgColor}}> |
| 16 | + <abbr>{thisDate.getDate()}</abbr> |
| 17 | + </button> |
| 18 | + </> |
| 19 | + ); |
| 20 | +}); |
| 21 | + |
| 22 | +const PriorDay = component$((props) => { |
| 23 | + const thisDate = new Date(props.dateObj); |
| 24 | + |
| 25 | + return ( |
| 26 | + <> |
| 27 | + <button disabled class={css.priorDay}> |
| 28 | + <abbr>{thisDate.getDate()}</abbr> |
| 29 | + </button> |
| 30 | + </> |
| 31 | + ); |
| 32 | +}) |
| 33 | + |
| 34 | +function getDaysSinceLastSunday(dateObj){ |
| 35 | + let days = []; |
| 36 | + |
| 37 | + while (dateObj.getDay() !== 0){ |
| 38 | + dateObj.setDate(dateObj.getDate() - 1); |
| 39 | + days.unshift(<PriorDay key={dateObj.valueOf()} dateObj={dateObj.valueOf()}/>); |
| 40 | + } |
| 41 | + |
| 42 | + return days; |
| 43 | +} |
| 44 | + |
| 45 | +function getMonthLength(dateObj){ |
| 46 | + let year = dateObj.getFullYear(); |
| 47 | + let month = dateObj.getMonth() + 1; |
| 48 | + |
| 49 | + return new Date(year, month, 0).getDate(); |
| 50 | +} |
| 51 | + |
| 52 | +function getDaysOfTheMonth(dateObj){ |
| 53 | + let days = []; |
| 54 | + |
| 55 | + let lens = getMonthLength(dateObj) |
| 56 | + |
| 57 | + for (let i = 0; i < lens; i++){ |
| 58 | + days.push(<Day key={dateObj.valueOf()} dateObj={dateObj.valueOf()}/>); |
| 59 | + dateObj.setDate(dateObj.getDate() + 1); |
| 60 | + } |
| 61 | + |
| 62 | + return days; |
| 63 | +} |
| 64 | + |
| 65 | +function getDaysUntilNextSaturday(dateObj){ |
| 66 | + let year = dateObj.getFullYear(); |
| 67 | + let month = dateObj.getMonth(); |
| 68 | + |
| 69 | + let newDateObj = new Date(year, month, 0); |
| 70 | + |
| 71 | + let days = []; |
| 72 | + |
| 73 | + let delta = 6 - newDateObj.getDay(); |
| 74 | + |
| 75 | + for (let i = 0; i < delta; i++){ |
| 76 | + newDateObj.setDate(newDateObj.getDate() + 1); |
| 77 | + days.push(<PriorDay key={newDateObj.valueOf()} dateObj={newDateObj.valueOf()}/>); |
| 78 | + } |
| 79 | + |
| 80 | + return days; |
| 81 | +} |
| 82 | + |
| 83 | +export const Days = component$((props) => { |
| 84 | + let newDate = new Date(props.dateObj); |
| 85 | + newDate.setDate(1); |
| 86 | + let daysSinceLastSunday = getDaysSinceLastSunday(newDate); |
| 87 | + |
| 88 | + newDate = new Date(props.dateObj); |
| 89 | + newDate.setDate(1); |
| 90 | + let daysOfTheMonth = getDaysOfTheMonth(newDate); |
| 91 | + |
| 92 | + let daysUntilNextSaturday = getDaysUntilNextSaturday(newDate); |
| 93 | + |
| 94 | + let days = [...daysSinceLastSunday, ...daysOfTheMonth, ...daysUntilNextSaturday]; |
| 95 | + |
| 96 | + return ( |
| 97 | + <> |
| 98 | + <div id={css.days}> |
| 99 | + {days} |
| 100 | + </div> |
| 101 | + </> |
| 102 | + ); |
| 103 | +}); |
0 commit comments