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">
import { type Food, FoodService } from '$lib/database/food'
import { toast } from 'svelte-sonner'
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 = {
food: '',
amount: 0,
description: ''
let item: Weight = {
weight: 0
}
let name: string = ''
let amount: string = ''
let description: string = ''
let per100: string = ''
let per100Edited: boolean = false
let per100Element: HTMLTableCellElement
let weight: string = item.weight.toString()
async function update(event: KeyboardEvent & { currentTarget: (EventTarget & HTMLTableCellElement) }) {
name = name.trim()
amount = amount.trim()
description = description.trim()
per100 = per100.trim()
if (!per100Edited && event.currentTarget === per100Element)
per100Edited = true
weight = weight.trim()
if (event.key == 'Enter') {
event.preventDefault()
item.food = name
item.description = description
item.amount = parseInt(amount)
item.per100 = parseInt(per100)
item.weight = parseInt(weight)
const [dbFood, err]: [Food, Err] = await FoodService.Create(item)
name = ''
amount = ''
description = ''
per100 = ''
per100Edited = false
const [dbRow, err]: [Weight, Err] = await WeightService.Create(item)
weight = ''
if (err) {
toast.error(err)
return
}
foodStore.update((food) => {
food.unshift(dbFood)
return food
weightStore.update((weight) => {
weight.unshift(dbRow)
return weight
})
}
if (!per100Edited)
FoodService.GetLatestPer100(name.trim()).then((res) => {
if (res[1])
// toast.error(res[1])
return
per100 = res[0].toString()
})
}
</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"
scope="row">
</th>
<td bind:innerText={name}
class:border-[3px]={!name}
class:border-red-600={!name}
<td bind:innerText={weight}
class:border-[3px]={!weight}
class:border-red-600={!weight}
class="px-6 py-4 overflow-hidden"
contenteditable="true"
autofocus
on:keydown={update}>
</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>
</tr>

View File

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

View File

@@ -1,15 +1,14 @@
<script lang="ts">
import type { Food } from '$lib/database/food'
import { GenerateColor } from '$lib/utils'
import { Weight } from 'lucide-svelte'
import EmptyWeightComp from '$components/Weight/EmptyWeightComp.svelte'
import WeightComp from '$components/Weight/WeightComp.svelte'
import type { Weight } from '$lib/database/weight'
export let items: Weight[] = []
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.date) return GenerateColor()
const date = item.date.toString().split(' ')[0]

View File

@@ -6,7 +6,7 @@ import { get } from 'svelte/store'
export type Weight = {
rowid?: number,
date?: Date,
weight: string,
weight: number,
}
export type AggregatedWeight = {
period: string,
@@ -16,7 +16,7 @@ export type AggregatedWeight = {
const columns = ['rowid', 'date', 'weight']
const aggColumns = ['period', 'amount']
const FoodService = {
const WeightService = {
async GetAll() {
return await db.select<Weight[]>(`
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 Yearly from '$router/routes/Energy/Yearly.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 WWeekly from '$router/routes/Weight/WWeekly.svelte'
import WMonthly from '$router/routes/Weight/WMonthly.svelte'
@@ -16,6 +17,7 @@
'/Energy/weekly': Weekly,
'/Energy/monthly': Monthly,
'/Energy/yearly': Yearly,
'/Weight': Weight,
'/Weight/daily': WDaily,
'/Weight/weekly': WWeekly,
'/Weight/monthly': WMonthly,

View File

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