Ne uplne otestovany prepis storu
This commit is contained in:
+102
-55
@@ -1,67 +1,114 @@
|
||||
import {defineStore} from 'pinia'
|
||||
import {apolloWrapper} from "src/graphql/apollo"
|
||||
import {FLIGHT_DETAIL, FLIGHTS_LIST} from "src/graphql/queries/flights";
|
||||
import {EDIT_FLIGHT, CREATE_FLIGHT, DELETE_FLIGHT} from "src/graphql/mutations/flight";
|
||||
import {updateInItems} from "stores/helpers";
|
||||
import {deleteFromItems, mutate, query, updateInItems} from "stores/helpers";
|
||||
import {ref} from "vue";
|
||||
import {DELETE_AIRCRAFT} from "src/graphql/mutations/aircrafts";
|
||||
|
||||
export const useFlightsStore = defineStore('Flights', {
|
||||
state: () => ({
|
||||
items: [],
|
||||
isMutationRunning: false
|
||||
}),
|
||||
export const useFlightsStore = defineStore('Flights', () => {
|
||||
const flight = ref(null);
|
||||
const flights = ref([]);
|
||||
const isMutationRunning = ref(false);
|
||||
const isQueryRunning = ref(false);
|
||||
|
||||
getters: {
|
||||
getDetail: (state) => (flightId) => {
|
||||
return state.items.find(f => f.id === flightId);
|
||||
},
|
||||
},
|
||||
const fetchDetail = (id, username) => {
|
||||
return query({
|
||||
isQueryRunning,
|
||||
query: FLIGHT_DETAIL,
|
||||
variables: {username, id},
|
||||
}).then(item => flight.value = item);
|
||||
};
|
||||
|
||||
actions: {
|
||||
create(formData) {
|
||||
delete formData['id'];
|
||||
return apolloWrapper.mutate(CREATE_FLIGHT, {input: formData}).then(({data}) => {
|
||||
const flightId = data.createFlight.id;
|
||||
const fetch = (username = undefined) => {
|
||||
return query({
|
||||
isQueryRunning,
|
||||
query: FLIGHTS_LIST,
|
||||
variables: {username}
|
||||
}).then(items => flights.value = items);
|
||||
}
|
||||
|
||||
// pitomy ohack kvuli tomu, ze se mi nedari dostat z API rovnou informace o nove vytvorenem letadle
|
||||
this.fetchDetail(flightId).then(flight => {
|
||||
this.items.push(flight);
|
||||
})
|
||||
|
||||
return data.createFlight;
|
||||
})
|
||||
},
|
||||
update(id, formData) {
|
||||
this.isMutationRunning = true;
|
||||
return apolloWrapper.mutate(EDIT_FLIGHT, {id: id, input: formData}).then(({data}) => {
|
||||
const flight = data.editFlight;
|
||||
updateInItems(this.items, "id", flight);
|
||||
const create = (input) => {
|
||||
return mutate({
|
||||
isMutationRunning,
|
||||
mutation: CREATE_FLIGHT,
|
||||
variables: {input},
|
||||
}).then(item => {
|
||||
flights.value.push(item);
|
||||
return item;
|
||||
})
|
||||
}
|
||||
|
||||
return flight;
|
||||
}).finally(() => this.isMutationRunning = false);
|
||||
},
|
||||
fetchDetail(id, username = null) {
|
||||
if (typeof id !== "number") {
|
||||
id = parseInt(id);
|
||||
}
|
||||
return apolloWrapper.query(FLIGHT_DETAIL, {id, username}).then(result => result.data.flight);
|
||||
},
|
||||
fetch(username = null) {
|
||||
return apolloWrapper
|
||||
.query(FLIGHTS_LIST, {username})
|
||||
.then((resp) => {
|
||||
const data = resp.data;
|
||||
this.items = data.flights;
|
||||
return data.flights;
|
||||
}).catch(err => console.log(err));
|
||||
},
|
||||
delete(id) {
|
||||
return apolloWrapper.mutate(DELETE_FLIGHT, {id}).then(({data}) => {
|
||||
const update = (id, input) => {
|
||||
return mutate({
|
||||
isMutationRunning,
|
||||
mutation: EDIT_FLIGHT,
|
||||
variables: {id, input}
|
||||
}).then(item => {
|
||||
updateInItems(flights.value, 'id', item);
|
||||
flight.value = item;
|
||||
})
|
||||
}
|
||||
|
||||
const idx = this.items.indexOf(this.items.find(f => f.id === id));
|
||||
this.items.splice(idx, 1);
|
||||
const destroy = (id) => {
|
||||
return mutate({
|
||||
isMutationRunning,
|
||||
mutation: DELETE_FLIGHT,
|
||||
variables: {id}
|
||||
}).then(item => deleteFromItems(flights.value, 'id', item))
|
||||
}
|
||||
|
||||
return data.deleteFlight;
|
||||
})
|
||||
},
|
||||
},
|
||||
|
||||
return {
|
||||
flight, flights, isMutationRunning, isQueryRunning, fetchDetail, fetch, create, update, destroy
|
||||
}
|
||||
//
|
||||
// actions: {
|
||||
// create(formData) {
|
||||
// delete formData['id'];
|
||||
// return apolloWrapper.mutate(CREATE_FLIGHT, {input: formData}).then(({data}) => {
|
||||
// const flightId = data.createFlight.id;
|
||||
//
|
||||
// // pitomy ohack kvuli tomu, ze se mi nedari dostat z API rovnou informace o nove vytvorenem letadle
|
||||
// this.fetchDetail(flightId).then(flight => {
|
||||
// this.items.push(flight);
|
||||
// })
|
||||
//
|
||||
// return data.createFlight;
|
||||
// })
|
||||
// },
|
||||
// update(id, formData) {
|
||||
// this.isMutationRunning = true;
|
||||
// return apolloWrapper.mutate(EDIT_FLIGHT, {id: id, input: formData}).then(({data}) => {
|
||||
// const flight = data.editFlight;
|
||||
// updateInItems(this.items, "id", flight);
|
||||
//
|
||||
// return flight;
|
||||
// }).finally(() => this.isMutationRunning = false);
|
||||
// },
|
||||
// fetchDetail(id, username = null) {
|
||||
// if (typeof id !== "number") {
|
||||
// id = parseInt(id);
|
||||
// }
|
||||
// return apolloWrapper.query(FLIGHT_DETAIL, {id, username}).then(result => result.data.flight);
|
||||
// },
|
||||
// fetch(username = null) {
|
||||
// return apolloWrapper
|
||||
// .query(FLIGHTS_LIST, {username})
|
||||
// .then((resp) => {
|
||||
// const data = resp.data;
|
||||
// this.items = data.flights;
|
||||
// return data.flights;
|
||||
// }).catch(err => console.log(err));
|
||||
// },
|
||||
// delete(id) {
|
||||
// return apolloWrapper.mutate(DELETE_FLIGHT, {id}).then(({data}) => {
|
||||
//
|
||||
// const idx = this.items.indexOf(this.items.find(f => f.id === id));
|
||||
// this.items.splice(idx, 1);
|
||||
//
|
||||
// return data.deleteFlight;
|
||||
// })
|
||||
// },
|
||||
// },
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user