@monorise/react 1.0.1 → 1.0.3

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 (41) hide show
  1. package/dist/actions/config.action.d.ts +10 -0
  2. package/dist/actions/config.action.d.ts.map +1 -0
  3. package/dist/actions/config.action.js +31 -0
  4. package/dist/actions/config.action.js.map +1 -0
  5. package/dist/actions/core.action.d.ts.map +1 -1
  6. package/dist/actions/core.action.js +2 -3
  7. package/dist/actions/core.action.js.map +1 -1
  8. package/dist/index.d.ts +388 -24
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +12 -8
  11. package/dist/index.js.map +1 -1
  12. package/dist/index.jsx +48 -0
  13. package/dist/index.jsx.map +1 -0
  14. package/dist/lib/api.d.ts.map +1 -1
  15. package/dist/lib/api.jsx +3 -2
  16. package/dist/lib/api.jsx.map +1 -1
  17. package/dist/services/core.service.d.ts +2 -1
  18. package/dist/services/core.service.d.ts.map +1 -1
  19. package/dist/services/core.service.js +9 -10
  20. package/dist/services/core.service.js.map +1 -1
  21. package/dist/store/monorise.store.d.ts +9 -3
  22. package/dist/store/monorise.store.d.ts.map +1 -1
  23. package/dist/store/monorise.store.js +3 -11
  24. package/dist/store/monorise.store.js.map +1 -1
  25. package/index.ts +18 -4
  26. package/package.json +6 -4
  27. package/actions/app.action.ts +0 -218
  28. package/actions/auth.action.ts +0 -71
  29. package/actions/core.action.ts +0 -878
  30. package/lib/api.tsx +0 -132
  31. package/lib/config.ts +0 -36
  32. package/lib/constant.ts +0 -1
  33. package/lib/entity.ts +0 -81
  34. package/lib/utils.ts +0 -12
  35. package/services/auth.service.ts +0 -59
  36. package/services/core.service.ts +0 -416
  37. package/services/filestore.service.ts +0 -136
  38. package/store/monorise.store.ts +0 -111
  39. package/types/api.type.ts +0 -57
  40. package/types/axios.type.ts +0 -14
  41. package/types/monorise.type.ts +0 -8
