-
Notifications
You must be signed in to change notification settings - Fork 14
/
createNavigateToInternalLink.js
53 lines (48 loc) · 2.46 KB
/
createNavigateToInternalLink.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// @flow
import type { NavigateToCategoryParamsType } from './createNavigateToCategory'
import createNavigateToCategory from './createNavigateToCategory'
import type { NavigateToEventParamsType } from './createNavigateToEvent'
import createNavigateToEvent from './createNavigateToEvent'
import type { NavigationStackProp } from 'react-navigation-stack'
import type { StoreActionType } from './StoreActionType'
import createNavigateToLanding from './createNavigateToLanding'
import type { Dispatch } from 'redux'
export type NavigateToInternalLinkParamsType = {| url: string, language: string |}
export const createNavigateToInternalLink = ({
navigateToLanding,
navigateToEvent,
navigateToCategory,
navigateToDashboard
}: {
navigateToLanding: () => void,
navigateToEvent: NavigateToEventParamsType => void,
navigateToCategory: NavigateToCategoryParamsType => void,
navigateToDashboard: NavigateToCategoryParamsType => void
}) => ({ url, language }: NavigateToInternalLinkParamsType) => {
const parts = url.split('/').filter(segment => segment)
const pathnameParts = parts.splice(2)
const newCity = pathnameParts[0]
const newLanguage = pathnameParts[1]
const pathname = pathnameParts.reduce((acc, part) => `${acc}/${part}`, '')
if (!newCity) { // '/'
navigateToLanding()
} else if (pathnameParts[2] === 'events') {
if (pathnameParts[3]) { // '/augsburg/de/events/some_event'
navigateToEvent({ cityCode: newCity, language: newLanguage, path: pathname })
} else { // '/augsburg/de/events'
navigateToEvent({ cityCode: newCity, language: newLanguage, path: null })
}
} else if (pathnameParts[2]) { // '/augsburg/de/willkommen'
navigateToCategory({ cityCode: newCity, language: newLanguage, path: pathname })
} else { // '/augsburg/de' or '/augsburg'
const path = newLanguage ? pathname : `${pathname}/${language}`
navigateToDashboard({ cityCode: newCity, language: newLanguage || language, path })
}
}
export default (dispatch: Dispatch<StoreActionType>, navigation: NavigationStackProp<*>) => {
const navigateToCategory = createNavigateToCategory('Categories', dispatch, navigation)
const navigateToDashboard = createNavigateToCategory('Dashboard', dispatch, navigation)
const navigateToEvent = createNavigateToEvent(dispatch, navigation)
const navigateToLanding = createNavigateToLanding(dispatch, navigation)
return createNavigateToInternalLink({ navigateToLanding, navigateToEvent, navigateToCategory, navigateToDashboard })
}