feat(#7): Rework rule-book UI for one-rule-book-per-user model

This commit is contained in:
Sirttas
2026-07-02 19:28:37 +02:00
parent e66fe9b40e
commit 052a0a5d03
10 changed files with 539 additions and 1288 deletions
+19 -71
View File
@@ -1,83 +1,31 @@
import {characterRuleBookApi, ruleBookApi} from "@/mammon";
import {
CharacterRuleBookResponse,
CreateRuleBookRequest,
RuleBookResponse,
SetCharacterRuleBookRequest
} from "@/generated/mammon";
import {ruleBookApi, ruleScriptApi} from "@/mammon";
import {RuleBookResponse, UpdateRuleBookRequest} from "@/generated/mammon";
import {isAxiosError} from "axios";
import {defineStore} from "pinia";
import {ref, triggerRef} from "vue";
import {ref} from "vue";
export type RuleBook = RuleBookResponse;
export const useRuleBooksStore = defineStore('rule-books', () => {
const ruleBooks = ref<RuleBook[]>([]);
export const useRuleBookStore = defineStore('rule-book', () => {
const ruleBook = ref<RuleBook | null>(null);
const addRuleBook = (ruleBook: RuleBook) => {
ruleBooks.value.push(ruleBook);
triggerRef(ruleBooks);
return ruleBook;
};
const refresh = () => ruleBookApi.findRuleBook()
.then(response => ruleBook.value = response.data)
.catch(error => {
if (isAxiosError(error) && error.response?.status === 404) {
ruleBook.value = null;
return;
}
throw error;
});
const replaceRuleBook = (ruleBook: RuleBook) => {
const index = ruleBooks.value.findIndex(rb => rb.ruleBookId === ruleBook.ruleBookId);
if (index !== -1) {
ruleBooks.value[index] = ruleBook;
}
triggerRef(ruleBooks);
return ruleBook;
};
const findById = (ruleBookId: string): RuleBook | undefined => ruleBooks.value.find(rb => rb.ruleBookId === ruleBookId);
const create = (ruleBook: CreateRuleBookRequest) => ruleBookApi.createRuleBook(ruleBook).then(response => addRuleBook(response.data));
const update = (ruleBookId: string, ruleBook: CreateRuleBookRequest) => ruleBookApi.updateRuleBook(ruleBookId, ruleBook).then(response => replaceRuleBook(response.data));
const duplicate = (ruleBook: RuleBook) => create({
name: `${ruleBook.name} (copy)`,
usedForAcquisitions: ruleBook.usedForAcquisitions,
ledgerRefs: [...ruleBook.ledgerRefs],
script: ruleBook.script,
});
const remove = (ruleBookId: string) => ruleBookApi.deleteRuleBook(ruleBookId).then(() => {
ruleBooks.value = ruleBooks.value.filter(rb => rb.ruleBookId !== ruleBookId);
});
const refresh = () => ruleBookApi.findAllRuleBooks().then(response => ruleBooks.value = response.data);
const update = (request: UpdateRuleBookRequest) => ruleBookApi.updateRuleBook(request)
.then(response => ruleBook.value = response.data);
refresh();
return {ruleBooks, findById, create, update, duplicate, remove, refresh};
})
export type CharacterRuleBook = CharacterRuleBookResponse;
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};
return {ruleBook, refresh, update};
})
export const fetchScriptDefinitions = (): Promise<string> =>
ruleBookApi.getScriptDefinitions({responseType: 'text'}).then(response => response.data);
ruleScriptApi.getScriptDefinitions({responseType: 'text'}).then(response => response.data);