28 lines
1.0 KiB
TypeScript
28 lines
1.0 KiB
TypeScript
import {activityApi, 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 reloadActivities = (characterId: number): Promise<void> => activityApi.fetchNewActivitiesForCharacter(characterId) as Promise<void>;
|
|
|
|
const refresh = () => characterApi.findAllCharacters().then(response => characters.value = response.data);
|
|
|
|
refresh();
|
|
|
|
return {characters, findById, reloadActivities, refresh};
|
|
}) |