@olea-bps/base 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (268) hide show
  1. package/components/AppBar/index.js +84 -0
  2. package/components/AppBar/styles.js +27 -0
  3. package/components/BookDetail/index.js +268 -0
  4. package/components/BookDetail/styles.js +124 -0
  5. package/components/ComponentJobsFilter/index.js +188 -0
  6. package/components/ComponentJobsFilter/styles.js +12 -0
  7. package/components/ConnectivityWarning/index.js +65 -0
  8. package/components/ConnectivityWarning/styles.js +19 -0
  9. package/components/ContactDetail/index.js +232 -0
  10. package/components/ContactDetail/styles.js +32 -0
  11. package/components/CourseDetail/index.js +357 -0
  12. package/components/CourseDetail/styles.js +59 -0
  13. package/components/CourseDetailDialog/index.js +169 -0
  14. package/components/CourseDetailDialog/styles.js +116 -0
  15. package/components/CourseInfo/index.js +219 -0
  16. package/components/CourseInfo/styles.js +40 -0
  17. package/components/DevelopmentDialog/index.js +214 -0
  18. package/components/DevelopmentDialog/styles.js +10 -0
  19. package/components/EventCodeInput/index.js +149 -0
  20. package/components/EventCodeInput/styles.js +108 -0
  21. package/components/FlexMenuEntry/index.js +84 -0
  22. package/components/FlexMenuEntry/styles.js +27 -0
  23. package/components/MainMenuEntry/index.js +88 -0
  24. package/components/MainMenuEntry/styles.js +28 -0
  25. package/components/MealItem/index.js +87 -0
  26. package/components/MealItem/styles.js +84 -0
  27. package/components/MensaMenu/index.js +305 -0
  28. package/components/MensaMenu/styles.js +94 -0
  29. package/components/MensaSlider/index.js +188 -0
  30. package/components/MensaSlider/styles.js +53 -0
  31. package/components/Modal/index.js +106 -0
  32. package/components/Modal/styles.js +8 -0
  33. package/components/NewsDetail/index.js +377 -0
  34. package/components/NewsDetail/styles.js +77 -0
  35. package/components/NewsList/index.js +120 -0
  36. package/components/NewsList/styles.js +19 -0
  37. package/components/NewsListItem/index.js +89 -0
  38. package/components/NewsListItem/styles.js +32 -0
  39. package/components/OtherCourses/index.js +152 -0
  40. package/components/OtherCourses/styles.js +10 -0
  41. package/components/PtsDeparture/index.js +140 -0
  42. package/components/PtsDeparture/styles.js +7 -0
  43. package/components/PtsStation/index.js +186 -0
  44. package/components/PtsStation/styles.js +47 -0
  45. package/components/QuickLinks/index.js +127 -0
  46. package/components/QuickLinks/styles.js +45 -0
  47. package/components/RoomDetail/index.js +281 -0
  48. package/components/RoomDetail/styles.js +56 -0
  49. package/components/ScaledImage/index.js +92 -0
  50. package/components/SearchResults/index.js +362 -0
  51. package/components/SearchResults/styles.js +59 -0
  52. package/components/SettingSection/index.js +54 -0
  53. package/components/SettingSection/styles.js +15 -0
  54. package/components/SettingsDialog/index.js +52 -0
  55. package/components/SettingsDialog/styles.js +12 -0
  56. package/components/SettingsDialogRadio/index.js +66 -0
  57. package/components/SettingsDialogRadio/styles.js +7 -0
  58. package/components/SettingsDialogSelect/index.js +73 -0
  59. package/components/SettingsDialogSelect/styles.js +7 -0
  60. package/components/TimetableCodeInput/index.js +205 -0
  61. package/components/TimetableCodeInput/styles.js +28 -0
  62. package/components/TimetableDay/index.js +252 -0
  63. package/components/TimetableDay/styles.js +103 -0
  64. package/components/TimetableEvent/index.js +163 -0
  65. package/components/TimetableEvent/styles.js +108 -0
  66. package/components/TimetableList/index.js +180 -0
  67. package/components/TimetableList/styles.js +106 -0
  68. package/components/TimetableMonth/index.js +156 -0
  69. package/components/TimetableMonth/styles.js +29 -0
  70. package/components/TimetableWeek/index.js +230 -0
  71. package/components/TimetableWeek/styles.js +58 -0
  72. package/components/TopNews/index.js +282 -0
  73. package/components/TopNews/styles.js +125 -0
  74. package/components/TopNewsHtwk/index.js +279 -0
  75. package/components/TopNewsHtwk/styles.js +142 -0
  76. package/components/WebView/index.js +108 -0
  77. package/components/WebView/styles.js +11 -0
  78. package/context/context-callmanager/TucCallManager.js +74 -0
  79. package/context/context-callmanager/index.js +128 -0
  80. package/context/context-canteen/AsistServerApiProvider.js +126 -0
  81. package/context/context-canteen/CollektorVersion2ApiProvider.js +100 -0
  82. package/context/context-canteen/index.js +692 -0
  83. package/context/context-connectivity/index.js +85 -0
  84. package/context/context-event/AsistServerClient.js +82 -0
  85. package/context/context-event/index.js +205 -0
  86. package/context/context-flex-menu/AsistServerApiProvider.js +0 -0
  87. package/context/context-flex-menu/CollectorVersion2ApiProvider.js +32 -0
  88. package/context/context-flex-menu/index.js +160 -0
  89. package/context/context-info-dialog/CollektorVersion2ApiProvider.js +53 -0
  90. package/context/context-info-dialog/ProviderErrors.js +22 -0
  91. package/context/context-info-dialog/index.js +190 -0
  92. package/context/context-mails/MailService.js +147 -0
  93. package/context/context-mails/TucUnseenApiProvider.js +65 -0
  94. package/context/context-mails/index.js +131 -0
  95. package/context/context-news/CollektorVersion2ApiProvider.js +68 -0
  96. package/context/context-news/index.js +155 -0
  97. package/context/context-public-transport-ticket/CollektorVersion2ApiProvider.js +45 -0
  98. package/context/context-public-transport-ticket/ProviderErrors.js +11 -0
  99. package/context/context-public-transport-ticket/index.js +175 -0
  100. package/context/context-timetable/CollectorVersion2ApiProvider.js +102 -0
  101. package/context/context-timetable/ProviderErrors.js +22 -0
  102. package/context/context-timetable/TimetableService.js +357 -0
  103. package/context/context-timetable/index.js +215 -0
  104. package/context/context-user/index.js +586 -0
  105. package/index.js +178 -0
  106. package/libraries/base-api-provider/hooks.js +24 -0
  107. package/libraries/base-api-provider/http-api-provider.js +168 -0
  108. package/libraries/base-api-provider/index.js +20 -0
  109. package/libraries/core/api/canteens.js +88 -0
  110. package/libraries/core/api/corona.js +45 -0
  111. package/libraries/core/api/courses.js +54 -0
  112. package/libraries/core/api/device.js +98 -0
  113. package/libraries/core/api/feed.js +144 -0
  114. package/libraries/core/api/feedback.js +123 -0
  115. package/libraries/core/api/jobs.js +48 -0
  116. package/libraries/core/api/library.js +67 -0
  117. package/libraries/core/api/lostfound.js +77 -0
  118. package/libraries/core/api/occupancy.js +44 -0
  119. package/libraries/core/api/officehours.js +25 -0
  120. package/libraries/core/api/poi.js +25 -0
  121. package/libraries/core/api/pts.js +68 -0
  122. package/libraries/core/api/rooms.js +31 -0
  123. package/libraries/core/api/survey.js +121 -0
  124. package/libraries/core/api/telephone.js +32 -0
  125. package/libraries/core/api/tucrooms.js +31 -0
  126. package/libraries/core/constants/actions.js +75 -0
  127. package/libraries/core/constants/api.js +146 -0
  128. package/libraries/core/constants/enums.js +5 -0
  129. package/libraries/core/helper/date.js +9 -0
  130. package/libraries/core/helper/format.helper.js +2 -0
  131. package/libraries/core/index.js +60 -0
  132. package/libraries/core/redux/actions/notifications.js +6 -0
  133. package/libraries/core/redux/actions/setting.js +75 -0
  134. package/libraries/core/redux/actions/state.js +74 -0
  135. package/libraries/core/redux/asyncDispatch.js +27 -0
  136. package/libraries/core/redux/reducers/api.js +190 -0
  137. package/libraries/core/redux/reducers/index.js +32 -0
  138. package/libraries/core/redux/reducers/notifications.js +16 -0
  139. package/libraries/core/redux/reducers/setting.js +99 -0
  140. package/libraries/core/redux/reducers/state.js +122 -0
  141. package/libraries/core/redux/selectors.js +123 -0
  142. package/libraries/core/redux/store.js +40 -0
  143. package/libraries/core/services/data.js +96 -0
  144. package/libraries/core/services/root-navigation.js +36 -0
  145. package/libraries/core/services/search.js +175 -0
  146. package/libraries/core-plugins/enums/replaceTypes.js +53 -0
  147. package/libraries/core-plugins/index.js +21 -0
  148. package/libraries/core-plugins/redux/actions/plugin.js +13 -0
  149. package/libraries/core-plugins/redux/reducers/plugin.js +210 -0
  150. package/libraries/icons-openasist/index.js +143 -0
  151. package/libraries/notifications/index.js +268 -0
  152. package/libraries/notifications/services/subscriber.service.js +123 -0
  153. package/libraries/react-native-webview-autoheight/README.md +47 -0
  154. package/libraries/react-native-webview-autoheight/index.js +140 -0
  155. package/libraries/react-native-webview-autoheight/package.json +37 -0
  156. package/libraries/react-native-webview-autoheight/utils.js +268 -0
  157. package/libraries/redux-offline/.eslintignore +1 -0
  158. package/libraries/redux-offline/.github/ISSUE_TEMPLATE/redux-offline-core-issues-template.md +25 -0
  159. package/libraries/redux-offline/.prettierrc +6 -0
  160. package/libraries/redux-offline/LICENSE +21 -0
  161. package/libraries/redux-offline/README.md +133 -0
  162. package/libraries/redux-offline/flow-typed/npm/redux-logger_vx.x.x.js +109 -0
  163. package/libraries/redux-offline/flow-typed/npm/redux_v4.x.x.js +100 -0
  164. package/libraries/redux-offline/lib/actions.js +46 -0
  165. package/libraries/redux-offline/lib/config.js +15 -0
  166. package/libraries/redux-offline/lib/constants.js +17 -0
  167. package/libraries/redux-offline/lib/defaults/defaultCommit.js +11 -0
  168. package/libraries/redux-offline/lib/defaults/defaultRollback.js +11 -0
  169. package/libraries/redux-offline/lib/defaults/detectNetwork.js +33 -0
  170. package/libraries/redux-offline/lib/defaults/detectNetwork.native.js +211 -0
  171. package/libraries/redux-offline/lib/defaults/discard.js +18 -0
  172. package/libraries/redux-offline/lib/defaults/effect.js +87 -0
  173. package/libraries/redux-offline/lib/defaults/index.js +32 -0
  174. package/libraries/redux-offline/lib/defaults/offlineStateLens.js +24 -0
  175. package/libraries/redux-offline/lib/defaults/persist.js +9 -0
  176. package/libraries/redux-offline/lib/defaults/persist.native.js +17 -0
  177. package/libraries/redux-offline/lib/defaults/persistAutoRehydrate.js +8 -0
  178. package/libraries/redux-offline/lib/defaults/queue.js +27 -0
  179. package/libraries/redux-offline/lib/defaults/retry.js +29 -0
  180. package/libraries/redux-offline/lib/index.js +115 -0
  181. package/libraries/redux-offline/lib/middleware.js +56 -0
  182. package/libraries/redux-offline/lib/offlineActionTracker.js +43 -0
  183. package/libraries/redux-offline/lib/send.js +112 -0
  184. package/libraries/redux-offline/lib/types.js +3 -0
  185. package/libraries/redux-offline/lib/updater.js +111 -0
  186. package/libraries/redux-offline/package.json +69 -0
  187. package/libraries/redux-offline/src/__tests__/defaults/discard.js +22 -0
  188. package/libraries/redux-offline/src/__tests__/defaults/effect.js +118 -0
  189. package/libraries/redux-offline/src/__tests__/index.js +116 -0
  190. package/libraries/redux-offline/src/__tests__/middleware.js +185 -0
  191. package/libraries/redux-offline/src/__tests__/offlineActionTracker.js +54 -0
  192. package/libraries/redux-offline/src/__tests__/send.js +208 -0
  193. package/libraries/redux-offline/src/actions.js +36 -0
  194. package/libraries/redux-offline/src/config.js +9 -0
  195. package/libraries/redux-offline/src/constants.js +18 -0
  196. package/libraries/redux-offline/src/defaults/defaultCommit.js +7 -0
  197. package/libraries/redux-offline/src/defaults/defaultRollback.js +7 -0
  198. package/libraries/redux-offline/src/defaults/detectNetwork.js +16 -0
  199. package/libraries/redux-offline/src/defaults/detectNetwork.native.js +148 -0
  200. package/libraries/redux-offline/src/defaults/discard.js +19 -0
  201. package/libraries/redux-offline/src/defaults/effect.js +86 -0
  202. package/libraries/redux-offline/src/defaults/index.js +25 -0
  203. package/libraries/redux-offline/src/defaults/offlineStateLens.js +11 -0
  204. package/libraries/redux-offline/src/defaults/persist.js +4 -0
  205. package/libraries/redux-offline/src/defaults/persist.native.js +9 -0
  206. package/libraries/redux-offline/src/defaults/persistAutoRehydrate.js +4 -0
  207. package/libraries/redux-offline/src/defaults/queue.js +20 -0
  208. package/libraries/redux-offline/src/defaults/retry.js +18 -0
  209. package/libraries/redux-offline/src/index.js +138 -0
  210. package/libraries/redux-offline/src/middleware.js +52 -0
  211. package/libraries/redux-offline/src/offlineActionTracker.js +40 -0
  212. package/libraries/redux-offline/src/send.js +111 -0
  213. package/libraries/redux-offline/src/types.js +127 -0
  214. package/libraries/redux-offline/src/updater.js +141 -0
  215. package/libraries/redux-offline/typings.d.ts +150 -0
  216. package/libraries/redux-offline/yarn.lock +6460 -0
  217. package/libraries/stored-state/index.js +155 -0
  218. package/package.json +68 -0
  219. package/views/Callmanager/index.js +148 -0
  220. package/views/Callmanager/styles.js +62 -0
  221. package/views/Canteens/index.js +383 -0
  222. package/views/Canteens/styles.js +89 -0
  223. package/views/Dashboard/index.js +176 -0
  224. package/views/Dashboard/styles.js +16 -0
  225. package/views/DashboardHtwk/index.js +197 -0
  226. package/views/DashboardHtwk/styles.js +24 -0
  227. package/views/Event/index.js +373 -0
  228. package/views/Event/styles.js +63 -0
  229. package/views/FeedList/index.js +165 -0
  230. package/views/FeedList/styles.js +15 -0
  231. package/views/FeedNews/index.js +243 -0
  232. package/views/FeedNews/styles.js +32 -0
  233. package/views/Flexmenu/index.js +185 -0
  234. package/views/Flexmenu/styles.js +62 -0
  235. package/views/FlexmenuWebview/index.js +167 -0
  236. package/views/FlexmenuWebview/styles.js +16 -0
  237. package/views/Howhy/index.js +151 -0
  238. package/views/Howhy/styles.js +11 -0
  239. package/views/Jobs/index.js +217 -0
  240. package/views/Jobs/styles.js +41 -0
  241. package/views/MainMenu/index.js +283 -0
  242. package/views/MainMenu/styles.js +36 -0
  243. package/views/NewsTabbar/index.js +164 -0
  244. package/views/NewsTabbar/styles.js +27 -0
  245. package/views/Opal/index.js +136 -0
  246. package/views/Opal/styles.js +11 -0
  247. package/views/PublicTransportTicket/index.js +173 -0
  248. package/views/PublicTransportTicket/styles.js +17 -0
  249. package/views/Search/index.js +186 -0
  250. package/views/Search/styles.js +67 -0
  251. package/views/SettingsAccessibility/index.js +148 -0
  252. package/views/SettingsAccessibility/styles.js +13 -0
  253. package/views/SettingsAppInfo/index.js +86 -0
  254. package/views/SettingsAppInfo/styles.js +18 -0
  255. package/views/SettingsCanteens/index.js +249 -0
  256. package/views/SettingsCanteens/styles.js +33 -0
  257. package/views/SettingsGeneral/index.js +173 -0
  258. package/views/SettingsGeneral/styles.js +28 -0
  259. package/views/SettingsNotifications/index.js +204 -0
  260. package/views/SettingsNotifications/styles.js +38 -0
  261. package/views/SettingsUser/index.js +170 -0
  262. package/views/SettingsUser/styles.js +17 -0
  263. package/views/TimetableCalendar/index.js +387 -0
  264. package/views/TimetableCalendar/styles.js +121 -0
  265. package/views/TimetableList/index.js +402 -0
  266. package/views/TimetableList/styles.js +55 -0
  267. package/views/Webviews/index.js +158 -0
  268. package/views/Webviews/styles.js +11 -0
