typing improvements

This commit is contained in:
Jörn-Michael Miehe 2023-09-21 09:53:30 +00:00
parent 7fc0d82354
commit b99a6ccc68
4 changed files with 32 additions and 21 deletions

View file

@ -46,7 +46,7 @@
</template>
<script lang="ts">
import { Dictionary } from "@/lib/api";
import { NumStrDict, objForEach } from "@/lib/api";
import { Options, Vue } from "vue-class-component";
import MultiModal from "../MultiModal.vue";
@ -62,7 +62,7 @@ import BulmaDrawer from "../bulma/Drawer.vue";
})
export default class extends Vue {
public day_data: {
[day: string]: {
[day: number]: {
part: string;
image_name: string;
};
@ -76,32 +76,32 @@ export default class extends Vue {
public on_open(ready: () => void, fail: () => void): void {
Promise.all([
this.$advent22.api_get<Dictionary>("admin/day_parts"),
this.$advent22.api_get<Dictionary>("admin/day_image_names"),
this.$advent22.api_get<NumStrDict>("admin/day_parts"),
this.$advent22.api_get<NumStrDict>("admin/day_image_names"),
])
.then(([day_parts, day_image_names]) => {
const _ensure_day = (day: string) => {
const _ensure_day_in_data = (day: number) => {
if (!(day in this.day_data)) {
this.day_data[day] = { part: "", image_name: "" };
}
};
for (const day in day_parts) {
_ensure_day(day);
this.day_data[day].part = day_parts[day];
}
objForEach(day_parts, (day, part) => {
_ensure_day_in_data(day);
this.day_data[day].part = part;
});
for (const day in day_image_names) {
_ensure_day(day);
this.day_data[day].image_name = day_image_names[day];
}
objForEach(day_image_names, (day, image_name) => {
_ensure_day_in_data(day);
this.day_data[day].image_name = image_name;
});
ready();
})
.catch(fail);
}
public door_click(day: unknown) {
public door_click(day: number) {
if (this.multi_modal === undefined) return;
this.multi_modal.show_progress();

View file

@ -122,7 +122,7 @@
</template>
<script lang="ts">
import { ConfigModel, Dictionary, DoorsSaved } from "@/lib/api";
import { ConfigModel, DoorsSaved, NumStrDict } from "@/lib/api";
import { DateTime } from "luxon";
import { Options, Vue } from "vue-class-component";
@ -167,7 +167,7 @@ export default class extends Vue {
config_file: "sed diam nonumy",
},
};
public day_parts: Dictionary = {};
public day_parts: NumStrDict = {};
public num_user_doors = 0;
public next_door: number | null = null;
public dav_credentials: Credentials = { username: "", password: "" };
@ -183,7 +183,7 @@ export default class extends Vue {
public on_open(ready: () => void, fail: () => void): void {
Promise.all([
this.$advent22.api_get<ConfigModel>("admin/config_model"),
this.$advent22.api_get<Dictionary>("admin/day_parts"),
this.$advent22.api_get<NumStrDict>("admin/day_parts"),
this.$advent22.api_get<DoorsSaved>("user/doors"),
this.$advent22.api_get<number | null>("user/next_door"),
])

View file

@ -23,8 +23,8 @@ export interface ConfigModel {
};
}
export interface Dictionary {
[day: string]: string;
export interface NumStrDict {
[key: number]: string;
}
export interface DoorSaved {
@ -36,3 +36,14 @@ export interface DoorSaved {
}
export type DoorsSaved = DoorSaved[];
export function objForEach<T>(
obj: T,
f: (k: keyof T, v: T[keyof T]) => void,
): void {
for (const k in obj) {
if (Object.prototype.hasOwnProperty.call(obj, k)) {
f(k, obj[k]);
}
}
}

View file

@ -24,11 +24,11 @@ export class Advent22 {
return `//${window.location.hostname}:8000/api`;
}
public name_door(day: unknown): string {
public name_door(day: number): string {
return `Türchen ${day}`;
}
public format_user_error([reason, endpoint]: unknown[]): string {
public format_user_error([reason, endpoint]: [unknown, string]): string {
let msg =
"Unbekannter Fehler, bitte wiederholen! Besteht das Problem länger, bitte Admin benachrichtigen!";
let code = "U";