63 lines
No EOL
2.5 KiB
TypeScript
63 lines
No EOL
2.5 KiB
TypeScript
import {defineStore} from 'pinia'
|
|
import {type Show, baseShow} from '@models/show/show'
|
|
import type {ShowDays} from "@models/show/showDays";
|
|
import axios, {type AxiosResponse} from "axios";
|
|
import {camelToSnake, cleanOptions, snakeToCamel} from "@/helpers/AxiosHelper.ts";
|
|
import { extractTimeUTC} from "@/helpers/DateFormatter.ts";
|
|
import {DateTime} from "luxon";
|
|
|
|
export const useShowStore = defineStore('show', {
|
|
state: () => ({
|
|
currentShow: {} as Show,
|
|
baseShowDays: {} as ShowDays
|
|
}),
|
|
actions: {
|
|
loadShow(showData: Show) {
|
|
this.currentShow = {...showData}
|
|
},
|
|
updateField(payload: { key: string; value: any }) {
|
|
this.currentShow[payload.key] = payload.value
|
|
},
|
|
resetShow() {
|
|
this.currentShow = { ...baseShow() };
|
|
},
|
|
async getShow(id: Number, options: {
|
|
withDjs?: boolean | null;
|
|
isScheduled?: boolean | null;
|
|
}) {
|
|
const filteredParams = cleanOptions(options);
|
|
return await axios.get(`/show/${id}`, {params: filteredParams})
|
|
.then((response: AxiosResponse) => {
|
|
return snakeToCamel(response.data);
|
|
}).catch((error: Error) => {
|
|
console.log("Error: " + error);
|
|
})
|
|
},
|
|
async createShow() {
|
|
let showData = {...this.currentShow};
|
|
showData.showDays.firstShow = DateTime.fromJSDate(showData.showDays.firstShow).setZone('utc').toISO();
|
|
if (showData.showDays.lastShow) {
|
|
showData.showDays.lastShow = DateTime.fromJSDate(showData.showDays.lastShow).setZone('utc').toISO();
|
|
}
|
|
showData.showDays.startTime = extractTimeUTC(showData.showDays.startTime)
|
|
showData = camelToSnake(showData);
|
|
return await axios.post(`/show`, showData)
|
|
.then((response: AxiosResponse) => {
|
|
return response.data
|
|
}).catch((error: Error) => {
|
|
console.error("Error: " + error);
|
|
})
|
|
},
|
|
async updateShow() {
|
|
let showData = {...this.currentShow};
|
|
showData = camelToSnake(showData);
|
|
return await axios.put(`/show/${showData.id}`, showData)
|
|
.then((response: AxiosResponse) => {
|
|
return response.data
|
|
}).catch((error: Error) => {
|
|
console.error("Error: " + error.message);
|
|
})
|
|
},
|
|
|
|
}
|
|
}) |