Skip to content

Commit 8c16109

Browse files
committed
feat: implement getFlight query, refactors fetchByFlightNumber to fetchByFlightId
1 parent e1c3b5f commit 8c16109

File tree

3 files changed

+25
-44
lines changed

3 files changed

+25
-44
lines changed

src/store/catalog/actions.js

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import Flight from "../../shared/models/FlightClass";
2-
import axios from "axios";
32
import { API, graphqlOperation } from "aws-amplify";
4-
import { listFlights } from "../../graphql/queries";
3+
import { listFlights, getFlight } from "../../graphql/queries";
54

65
/**
76
*
@@ -73,52 +72,42 @@ export async function fetchFlights({ commit }, { date, departure, arrival }) {
7372

7473
/**
7574
*
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.
7776
*
7877
* Similarly to fetchFlights, it also controls Flight Loader when fetching data from Catalog service.
7978
*
8079
* **NOTE**: It doesn't mutate the store
8180
* @param {object} context - Vuex action context (context.commit, context.getters, context.state, context.dispatch)
8281
* @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
8783
* @returns {promise} - Promise representing flight from Catalog service.
8884
* @see {@link SET_LOADER} for more info on mutation
8985
* @example
9086
* // exerpt from src/views/FlightSelection.vue
9187
* async beforeMount() {
9288
* if (this.isAuthenticated) {
9389
* 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
9992
* });
10093
* }
10194
* }
10295
* },
10396
*/
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 }));
115104

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+
}
124113
}

src/views/FlightResults.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
:to="{
9292
name: 'selectedFlight',
9393
params: { flight: flight },
94-
query: { flightNumber: flight.flightNumber, date, departure, arrival }
94+
query: { flightId: flight.id }
9595
}"
9696
v-for="flight in filteredFlights"
9797
:key="flight.id"

src/views/FlightSelection.vue

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,11 @@ export default {
118118
name: "FlightSelection",
119119
/**
120120
* @param {Flight} flight - Selected Flight
121-
* @param {number} flightNumber - Selected Flight Number
122-
* @param {string} date - Selected departure date
123-
* @param {string} date - Selected arrival date
121+
* @param {string} flightId - Selected Flight Unique Identifier
124122
*/
125123
props: {
126124
flight: { type: FlightClass },
127-
flightNumber: { type: [Number, String], required: true }, // depending on browser flightNumber may come as string
128-
date: { type: String, required: true },
129-
departure: { type: String, required: true },
130-
arrival: { type: String, required: true }
125+
flightId: { type: String, required: true }
131126
},
132127
components: {
133128
FlightCard,
@@ -168,12 +163,9 @@ export default {
168163
if (this.isAuthenticated) {
169164
if (!this.flight) {
170165
this.selectedFlight = await this.$store.dispatch(
171-
"catalog/fetchByFlightNumber",
166+
"catalog/fetchByFlightId",
172167
{
173-
date: this.date,
174-
departure: this.departure,
175-
arrival: this.arrival,
176-
flightNumber: parseInt(this.flightNumber)
168+
flightId: this.flightId
177169
}
178170
);
179171
}

0 commit comments

Comments
 (0)