50 lines
1.7 KiB
TypeScript
50 lines
1.7 KiB
TypeScript
import { marbasAxiosInstance, MarbasObject } from "@/marbas";
|
|
import { getHistory, jitaId, MarketOrderHistory, MarketType, MarketTypePrice } from "@/market";
|
|
import log from "loglevel";
|
|
import { defineStore } from "pinia";
|
|
import { computed, ref } from "vue";
|
|
|
|
export type TrackingResult = {
|
|
type: MarketType;
|
|
history: MarketOrderHistory[];
|
|
buy: number,
|
|
sell: number,
|
|
orderCount: number,
|
|
}
|
|
|
|
export type MarbasTrackedType = MarbasObject & {
|
|
type: number
|
|
};
|
|
|
|
const endpoint = '/api/types_tracking/';
|
|
|
|
export const useMarketTrackingStore = defineStore('marketTracking', () => {
|
|
const trackedTypes = ref<MarbasTrackedType[]>([]);
|
|
|
|
const types = computed(() => trackedTypes.value.map(item => item.type) ?? []);
|
|
const addType = async (type: number) => {
|
|
const found = trackedTypes.value.find(item => item.type === type);
|
|
|
|
if (!found) {
|
|
trackedTypes.value = [...trackedTypes.value, (await marbasAxiosInstance.post<MarbasTrackedType>(endpoint, { type })).data];
|
|
log.info(`Tracking type ${type}`);
|
|
}
|
|
}
|
|
const removeType = async (type: number) => {
|
|
const found = trackedTypes.value.find(item => item.type === type);
|
|
|
|
if (!found) {
|
|
return;
|
|
}
|
|
|
|
trackedTypes.value = trackedTypes.value.filter(t => t.id !== found.id);
|
|
await marbasAxiosInstance.delete(`${endpoint}${found.id}`);
|
|
log.info(`Stopped tracking type ${type}`);
|
|
}
|
|
|
|
marbasAxiosInstance.get<MarbasTrackedType[]>(endpoint).then(res => trackedTypes.value = res.data);
|
|
|
|
return { types, addType, removeType };
|
|
});
|
|
|
|
export const createResult = async (id: number, price: MarketTypePrice): Promise<TrackingResult> => ({ history: await getHistory(jitaId, id), ...price }); |