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

75 lines
2.8 KiB
Svelte
Raw Normal View History

2024-01-24 11:46:09 +00:00
<script lang="ts">
import { onMount } from 'svelte';
import { pb, currentUser } from '$lib/pocketbase';
import Counters from '$lib/components/counters.svelte';
2024-01-24 11:46:09 +00:00
type Action = {
type: string;
timestamp: string;
2024-01-24 11:46:09 +00:00
};
let actions: Action[] = [];
onMount(async () => {
await fetchActions();
});
async function recordAction(actionType: string) {
const action = { // No longer specifying a timestamp here
type: actionType,
timestamp: new Date().toISOString()
2024-01-24 11:46:09 +00:00
};
// Send action to PocketBase
await pb.collection('actions').create(action);
// Refresh actions list
await fetchActions();
}
async function fetchActions() {
// Fetch actions for the current day from PocketBase
const today = new Date().toISOString().split('T')[0];
const result = await pb.collection('actions').getList<Action>(1, 50, {
2024-01-25 07:36:46 +00:00
filter: `timestamp >= '${today}'`,
sort: 'timestamp'
2024-01-24 11:46:09 +00:00
});
// Assuming the items are in the 'items' property of the result
actions = result.items;
}
2024-01-24 22:54:13 +00:00
$: isLastActionAsleep = actions.length > 0 && actions[actions.length - 1].type === 'asleep';
$: isLastActionAwake = actions.length > 0 && actions[actions.length - 1].type === 'awake';
$: isLastActionDiaper = actions.length > 0 && actions[actions.length - 1].type === 'diaper';
2024-01-24 11:46:09 +00:00
</script>
{#if $currentUser}
<aside>
2024-01-24 22:54:13 +00:00
{#if isLastActionAsleep}
<button class="button--awake" on:click={() => recordAction('awake')}>Awake</button>
{:else}
{#if !isLastActionAwake}
<button class="button--awake" on:click={() => recordAction('awake')}>Awake</button>
{/if}
<button class="button--asleep" on:click={() => recordAction('asleep')}>Asleep</button>
<button class="button--food" on:click={() => recordAction('food')}>Food</button>
<button class="button--diaper" on:click={() => recordAction('diaper')}>Diaper</button>
{#if isLastActionDiaper}
<button class="button--poop" on:click={() => recordAction('poop')}>Poop</button>
{/if}
{/if}
</aside>
<section>
<h2>Today</h2>
<div class="today__list">
<ul>
{#each actions as action}
<li><span class="badge badge--{action.type}">{action.type}</span> at {new Date(action.timestamp).toLocaleTimeString('en-NL', { hour: '2-digit', minute: '2-digit' })}</li>
{/each}
</ul>
<Counters {actions} />
</div>
</section>
2024-01-24 11:46:09 +00:00
{/if}