Implement lookback days
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
import { cn } from '$lib/utils'
|
||||
import { dbStateStore } from '$lib/store/dbState'
|
||||
import { DBService } from '$lib/database'
|
||||
import { lookbackDaysStore } from '$lib/store/lookbackDaysStore'
|
||||
|
||||
Fa
|
||||
|
||||
@@ -66,6 +67,7 @@
|
||||
<div class="cursor-pointer" class:text-emerald-600={$dbStateStore.transacting} on:click={DBService.Commit}>C
|
||||
</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 }}
|
||||
<a
|
||||
use:link
|
||||
|
@@ -1,5 +1,7 @@
|
||||
import { db } from '$lib/database'
|
||||
import type { Err } from '$lib/types'
|
||||
import { lookbackDaysStore } from '$lib/store/lookbackDaysStore'
|
||||
import { get } from 'svelte/store'
|
||||
|
||||
export type Food = {
|
||||
rowid?: number,
|
||||
@@ -33,7 +35,7 @@ order by date desc
|
||||
return await db.select<Food[]>(`
|
||||
select ${columns.join(', ')}
|
||||
from foodView
|
||||
where date > datetime('now', "-48 hours")
|
||||
where date > datetime('now', "-${get(lookbackDaysStore)} days")
|
||||
order by date DESC;
|
||||
`)
|
||||
},
|
||||
|
@@ -1,6 +1,9 @@
|
||||
<script lang="ts">
|
||||
import { foodStore } from '$lib/store/foodStore'
|
||||
import FoodTable from '$components/Food/FoodTable.svelte'
|
||||
import { lookbackDaysStore } from '$lib/store/lookbackDaysStore'
|
||||
// @ts-ignore
|
||||
lookbackDaysStore.subscribe((n) => foodStore.refresh())
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
@@ -8,7 +8,12 @@ async function createStore(): Promise<Writable<Food[]>> {
|
||||
return {
|
||||
subscribe,
|
||||
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