|
1 | 1 | import Flight from "../../shared/models/FlightClass"; |
2 | 2 | import axios from "axios"; |
3 | | -import Amplify, { API, graphqlOperation } from "aws-amplify"; |
| 3 | +import { API, graphqlOperation } from "aws-amplify"; |
4 | 4 | import { listFlights } from "../../graphql/queries"; |
5 | 5 |
|
6 | 6 | /** |
@@ -32,46 +32,42 @@ import { listFlights } from "../../graphql/queries"; |
32 | 32 | * this.filteredFlights = this.sortByDeparture(this.flights); |
33 | 33 | * } |
34 | 34 | */ |
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 |
52 | 49 | } |
53 | | - }; |
| 50 | + } |
| 51 | + }; |
54 | 52 |
|
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)); |
63 | 59 |
|
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)); |
65 | 64 |
|
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 | + } |
75 | 71 | } |
76 | 72 |
|
77 | 73 | /** |
|
0 commit comments