|
1 | 1 | import Flight from "../../shared/models/FlightClass"; |
2 | | -import axios from "axios"; |
3 | 2 | import { API, graphqlOperation } from "aws-amplify"; |
4 | | -import { listFlights } from "../../graphql/queries"; |
| 3 | +import { listFlights, getFlight } from "../../graphql/queries"; |
5 | 4 |
|
6 | 5 | /** |
7 | 6 | * |
@@ -73,52 +72,42 @@ export async function fetchFlights({ commit }, { date, departure, arrival }) { |
73 | 72 |
|
74 | 73 | /** |
75 | 74 | * |
76 | | - * Catalog [Vuex Module Action](https://vuex.vuejs.org/guide/actions.html) - fetchByFlightNumber retrieves a flight given a flight number identification from Catalog service. |
| 75 | + * Catalog [Vuex Module Action](https://vuex.vuejs.org/guide/actions.html) - fetchByFlightId retrieves a unique flight from Catalog service. Flight Number may be reused but not ID. |
77 | 76 | * |
78 | 77 | * Similarly to fetchFlights, it also controls Flight Loader when fetching data from Catalog service. |
79 | 78 | * |
80 | 79 | * **NOTE**: It doesn't mutate the store |
81 | 80 | * @param {object} context - Vuex action context (context.commit, context.getters, context.state, context.dispatch) |
82 | 81 | * @param {object} obj - Object containing params to filter flights from catalog |
83 | | - * @param {Date} obj.date - Date in DD-MM-YYYY format |
84 | | - * @param {string} obj.departure - Airport IATA to be filtered as departure |
85 | | - * @param {string} obj.arrival - Airport IATA to be filtered as arrival |
86 | | - * @param {number} obj.flightNumber - Flight Number |
| 82 | + * @param {string} obj.flightId - Flight Unique Identifier |
87 | 83 | * @returns {promise} - Promise representing flight from Catalog service. |
88 | 84 | * @see {@link SET_LOADER} for more info on mutation |
89 | 85 | * @example |
90 | 86 | * // exerpt from src/views/FlightSelection.vue |
91 | 87 | * async beforeMount() { |
92 | 88 | * if (this.isAuthenticated) { |
93 | 89 | * if (!this.flight) { |
94 | | - * this.selectedFlight = await this.$store.dispatch("catalog/fetchByFlightNumber", { |
95 | | - * date: this.date, |
96 | | - * departure: this.departure, |
97 | | - * arrival: this.arrival, |
98 | | - * flightNumber: parseInt(this.flightNumber) |
| 90 | + * this.selectedFlight = await this.$store.dispatch("catalog/fetchByFlightId", { |
| 91 | + * flightId: this.flightId |
99 | 92 | * }); |
100 | 93 | * } |
101 | 94 | * } |
102 | 95 | * }, |
103 | 96 | */ |
104 | | -export function fetchByFlightNumber( |
105 | | - { commit }, |
106 | | - { date, departure, arrival, flightNumber } |
107 | | -) { |
108 | | - return new Promise(async (resolve, reject) => { |
109 | | - try { |
110 | | - commit("SET_LOADER", true); |
111 | | - const { data: flightData } = await axios.get("/mocks/flights.json"); |
112 | | - const flight = new Flight( |
113 | | - flightData.find(flight => flight.flightNumber === flightNumber) |
114 | | - ); |
| 97 | +export async function fetchByFlightId({ commit }, { flightId }) { |
| 98 | + try { |
| 99 | + commit("SET_LOADER", true); |
| 100 | + const { |
| 101 | + // @ts-ignore |
| 102 | + data: { getFlight: flightData } |
| 103 | + } = await API.graphql(graphqlOperation(getFlight, { id: flightId })); |
115 | 104 |
|
116 | | - commit("SET_LOADER", false); |
117 | | - resolve(flight); |
118 | | - } catch (error) { |
119 | | - console.error(error); |
120 | | - commit("SET_LOADER", false); |
121 | | - reject(error); |
122 | | - } |
123 | | - }); |
| 105 | + const flight = new Flight(flightData); |
| 106 | + commit("SET_LOADER", false); |
| 107 | + return flight; |
| 108 | + } catch (error) { |
| 109 | + console.error(error); |
| 110 | + commit("SET_LOADER", false); |
| 111 | + throw error; |
| 112 | + } |
124 | 113 | } |
0 commit comments