Implement food and settings stores

This commit is contained in:
2024-08-09 18:52:13 +02:00
parent 5ec2ee18b8
commit 4c2d104f49
4 changed files with 70 additions and 24 deletions

View File

@@ -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>

View 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();

View 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();

View File

@@ -1,5 +1,5 @@
import {defineConfig} from 'vite'
import {svelte} from '@sveltejs/vite-plugin-svelte'
import { defineConfig } from "vite";
import { svelte } from "@sveltejs/vite-plugin-svelte";
import { join } from "node:path";
// https://vitejs.dev/config/
@@ -8,10 +8,14 @@ export default defineConfig({
resolve: {
alias: {
$lib: join(__dirname, 'src/lib'),
$components: join(__dirname, 'src/lib/components'),
$router: join(__dirname, 'src/lib/router'),
$wails: join(__dirname, 'wailsjs/go'),
}
$lib: join(__dirname, "src/lib"),
$components: join(__dirname, "src/lib/components"),
$router: join(__dirname, "src/lib/router"),
$wails: join(__dirname, "wailsjs/go"),
},
})
},
build: {
target: "esnext",
},
});