Copy paste food to weight

This commit is contained in:
2024-06-13 18:05:14 +02:00
parent c8dd478419
commit 800ac867f2
11 changed files with 128 additions and 73 deletions

View File

@@ -1,62 +1,33 @@
<script lang="ts"> <script lang="ts">
import { type Food, FoodService } from '$lib/database/food'
import { toast } from 'svelte-sonner' import { toast } from 'svelte-sonner'
import type { Err } from '$lib/types' import type { Err } from '$lib/types'
import { foodStore } from '$lib/store/energy/foodStore' import { type Weight, WeightService } from '$lib/database/weight'
import { weightStore } from '$lib/store/weight/weightStore'
let item: Food = { let item: Weight = {
food: '', weight: 0
amount: 0,
description: ''
} }
let name: string = '' let weight: string = item.weight.toString()
let amount: string = ''
let description: string = ''
let per100: string = ''
let per100Edited: boolean = false
let per100Element: HTMLTableCellElement
async function update(event: KeyboardEvent & { currentTarget: (EventTarget & HTMLTableCellElement) }) { async function update(event: KeyboardEvent & { currentTarget: (EventTarget & HTMLTableCellElement) }) {
name = name.trim() weight = weight.trim()
amount = amount.trim()
description = description.trim()
per100 = per100.trim()
if (!per100Edited && event.currentTarget === per100Element)
per100Edited = true
if (event.key == 'Enter') { if (event.key == 'Enter') {
event.preventDefault() event.preventDefault()
item.food = name item.weight = parseInt(weight)
item.description = description
item.amount = parseInt(amount)
item.per100 = parseInt(per100)
const [dbFood, err]: [Food, Err] = await FoodService.Create(item) const [dbRow, err]: [Weight, Err] = await WeightService.Create(item)
name = '' weight = ''
amount = ''
description = ''
per100 = ''
per100Edited = false
if (err) { if (err) {
toast.error(err) toast.error(err)
return return
} }
foodStore.update((food) => { weightStore.update((weight) => {
food.unshift(dbFood) weight.unshift(dbRow)
return food return weight
}) })
} }
if (!per100Edited)
FoodService.GetLatestPer100(name.trim()).then((res) => {
if (res[1])
// toast.error(res[1])
return
per100 = res[0].toString()
})
} }
</script> </script>
@@ -65,34 +36,14 @@
<th class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap bg-gray-50 dark:text-white dark:bg-gray-800" <th class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap bg-gray-50 dark:text-white dark:bg-gray-800"
scope="row"> scope="row">
</th> </th>
<td bind:innerText={name} <td bind:innerText={weight}
class:border-[3px]={!name} class:border-[3px]={!weight}
class:border-red-600={!name} class:border-red-600={!weight}
class="px-6 py-4 overflow-hidden" class="px-6 py-4 overflow-hidden"
contenteditable="true" contenteditable="true"
autofocus autofocus
on:keydown={update}> on:keydown={update}>
</td> </td>
<td bind:innerText={description}
class="px-6 py-4 bg-gray-50 dark:bg-gray-800 overflow-hidden"
contenteditable="true"
on:keydown={update}>
</td>
<td bind:innerText={amount}
class:border-[3px]={!amount}
class:border-red-600={!amount}
class="px-6 py-4 overflow-hidden"
contenteditable="true"
on:keydown={update}>
</td>
<td bind:this={per100Element}
bind:innerText={per100}
class="px-6 py-4 bg-gray-50 dark:bg-gray-800 overflow-hidden"
class:border-[3px]={!per100}
class:border-orange-600={!per100}
contenteditable="true"
on:keydown={update}>
</td>
<td class="px-6 py-4"> <td class="px-6 py-4">
</td> </td>
</tr> </tr>

View File

@@ -1,5 +1,5 @@
<script lang="ts"> <script lang="ts">
import type { Weight } from '$lib/database/weight' import { type Weight, WeightService } from '$lib/database/weight'
export let item: Weight export let item: Weight
export let dateColor: string export let dateColor: string

View File

