Add ticket to storeand catch error with toast in component

This commit is contained in:
Boris Waaub 2024-04-19 17:46:12 +02:00
parent 2cdfb50058
commit baab8e94ce
5 changed files with 56 additions and 20 deletions

View File

@ -18,9 +18,10 @@
<script lang="ts"> <script lang="ts">
import { computed, defineComponent, onMounted } from 'vue'; import { computed, defineComponent, inject, onMounted } from 'vue';
import { useStore } from 'vuex'; import { useStore } from 'vuex';
// Types // Types
import { Person } from '../../../../../../ChillPersonBundle/Resources/public/types'; import { Person } from '../../../../../../ChillPersonBundle/Resources/public/types';
import { Motive, Ticket } from '../../types'; import { Motive, Ticket } from '../../types';
@ -31,11 +32,19 @@
setup() { setup() {
const store = useStore(); const store = useStore();
const ticket = JSON.parse(window.initialTicket) as Ticket; const toast = inject('toast') as any;
store.commit('setTicket', JSON.parse(window.initialTicket) as Ticket);
const motives = computed(() => store.getters.getMotives as Motive[]) const motives = computed(() => store.getters.getMotives as Motive[])
const ticket = computed(() => store.getters.getTicket as Ticket)
onMounted(() => { onMounted(() => {
store.dispatch('fetchMotives'); try {
store.dispatch('fetchMotives')
} catch (error) {
toast.error(error)
};
}); });
return { return {

View File

@ -19,10 +19,14 @@ const i18n = _createI18n(messages);
const _app = createApp({ const _app = createApp({
template: '<app></app>', template: '<app></app>',
}) });
_app
.use(store) .use(store)
.use(i18n) .use(i18n)
.use(VueToast) // Cant use this.$toast in components in composition API so we need to provide it
// Fix: with vue-toast-notification@^3
.use(VueToast).provide('toast', _app.config.globalProperties.$toast)
.component('app', App) .component('app', App)
.mount('#ticketRoot'); .mount('#ticketRoot');

View File

@ -1,12 +1,15 @@
import { createStore } from "vuex"; import { createStore } from "vuex";
import { State as MotiveStates, moduleMotive } from "./modules/motive"; import { State as MotiveStates, moduleMotive } from "./modules/motive";
import { State as TicketStates, moduleTicket } from "./modules/ticket";
export type RootState = { export type RootState = {
motive: MotiveStates; motive: MotiveStates;
ticket: TicketStates;
}; };
export const store = createStore({ export const store = createStore({
modules: { modules: {
motive:moduleMotive, motive:moduleMotive,
ticket:moduleTicket,
} }
}); });

View File

@ -11,7 +11,7 @@ export interface State {
export const moduleMotive: Module<State, RootState> ={ export const moduleMotive: Module<State, RootState> ={
state: () => ({ state: () => ({
motives: [], motives: [] as Array<Motive>,
}), }),
getters: { getters: {
getMotives(state) { getMotives(state) {
@ -25,23 +25,15 @@ export const moduleMotive: Module<State, RootState> ={
}, },
actions: { actions: {
async fetchMotives({ commit }) { async fetchMotives({ commit }) {
try{ const results = await fetchResults("/api/1.0/ticket/motive.json") as Motive[];
const results = await fetchResults("/api/1.0/ticket/motive.json") as Motive[]; commit("setMotives", results);
commit("setMotives", results); return results;
}
catch (e) {
console.error("Error while fetching motives", e);
}
}, },
async createMotive({ commit }, datas: {currentMotiveId: number, motive: Motive}) { async createMotive({ commit }, datas: {currentMotiveId: number, motive: Motive}) {
const { currentMotiveId, motive } = datas; const { currentMotiveId, motive } = datas;
try { const result = await makeFetch("POST", `/api/1.0/ticket/${currentMotiveId}/motive/set`, motive);
const result = await makeFetch("POST", `/api/1.0/ticket/${currentMotiveId}/motive/set`, motive); commit("setMotives", result);
commit("setMotives", result);; return result;
}
catch (e) {
console.error("Error while creating motive", e);
}
} }
}, },
}; };

View File

@ -0,0 +1,28 @@
import { fetchResults, makeFetch } from "../../../../../../../../ChillMainBundle/Resources/public/lib/api/apiMethods";
import { Module } from "vuex";
import { RootState } from "..";
import { Ticket } from "../../../../types";
export interface State {
ticket: Ticket;
}
export const moduleTicket: Module<State, RootState> ={
state: () => ({
ticket: {} as Ticket,
}),
getters: {
getTicket(state) {
return state.ticket;
}
},
mutations: {
setTicket(state, ticket) {
state.ticket = ticket;
}
},
actions: {
},
};