26 lines
863 B
TypeScript
26 lines
863 B
TypeScript
import {characterApi} from "@/mammon";
|
|
import {defineStore} from "pinia";
|
|
import {ref} from "vue";
|
|
import {CharacterResponse} from "@/generated/mammon";
|
|
|
|
export type Character = CharacterResponse
|
|
|
|
export const useCharactersStore = defineStore('characters', () => {
|
|
const characters = ref<Character[]>([]);
|
|
|
|
const findById = async (characterId: number): Promise<Character | undefined> => {
|
|
let character = characters.value.find(c => c.characterId === characterId);
|
|
|
|
if (!character) {
|
|
await refresh(); // TODO call api instead of refresh
|
|
character = characters.value.find(c => c.characterId === characterId);
|
|
}
|
|
return character;
|
|
}
|
|
|
|
const refresh = () => characterApi.findAllCharacters().then(response => characters.value = response.data);
|
|
|
|
refresh();
|
|
|
|
return {characters, findById};
|
|
}) |