@@ -0,0 +1,111 @@
1
+ // @flow
2
+ import { busy, scheduleRetry } from './actions';
3
+ import { JS_ERROR } from './constants';
4
+ import type {
5
+ Config,
6
+ OfflineAction,
7
+ ResultAction,
8
+ DefaultAction
9
+ } from './types';
10
+
11
+ type CompleteSuccessResult = {
12
+ success: true,
13
+ payload: {}
14
+ };
15
+
16
+ type CompleteFailResult = {
17
+ success: false,
18
+ payload?: Error
19
+ };
20
+
21
+ const complete = (
22
+ action: DefaultAction | ResultAction,
23
+ result: CompleteSuccessResult | CompleteFailResult,
24
+ offlineAction: OfflineAction,
25
+ config: Config
26
+ ): ResultAction => {
27
+ const { resolveAction, rejectAction } = config.offlineActionTracker;
28
+ if (result.success) {
29
+ resolveAction(offlineAction.meta.transaction, result.payload);
30
+ } else if (result.payload) {
31
+ rejectAction(offlineAction.meta.transaction, result.payload);
32
+ }
33
+ return (({
34
+ ...action,
35
+ payload: result.payload,
36
+ meta: { ...action.meta, success: result.success, completed: true }
37
+ }: any): ResultAction);
38
+ };
39
+
40
+ const handleJsError = (error: Error): ResultAction =>
41
+ (({
42
+ type: JS_ERROR,
43
+ meta: { error, success: false, completed: true }
44
+ }: any): ResultAction);
45
+
46
+ const send = (
47
+ action: OfflineAction,
48
+ dispatch: any => any,
49
+ config: Config,
50
+ retries: number = 0
51
+ ) => {
52
+ const metadata = action.meta.offline;
53
+ dispatch(busy(true));
54
+ return config
55
+ .effect(metadata.effect, action)
56
+ .then(result => {
57
+ const commitAction =
58
+ metadata.commit ||
59
+ ({
60
+ ...config.defaultCommit,
61
+ meta: { ...config.defaultCommit.meta, offlineAction: action }
62
+ }: DefaultAction);
63
+ try {
64
+ return dispatch(
65
+ complete(
66
+ commitAction,
67
+ { success: true, payload: result },
68
+ action,
69
+ config
70
+ )
71
+ );
72
+ } catch (error) {
73
+ return dispatch(handleJsError(error));
74
+ }
75
+ })
76
+ .catch(async error => {
77
+ const rollbackAction =
78
+ metadata.rollback ||
79
+ ({
80
+ ...config.defaultRollback,
81
+ meta: { ...config.defaultRollback.meta, offlineAction: action }
82
+ }: DefaultAction);
83
+
84
+ // discard
85
+ let mustDiscard = true;
86
+ try {
87
+ mustDiscard = await config.discard(error, action, retries);
88
+ } catch (e) {
89
+ console.warn(e);
90
+ }
91
+
92
+ if (!mustDiscard) {
93
+ const delay = config.retry(action, retries);
94
+ if (delay != null) {
95
+ return dispatch(scheduleRetry(delay));
96
+ }
97
+ }
98
+
99
+ return dispatch(
100
+ complete(
101
+ rollbackAction,
102
+ { success: false, payload: error },
103
+ action,
104
+ config
105
+ )
106
+ );
107
+ })
108
+ .finally(() => dispatch(busy(false)));
109
+ };
110
+
111
+ export default send;
@@ -0,0 +1,127 @@
1
+ // @flow
2
+ import {
3
+ OFFLINE_STATUS_CHANGED,
4
+ OFFLINE_SCHEDULE_RETRY,
5
+ PERSIST_REHYDRATE
6
+ } from './constants';
7
+
8
+ export type ResultAction = {
9
+ type: string,
10
+ payload?: {},
11
+ meta: {
12
+ offline?: {},
13
+ success: boolean,
14
+ completed: boolean,
15
+ error?: Error
16
+ }
17
+ };
18
+
19
+ export type OfflineMetadata = {
20
+ effect: {},
21
+ commit?: ResultAction,
22
+ rollback?: ResultAction
23
+ };
24
+
25
+ // User passed action
26
+ // it is impossible to use a type literal for it,
27
+ // since it can be any user passed string
28
+ export type OfflineAction = {
29
+ type: string,
30
+ payload?: any,
31
+ meta: {
32
+ transaction: number,
33
+ offline: OfflineMetadata
34
+ }
35
+ };
36
+
37
+ export type NetInfo = {
38
+ isConnectionExpensive: ?boolean,
39
+ reach: string
40
+ };
41
+
42
+ export type OfflineStatusChangeAction = {
43
+ type: typeof OFFLINE_STATUS_CHANGED,
44
+ payload: {
45
+ online: boolean,
46
+ netInfo?: NetInfo
47
+ },
48
+ meta: typeof undefined
49
+ };
50
+
51
+ export type OfflineScheduleRetryAction = {
52
+ type: typeof OFFLINE_SCHEDULE_RETRY
53
+ };
54
+
55
+ export type Outbox = Array<OfflineAction>;
56
+
57
+ export type OfflineState = {
58
+ busy: boolean,
59
+ lastTransaction: number,
60
+ online: boolean,
61
+ outbox: Outbox,
62
+ netInfo?: NetInfo,
63
+ retryCount: number,
64
+ retryScheduled: boolean
65
+ };
66
+
67
+ export type PersistRehydrateAction = {
68
+ type: typeof PERSIST_REHYDRATE,
69
+ payload: {
70
+ offline: OfflineState
71
+ },
72
+ meta: typeof undefined
73
+ };
74
+
75
+ export type DefaultAction = {
76
+ type: string,
77
+ meta: {
78
+ offline: typeof undefined,
79
+ offlineAction: { type: string }
80
+ }
81
+ };
82
+
83
+ export type AppState = {
84
+ offline: OfflineState
85
+ };
86
+
87
+ type NetworkCallback = (result: boolean) => void;
88
+
89
+ export type Config = {
90
+ detectNetwork: (callback: NetworkCallback) => void,
91
+ persist: (store: any, options: {}, callback: () => void) => any,
92
+ effect: (effect: any, action: OfflineAction) => Promise<*>,
93
+ retry: (action: OfflineAction, retries: number) => ?number,
94
+ discard: (error: any, action: OfflineAction, retries: number) => boolean,
95
+ persistOptions: {},
96
+ persistCallback: (callback: any) => any,
97
+ defaultCommit: DefaultAction,
98
+ defaultRollback: DefaultAction,
99
+ persistAutoRehydrate: (config: ?{}) => (next: any) => any,
100
+ offlineStateLens: (
101
+ state: any
102
+ ) => { get: OfflineState, set: (offlineState: ?OfflineState) => any },
103
+ queue: {
104
+ enqueue: (
105
+ array: Array<OfflineAction>,
106
+ item: OfflineAction,
107
+ context: { offline: OfflineState }
108
+ ) => Array<OfflineAction>,
109
+ dequeue: (
110
+ array: Array<OfflineAction>,
111
+ item: ResultAction,
112
+ context: { offline: OfflineState }
113
+ ) => Array<OfflineAction>,
114
+ peek: (
115
+ array: Array<OfflineAction>,
116
+ item: any,
117
+ context: { offline: OfflineState }
118
+ ) => OfflineAction
119
+ },
120
+ offlineActionTracker: {
121
+ registerAction: number => Promise<*> | (() => void),
122
+ resolveAction: (number, any) => void | (() => void),
123
+ rejectAction: (number, Error) => void | (() => void)
124
+ },
125
+ returnPromises?: boolean,
126
+ rehydrate?: boolean
127
+ };
@@ -0,0 +1,141 @@
1
+ // @flow
2
+ /* global $Shape */
3
+
4
+ import type {
5
+ OfflineStatusChangeAction,
6
+ OfflineScheduleRetryAction,
7
+ PersistRehydrateAction,
8
+ OfflineAction,
9
+ OfflineState,
10
+ ResultAction,
11
+ Config
12
+ } from './types';
13
+ import {
14
+ OFFLINE_STATUS_CHANGED,
15
+ OFFLINE_SCHEDULE_RETRY,
16
+ OFFLINE_COMPLETE_RETRY,
17
+ OFFLINE_BUSY,
18
+ RESET_STATE,
19
+ PERSIST_REHYDRATE
20
+ } from './constants';
21
+
22
+ export const initialState: OfflineState = {
23
+ busy: false,
24
+ lastTransaction: 0,
25
+ online: false,
26
+ outbox: [],
27
+ retryCount: 0,
28
+ retryScheduled: false,
29
+ netInfo: {
30
+ isConnectionExpensive: null,
31
+ reach: 'NONE'
32
+ }
33
+ };
34
+
35
+ type Dequeue = $PropertyType<$PropertyType<Config, 'queue'>, 'dequeue'>;
36
+ type Enqueue = $PropertyType<$PropertyType<Config, 'queue'>, 'enqueue'>;
37
+
38
+ export const buildOfflineUpdater = (dequeue: Dequeue, enqueue: Enqueue) =>
39
+ function offlineUpdater(
40
+ state: OfflineState = initialState,
41
+ action:
42
+ | OfflineStatusChangeAction
43
+ | OfflineScheduleRetryAction
44
+ | ResultAction
45
+ | PersistRehydrateAction
46
+ ): OfflineState {
47
+ // Update online/offline status
48
+ if (action.type === OFFLINE_STATUS_CHANGED && !action.meta) {
49
+ return {
50
+ ...state,
51
+ online: action.payload.online,
52
+ netInfo: action.payload.netInfo
53
+ };
54
+ }
55
+
56
+ if (action.type === PERSIST_REHYDRATE && action.payload) {
57
+ return {
58
+ ...state,
59
+ ...(action.payload.offline || {}),
60
+ online: state.online,
61
+ netInfo: state.netInfo,
62
+ retryScheduled: initialState.retryScheduled,
63
+ retryCount: initialState.retryCount,
64
+ busy: initialState.busy
65
+ };
66
+ }
67
+
68
+ if (action.type === OFFLINE_SCHEDULE_RETRY) {
69
+ return {
70
+ ...state,
71
+ retryScheduled: true,
72
+ retryCount: state.retryCount + 1
73
+ };
74
+ }
75
+
76
+ if (action.type === OFFLINE_COMPLETE_RETRY) {
77
+ return { ...state, retryScheduled: false };
78
+ }
79
+
80
+ if (
81
+ action.type === OFFLINE_BUSY &&
82
+ !action.meta &&
83
+ action.payload &&
84
+ typeof action.payload.busy === 'boolean'
85
+ ) {
86
+ return { ...state, busy: action.payload.busy };
87
+ }
88
+
89
+ // Add offline actions to queue
90
+ if (action.meta && action.meta.offline) {
91
+ const transaction = state.lastTransaction + 1;
92
+ const stamped = (({
93
+ ...action,
94
+ meta: { ...action.meta, transaction }
95
+ }: any): OfflineAction);
96
+ const offline = state;
97
+ return {
98
+ ...state,
99
+ lastTransaction: transaction,
100
+ outbox: enqueue(offline.outbox, stamped, { offline })
101
+ };
102
+ }
103
+
104
+ // Remove completed actions from queue (success or fail)
105
+ if (action.meta && action.meta.completed === true) {
106
+ const offline = state;
107
+ return {
108
+ ...state,
109
+ outbox: dequeue(offline.outbox, action, { offline }),
110
+ retryCount: 0
111
+ };
112
+ }
113
+
114
+ if (action.type === RESET_STATE) {
115
+ return {
116
+ ...initialState,
117
+ online: state.online,
118
+ netInfo: state.netInfo
119
+ };
120
+ }
121
+
122
+ return state;
123
+ };
124
+
125
+ export const enhanceReducer = (reducer: any, config: $Shape<Config>) => {
126
+ const { dequeue, enqueue } = config.queue;
127
+ const offlineUpdater = buildOfflineUpdater(dequeue, enqueue);
128
+
129
+ return (state: any, action: any): any => {
130
+ let offlineState;
131
+ let restState;
132
+ if (typeof state !== 'undefined') {
133
+ offlineState = config.offlineStateLens(state).get;
134
+ restState = config.offlineStateLens(state).set();
135
+ }
136
+
137
+ return config
138
+ .offlineStateLens(reducer(restState, action))
139
+ .set(offlineUpdater(offlineState, action));
140
+ };
141
+ };
@@ -0,0 +1,150 @@
1
+ declare module '@olea-bps/redux-offline/lib/defaults' {
2
+ import { Config } from '@olea-bps/redux-offline/lib/types';
3
+
4
+ const config: Config;
5
+ export default config;
6
+ }
7
+
8
+ declare module '@olea-bps/redux-offline/lib/types' {
9
+ export interface ResultAction {
10
+ meta: {
11
+ completed: boolean;
12
+ success: boolean;
13
+ };
14
+ payload?: object;
15
+ type: string;
16
+ }
17
+
18
+ export interface OfflineMetadata {
19
+ commit?: ResultAction;
20
+ effect: object;
21
+ rollback?: ResultAction;
22
+ }
23
+
24
+ export interface OfflineAction {
25
+ meta: {
26
+ offline: OfflineMetadata;
27
+ transaction?: number;
28
+ };
29
+ payload?: object;
30
+ type: string;
31
+ }
32
+
33
+ export interface NetInfo {
34
+ isConnectionExpensive?: boolean;
35
+ reach: string;
36
+ }
37
+
38
+ export interface OfflineStatusChangeAction {
39
+ payload: {
40
+ netInfo?: NetInfo;
41
+ online: boolean;
42
+ };
43
+ type: string;
44
+ }
45
+
46
+ export interface OfflineScheduleRetryAction {
47
+ type: string;
48
+ }
49
+
50
+ export type Outbox = OfflineAction[];
51
+
52
+ export interface OfflineState {
53
+ busy: boolean;
54
+ lastTransaction: number;
55
+ netInfo?: NetInfo;
56
+ online: boolean;
57
+ outbox: Outbox;
58
+ retryCount: number;
59
+ retryScheduled: boolean;
60
+ }
61
+
62
+ export interface PersistRehydrateAction {
63
+ payload: {
64
+ offline: OfflineState;
65
+ };
66
+ type: string;
67
+ }
68
+
69
+ export interface AppState {
70
+ offline: OfflineState;
71
+ }
72
+
73
+ export type NetworkCallback = (result: boolean) => void;
74
+
75
+ export interface Config {
76
+ defaultCommit: { type: string };
77
+ defaultRollback: { type: string };
78
+ detectNetwork: (callback: NetworkCallback) => void;
79
+ discard: (error: any, action: OfflineAction, retries: number) => boolean;
80
+ effect: (effect: any, action: OfflineAction) => Promise<any>;
81
+ offlineStateLens: (
82
+ state: any,
83
+ ) => { get: OfflineState, set: (offlineState?: OfflineState) => any };
84
+ persist: (store: any) => any;
85
+ persistAutoRehydrate: (config?: { [key: string]: any }) => (next: any) => any;
86
+ persistCallback: (callback?: any) => any;
87
+ persistOptions: { [key: string]: any };
88
+ retry: (action: OfflineAction, retries: number) => number | void;
89
+ queue: {
90
+ enqueue: (
91
+ array: Array<OfflineAction>,
92
+ item: OfflineAction,
93
+ context: { offline: OfflineState }
94
+ ) => Array<OfflineAction>,
95
+ dequeue: (
96
+ array: Array<OfflineAction>,
97
+ item: ResultAction,
98
+ context: { offline: OfflineState }
99
+ ) => Array<OfflineAction>,
100
+ peek: (
101
+ array: Array<OfflineAction>,
102
+ item: any,
103
+ context: { offline: OfflineState }
104
+ ) => OfflineAction
105
+ };
106
+ offlineActionTracker: {
107
+ registerAction: (transaction: number) => Promise<any> | (() => void),
108
+ resolveAction: (transaction: number, value: any) => void | (() => void),
109
+ rejectAction: (transaction: number, error: Error) => void | (() => void)
110
+ };
111
+ returnPromises?: boolean;
112
+ rehydrate?: boolean;
113
+ }
114
+ }
115
+
116
+ declare module '@olea-bps/redux-offline/lib/constants' {
117
+ export const DEFAULT_ROLLBACK: string;
118
+ export const DEFAULT_COMMIT: string;
119
+ export const JS_ERROR: string;
120
+ export const PERSIST_REHYDRATE: string;
121
+ export const RESET_STATE: string;
122
+ export const OFFLINE_BUSY: string;
123
+ export const OFFLINE_SEND: string;
124
+ export const OFFLINE_COMPLETE_RETRY: string;
125
+ export const OFFLINE_SCHEDULE_RETRY: string;
126
+ export const OFFLINE_STATUS_CHANGED: string;
127
+ }
128
+
129
+ declare module '@olea-bps/redux-offline' {
130
+ import { createStore as createReduxStore, Store, StoreEnhancer, Dispatch, Middleware } from 'redux';
131
+
132
+ import { Config } from '@olea-bps/redux-offline/lib/types';
133
+
134
+ export const offline: (userConfig: Partial<Config>) => (createStore: typeof createReduxStore) =>
135
+ <T extends { [key: string]: any }>(
136
+ reducer: (state: T, action: any) => T,
137
+ preloadedState: T,
138
+ enhancer: StoreEnhancer<T>,
139
+ ) => Store<T>;
140
+
141
+ export const createOffline: (userConfig: Partial<Config>) => ({
142
+ enhanceReducer: (reducer: any) => (state: any, action: any) => any,
143
+ enhanceStore: (next: any) => <T extends { [key: string]: any }>(
144
+ reducer: (state: T, action: any) => T,
145
+ preloadedState: T,
146
+ enhancer: StoreEnhancer<T>,
147
+ ) => Store<T>,
148
+ middleware: Middleware
149
+ });
150
+ }