character rule book store

This commit is contained in:
Sirttas
2026-06-11 19:42:56 +02:00
parent 9cd0d5fb5e
commit f3cb4798d5
5 changed files with 540 additions and 55 deletions
+27 -5
View File
@@ -50,12 +50,34 @@ export const useRuleBooksStore = defineStore('rule-books', () => {
return {ruleBooks, findById, create, update, duplicate, remove, refresh};
})
export const findCharacterRuleBookByCharacterId = (characterId: number): Promise<CharacterRuleBookResponse> => characterRuleBookApi.findCharacterRuleBookByCharacterId(characterId)
.then(response => response.data)
.catch(() => ({characterId, ruleBookId: '', bindings: {}}));
export type CharacterRuleBook = CharacterRuleBookResponse;
export const setCharacterRuleBookForCharacter = (characterId: number, ruleBook: SetCharacterRuleBookRequest): Promise<CharacterRuleBookResponse> => characterRuleBookApi.setCharacterRuleBookForCharacter(characterId, ruleBook)
.then(response => response.data);
export const useCharacterRuleBooksStore = defineStore('character-rule-books', () => {
const characterRuleBooks = ref<CharacterRuleBook[]>([]);
const replaceCharacterRuleBook = (characterRuleBook: CharacterRuleBook) => {
const index = characterRuleBooks.value.findIndex(crb => crb.character.characterId === characterRuleBook.character.characterId);
if (index !== -1) {
characterRuleBooks.value[index] = characterRuleBook;
} else {
characterRuleBooks.value.push(characterRuleBook);
}
triggerRef(characterRuleBooks);
return characterRuleBook;
};
const findByCharacterId = (characterId: number): CharacterRuleBook | undefined => characterRuleBooks.value.find(crb => crb.character.characterId === characterId);
const setForCharacter = (characterId: number, ruleBook: SetCharacterRuleBookRequest) => characterRuleBookApi.setCharacterRuleBookForCharacter(characterId, ruleBook)
.then(response => replaceCharacterRuleBook(response.data));
const refresh = () => characterRuleBookApi.findAllCharacterRuleBooks().then(response => characterRuleBooks.value = response.data);
refresh();
return {characterRuleBooks, findByCharacterId, setForCharacter, refresh};
})
export const fetchScriptDefinitions = (): Promise<string> =>
ruleBookApi.getScriptDefinitions({responseType: 'text'}).then(response => response.data);