character rule book store
This commit is contained in:
+27
-5
@@ -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);
|
||||
Reference in New Issue
Block a user