Instantly share code, notes, and snippets.

Embed
What would you like to do?
Binding NgRx store with Ionic navigation!
const actionMap: any = {
[gateAccess.REWIND_REGISTRATION]: (state, action) => {
switch (state.step) {
case 'vehicle':
return resetState(state);
case 'driver':
return updateObject(state, { lastAccess: null, step: 'vehicle' });
case 'confirmation':
return updateObject(state, {
lastAccess: null,
step: (state.mode === 'exit' || hasTimetable(state)) ? 'driver' : 'destination',
});
case 'destination':
return updateObject(state, { lastAccess: null, step: 'driver' });
default:
return updateObject(state, {});
}
},
[gateAccess.FINISH_DRIVER_REGISTRATION]: (state, action) => {
if (state.mode === 'exit') {
return updateObject(state, { lastAccess: null, step: 'confirmation' });
} else {
const nextStep: string = hasTimetable(state) ? 'confirmation' : 'destination';
return updateObject(state, { lastAccess: null, step: nextStep });
}
},
}
public ionViewDidLoad() {
const STEP_MAP: {[step: string]: Page} = {
vehicle: GateVehicleRegistrationPage,
driver: DriverRegistrationPage,
confirmation: GateAccessConfirmationPage,
destination: GateDestinationSelectionPage,
};
const managedPages: {[step: string]: Page} = {...STEP_MAP, home: VehicleGateHomePage };
this.stepSub = this.store.select(getGateAccessStep).subscribe((step: string) => {
let offset: number = this.navCtrl.indexOf(this.viewCtrl);
let length: number = this.navCtrl.length();
let toRemove: number = length - (offset + 1);
if (step !== 'waiting') {
this.navCtrl.push(STEP_MAP[step]);
toRemove -= 1; // dont remove newly pushed page
}
const isAllowedPage: boolean = includes(managedPages, this.navCtrl.getActive().component);
if (toRemove > 0 && isAllowedPage) {
this.navCtrl.remove(offset + 1, toRemove);
}
});
this.platform.registerBackButtonAction(
() => this.store.dispatch({ type: fromGateAccess.REWIND_REGISTRATION }),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment