2024-01-24 11:46:09 +00:00
< script lang = "ts" >
import { onMount } from 'svelte';
import { pb , currentUser } from '$lib/pocketbase';
2024-01-24 16:43:18 +00:00
import Counters from '$lib/components/counters.svelte';
2024-01-24 11:46:09 +00:00
type Action = {
type: string;
2024-01-24 22:13:00 +00:00
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,
2024-01-24 22:13:00 +00:00
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 }
2024-01-24 16:43:18 +00:00
< 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 }
2024-01-24 16:43:18 +00:00
< / aside >
< section >
< h2 > Today< / h2 >
2024-01-25 11:06:23 +00:00
< div class = "today__list" >
< ul >
2024-01-24 16:43:18 +00:00
{ #each actions as action }
2024-01-24 22:13:00 +00:00
< 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 >
2024-01-24 16:43:18 +00:00
{ /each }
< / ul >
< Counters { actions } />
< / div >
< / section >
2024-01-24 11:46:09 +00:00
{ /if }