Ne uplne otestovany prepis storu

This commit is contained in:
Michal Kváček
2023-11-19 00:44:01 +01:00
parent e65491e00b
commit f9d70a5d57
76 changed files with 1445 additions and 1463 deletions
+48 -48
View File
@@ -1,57 +1,57 @@
import {defineStore} from 'pinia'
import {apolloWrapper} from "src/graphql/apollo";
import {defineStore} from "pinia";
import {ref} from "vue";
import {AIRCRAFT_DETAIL, AIRCRAFTS_LIST} from "src/graphql/queries/aircrafts";
import {CREATE_AIRCRAFT, DELETE_AIRCRAFT, EDIT_AIRCRAFT} from "src/graphql/mutations/aircrafts";
import {deleteFromItems, updateInItems} from "stores/helpers";
import {deleteFromItems, mutate, query, updateInItems} from "stores/helpers";
import {CREATE_AIRCRAFT, EDIT_AIRCRAFT, DELETE_AIRCRAFT} from "src/graphql/mutations/aircrafts";
export const useAircraftsStore = defineStore('Aircrafts', {
state: () => ({
items: [],
isMutationRunning: false
}),
export const useAircraftsStore = defineStore('Aircrafts', () => {
const aircraft = ref(null);
const aircrafts = ref([]);
const isMutationRunning = ref(false);
const isQueryRunning = ref(false);
getters: {
getDetail: (state) => (aircraftId) => state.items.find(a => a.id === aircraftId) || {}
},
const fetchDetail = (id) => {
return query({
isQueryRunning,
query: AIRCRAFT_DETAIL,
variables: {id},
}).then(item => aircraft.value = item);
};
actions: {
fetchDetail(id) {
return apolloWrapper
.query(AIRCRAFT_DETAIL, {id})
.then((result) => result.data.aircraft);
},
create(formData) {
delete formData['id'];
const fetch = () => {
return query({
isQueryRunning,
query: AIRCRAFTS_LIST
}).then(items => aircrafts.value = items);
}
this.isMutationRunning = true;
return apolloWrapper.mutate(CREATE_AIRCRAFT, {input: formData}).then(({data}) => {
// this.items.push(data.createAircraft);
const create = (input) => {
return mutate({
isMutationRunning,
mutation: CREATE_AIRCRAFT,
variables: {input}
}).then(item => aircrafts.value.push(item))
}
return data.createAircraft;
}).finally(() => this.isMutationRunning = false);
},
delete(id) {
return apolloWrapper.mutate(DELETE_AIRCRAFT, {id}).then(({data}) => {
deleteFromItems(this.items, 'id', id);
const update = (id, input) => {
return mutate({
isMutationRunning,
mutation: EDIT_AIRCRAFT,
variables: {id, input}
}).then(item => updateInItems(aircrafts.value, 'id', item))
}
return data;
});
},
update(id, formData) {
this.isMutationRunning = true;
return apolloWrapper.mutate(EDIT_AIRCRAFT, {id: id, input: formData}).then(({data}) => {
const aircraft = data.editAircraft;
updateInItems(this.items, 'id', aircraft);
return aircraft;
}).finally(() => this.isMutationRunning = false);
},
fetch() {
return apolloWrapper
.query(AIRCRAFTS_LIST)
.then((result) => {
this.items = result.data.aircrafts;
return this.items;
});
const destroy = (id) => {
return mutate({
isMutationRunning,
mutation: DELETE_AIRCRAFT,
variables: {id}
}).then(item => deleteFromItems(aircrafts.value, 'id', item))
}
return {
aircrafts, aircraft, isQueryRunning, isMutationRunning, fetchDetail, fetch, create,
update, destroy
}
}
})
);
+12 -14
View File
@@ -1,20 +1,18 @@
import {defineStore} from 'pinia'
import {apolloWrapper} from "src/graphql/apollo";
import {AIRPORTS_LIST} from "src/graphql/queries/airports";
import {ref} from "vue";
import {query} from "stores/helpers";
export const useAirportsStore = defineStore('Airports', {
state: () => ({
items: [],
}),
export const useAirportsStore = defineStore('Airports', () => {
const airports = ref([])
const isQueryRunning = ref(false);
actions: {
fetch() {
return apolloWrapper
.query(AIRPORTS_LIST)
.then((result) => {
this.items = result.data.airports;
return result.data.airports;
});
}
const fetch = () => {
return query({
isQueryRunning,
query: AIRPORTS_LIST,
}).then(items => airports.value = items);
}
return {airports, isQueryRunning, fetch};
})
+23 -18
View File
@@ -1,22 +1,27 @@
import {defineStore} from 'pinia'
import {ref} from "vue";
export const useAppStore = defineStore('App', {
state: () => ({
pageTitle: null,
breadcrumbs: [],
appStatus: 200
}),
actions: {
resetAfterRouteChange() {
this.setPageTitle("");
this.setBreadcrumbs([]);
this.appStatus = 200;
},
setPageTitle(pageTitle) {
this.pageTitle = pageTitle;
},
setBreadcrumbs(breadcrumbs) {
this.breadcrumbs = breadcrumbs;
}
export const useAppStore = defineStore('App', () => {
const pageTitle = ref(null);
const breadcrumbs = ref([]);
const appStatus = ref(200); // TODO: toto nejspis nebude potreba!
const resetAfterRouteChange = () => {
setPageTitle("");
setBreadcrumbs([]);
appStatus.value = 200;
};
const setPageTitle = (value) => {
// TODO: je toto vubec potreba?
pageTitle.value = value;
}
const setBreadcrumbs = (items) => {
// TODO: je toto vubec potreba?
breadcrumbs.value = items;
}
return {
pageTitle, breadcrumbs, appStatus, resetAfterRouteChange, setPageTitle, setBreadcrumbs
}
})
+40 -33
View File
@@ -1,41 +1,48 @@
import {defineStore} from 'pinia'
import {apolloWrapper} from "src/graphql/apollo";
import {COPILOT_DETAIL, COPILOTS_LIST} from "src/graphql/queries/copilots";
import {CREATE_COPILOT, EDIT_COPILOT} from "src/graphql/mutations/copilot";
import {updateInItems} from "stores/helpers";
import {mutate, query, updateInItems} from "stores/helpers";
import {ref} from "vue";
export const useCopilotsStore = defineStore('Copilots', {
state: () => ({
items: [],
isMutationRunning: false,
}),
export const useCopilotsStore = defineStore('Copilots', () => {
const copilot = ref(null);
const copilots = ref([]);
const isMutationRunning = ref(false);
const isQueryRunning = ref(false);
actions: {
fetchDetail(copilotId, pilotUsername) {
return apolloWrapper.query(COPILOT_DETAIL, {id: copilotId, pilotUsername: pilotUsername}).then(({data}) => data.copilot);
},
fetch() {
return apolloWrapper.query(COPILOTS_LIST).then(result => {
this.items = result.data.copilots;
return result.data.copilots;
});
},
create(formData) {
delete formData['id'];
const fetchDetail = (id) => {
return query({
isQueryRunning,
query: COPILOT_DETAIL,
variables: {id},
}).then(item => copilot.value = item);
};
this.isMutationRunning = true;
return apolloWrapper.mutate(CREATE_COPILOT, {input: formData}).then(({data}) => {
const copilot = data.createCopilot;
this.items.push(copilot);
return copilot;
}).finally(() => this.isMutationRunning = false);
},
update(id, formData) {
return apolloWrapper.mutate(EDIT_COPILOT, {id: id, input: formData}).then(({data}) => {
const copilot = data.editCopilot;
updateInItems(this.items, "id", copilot);
return copilot;
}).finally(() => this.isMutationRunning = false);
},
const fetch = () => {
return query({
isQueryRunning,
query: COPILOTS_LIST,
}).then(items => copilots.value = items);
}
const create = (input) => {
return mutate({
isMutationRunning,
mutation: CREATE_COPILOT,
variables: {input},
}).then(item => copilots.value.push(item))
}
const update = (id, input) => {
return mutate({
isMutationRunning,
mutation: EDIT_COPILOT,
variables: {id, input}
}).then(item => updateInItems(copilots.value, 'id', item))
}
return {
copilot, copilots, isMutationRunning, isQueryRunning,
fetch, fetchDetail, create, update
}
})
+40 -34
View File
@@ -1,42 +1,48 @@
import {defineStore} from 'pinia'
import {apolloWrapper} from "src/graphql/apollo";
import {CREATE_EVENT, EDIT_EVENT} from "src/graphql/mutations/event";
import {updateInItems} from "stores/helpers";
import {mutate, query, updateInItems} from "stores/helpers";
import {ref} from "vue";
import {EVENT_DETAIL, EVENTS_LIST} from "src/graphql/queries/event";
export const useEventsStore = defineStore('Events', {
state: () => ({
items: [],
isMutationRunning: false,
}),
export const useEventsStore = defineStore('Copilots', () => {
const event = ref(null);
const events = ref([]);
const isMutationRunning = ref(false);
const isQueryRunning = ref(false);
actions: {
fetchDetail(eventId, username) {
return apolloWrapper.query(EVENT_DETAIL, {id: eventId, username: username}).then(({data}) => data.event);
},
fetch(username) {
return apolloWrapper.query(EVENTS_LIST, {username}).then(result => {
this.items = result.data.events;
return result.data.events;
});
},
create(formData) {
delete formData['id'];
const fetchDetail = (id) => {
return query({
isQueryRunning,
query: EVENT_DETAIL,
variables: {id},
}).then(item => event.value = item);
};
this.isMutationRunning = true;
return apolloWrapper.mutate(CREATE_EVENT, {input: formData}).then(({data}) => {
const event = data.createEvent;
event.flights = event.flights || [];
this.items.push(event);
return event;
}).finally(() => this.isMutationRunning = false);
},
update(id, formData) {
return apolloWrapper.mutate(EDIT_EVENT, {id: id, input: formData}).then(({data}) => {
const event = data.editEvent;
updateInItems(this.items, "id", event);
return event;
}).finally(() => this.isMutationRunning = false);
},
const fetch = (username = undefined) => {
return query({
isQueryRunning,
query: EVENTS_LIST,
variables: {username}
}).then(items => events.value = items);
}
const create = (input) => {
return mutate({
isMutationRunning,
mutation: CREATE_EVENT,
variables: {input},
}).then(item => events.value.push(item))
}
const update = (id, input) => {
return mutate({
isMutationRunning,
mutation: EDIT_EVENT,
variables: {id, input}
}).then(item => updateInItems(events.value, 'id', item))
}
return {
event, events, isMutationRunning, isQueryRunning, fetch, fetchDetail, create, update
}
})
+102 -55
View File
@@ -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;
// })
// },
// },
});
+109 -47
View File
@@ -1,56 +1,118 @@
import {defineStore} from 'pinia'
import {apolloWrapper} from "src/graphql/apollo";
import {apolloWrapper} from "boot/apollo";
import {ADD_USER_TO_ORGANIZATION, CREATE_ORGANIZATION, EDIT_ORGANIZATION, REMOVE_USER_FROM_ORGANIZATION} from "src/graphql/mutations/organization";
import {ORGANIZATION_DETAIL, ORGANIZATIONS_LIST} from "src/graphql/queries/organization";
import {updateInItems} from "stores/helpers";
import {mutate, query, updateInItems} from "stores/helpers";
import {ref} from "vue";
import {useUserStore} from "stores/User";
export const useOrganizationsStore = defineStore('Organizations', {
state: () => ({
items: [],
isMutationRunning: false,
}),
export const useOrganizationsStore = defineStore('Organizations', () => {
actions: {
fetchDetail(organizationId) {
return apolloWrapper.query(ORGANIZATION_DETAIL, {id: organizationId}).then(({data}) => data.organization);
},
fetch() {
return apolloWrapper.query(ORGANIZATIONS_LIST).then(result => {
this.items = result.data.organizations;
return result.data.organizations;
});
},
create(formData) {
delete formData['id'];
const organization = ref(null);
const organizations = ref([]);
const isMutationRunning = ref(false);
const isQueryRunning = ref(false);
this.isMutationRunning = true;
return apolloWrapper.mutate(CREATE_ORGANIZATION, {input: formData}).then(({data}) => {
const organization = data.createOrganization;
this.items.push(organization);
return organization;
}).finally(() => this.isMutationRunning = false);
},
const fetchDetail = (id) => query({
isQueryRunning,
query: ORGANIZATION_DETAIL,
variables: {id}
}).then(item => organization.value = item);
addUserToOrganization(organizationId) {
this.isMutationRunning = true;
return apolloWrapper.mutate(ADD_USER_TO_ORGANIZATION, {organizationId}).then(({data}) => {
return data.addToOrganization;
}).finally(() => this.isMutationRunning = false);
},
const fetch = () => query({
isQueryRunning,
query: ORGANIZATIONS_LIST
}).then(items => organizations.value = items);
removeUserFromOrganization(organizationId) {
this.isMutationRunning = true;
return apolloWrapper.mutate(REMOVE_USER_FROM_ORGANIZATION, {organizationId}).then(({data}) => {
return data.removeFromOrganization;
}).finally(() => this.isMutationRunning = false);
},
const create = (input) => mutate({
isMutationRunning,
mutation: CREATE_ORGANIZATION,
variables: {input}
}).then(item => organizations.value.push(item));
update(id, formData) {
return apolloWrapper.mutate(EDIT_ORGANIZATION, {id: id, input: formData}).then(({data}) => {
const organization = data.editOrganization;
updateInItems(this.items, "id", organization);
return organization;
}).finally(() => this.isMutationRunning = false);
},
}
})
const update = (id, input) => mutate({
isMutationRunning,
mutation: EDIT_ORGANIZATION,
variables: {id, input}
}).then(item => {
updateInItems(organizations.value, 'id', item);
return item;
});
const addUserToOrganization = (organizationId) => mutate({
isMutationRunning,
mutation: ADD_USER_TO_ORGANIZATION,
variables: {organizationId}
}).then(item => {
useUserStore().user.organizations.push(item);
organization.value.users = item.users;
return item;
});
const removeUserFromOrganization = (organizationId) => mutate({
isMutationRunning,
mutation: REMOVE_USER_FROM_ORGANIZATION,
variables: {organizationId},
}).then(item => {
const idx = useUserStore().user.organizations.findIndex(o => o.id === organizationId);
useUserStore().user.organizations.splice(idx, 1);
organization.value.users = item.users;
return item;
});
return {
organization, organizations, isMutationRunning, isQueryRunning, fetchDetail, fetch, create, update,
addUserToOrganization, removeUserFromOrganization
};
//
//
// state: () => ({
// items: [],
// isMutationRunning: false,
// }),
//
// actions: {
// fetchDetail(organizationId) {
// return apolloWrapper.query(ORGANIZATION_DETAIL, {id: organizationId}).then(({data}) => data.organization);
// },
// fetch() {
// return apolloWrapper.query(ORGANIZATIONS_LIST).then(result => {
// this.items = result.data.organizations;
// return result.data.organizations;
// });
// },
// create(formData) {
// delete formData['id'];
//
// this.isMutationRunning = true;
// return apolloWrapper.mutate(CREATE_ORGANIZATION, {input: formData}).then(({data}) => {
// const organization = data.createOrganization;
// this.items.push(organization);
// return organization;
// }).finally(() => this.isMutationRunning = false);
// },
//
// addUserToOrganization(organizationId) {
// this.isMutationRunning = true;
// return apolloWrapper.mutate(ADD_USER_TO_ORGANIZATION, {organizationId}).then(({data}) => {
// return data.addToOrganization;
// }).finally(() => this.isMutationRunning = false);
// },
//
// removeUserFromOrganization(organizationId) {
// this.isMutationRunning = true;
// return apolloWrapper.mutate(REMOVE_USER_FROM_ORGANIZATION, {organizationId}).then(({data}) => {
// return data.removeFromOrganization;
// }).finally(() => this.isMutationRunning = false);
// },
//
// update(id, formData) {
// return apolloWrapper.mutate(EDIT_ORGANIZATION, {id: id, input: formData}).then(({data}) => {
// const organization = data.editOrganization;
// updateInItems(this.items, "id", organization);
// return organization;
// }).finally(() => this.isMutationRunning = false);
// },
// }
});
+66 -52
View File
@@ -1,65 +1,79 @@
import {defineStore} from "pinia";
import {apolloWrapper} from "src/graphql/apollo";
import {ADJUST_PHOTO, CHANGE_PHOTO_ORIENTATION, DELETE_PHOTO, EDIT_PHOTO, UPLOAD_PHOTO} from "src/graphql/mutations/photo";
import {PHOTO_DETAIL} from "src/graphql/queries/photo";
import {ref} from "vue";
import {mutate, query} from "stores/helpers";
export const usePhotosStore = defineStore('Photos', {
state: () => ({
isMutationRunning: false,
}),
export const usePhotosStore = defineStore('Photos', () => {
const photo = ref(null);
const isMutationRunning = ref(false);
const isQueryRunning = ref(false);
actions: {
fetchDetail(id) {
return apolloWrapper.query(PHOTO_DETAIL, {id}).then(({data}) => {
return data.photo;
});
},
upload(flightId, formData) {
if (!flightId) {
console.error("No flightId specified for photo upload! Aborting");
return;
}
const fetchDetail = (id) => query({
isQueryRunning,
query: PHOTO_DETAIL,
variables: {id}
}).then(item => photo.value = item);
formData.flightId = flightId;
const upload = (flightId, input) => {
if (!flightId) {
console.error("No flightId specified for photo upload! Aborting");
return;
}
if (!formData.name) {
formData.name = "";
}
input.flightId = flightId;
if (!formData.description) {
formData.description = "";
}
if (!input.name) {
input.name = "";
}
this.isMutationRunning = true;
return apolloWrapper.mutate(UPLOAD_PHOTO, {input: formData}).then(({data}) => {
return data.uploadPhoto;
}).finally(() => this.isMutationRunning = false);
},
update(id, formData) {
this.isMutationRunning = true;
return apolloWrapper.mutate(EDIT_PHOTO, {id, input: formData}).then(({data}) => {
return data.editPhoto;
}).finally(() => this.isMutationRunning = false);
},
if (!input.description) {
input.description = "";
}
changeOrientation(id, direction) {
this.isMutationRunning = true;
return apolloWrapper.mutate(CHANGE_PHOTO_ORIENTATION, {id, direction}).then(({data}) => {
return data.changeOrientation;
}).finally(() => this.isMutationRunning = false);
},
// TODO: ^^ patri tohle do storu?
adjust(id, adjustment) {
this.isMutationRunning = true;
return apolloWrapper.mutate(ADJUST_PHOTO, {id, adjustment}).then(({data}) => {
return data.adjustPhoto;
}).finally(() => this.isMutationRunning = false);
},
delete(photoId) {
this.isMutationRunning = true;
return apolloWrapper.mutate(DELETE_PHOTO, {id: photoId}).then(({data}) => {
return data.deletePhoto;
}).finally(() => this.isMutationRunning = false);
return mutate({
isMutationRunning,
mutation: UPLOAD_PHOTO,
variables: {input}
})
};
const update = (id, input) => mutate({
isMutationRunning,
mutation: EDIT_PHOTO,
variables: {id, input}
}).then(item => {
photo.value = item;
return item;
})
const changeOrientation = (id, direction) => mutate({
isMutationRunning,
mutation: CHANGE_PHOTO_ORIENTATION,
variables: {id, direction}
}).then(item => {
photo.value = item;
return item;
});
const adjust = (id, adjustment) => mutate({
isMutationRunning,
mutation: ADJUST_PHOTO,
variables: {id, adjustment}
}).then(item => {
photo.value = item;
return item;
});
const destroy = (id) => mutate({
isMutationRunning,
mutation: DELETE_PHOTO,
variables: {id}
});
return {
photo, isMutationRunning, isQueryRunning, fetchDetail, upload, update, changeOrientation, adjust, destroy
}
}
});
+18 -32
View File
@@ -1,39 +1,25 @@
import {defineStore} from 'pinia'
import {apolloWrapper} from "src/graphql/apollo";
import {POI_TYPE_DETAIL, POI_TYPE_LIST} from "src/graphql/queries/poi_type";
import {ref} from "vue";
import {query} from "stores/helpers";
export const usePointsOfInterestTypeStore = defineStore('PointsOfInterestType', {
state: () => ({
items: []
}),
export const usePointsOfInterestTypeStore = defineStore('PointsOfInterestType', () => {
const type = ref(null);
const types = ref([]);
const isQueryRunning = ref(false);
const fetchDetail = (id) => query({
isQueryRunning,
query: POI_TYPE_DETAIL,
variables: {id}
}).then(item => type.value = item);
actions: {
// create(input) {
// return apolloWrapper.mutate(CREATE_POI, {input}).then((data) => {
// const poi = data.data.createPointOfInterest;
// this.items.push(poi);
// return poi;
// })
// },
// update(id, input) {
// return apolloWrapper.mutate(EDIT_POI, {id, input}).then((data) => {
// console.log("Pridat update POI ve store");
// return data.data.editPointOfInterest;
// })
// },
const fetch = () => query({
isQueryRunning,
query: POI_TYPE_LIST,
}).then(item => types.value = item);
fetchDetail(id) {
if (typeof id !== "number") {
id = parseInt(id);
}
return apolloWrapper.query(POI_TYPE_DETAIL, {id}).then(result => result.data.pointOfInterest);
},
fetch() {
return apolloWrapper.query(POI_TYPE_LIST).then(result => {
this.items = result.data.pointOfInterestTypes;
return result.data.pointOfInterestTypes;
});
}
}
return {types, fetchDetail, fetch};
})
+38 -41
View File
@@ -1,52 +1,49 @@
import {defineStore} from 'pinia'
import {apolloWrapper} from "src/graphql/apollo";
import {POI_DETAIL, POI_LIST} from "src/graphql/queries/poi";
import {CREATE_POI, EDIT_POI} from "src/graphql/mutations/poi";
import {updateInItems} from "stores/helpers";
import {mutate, query, updateInItems} from "stores/helpers";
import {ref} from "vue";
export const usePointsOfInterestStore = defineStore('PointsOfInterest', {
state: () => ({
items: [],
isMutationRunning: false,
}),
export const usePointsOfInterestStore = defineStore('PointsOfInterest', () => {
getters: {
getDetail: (state) => (poiId) => {
return state.items.find(p => p.id === poiId);
},
},
const pointOfInterest = ref(null);
const pointsOfInterest = ref([]);
const isMutationRunning = ref(false);
const isQueryRunning = ref(false);
actions: {
create(input) {
this.isMutationRunning = true;
return apolloWrapper.mutate(CREATE_POI, {input}).then((data) => {
const poi = data.data.createPointOfInterest;
this.items.push(poi);
return poi;
}).finally(() => this.isMutationRunning = false);
},
update(id, input) {
this.isMutationRunning = true;
const fetchDetail = (id) => {
return query({
isQueryRunning,
query: POI_DETAIL,
variables: {id},
}).then(item => pointOfInterest.value = item);
};
return apolloWrapper.mutate(EDIT_POI, {id, input}).then((data) => {
const poi = data.data.editPointOfInterest;
updateInItems(this.items, "id", poi);
return poi;
}).finally(() => this.isMutationRunning = false);
},
const fetch = () => {
return query({
isQueryRunning,
query: POI_LIST,
}).then(items => pointsOfInterest.value = items);
}
fetchDetail(id) {
if (typeof id !== "number") {
id = parseInt(id);
}
return apolloWrapper.query(POI_DETAIL, {id}).then(result => result.data.pointOfInterest);
},
fetch() {
return apolloWrapper.query(POI_LIST).then(result => {
this.items = result.data.pointsOfInterest;
return result.data.pointsOfInterest;
});
const create = (input) => {
return mutate({
isMutationRunning,
mutation: CREATE_POI,
variables: {input},
}).then(item => pointsOfInterest.value.push(item))
}
const update = (id, input) => {
return mutate({
isMutationRunning,
mutation: EDIT_POI,
variables: {id, input}
}).then(item => updateInItems(pointsOfInterest.value, 'id', item))
}
return {
pointOfInterest, pointsOfInterest, isMutationRunning, isQueryRunning, fetch, fetchDetail, create, update
}
}
})
+84 -80
View File
@@ -1,90 +1,94 @@
import {defineStore} from 'pinia'
import {api} from "boot/axios";
import {apolloWrapper} from "src/graphql/apollo";
import {EDIT_USER} from "src/graphql/mutations/user";
import {LOGGED_USER_DETAIL, USER_DETAIL} from "src/graphql/queries/user";
import {ref} from "vue";
import {mutate, query} from "stores/helpers";
export const useUserStore = defineStore('LoggedUser', {
state: () => ({
user: {},
isRefreshing: false,
isMutationRunning: false,
}),
export const useUserStore = defineStore('LoggedUser', () => {
const user = ref(null);
const publicUser = ref(null);
const isQueryRunning = ref(false);
const isMutationRunning = ref(false);
const isRefreshing = ref(false);
actions: {
update(formData) {
this.isMutationRunning = true;
const fetchLoggedUserDetail = () => query({
isQueryRunning,
query: LOGGED_USER_DETAIL
}).then(item => {
user.value = item;
return item;
});
apolloWrapper.mutate(EDIT_USER, {input: formData}).then((response) => {
const user = response.data?.editLoggedUser;
this.user = user;
return user;
}).finally(() => this.isMutationRunning = false);
},
const fetchPublicUserDetail = (username) => query({
isQueryRunning,
query: USER_DETAIL,
variables: {username}
}).then(item => {
publicUser.value = item;
return item;
});
fetchLoggedUserDetail() {
return apolloWrapper.query(LOGGED_USER_DETAIL).then((response) => {
const user = response.data?.loggedUser;
this.user = user;
return user;
})
},
const update = (input) => mutate({
isMutationRunning,
mutation: EDIT_USER,
variables: {input}
}).then(item => {
user.value = item;
return item;
});
fetchDetail(username) {
return apolloWrapper.query(USER_DETAIL, {username}).then((response) => {
return response.data?.user;
});
},
async register(formData) {
return api.post('/registration', formData).then(response => {
const registeredUser = response.data;
const isSuccess = response.status === 201
})
},
async login(formData) {
return api.post("/login", formData).then(({data}) => {
this.setTokens(data);
this.user = data.user;
})
},
logout() {
return api.post("/logout").then(response => {
Object.assign(this.user, {});
localStorage.removeItem("accessToken");
localStorage.removeItem("accessTokenValidity");
});
},
setTokens({access_token, access_token_validity}) {
const now = new Date().getTime() / 1000;
localStorage.setItem("accessToken", access_token);
localStorage.setItem("accessTokenValidity", now + access_token_validity);
},
getAccessToken() {
const tokenValidity = localStorage.getItem("accessTokenValidity");
const now = new Date().getTime() / 1000;
if (tokenValidity < now) {
localStorage.removeItem("accessToken");
localStorage.removeItem("accessTokenValidity");
}
return localStorage.getItem(`accessToken`);
},
hasAccessToken() {
return !!localStorage.getItem("accessToken");
},
async refreshAccessToken() {
return api.post("/refresh")
.then(({data}) => {
this.setTokens(data);
return data.access_token;
const register = async (formData) => {
return api.post('/registration', formData).then(response => {
const registeredUser = response.data;
const isSuccess = response.status === 201
})
},
}
})
};
const login = async (formData) => {
return api.post("/login", formData).then(({data}) => {
setTokens(data);
this.user = data.user;
})
};
const logout = () => {
return api.post("/logout").then(response => {
localStorage.removeItem("accessToken");
localStorage.removeItem("accessTokenValidity");
user.value = null;
});
};
const setTokens = ({access_token, access_token_validity}) => {
const now = new Date().getTime() / 1000;
localStorage.setItem("accessToken", access_token);
localStorage.setItem("accessTokenValidity", now + access_token_validity);
};
const getAccessToken = () => {
const tokenValidity = localStorage.getItem("accessTokenValidity");
const now = new Date().getTime() / 1000;
if (tokenValidity < now) {
localStorage.removeItem("accessToken");
localStorage.removeItem("accessTokenValidity");
}
return localStorage.getItem(`accessToken`);
};
const hasAccessToken = () => !!localStorage.getItem("accessToken");
const refreshAccessToken = async () => api.post("/refresh")
.then(({data}) => {
setTokens(data);
return data.access_token;
});
return {
isQueryRunning, isMutationRunning, user, publicUser, fetchLoggedUserDetail, fetchPublicUserDetail, update,
register, login, logout,
getAccessToken, hasAccessToken, refreshAccessToken, isRefreshing
}
});
+25
View File
@@ -1,8 +1,13 @@
import {apolloWrapper} from "boot/apollo";
import {getOperationName} from "@apollo/client/utilities";
export const updateInItems = (items, key, data) => {
const idx = items.findIndex(i => i[key] === data[key]);
if (idx >= 0)
items[idx] = data;
else items.push(data);
return items;
}
export const deleteFromItems = (items, key, value) => {
@@ -11,3 +16,23 @@ export const deleteFromItems = (items, key, value) => {
items.splice(idx, 1);
}
export const query = ({isQueryRunning, query, variables}) => {
if (variables && variables.id) {
variables.id = parseInt(variables.id);
}
isQueryRunning.value = true;
const queryName = query.definitions[0].selectionSet.selections[0].name.value;
return apolloWrapper.query(query, variables)
.then((result) => result.data[queryName])
.finally(() => isQueryRunning.value = false);
}
export const mutate = ({isMutationRunning, mutation, variables}) => {
isMutationRunning.value = true;
const mutationName = getOperationName(mutation);
return apolloWrapper.mutate(mutation, variables)
.then(result => result.data[mutationName])
.finally(() => isMutationRunning.value = false);
}
+1 -14
View File
@@ -1,20 +1,7 @@
import { store } from 'quasar/wrappers'
import { createPinia } from 'pinia'
/*
* If not building with SSR mode, you can
* directly export the Store instantiation;
*
* The function below can be async too; either use
* async/await or return a Promise which resolves
* with the Store instance.
*/
export default store((/* { ssrContext } */) => {
const pinia = createPinia()
// You can add Pinia plugins here
// pinia.use(SomePiniaPlugin)
const pinia = createPinia();
return pinia
})