Created
August 3, 2018 15:59
-
-
Save llekn/e93dccb2b3dba1744949497a1b054798 to your computer and use it in GitHub Desktop.
Binding NgRx store with Ionic navigation!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | |
} | |
}, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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