@@ -1,15 +1,14 @@
<script lang="ts"> <script lang="ts">
import type { Food } from '$lib/database/food'
import { GenerateColor } from '$lib/utils' import { GenerateColor } from '$lib/utils'
import { Weight } from 'lucide-svelte'
import EmptyWeightComp from '$components/Weight/EmptyWeightComp.svelte' import EmptyWeightComp from '$components/Weight/EmptyWeightComp.svelte'
import WeightComp from '$components/Weight/WeightComp.svelte' import WeightComp from '$components/Weight/WeightComp.svelte'
import type { Weight } from '$lib/database/weight'
export let items: Weight[] = [] export let items: Weight[] = []
const dateColors: Map<string, string> = new Map<string, string>() const dateColors: Map<string, string> = new Map<string, string>()
function getDateColor(item: Food): string { function getDateColor(item: Weight): string {
if (!item) return GenerateColor() if (!item) return GenerateColor()
if (!item.date) return GenerateColor() if (!item.date) return GenerateColor()
const date = item.date.toString().split(' ')[0] const date = item.date.toString().split(' ')[0]

View File

@@ -6,7 +6,7 @@ import { get } from 'svelte/store'
export type Weight = { export type Weight = {
rowid?: number, rowid?: number,
date?: Date, date?: Date,
weight: string, weight: number,
} }
export type AggregatedWeight = { export type AggregatedWeight = {
period: string, period: string,
@@ -16,7 +16,7 @@ export type AggregatedWeight = {
const columns = ['rowid', 'date', 'weight'] const columns = ['rowid', 'date', 'weight']
const aggColumns = ['period', 'amount'] const aggColumns = ['period', 'amount']
const FoodService = { const WeightService = {
async GetAll() { async GetAll() {
return await db.select<Weight[]>(` return await db.select<Weight[]>(`
select ${columns.join(', ')} select ${columns.join(', ')}
@@ -74,4 +74,4 @@ where rowid = $2
} }
} }
export { FoodService } export { WeightService }

View File

@@ -5,6 +5,7 @@
import Monthly from '$router/routes/Energy/Monthly.svelte' import Monthly from '$router/routes/Energy/Monthly.svelte'
import Yearly from '$router/routes/Energy/Yearly.svelte' import Yearly from '$router/routes/Energy/Yearly.svelte'
import Energy from '$router/routes/Energy/Energy.svelte' import Energy from '$router/routes/Energy/Energy.svelte'
import Weight from '$router/routes/Weight/Weight.svelte'
import WDaily from '$router/routes/Weight/WDaily.svelte' import WDaily from '$router/routes/Weight/WDaily.svelte'
import WWeekly from '$router/routes/Weight/WWeekly.svelte' import WWeekly from '$router/routes/Weight/WWeekly.svelte'
import WMonthly from '$router/routes/Weight/WMonthly.svelte' import WMonthly from '$router/routes/Weight/WMonthly.svelte'
@@ -16,6 +17,7 @@
'/Energy/weekly': Weekly, '/Energy/weekly': Weekly,
'/Energy/monthly': Monthly, '/Energy/monthly': Monthly,
'/Energy/yearly': Yearly, '/Energy/yearly': Yearly,
'/Weight': Weight,
'/Weight/daily': WDaily, '/Weight/daily': WDaily,
'/Weight/weekly': WWeekly, '/Weight/weekly': WWeekly,
'/Weight/monthly': WMonthly, '/Weight/monthly': WMonthly,

View File

@@ -2,10 +2,12 @@
import { foodStore } from '$lib/store/energy/foodStore' import { foodStore } from '$lib/store/energy/foodStore'
import FoodTable from '$components/Energy/Food/FoodTable.svelte' import FoodTable from '$components/Energy/Food/FoodTable.svelte'
import { lookbackDaysStore } from '$lib/store/energy/lookbackDaysStore' import { lookbackDaysStore } from '$lib/store/energy/lookbackDaysStore'
import WeightTable from '$components/Weight/WeightTable.svelte'
import { weightStore } from '$lib/store/weight/weightStore'
// @ts-ignore // @ts-ignore
lookbackDaysStore.subscribe((n) => foodStore.refresh()) lookbackDaysStore.subscribe((n) => weightStore.refresh())
</script> </script>
<template> <template>
<FoodTable items="{$foodStore}" /> <WeightTable items="{$weightStore}" />
</template> </template>

View File

@@ -0,0 +1,20 @@
import { type Writable, writable } from 'svelte/store'
import { type AggregatedWeight, WeightService } from '$lib/database/weight'
async function createStore(): Promise<Writable<AggregatedWeight[]>> {
let [rows, err] = await WeightService.GetDaily()
if (err) {
rows = []
console.error(err)
}
const { subscribe, update, set } = writable(rows)
return {
subscribe,
update,
set
}
}
export const dailyWeightStore = await createStore()

View File

@@ -0,0 +1,21 @@
import { type Writable, writable } from 'svelte/store'
import { FoodService } from '$lib/database/food'
import type { AggregatedWeight } from '$lib/database/weight'
async function createStore(): Promise<Writable<AggregatedWeight[]>> {
let [rows, err] = await FoodService.GetMonthly()
if (err) {
rows = []
console.error(err)
}
const { subscribe, update, set } = writable(rows)
return {
subscribe,
update,
set
}
}
export const monthlyWeightStore = await createStore()

View File

@@ -0,0 +1,20 @@
import { type Writable, writable } from 'svelte/store'
import { type AggregatedWeight, WeightService } from '$lib/database/weight'
async function createStore(): Promise<Writable<AggregatedWeight[]>> {
let [rows, err] = await WeightService.GetWeekly()
if (err) {
rows = []
console.error(err)
}
const { subscribe, update, set } = writable(rows)
return {
subscribe,
update,
set
}
}
export const weeklyWeightStore = await createStore()

View File

@@ -0,0 +1,20 @@
import { type Writable, writable } from 'svelte/store'
import { type Weight, WeightService } from '$lib/database/weight'
async function createStore(): Promise<Writable<Weight[]>> {
const rows = await WeightService.GetRecent()
const { subscribe, update, set } = writable(rows)
return {
subscribe,
update,
set,
// @ts-ignore
refresh: async () => {
const foods = await WeightService.GetRecent()
set(foods)
}
}
}
export const weightStore = await createStore()

View File

@@ -0,0 +1,20 @@
import { type Writable, writable } from 'svelte/store'
import { type AggregatedWeight, WeightService } from '$lib/database/weight'
async function createStore(): Promise<Writable<AggregatedWeight[]>> {
let [rows, err] = await WeightService.GetYearly()
if (err) {
rows = []
console.error(err)
}
const { subscribe, update, set } = writable(rows)
return {
subscribe,
update,
set
}
}
export const yearlyWeightStore = await createStore()