Implement food and settings stores
This commit is contained in:
@@ -1,20 +1,9 @@
|
||||
<script lang="ts">
|
||||
import { GetFood } from "$wails/main/App.js";
|
||||
import { main } from '$wails/models'
|
||||
|
||||
let food: main.Food[] = [];
|
||||
GetFood().then((result) => {
|
||||
if (!result.success) {
|
||||
console.error(result.error);
|
||||
return;
|
||||
}
|
||||
food = result.data
|
||||
console.log(food);
|
||||
});
|
||||
import { foodStore } from "$lib/store/FoodStore";
|
||||
</script>
|
||||
|
||||
<template>
|
||||
{#each food as f}
|
||||
{#each $foodStore as f}
|
||||
<div>{f.food}</div>
|
||||
{/each}
|
||||
</template>
|
31
frontend/src/lib/store/FoodStore.ts
Normal file
31
frontend/src/lib/store/FoodStore.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { type Writable, writable } from "svelte/store";
|
||||
import { main } from "$wails/models";
|
||||
import { GetFood } from "$wails/main/App";
|
||||
|
||||
async function createStore(): Promise<Writable<main.Food[]>> {
|
||||
let foods: main.Food[] = [];
|
||||
let res: main.WailsFood = await GetFood();
|
||||
if (!res.success) {
|
||||
console.error("failed to get foods with error" + res.error);
|
||||
} else {
|
||||
foods = res.data;
|
||||
}
|
||||
|
||||
const { subscribe, update, set } = writable(foods);
|
||||
return {
|
||||
subscribe,
|
||||
update,
|
||||
set,
|
||||
// @ts-ignore
|
||||
refresh: async () => {
|
||||
const res = await GetFood();
|
||||
if (!res.success) {
|
||||
console.error("failed to get foods with error" + res.error);
|
||||
return;
|
||||
}
|
||||
set(res.data);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export const foodStore = await createStore();
|
22
frontend/src/lib/store/SettingsStore.ts
Normal file
22
frontend/src/lib/store/SettingsStore.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { type Writable, writable } from "svelte/store";
|
||||
import { main } from "$wails/models";
|
||||
import { GetSettings } from "$wails/main/App";
|
||||
|
||||
async function createStore(): Promise<Writable<main.settings>> {
|
||||
// This should never fail
|
||||
const settings: main.settings = await GetSettings();
|
||||
|
||||
const { subscribe, update, set } = writable(settings);
|
||||
return {
|
||||
subscribe,
|
||||
update,
|
||||
set,
|
||||
// @ts-ignore
|
||||
refresh: async () => {
|
||||
const settings: main.settings = await GetSettings();
|
||||
set(settings);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export const settingsStore = await createStore();
|
Reference in New Issue
Block a user