Skip to content

Commit

Permalink
store statistics in redux cache (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
doerfli committed Oct 1, 2024
1 parent dd0c32e commit 219d45c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/app/api/quote/[carrier]/[flightNumber]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function GET(request: NextRequest, { params } : { params: { carrier
const rating = jsonResponse.ratings[0];

LOGGER.debug(`[${reqId}] Ratings:ontime ${rating.ontime}, observations: ${rating.observations}, late15 ${rating.late15}, late30 ${rating.late30}, `
+ `late45 ${rating.late45}, cancelled ${rating.cancelled}, diverted ${rating.diverted} ontimepercent ${rating.ontimepercent}`);
+ `late45 ${rating.late45}, cancelled ${rating.cancelled}, diverted ${rating.diverted} ontimepercent ${rating.ontimePercent}`);

const signer = await getBackendVoidSigner();

Expand All @@ -54,5 +54,6 @@ export async function GET(request: NextRequest, { params } : { params: { carrier
premium,
payouts,
ontimepercent: rating.ontimePercent,
statistics: [rating.observations, rating.late15, rating.late30, rating.late45, rating.cancelled, rating.diverted],
}, { status: 200 });
}
2 changes: 1 addition & 1 deletion src/components/Application/application.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function Application() {
const isArrivalAirportWhiteListed = useSelector((state: RootState) => state.flightData.arrivalAirport?.whitelisted || true);
const loadingFlightData = useSelector((state: RootState) => state.flightData.loading);
const loadingQuote = useSelector((state: RootState) => state.flightData.loadingQuote);
const flightFound = useSelector((state: RootState) => state.flightData.arrivalAirport !== null);
const flightFound = useSelector((state: RootState) => !state.flightData.loading && !state.flightData.loadingQuote && state.flightData.arrivalAirport !== null && state.flightData.premium !== null);
const walletIsConnected = useSelector((state: RootState) => state.wallet.address !== null);

let error = <></>;
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/api/use_flightstats_api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function useFlightstatsApi() {
};
}

async function fetchQuote(carrier: string, flightNumber: string): Promise<{premium: number, ontimepercent: number, payouts: PayoutAmounts}> {
async function fetchQuote(carrier: string, flightNumber: string): Promise<{premium: number, ontimepercent: number, payouts: PayoutAmounts, statistics: bigint[]}> {
console.log("fetching quote for", carrier, flightNumber);
const uri = `/api/quote/${encodeURIComponent(carrier)}/${encodeURIComponent(flightNumber)}`;
const res = await fetch(uri);
Expand Down
42 changes: 26 additions & 16 deletions src/redux/slices/flightData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@ import { logErrorOnBackend } from '../../utils/logger';
import { fetchFlightData, fetchQuote } from '../thunks/flightData';

export interface FlightDataState {
carrier: string | null,
flightNumber: string | null,
departureDate: string | null,
departureAirport: Airport | null,
arrivalAirport: Airport | null,
departureTime: string | null,
arrivalTime: string | null,
loading: boolean,
loadingQuote: boolean,
errorReason: Reason | null,
errorData: unknown | null,
errorReasonQuote: Reason | null,
errorDataQuote: unknown | null,
premium: number | null,
ontime: number | null,
payoutAmounts: PayoutAmounts | null,
carrier: string | null;
flightNumber: string | null;
departureDate: string | null;
departureAirport: Airport | null;
arrivalAirport: Airport | null;
departureTime: string | null;
arrivalTime: string | null;
loading: boolean;
loadingQuote: boolean;
errorReason: Reason | null;
errorData: unknown | null;
errorReasonQuote: Reason | null;
errorDataQuote: unknown | null;
premium: number | null;
ontime: number | null;
/** this is a list of 6 values: [observations, late15, late30, late45, cancelled, diverted] */
statistics: bigint[] | null;
payoutAmounts: PayoutAmounts | null;
}

export interface Airport {
Expand Down Expand Up @@ -60,6 +62,7 @@ const initialState: FlightDataState = {
errorDataQuote: null,
premium: null,
ontime: null,
statistics: null,
payoutAmounts: null,
};

Expand Down Expand Up @@ -117,6 +120,13 @@ export const flightDataSlice = createSlice({
state.premium = response.premium;
state.ontime = response.ontimepercent;
state.payoutAmounts = response.payouts;
state.statistics = response.statistics;
});
builder.addCase(fetchQuote.rejected, (state, action) => {
state.loadingQuote = false;
state.errorReasonQuote = Reason.COMM_ERROR;
state.errorDataQuote = action.error;
logErrorOnBackend(`${action.error.message}`, JSON.stringify(action.error), 'flightData/fetchQuote');
});
},
});
Expand Down

0 comments on commit 219d45c

Please sign in to comment.