Skip to content

Commit

Permalink
Dashboard: Create reducers, actions and dispatchers
Browse files Browse the repository at this point in the history
Operators and PaymentZones get stored in root state.
  • Loading branch information
Kevin Seestrand authored and Kevin Seestrand committed May 24, 2022
1 parent 61c14fc commit 0d4f278
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 3 deletions.
40 changes: 38 additions & 2 deletions dashboard/src/actions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Moment } from 'moment';

import { ParkingList, RegionList, RegionStatsList } from './api/types';
import { ParkingList, RegionList, RegionStatsList, PaymentZoneList, OperatorList } from './api/types';
import { MapViewport } from './components/types';

interface CheckExistingLoginAction {
Expand Down Expand Up @@ -139,6 +139,39 @@ export function receiveValidParkings(
return {type: 'RECEIVE_VALID_PARKINGS', data, time};
}

interface ReceiveCsvAction {
type: 'RECEIVE_CSV';
data: string;
}

export function receiveCSV(
data: string
): ReceiveCsvAction {
return {type: 'RECEIVE_CSV', data}
}

interface ReceiveOperatorsAction {
type: 'RECEIVE_OPERATORS';
data: OperatorList;
}

export function receiveOperators(
data: OperatorList
): ReceiveOperatorsAction {
return {type: 'RECEIVE_OPERATORS', data}
}

interface ReceivePaymentZonesAction {
type: 'RECEIVE_PAYMENT_ZONES';
data: PaymentZoneList;
}

export function receivePaymentZones(
data: PaymentZoneList
): ReceivePaymentZonesAction {
return {type: 'RECEIVE_PAYMENT_ZONES', data}
}

export type Action =
CheckExistingLoginAction |
ResolveExistingLoginCheckAction |
Expand All @@ -155,4 +188,7 @@ export type Action =
SetSelectedRegionAction |
ReceiveRegionStatsAction |
ReceiveRegionInfoAction |
ReceiveValidParkingsAction;
ReceiveValidParkingsAction |
ReceiveOperatorsAction |
ReceivePaymentZonesAction |
ReceiveCsvAction;
14 changes: 14 additions & 0 deletions dashboard/src/converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ export function convertRegionStats(
};
}

export function convertOperator(operator: api.Operator): ui.Operator {
return {
id: operator.id,
name: operator.name!,
}
}

export function convertPaymentZone(paymentZone: api.PaymentZone): ui.PaymentZone {
return {
code: paymentZone.code,
name: paymentZone.name!,
}
}

export function convertParking(parking: api.Parking): ui.Parking {
const props = parking.properties;
return {
Expand Down
45 changes: 45 additions & 0 deletions dashboard/src/dispatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as actions from './actions';
import { Action } from './actions';
import api from './api';
import { MapViewport } from './components/types';
import { ExportFilters } from './api/types';
import { RootState } from './types';

const updateInterval = 5 * 60 * 1000; // 5 minutes in ms
Expand Down Expand Up @@ -91,6 +92,13 @@ export function setDataTime(time?: moment.Moment) {
};
}

export function fetchData() {
return (dispatch: Dispatch<any>) => {
dispatch(fetchOperators());
dispatch(fetchPaymentZones());
};
}

function fetchMissingData(time: moment.Moment) {
return (dispatch: Dispatch<any>, getState: () => RootState) => {
const timestamp = time.valueOf();
Expand Down Expand Up @@ -164,3 +172,40 @@ export function fetchValidParkings(time: moment.Moment) {
});
};
}

export function downloadCSV(filters: ExportFilters) {
return (dispatch: Dispatch<Action>) => {
api.downloadCSV(
filters,
(response) => {
dispatch(actions.receiveCSV(response.data));
},
(error) => {
alert('CSV download failed: ' + error);
});
};
}

export function fetchOperators() {
return (dispatch: Dispatch<Action>) => {
api.fetchOperators(
(response) => {
dispatch(actions.receiveOperators(response.data));
},
(error) => {
alert('Operator fetch failed: ' + error);
});
};
}

export function fetchPaymentZones() {
return (dispatch: Dispatch<Action>) => {
api.fetchPaymentZones(
(response) => {
dispatch(actions.receivePaymentZones(response.data));
},
(error) => {
alert('Payment zone fetch failed: ' + error);
});
};
}
20 changes: 19 additions & 1 deletion dashboard/src/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { centerCoordinates } from './config';
import * as conv from './converters';
import {
AuthenticationState, ParkingRegionMapState, ParkingsMap, RegionsMap,
RegionUsageHistory, ValidParkingsHistory,
RegionUsageHistory, ValidParkingsHistory, PaymentZones, Operators,
ViewState } from './types';

// Auth state reducer ////////////////////////////////////////////////
Expand Down Expand Up @@ -151,6 +151,22 @@ function parkings(state: ParkingsMap = {}, action: Action): ParkingsMap {
return state;
}

function operators(state: any = {}, action: Action): Operators {
if (action.type === 'RECEIVE_OPERATORS') {
const newOperators = mapByIdAndApply(action.data.results, conv.convertOperator as any);
return {...state, ...newOperators } as Operators;
}
return state;
}

function paymentZones(state: any = {}, action: Action): PaymentZones {
if (action.type === 'RECEIVE_PAYMENT_ZONES') {
const newPaymentZones = _.assign({}, ...action.data.results.map((item: any) => ({[item.code]: conv.convertPaymentZone(item)})));
return {...state, ...newPaymentZones} as PaymentZones;
}
return state;
}

function regionUsageHistory(
state: RegionUsageHistory = {},
action: Action
Expand Down Expand Up @@ -202,6 +218,8 @@ const rootReducer = () => combineReducers({
parkings,
regionUsageHistory,
validParkingsHistory,
operators,
paymentZones,
});

export default rootReducer;

0 comments on commit 0d4f278

Please sign in to comment.