Make food a global store
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
import { type Food, FoodService } from '$lib/database/food'
|
import { type Food, FoodService } from '$lib/database/food'
|
||||||
import { toast } from 'svelte-sonner'
|
import { toast } from 'svelte-sonner'
|
||||||
import { createEventDispatcher } from 'svelte'
|
import { createEventDispatcher } from 'svelte'
|
||||||
|
import type { Err } from '$lib/types'
|
||||||
|
import { foodStore } from '$lib/store/foodStore'
|
||||||
|
|
||||||
const dispatch = createEventDispatcher()
|
const dispatch = createEventDispatcher()
|
||||||
|
|
||||||
@@ -19,7 +21,7 @@
|
|||||||
item.food = name.trim()
|
item.food = name.trim()
|
||||||
item.amount = parseInt(amount.trim())
|
item.amount = parseInt(amount.trim())
|
||||||
item.description = description.trim()
|
item.description = description.trim()
|
||||||
const [food, err] = await FoodService.Create(item)
|
const [dbFood, err]: [Food, Err] = await FoodService.Create(item)
|
||||||
name = ''
|
name = ''
|
||||||
amount = ''
|
amount = ''
|
||||||
description = ''
|
description = ''
|
||||||
@@ -28,7 +30,10 @@
|
|||||||
toast.error(err)
|
toast.error(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
dispatch('created', food)
|
foodStore.update((food) => {
|
||||||
|
food.push(dbFood)
|
||||||
|
return food
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
@@ -21,14 +21,6 @@
|
|||||||
label: 'Home',
|
label: 'Home',
|
||||||
href: '/'
|
href: '/'
|
||||||
},
|
},
|
||||||
{
|
|
||||||
label: 'IPC',
|
|
||||||
href: '/#IPC'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
label: 'Versions',
|
|
||||||
href: '/#versions'
|
|
||||||
}
|
|
||||||
]
|
]
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
@@ -3,16 +3,14 @@
|
|||||||
import { type Food, FoodService } from '$lib/database/food'
|
import { type Food, FoodService } from '$lib/database/food'
|
||||||
import FoodComp from '$components/FoodComp.svelte'
|
import FoodComp from '$components/FoodComp.svelte'
|
||||||
import EmptyFoodComp from '$components/EmptyFoodComp.svelte'
|
import EmptyFoodComp from '$components/EmptyFoodComp.svelte'
|
||||||
|
import { foodStore } from '$lib/store/foodStore'
|
||||||
let food: Food[] = []
|
|
||||||
onMount(async () => {
|
|
||||||
food = await FoodService.GetAllForDate(new Date());
|
|
||||||
})
|
|
||||||
|
|
||||||
function newFood(event: CustomEvent<Food>) {
|
function newFood(event: CustomEvent<Food>) {
|
||||||
console.log(event)
|
console.log(event)
|
||||||
food.push(event.detail)
|
foodStore.update((food) => {
|
||||||
food = food
|
food.push(event.detail)
|
||||||
|
return food
|
||||||
|
})
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -43,8 +41,8 @@
|
|||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<EmptyFoodComp on:created={newFood}/>
|
<EmptyFoodComp/>
|
||||||
{#each food as f}
|
{#each $foodStore as f}
|
||||||
<FoodComp item="{f}" />
|
<FoodComp item="{f}" />
|
||||||
{/each}
|
{/each}
|
||||||
</tbody>
|
</tbody>
|
||||||
|
15
src/lib/store/foodStore.ts
Normal file
15
src/lib/store/foodStore.ts
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import { type Writable, writable } from 'svelte/store'
|
||||||
|
import { type Food, FoodService } from '$lib/database/food'
|
||||||
|
|
||||||
|
|
||||||
|
async function createStore(): Promise<Writable<Food[]>> {
|
||||||
|
const foods = await FoodService.GetAllForDate(new Date())
|
||||||
|
const { subscribe, update, set } = writable(foods)
|
||||||
|
return {
|
||||||
|
subscribe,
|
||||||
|
update,
|
||||||
|
set
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const foodStore = await createStore()
|
Reference in New Issue
Block a user