Pass error to svelte using a struct

...a very cursed struct
This commit is contained in:
2024-08-09 18:15:08 +02:00
parent 2fe3bdbb8a
commit b7760c0942
7 changed files with 108 additions and 24 deletions

View File

@@ -1,9 +1,6 @@
<script lang="ts">
import Header from "$lib/components/Header.svelte";
import Router from "$lib/router/Router.svelte";
import { GetFood } from "../wailsjs/go/main/App.js";
GetFood().then((result) => console.log(result));
</script>
<template>

View File

@@ -0,0 +1,20 @@
<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);
});
</script>
<template>
{#each food as f}
<div>{f.food}</div>
{/each}
</template>

View File

@@ -2,4 +2,4 @@
// This file is automatically generated. DO NOT EDIT
import {main} from '../models';
export function GetFood():Promise<Array<main.Food>>;
export function GetFood():Promise<main.WailsFood>;

View File

@@ -1,7 +1,13 @@
export namespace main {
export class Food {
rowid: number;
date: string;
food: string;
description: string;
amount: number;
per100: number;
energy: number;
static createFrom(source: any = {}) {
return new Food(source);
@@ -9,9 +15,49 @@ export namespace main {
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.rowid = source["rowid"];
this.date = source["date"];
this.food = source["food"];
this.description = source["description"];
this.amount = source["amount"];
this.per100 = source["per100"];
this.energy = source["energy"];
}
}
export class WailsFood {
data: Food[];
success: boolean;
error?: string;
static createFrom(source: any = {}) {
return new WailsFood(source);
}
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.data = this.convertValues(source["data"], Food);
this.success = source["success"];
this.error = source["error"];
}
convertValues(a: any, classs: any, asMap: boolean = false): any {
if (!a) {
return a;
}
if (a.slice && a.map) {
return (a as any[]).map(elem => this.convertValues(elem, classs));
} else if ("object" === typeof a) {
if (asMap) {
for (const key of Object.keys(a)) {
a[key] = new classs(a[key]);
}
return a;
}
return new classs(a);
}
return a;
}
}
}