Implement lookback days
This commit is contained in:
@@ -10,6 +10,7 @@
|
|||||||
import { cn } from '$lib/utils'
|
import { cn } from '$lib/utils'
|
||||||
import { dbStateStore } from '$lib/store/dbState'
|
import { dbStateStore } from '$lib/store/dbState'
|
||||||
import { DBService } from '$lib/database'
|
import { DBService } from '$lib/database'
|
||||||
|
import { lookbackDaysStore } from '$lib/store/lookbackDaysStore'
|
||||||
|
|
||||||
Fa
|
Fa
|
||||||
|
|
||||||
@@ -66,6 +67,7 @@
|
|||||||
<div class="cursor-pointer" class:text-emerald-600={$dbStateStore.transacting} on:click={DBService.Commit}>C
|
<div class="cursor-pointer" class:text-emerald-600={$dbStateStore.transacting} on:click={DBService.Commit}>C
|
||||||
</div>
|
</div>
|
||||||
<div class="cursor-pointer" class:text-red-700={$dbStateStore.transacting} on:click={DBService.Rollback}>R</div>
|
<div class="cursor-pointer" class:text-red-700={$dbStateStore.transacting} on:click={DBService.Rollback}>R</div>
|
||||||
|
<div contenteditable="true" bind:innerText={$lookbackDaysStore}></div>
|
||||||
{#each links as { href, label }}
|
{#each links as { href, label }}
|
||||||
<a
|
<a
|
||||||
use:link
|
use:link
|
||||||
|
@@ -1,5 +1,7 @@
|
|||||||
import { db } from '$lib/database'
|
import { db } from '$lib/database'
|
||||||
import type { Err } from '$lib/types'
|
import type { Err } from '$lib/types'
|
||||||
|
import { lookbackDaysStore } from '$lib/store/lookbackDaysStore'
|
||||||
|
import { get } from 'svelte/store'
|
||||||
|
|
||||||
export type Food = {
|
export type Food = {
|
||||||
rowid?: number,
|
rowid?: number,
|
||||||
@@ -33,7 +35,7 @@ order by date desc
|
|||||||
return await db.select<Food[]>(`
|
return await db.select<Food[]>(`
|
||||||
select ${columns.join(', ')}
|
select ${columns.join(', ')}
|
||||||
from foodView
|
from foodView
|
||||||
where date > datetime('now', "-48 hours")
|
where date > datetime('now', "-${get(lookbackDaysStore)} days")
|
||||||
order by date DESC;
|
order by date DESC;
|
||||||
`)
|
`)
|
||||||
},
|
},
|
||||||
|
@@ -1,6 +1,9 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { foodStore } from '$lib/store/foodStore'
|
import { foodStore } from '$lib/store/foodStore'
|
||||||
import FoodTable from '$components/Food/FoodTable.svelte'
|
import FoodTable from '$components/Food/FoodTable.svelte'
|
||||||
|
import { lookbackDaysStore } from '$lib/store/lookbackDaysStore'
|
||||||
|
// @ts-ignore
|
||||||
|
lookbackDaysStore.subscribe((n) => foodStore.refresh())
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
@@ -8,7 +8,12 @@ async function createStore(): Promise<Writable<Food[]>> {
|
|||||||
return {
|
return {
|
||||||
subscribe,
|
subscribe,
|
||||||
update,
|
update,
|
||||||
set
|
set,
|
||||||
|
// @ts-ignore
|
||||||
|
refresh: async () => {
|
||||||
|
const foods = await FoodService.GetRecent()
|
||||||
|
set(foods)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
20
src/lib/store/lookbackDaysStore.ts
Normal file
20
src/lib/store/lookbackDaysStore.ts
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import { type Writable, writable } from 'svelte/store'
|
||||||
|
|
||||||
|
async function createStore(): Promise<Writable<number>> {
|
||||||
|
const days: number = parseInt(localStorage.getItem('lookbackDays') || '2')
|
||||||
|
const { subscribe, update, set } = writable(days)
|
||||||
|
return {
|
||||||
|
subscribe,
|
||||||
|
update,
|
||||||
|
set(value: number) {
|
||||||
|
if (value > 365)
|
||||||
|
value = 365
|
||||||
|
if (value < 1)
|
||||||
|
value = 1
|
||||||
|
localStorage.setItem('lookbackDays', value.toString())
|
||||||
|
update(() => value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const lookbackDaysStore = await createStore()
|
Reference in New Issue
Block a user