This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Provider.tsx
107 lines (98 loc) · 3.08 KB
/
Provider.tsx
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import React from 'react'
import { Action, Store } from 'redux'
import { Provider as ReduxProvider } from 'react-redux'
import { Route } from './interfaces/router'
import { ClientReducersMapObject, Store as CoreStore } from './interfaces/store'
import { Location } from 'history'
import { AxiosInstance } from 'axios'
import { initHistory } from './reducers/router'
import { initLocale } from './imports/i18n'
import { Resource, i18n } from 'i18next'
import CustomEpics, { AnyEpic } from './interfaces/customEpics'
import { CustomMiddlewares } from './interfaces/customMiddlewares'
import { defaultBuildLocation, defaultParseLocation } from './utils/history'
import { configureStore } from './utils/configureStore'
import { CustomWidgetDescriptor } from './interfaces/widget'
import extendPopupWidgetTypes from './utils/extendPopupWidgetTypes'
export interface ProviderProps<ClientState, ClientActions> {
children: React.ReactNode
customReducers?: ClientReducersMapObject<ClientState, ClientActions>
customActions?: any
customEpics?: CustomEpics | AnyEpic
customMiddlewares?: CustomMiddlewares
axiosInstance?: AxiosInstance
customWidgets?: Record<string, CustomWidgetDescriptor>
parseLocation?: (loc: Location<any>) => Route // TODO: Combine into configuration object
buildLocation?: (route: Route) => string // TODO: Combine into configuration object
useEpics?: boolean
lang?: string
langDictionary?: Resource
}
/**
* @category Utils
*/
export let store: Store<CoreStore> = null
export let axiosInstance: AxiosInstance = null
export let parseLocation: (loc: Location<any>) => Route = defaultParseLocation
export let buildLocation: (route: Route) => string = defaultBuildLocation
export let localeProviderInstance: i18n = null
/**
* TODO
*
* @category Utils
*/
export function getStoreInstance() {
return store
}
/**
* TODO
*
* @param storeInstance
*/
export function setStoreInstance(storeInstance: Store<CoreStore>) {
store = storeInstance
}
/**
* TODO
*/
export function getParseLocationInstance() {
return parseLocation
}
/**
* TODO
*/
export function getBuildLocationInstance() {
return buildLocation
}
/**
* @category Utils
*/
export function getLocaleProviderInstance() {
return localeProviderInstance
}
/**
*
* @param props
* @category Components
*/
const Provider = <ClientState extends Partial<CoreStore>, ClientActions extends Action<any>>(
props: ProviderProps<ClientState, ClientActions>
) => {
store = configureStore(props.customReducers, props.customEpics, props.useEpics, props.customMiddlewares)
initHistory()
localeProviderInstance = initLocale(props.lang || 'en', props.langDictionary)
if (props.axiosInstance) {
axiosInstance = props.axiosInstance
}
if (props.parseLocation) {
parseLocation = props.parseLocation
}
if (props.buildLocation) {
buildLocation = props.buildLocation
}
if (props.customWidgets) {
extendPopupWidgetTypes(props.customWidgets)
}
return <ReduxProvider store={store}>{props.children}</ReduxProvider>
}
export default Provider