Skip to content

Commit

Permalink
fix: navigation methods
Browse files Browse the repository at this point in the history
  • Loading branch information
siepra committed Oct 11, 2023
1 parent ce3349b commit 0a211d9
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 15 deletions.
7 changes: 7 additions & 0 deletions packages/mobile/src/RootNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ export const replaceScreen = <Params extends Record<string, unknown>>(screen: Sc
navigationRef.dispatch(StackActions.replace(screen, params))
}
}

export const pop = (): void => {
if (navigationRef.isReady()) {
// @ts-ignore
navigationRef.dispatch(StackActions.pop())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ export const ChannelListScreen: FC = () => {
useEffect(() => {
if (usernameTaken) {
dispatch(
navigationActions.replaceScreen({
navigationActions.navigation({
screen: ScreenNames.UsernameTakenScreen,
})
)
}

if (duplicateCerts) {
dispatch(
navigationActions.replaceScreen({
navigationActions.navigation({
screen: ScreenNames.PossibleImpersonationAttackScreen,
})
)
}
}, [dispatch, usernameTaken, duplicateCerts])
}, [dispatch, duplicateCerts])

const redirect = useCallback(
(id: string) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ import { navigationActions } from '../../store/navigation/navigation.slice'

const UsernameTakenScreen: React.FC<UsernameTakenScreenProps> = () => {
const dispatch = useDispatch()

const currentIdentity = useSelector(identity.selectors.currentIdentity)

const usernameRegistered = currentIdentity?.userCertificate != null
const error = useSelector(errors.selectors.registrarErrors)

const registeredUsers = useSelector(users.selectors.certificatesMapping)

const error = useSelector(errors.selectors.registrarErrors)

const handleBackButton = useCallback(() => {
dispatch(
navigationActions.replaceScreen({
screen: ScreenNames.ChannelListScreen,
})
navigationActions.pop()
)
}, [dispatch])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ describe('showNotificationSaga', () => {
},
[StoreKeys.Navigation]: {
...new NavigationState(),
currentScreen: ScreenNames.ChannelScreen,
backStack: [ScreenNames.ChannelScreen],
},
}
)
Expand Down Expand Up @@ -174,7 +174,7 @@ describe('showNotificationSaga', () => {
},
[StoreKeys.Navigation]: {
...new NavigationState(),
currentScreen: ScreenNames.ChannelScreen,
backStack: [ScreenNames.ChannelScreen],
},
}
)
Expand Down Expand Up @@ -213,7 +213,7 @@ describe('showNotificationSaga', () => {
},
[StoreKeys.Navigation]: {
...new NavigationState(),
currentScreen: ScreenNames.ChannelScreen,
backStack: [ScreenNames.ChannelScreen],
},
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { navigationActions } from './navigation.slice'
import { redirectionSaga } from './redirection/redirection.saga'
import { navigationSaga } from './navigation/navigation.saga'
import { replaceScreenSaga } from './replaceScreen/replaceScreen.saga'
import { popSaga } from './pop/pop.saga'

export function* navigationMasterSaga(): Generator {
yield all([
takeEvery(navigationActions.redirection.type, redirectionSaga),
takeEvery(navigationActions.navigation.type, navigationSaga),
takeEvery(navigationActions.replaceScreen.type, replaceScreenSaga),
takeEvery(navigationActions.pop.type, popSaga),
])
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CreatedSelectors, StoreState } from '../store.types'

const navigationSlice: CreatedSelectors[StoreKeys.Navigation] = (state: StoreState) => state[StoreKeys.Navigation]

export const currentScreen = createSelector(navigationSlice, reducerState => reducerState.currentScreen)
export const currentScreen = createSelector(navigationSlice, reducerState => reducerState.backStack.slice(-1)[0])

export const contextMenuVisibility = (menu: MenuName) =>
createSelector(navigationSlice, reducerState => {
Expand Down
13 changes: 9 additions & 4 deletions packages/mobile/src/store/navigation/navigation.slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { ScreenNames } from '../../const/ScreenNames.enum'
import { MenuName } from '../../const/MenuNames.enum'

export class NavigationState {
public currentScreen: ScreenNames = ScreenNames.SplashScreen
public backStack: ScreenNames[] = [
ScreenNames.SplashScreen
]
public confirmationBox: ConfirmationBox = {
open: false,
args: {},
Expand Down Expand Up @@ -53,16 +55,19 @@ export const navigationSlice = createSlice({
redirection: state => state,
navigation: (state, action: PayloadAction<NavigationPayload>) => {
const { screen } = action.payload
state.currentScreen = screen
state.backStack.push(screen)
},
// Replace screen overrides last screen in backstack
replaceScreen: (state, action: PayloadAction<NavigationPayload>) => {
const { screen } = action.payload
state.currentScreen = screen
state.backStack.pop()
state.backStack.push(screen)
},
pop: (state) => {
state.backStack.pop()
},
setPendingNavigation: (state, action: PayloadAction<PendingNavigationPayload>) => {
const { screen } = action.payload
state.currentScreen = screen
state.pendingNavigation = screen
},
clearPendingNavigation: state => {
Expand Down
10 changes: 10 additions & 0 deletions packages/mobile/src/store/navigation/pop/pop.saga.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { PayloadAction } from '@reduxjs/toolkit'
import { call } from 'typed-redux-saga'
import { navigationActions } from '../navigation.slice'
import { pop } from '../../../RootNavigation'

export function* popSaga(
_action: PayloadAction<ReturnType<typeof navigationActions.pop>['payload']>
): Generator {
yield* call(pop)
}

0 comments on commit 0a211d9

Please sign in to comment.