woodpecker/web/src/compositions/useForgeStore.ts
2024-06-20 13:08:54 +02:00

30 lines
770 B
TypeScript

import { defineStore } from 'pinia';
import { computed, reactive, type Ref } from 'vue';
import useApiClient from '~/compositions/useApiClient';
import type { Forge } from '~/lib/api/types';
export const useForgeStore = defineStore('forges', () => {
const apiClient = useApiClient();
const forges = reactive<Map<number, Forge>>(new Map());
async function loadForge(forgeId: number): Promise<Forge> {
const forge = await apiClient.getForge(forgeId);
forges.set(forge.id, forge);
return forge;
}
async function getForge(forgeId: number): Promise<Ref<Forge | undefined>> {
if (!forges.has(forgeId)) {
await loadForge(forgeId);
}
return computed(() => forges.get(forgeId));
}
return {
getForge,
loadForge,
};
});