Skip to content

Commit 01e03d4

Browse files
committed
improv: cleans up Vuex syntax w/ async/await
1 parent cce7a74 commit 01e03d4

File tree

1 file changed

+33
-37
lines changed

1 file changed

+33
-37
lines changed

src/store/catalog/actions.js

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Flight from "../../shared/models/FlightClass";
22
import axios from "axios";
3-
import Amplify, { API, graphqlOperation } from "aws-amplify";
3+
import { API, graphqlOperation } from "aws-amplify";
44
import { listFlights } from "../../graphql/queries";
55

66
/**
@@ -32,46 +32,42 @@ import { listFlights } from "../../graphql/queries";
3232
* this.filteredFlights = this.sortByDeparture(this.flights);
3333
* }
3434
*/
35-
export function fetchFlights({ commit }, { date, departure, arrival }) {
36-
return new Promise(async (resolve, reject) => {
37-
commit("SET_LOADER", true);
38-
try {
39-
// flight filter
40-
41-
const flightFilter = {
42-
filter: {
43-
departureDate: {
44-
beginsWith: date
45-
},
46-
departureAirportCode: {
47-
eq: departure
48-
},
49-
arrivalAirportCode: {
50-
eq: arrival
51-
}
35+
export async function fetchFlights({ commit }, { date, departure, arrival }) {
36+
commit("SET_LOADER", true);
37+
try {
38+
// listFlights query filter
39+
const flightFilter = {
40+
filter: {
41+
departureDate: {
42+
beginsWith: date
43+
},
44+
departureAirportCode: {
45+
eq: departure
46+
},
47+
arrivalAirportCode: {
48+
eq: arrival
5249
}
53-
};
50+
}
51+
};
5452

55-
// graphQL API
56-
const {
57-
data: {
58-
listFlights: { items: flightData }
59-
}
60-
} = await API.graphql(graphqlOperation(listFlights, flightFilter));
61-
62-
console.log(flightData);
53+
const {
54+
// @ts-ignore
55+
data: {
56+
listFlights: { items: flightData }
57+
}
58+
} = await API.graphql(graphqlOperation(listFlights, flightFilter));
6359

64-
const flights = flightData.map(flight => new Flight(flight));
60+
// data mutations happen within a Flight class
61+
// here we convert graphQL results into an array of Flights
62+
// before comitting to Vuex State Management
63+
const flights = flightData.map(flight => new Flight(flight));
6564

66-
commit("SET_FLIGHTS", flights);
67-
commit("SET_LOADER", false);
68-
resolve();
69-
} catch (error) {
70-
console.error(error);
71-
commit("SET_LOADER", false);
72-
reject(error);
73-
}
74-
});
65+
commit("SET_FLIGHTS", flights);
66+
commit("SET_LOADER", false);
67+
} catch (error) {
68+
console.error(error);
69+
commit("SET_LOADER", false);
70+
}
7571
}
7672

7773
/**

0 commit comments

Comments
 (0)