rule book front
This commit is contained in:
+37
-4
@@ -1,5 +1,7 @@
|
||||
import {ruleBookControllerApi} from "@/mammon";
|
||||
import {RuleBookResponse, RuleClauseResponseRateEnum, RuleResponse} from "@/generated/mammon";
|
||||
import {ledgerControllerApi, ruleBookControllerApi} from "@/mammon";
|
||||
import {CreateRuleBookRequest, RuleBookResponse, RuleClauseResponseRateEnum, RuleResponse} from "@/generated/mammon";
|
||||
import {defineStore} from "pinia";
|
||||
import {ref, triggerRef} from "vue";
|
||||
|
||||
export const activityTypes = {
|
||||
itemBought: {key: "ITEM_BOUGHT", name: "Item Bought"},
|
||||
@@ -10,7 +12,8 @@ export const activityTypes = {
|
||||
|
||||
export type Activity = { key: ActivityType, name: string }
|
||||
export type ActivityType = typeof activityTypes[keyof typeof activityTypes]['key'];
|
||||
export type RuleBook = RuleBookResponse & { rules: { [key: ActivityType]: RuleResponse; } }
|
||||
export type Rules = { [key: ActivityType]: RuleResponse; };
|
||||
export type RuleBook = RuleBookResponse & { rules: Rules }
|
||||
|
||||
export const ratesTypes = {
|
||||
None: {key: "NONE", name: "0 ISK"},
|
||||
@@ -22,7 +25,37 @@ export const ratesTypes = {
|
||||
|
||||
export type Rate = { key: RuleClauseResponseRateEnum, name: string }
|
||||
|
||||
export const findByCharacterId = (characterId: number): Promise<RuleBook> => ruleBookControllerApi.findByCharacterId(characterId)
|
||||
export const useRuleBooksStore = defineStore('rule-books', () => {
|
||||
const ruleBooks = ref<RuleBook[]>([]);
|
||||
|
||||
const addRuleBook = (ruleBook: RuleBook) => {
|
||||
ruleBooks.value.push(ruleBook);
|
||||
triggerRef(ruleBooks);
|
||||
return ruleBook;
|
||||
};
|
||||
|
||||
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) => ruleBookControllerApi.createRuleBook(ruleBook).then(response => addRuleBook(response.data));
|
||||
const update = (ruleBookId: string, ruleBook: CreateRuleBookRequest) => ledgerControllerApi.updateMainLedger(ruleBookId, ruleBook).then(response => replaceRuleBook(response.data));
|
||||
|
||||
const refresh = () => ruleBookControllerApi.findAllRuleBooks().then(response => ruleBooks.value = response.data as RuleBook[]);
|
||||
|
||||
refresh();
|
||||
|
||||
return {ruleBooks, findById, create, update, refresh};
|
||||
})
|
||||
|
||||
export const findByCharacterId = (characterId: number): Promise<RuleBook> => ruleBookControllerApi.findById(characterId)
|
||||
.then(response => response.data)
|
||||
.catch(() => ({characterId, rules: {}}));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user