baby-tracker/src/routes/calendar/+page.svelte

144 lines
3.9 KiB
Svelte
Raw Normal View History

2024-01-24 11:46:09 +00:00
<script lang="ts">
import { onMount } from 'svelte';
import { pb } from '$lib/pocketbase';
type Action = {
type: string;
timestamp: string;
2024-01-24 11:46:09 +00:00
};
onMount(async () => {
actions = await fetchAllActions();
});
async function fetchAllActions() {
// Fetch all actions. Adjust as needed for date range or pagination
2024-01-25 07:36:46 +00:00
const result = await pb.collection('actions').getList(1, 1000,{
sort: 'timestamp'
});
2024-01-24 11:46:09 +00:00
return result.items;
}
let actions: Action[] = [];
// Function to get the day of the week from a date string
const getDayOfWeek = (dateString: string) => {
return new Date(dateString).getDay();
};
// Function to get the hour of the day from a date string
const getHourOfDay = (dateString: string) => {
return new Date(dateString).getHours();
};
// Days of the week for headers
//const daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const daysOfWeek = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
2024-01-24 11:46:09 +00:00
// Generate hours for the grid
const hours = Array.from({ length: 24 }, (_, i) => i);
// Function to format hour in HH:mm format
const formatHour = (hour: number) => {
return `${hour.toString().padStart(2, '0')}:00`;
};
// function formatTime(date: Date): string {
// const hours = date.getHours().toString().padStart(2, '0');
// const minutes = date.getMinutes().toString().padStart(2, '0');
// return `${hours}:${minutes}`;
// }
function formatTime(dateString: string): string {
const date = new Date(dateString);
const hours = date.getHours().toString().padStart(2, '0');
const minutes = date.getMinutes().toString().padStart(2, '0');
return `${hours}:${minutes}`;
}
2024-01-24 11:46:09 +00:00
</script>
<style>
.calendar-wrapper {
max-width: 100%;
/* outline: 1px solid #ccc; */
overflow-x:auto;
position: relative;
}
2024-01-24 11:46:09 +00:00
.calendar {
display: grid;
grid-template-columns: 60px repeat(7, 1fr); /* Adjusted for hour column */
2024-01-24 11:46:09 +00:00
text-align: center;
min-width: 700px;
2024-01-24 11:46:09 +00:00
}
.day-header, .hour-label {
2024-01-24 11:46:09 +00:00
font-weight: bold;
font-size: 1.2rem;
background: var(--c-border);
}
.day-header {
border-radius: 1em 1em 0 0;
margin-top: 1rem;
padding: .5rem;
}
.hour-label{
border-radius: 1em 0 0 1em;
padding: .5rem;
margin-left: .5rem;
2024-01-24 11:46:09 +00:00
}
.hour {
border: 1px solid var(--c-border);
min-height: 60px;
2024-01-24 11:46:09 +00:00
}
.event {
background-color: var(--color);
2024-01-24 11:46:09 +00:00
border-radius: 4px;
padding: 0.3em 0.8em;
line-height: 1;
margin: 2px;
display: flex;
align-items: center;
justify-content: space-between;
color: rgba(255,255,255,.5);
2024-01-24 11:46:09 +00:00
}
.event__title {
font-weight: 900;
font-size: 1.2rem;
text-transform: uppercase;
letter-spacing: .2em;
}
.event__time {
line-height: 1;
font-size: 1.2rem;
font-weight: 300;
color: rgba(255,255,255,.75);
}
2024-01-24 11:46:09 +00:00
</style>
<div class="calendar-wrapper">
<div class="calendar">
<!-- Empty cell for top-left corner -->
<div></div>
<!-- Day Headers -->
{#each daysOfWeek as day (day)}
<div class="day-header">{day}</div>
{/each}
<!-- Hour Labels and Calendar Grid -->
{#each hours as hour (hour)}
<div class="hour-label">{formatHour(hour)}</div> <!-- Hour label -->
{#each daysOfWeek as _, dayIndex}
<div class="hour">
{#each actions as action (action.id)}
{#if getDayOfWeek(action.timestamp) === dayIndex && getHourOfDay(action.timestamp) === hour}
<div class="event event--{action.type}"><span class="event__title">{action.type}</span><span class="event__time">{formatTime(action.timestamp)}</span></div>
{/if}
{/each}
</div>
{/each}
2024-01-24 11:46:09 +00:00
{/each}
</div>
2024-01-24 11:46:09 +00:00
</div>