@@ -1,218 +0,0 @@
1
- import { produce } from 'immer';
2
- import { useEffect, useState } from 'react';
3
- import type { ApplicationRequestError, ModalProps } from '../types/api.type';
4
- import type { MonoriseStore } from '../store/monorise.store';
5
-
6
- type StartLoadingPayload<T> = {
7
- requestKey: string;
8
- isInterruptive?: boolean;
9
- message?: string | boolean;
10
- request: Promise<T>;
11
- };
12
-
13
- type EndLoadingPayload = {
14
- requestKey: string;
15
- isInterruptive?: boolean;
16
- };
17
-
18
- type SetErrorPayload = {
19
- requestKey: string;
20
- error: ApplicationRequestError;
21
- };
22
-
23
- type Options = {
24
- modals?: Record<string, React.ComponentType<unknown>>;
25
- };
26
-
27
- const initAppActions = (store: MonoriseStore) => {
28
- let options: Options = {};
29
-
30
- const { modals = {} } = options;
31
- type AppModalProps = ModalProps<typeof modals>;
32
-
33
- const startLoading = <T>({
34
- requestKey,
35
- isInterruptive,
36
- message,
37
- request,
38
- }: StartLoadingPayload<T>) => {
39
- store.setState(
40
- produce((state) => {
41
- const stackType = isInterruptive ? 'intLoadStack' : 'loadStack';
42
- const requestKeyCount = state.app[stackType].get(requestKey);
43
-
44
- state.app.loadingMessage =
45
- typeof message === 'string' ? message : 'Loading';
46
- state.app.ongoingRequests.set(requestKey, request);
47
- state.app[stackType].set(
48
- requestKey,
49
- requestKeyCount ? requestKeyCount + 1 : 1,
50
- );
51
- }),
52
- undefined,
53
- `mr/start-loading/${requestKey}`,
54
- );
55
- };
56
-
57
- const endLoading = ({ requestKey, isInterruptive }: EndLoadingPayload) => {
58
- setTimeout(() => {
59
- store.setState(
60
- produce((state) => {
61
- const stackType = isInterruptive ? 'intLoadStack' : 'loadStack';
62
- const requestKeyCount = state.app[stackType].get(requestKey);
63
-
64
- if (state.app.ongoingRequests.has(requestKey)) {
65
- state.app.ongoingRequests.delete(requestKey);
66
- }
67
-
68
- if (typeof requestKeyCount === 'number' && requestKeyCount > 1) {
69
- state.app[stackType].set(requestKey, requestKeyCount - 1);
70
- } else {
71
- state.app[stackType].delete(requestKey);
72
- }
73
- }),
74
- undefined,
75
- `mr/end-loading/${requestKey}`,
76
- );
77
- }, 500);
78
- };
79
-
80
- const checkIsLoading = (requestKey?: string) => {
81
- const { intLoadStack, loadStack } = store.getState().app;
82
- if (!requestKey) {
83
- return intLoadStack.size > 0 || loadStack.size > 0;
84
- }
85
-
86
- const activeIntLoadingCount = intLoadStack.get(requestKey);
87
- const isIntLoading =
88
- typeof activeIntLoadingCount === 'number' && activeIntLoadingCount > 0;
89
-
90
- const activeLoadingCount = loadStack.get(requestKey);
91
- const isLoading =
92
- typeof activeLoadingCount === 'number' && activeLoadingCount > 0;
93
-
94
- return isIntLoading || isLoading;
95
- };
96
-
97
- const setError = ({ requestKey, error }: SetErrorPayload) =>
98
- store.setState(
99
- produce((state) => {
100
- state.app.errorStack.set(requestKey, error);
101
- }),
102
- undefined,
103
- `mr/set-error/${requestKey}`,
104
- );
105
-
106
- const getError = (requestKey: string) => {
107
- const { errorStack } = store.getState().app;
108
- return errorStack.get(requestKey);
109
- };
110
-
111
- const clearError = (requestKey: string) => {
112
- const { errorStack } = store.getState().app;
113
-
114
- if (errorStack.has(requestKey)) {
115
- store.setState(
116
- produce((state) => {
117
- state.app.errorStack.delete(requestKey);
118
- }),
119
- undefined,
120
- `mr/clear-error/${requestKey}`,
121
- );
122
- }
123
- };
124
-
125
- const openModal = (modal: {
126
- name: keyof AppModalProps;
127
- context?: AppModalProps[keyof AppModalProps];
128
- }) =>
129
- store.setState(
130
- produce((state) => {
131
- state.app.modal = modal;
132
- }),
133
- undefined,
134
- `mr/open-modal/${modal.name}`,
135
- );
136
-
137
- const closeModal = () =>
138
- store.setState(
139
- produce((state) => {
140
- state.app.modal = { name: null };
141
- }),
142
- undefined,
143
- 'mr/close-modal',
144
- );
145
-
146
- const useLoadStore = (requestKey?: string) => {
147
- const { loadStack, intLoadStack } = store((state) => ({
148
- loadStack: state.app.loadStack,
149
- intLoadStack: state.app.intLoadStack,
150
- }));
151
- const [isLoading, setIsLoading] = useState(false);
152
-
153
- useEffect(() => {
154
- if (loadStack && intLoadStack && requestKey) {
155
- setIsLoading(checkIsLoading(requestKey));
156
- }
157
- }, [loadStack, intLoadStack, requestKey]);
158
-
159
- return isLoading;
160
- };
161
-
162
- const useInterruptiveLoadStore = () => {
163
- const { intLoadStack, loadingMessage } = store((state) => ({
164
- intLoadStack: state.app.intLoadStack,
165
- loadingMessage: state.app.loadingMessage,
166
- }));
167
-
168
- return {
169
- isLoading: intLoadStack.size > 0,
170
- message: loadingMessage,
171
- };
172
- };
173
-
174
- const useErrorStore = (requestKey: string) => {
175
- const error = store((state) => state.app.errorStack.get(requestKey));
176
-
177
- useEffect(() => {
178
- if (error) {
179
- return () => {
180
- clearError(requestKey);
181
- };
182
- }
183
- }, [error, requestKey]);
184
-
185
- return error;
186
- };
187
-
188
- const useModalStore = () => {
189
- return store((state) => state.app.modal);
190
- };
191
-
192
- const setOptions = (opts: Options) => {
193
- options = {
194
- ...options,
195
- ...opts,
196
- };
197
- };
198
-
199
- return {
200
- startLoading,
201
- endLoading,
202
- setError,
203
- getError,
204
- clearError,
205
- openModal,
206
- closeModal,
207
- checkIsLoading,
208
- useLoadStore,
209
- useInterruptiveLoadStore,
210
- useErrorStore,
211
- useModalStore,
212
- setOptions,
213
- };
214
- };
215
-
216
- export { initAppActions };
217
-
218
- export type AppActions = ReturnType<typeof initAppActions>;
@@ -1,71 +0,0 @@
1
- import type { Entity } from '@monorise/base';
2
- import { produce } from 'immer';
3
- import type { AuthService } from '../services/auth.service';
4
- import type { MonoriseStore } from '../store/monorise.store';
5
-
6
- const initAuthActions = (store: MonoriseStore, authService: AuthService) => {
7
- const requestLogin = async (
8
- entityType: Entity,
9
- payload: { email: string },
10
- ) => {
11
- const { data } = await authService.requestLoginMagicLink(
12
- entityType,
13
- payload,
14
- );
15
-
16
- return data;
17
- };
18
-
19
- const useProfile = <T extends Record<string, unknown>>(): T & {
20
- displayName: string;
21
- email: string;
22
- accountId: string;
23
- impersonatorId?: string;
24
- } => {
25
- const profile = store((state) => state.auth.profile);
26
-
27
- return profile as T & typeof profile;
28
- };
29
-
30
- const getProfile = async () => {
31
- const {
32
- data: { displayName, email, accountId, ...rest },
33
- } = await authService.getSessionProfile();
34
-
35
- store.setState(
36
- produce((state) => {
37
- state.auth.profile = {
38
- displayName,
39
- email,
40
- accountId,
41
- ...rest,
42
- };
43
- }),
44
- undefined,
45
- 'mr/auth/get-profile',
46
- );
47
- };
48
-
49
- const useIsUnauthorized = () => {
50
- return store((state) => state.auth.isUnauthorized);
51
- };
52
-
53
- const setIsUnauthorized = (isUnauthorized: boolean) =>
54
- store.setState(
55
- produce((state) => {
56
- state.auth.isUnauthorized = isUnauthorized;
57
- }),
58
- );
59
-
60
- return {
61
- requestLogin,
62
- useProfile,
63
- getProfile,
64
- useIsUnauthorized,
65
- setIsUnauthorized,
66
- };
67
- };
68
-
69
- export { initAuthActions };
70
-
71
- export type AuthActions = ReturnType<typeof initAuthActions>;