@equinor/fusion-framework-module-bookmark 4.0.2 → 4.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 (117) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/esm/BookmarkClient.js +18 -3
  3. package/dist/esm/BookmarkClient.js.map +1 -1
  4. package/dist/esm/{BookmarkProvider.error.js → BookmarkFlowError.js} +9 -7
  5. package/dist/esm/BookmarkFlowError.js.map +1 -0
  6. package/dist/esm/{BookmarkConfigurator.js → BookmarkModuleConfigurator.js} +36 -5
  7. package/dist/esm/BookmarkModuleConfigurator.js.map +1 -0
  8. package/dist/esm/BookmarkProvider.js +112 -23
  9. package/dist/esm/BookmarkProvider.js.map +1 -1
  10. package/dist/esm/BookmarkProvider.selectors.js +8 -0
  11. package/dist/esm/BookmarkProvider.selectors.js.map +1 -1
  12. package/dist/esm/BookmarkProviderError.js +20 -0
  13. package/dist/esm/BookmarkProviderError.js.map +1 -0
  14. package/dist/esm/{BookmarkProvider.actions.js → bookmark-actions.js} +1 -1
  15. package/dist/esm/bookmark-actions.js.map +1 -0
  16. package/dist/esm/bookmark-config.schema.js +2 -0
  17. package/dist/esm/bookmark-config.schema.js.map +1 -1
  18. package/dist/esm/bookmark-flows/bookmark-api-flows.js +28 -0
  19. package/dist/esm/bookmark-flows/bookmark-api-flows.js.map +1 -0
  20. package/dist/esm/bookmark-flows/handle-add-bookmark-as-favorite.js +27 -0
  21. package/dist/esm/bookmark-flows/handle-add-bookmark-as-favorite.js.map +1 -0
  22. package/dist/esm/bookmark-flows/handle-create-bookmark.js +27 -0
  23. package/dist/esm/bookmark-flows/handle-create-bookmark.js.map +1 -0
  24. package/dist/esm/bookmark-flows/handle-delete-bookmark.js +26 -0
  25. package/dist/esm/bookmark-flows/handle-delete-bookmark.js.map +1 -0
  26. package/dist/esm/bookmark-flows/handle-fetch-all-bookmark.js +29 -0
  27. package/dist/esm/bookmark-flows/handle-fetch-all-bookmark.js.map +1 -0
  28. package/dist/esm/bookmark-flows/handle-fetch-bookmark-data.js +26 -0
  29. package/dist/esm/bookmark-flows/handle-fetch-bookmark-data.js.map +1 -0
  30. package/dist/esm/bookmark-flows/handle-fetch-bookmark.js +30 -0
  31. package/dist/esm/bookmark-flows/handle-fetch-bookmark.js.map +1 -0
  32. package/dist/esm/bookmark-flows/handle-remove-bookmark-from-favorites.js +28 -0
  33. package/dist/esm/bookmark-flows/handle-remove-bookmark-from-favorites.js.map +1 -0
  34. package/dist/esm/bookmark-flows/handle-remove-bookmark.js +49 -0
  35. package/dist/esm/bookmark-flows/handle-remove-bookmark.js.map +1 -0
  36. package/dist/esm/bookmark-flows/handle-update-bookmark.js +28 -0
  37. package/dist/esm/bookmark-flows/handle-update-bookmark.js.map +1 -0
  38. package/dist/esm/bookmark-flows/index.js +11 -0
  39. package/dist/esm/bookmark-flows/index.js.map +1 -0
  40. package/dist/esm/bookmark-module.js +2 -2
  41. package/dist/esm/bookmark-module.js.map +1 -1
  42. package/dist/esm/bookmark.schemas.js +9 -3
  43. package/dist/esm/bookmark.schemas.js.map +1 -1
  44. package/dist/esm/create-bookmark-reducer.js +122 -0
  45. package/dist/esm/create-bookmark-reducer.js.map +1 -0
  46. package/dist/esm/{BookmarkProvider.store.js → create-bookmark-store.js} +3 -3
  47. package/dist/esm/create-bookmark-store.js.map +1 -0
  48. package/dist/esm/enable-bookmark.js +2 -1
  49. package/dist/esm/enable-bookmark.js.map +1 -1
  50. package/dist/esm/index.js +12 -1
  51. package/dist/esm/index.js.map +1 -1
  52. package/dist/esm/version.js +1 -1
  53. package/dist/tsconfig.tsbuildinfo +1 -1
  54. package/dist/types/BookmarkClient.d.ts +1 -0
  55. package/dist/types/{BookmarkProvider.error.d.ts → BookmarkFlowError.d.ts} +9 -4
  56. package/dist/types/{BookmarkConfigurator.d.ts → BookmarkModuleConfigurator.d.ts} +9 -4
  57. package/dist/types/BookmarkProvider.d.ts +81 -18
  58. package/dist/types/BookmarkProvider.interface.d.ts +1 -1
  59. package/dist/types/BookmarkProvider.selectors.d.ts +3 -3
  60. package/dist/types/BookmarkProviderError.d.ts +16 -0
  61. package/dist/types/{BookmarkProvider.actions.d.ts → bookmark-actions.d.ts} +3 -3
  62. package/dist/types/bookmark-flows/bookmark-api-flows.d.ts +11 -0
  63. package/dist/types/bookmark-flows/handle-add-bookmark-as-favorite.d.ts +10 -0
  64. package/dist/types/bookmark-flows/handle-create-bookmark.d.ts +10 -0
  65. package/dist/types/bookmark-flows/handle-delete-bookmark.d.ts +10 -0
  66. package/dist/types/bookmark-flows/handle-fetch-all-bookmark.d.ts +10 -0
  67. package/dist/types/bookmark-flows/handle-fetch-bookmark-data.d.ts +10 -0
  68. package/dist/types/bookmark-flows/handle-fetch-bookmark.d.ts +10 -0
  69. package/dist/types/bookmark-flows/handle-remove-bookmark-from-favorites.d.ts +10 -0
  70. package/dist/types/bookmark-flows/handle-remove-bookmark.d.ts +14 -0
  71. package/dist/types/bookmark-flows/handle-update-bookmark.d.ts +10 -0
  72. package/dist/types/bookmark-flows/index.d.ts +10 -0
  73. package/dist/types/bookmark-module.d.ts +1 -1
  74. package/dist/types/{BookmarkProvider.reducer.d.ts → create-bookmark-reducer.d.ts} +39 -39
  75. package/dist/types/{BookmarkProvider.store.d.ts → create-bookmark-store.d.ts} +2 -2
  76. package/dist/types/enable-bookmark.d.ts +1 -1
  77. package/dist/types/index.d.ts +12 -1
  78. package/dist/types/version.d.ts +1 -1
  79. package/package.json +11 -11
  80. package/src/BookmarkClient.ts +21 -3
  81. package/src/{BookmarkProvider.error.ts → BookmarkFlowError.ts} +9 -7
  82. package/src/{BookmarkConfigurator.ts → BookmarkModuleConfigurator.ts} +40 -11
  83. package/src/BookmarkProvider.interface.ts +3 -2
  84. package/src/BookmarkProvider.selectors.ts +10 -2
  85. package/src/BookmarkProvider.ts +112 -23
  86. package/src/BookmarkProviderError.ts +19 -0
  87. package/src/{BookmarkProvider.actions.ts → bookmark-actions.ts} +1 -1
  88. package/src/bookmark-config.schema.ts +2 -0
  89. package/src/bookmark-flows/bookmark-api-flows.ts +45 -0
  90. package/src/bookmark-flows/handle-add-bookmark-as-favorite.ts +49 -0
  91. package/src/bookmark-flows/handle-create-bookmark.ts +47 -0
  92. package/src/bookmark-flows/handle-delete-bookmark.ts +47 -0
  93. package/src/bookmark-flows/handle-fetch-all-bookmark.ts +53 -0
  94. package/src/bookmark-flows/handle-fetch-bookmark-data.ts +58 -0
  95. package/src/bookmark-flows/handle-fetch-bookmark.ts +64 -0
  96. package/src/bookmark-flows/handle-remove-bookmark-from-favorites.ts +49 -0
  97. package/src/bookmark-flows/handle-remove-bookmark.ts +76 -0
  98. package/src/bookmark-flows/handle-update-bookmark.ts +48 -0
  99. package/src/bookmark-flows/index.ts +10 -0
  100. package/src/bookmark-module.ts +2 -2
  101. package/src/bookmark.schemas.ts +10 -4
  102. package/src/create-bookmark-reducer.ts +149 -0
  103. package/src/{BookmarkProvider.store.ts → create-bookmark-store.ts} +4 -4
  104. package/src/enable-bookmark.ts +3 -2
  105. package/src/index.ts +12 -1
  106. package/src/version.ts +1 -1
  107. package/dist/esm/BookmarkConfigurator.js.map +0 -1
  108. package/dist/esm/BookmarkProvider.actions.js.map +0 -1
  109. package/dist/esm/BookmarkProvider.error.js.map +0 -1
  110. package/dist/esm/BookmarkProvider.flows.js +0 -217
  111. package/dist/esm/BookmarkProvider.flows.js.map +0 -1
  112. package/dist/esm/BookmarkProvider.reducer.js +0 -113
  113. package/dist/esm/BookmarkProvider.reducer.js.map +0 -1
  114. package/dist/esm/BookmarkProvider.store.js.map +0 -1
  115. package/dist/types/BookmarkProvider.flows.d.ts +0 -79
  116. package/src/BookmarkProvider.flows.ts +0 -421
  117. package/src/BookmarkProvider.reducer.ts +0 -144
@@ -1,79 +0,0 @@
1
- import { type Flow } from '@equinor/fusion-observable';
2
- import { type BookmarkActions } from './BookmarkProvider.actions';
3
- import type { IBookmarkClient } from './BookmarkClient.interface';
4
- import type { BookmarkState } from './BookmarkProvider.store';
5
- /**
6
- * Handles the fetch bookmark action by making an API request to get the bookmark by its ID.
7
- *
8
- * @param api - The bookmark client API.
9
- * @returns A flow that handles the fetch bookmark action.
10
- */
11
- export declare const handleFetchBookmark: (api: IBookmarkClient) => Flow<BookmarkActions>;
12
- /**
13
- * Creates a flow for handling fetching bookmark data.
14
- *
15
- * @param api - The bookmark client API.
16
- * @returns A flow of bookmark actions.
17
- */
18
- export declare const handleFetchBookmarkData: (api: IBookmarkClient) => Flow<BookmarkActions>;
19
- /**
20
- * Creates a Flow for handling fetching users bookmarks.
21
- *
22
- * @param api - The bookmark API client to use for fetching bookmarks.
23
- * @param options - Optional configuration options, including a `throttle` value in milliseconds.
24
- * @returns An observable that emits the result of the bookmark fetch operation.
25
- */
26
- export declare const handleFetchAllBookmark: (api: IBookmarkClient) => Flow<BookmarkActions>;
27
- /**
28
- * Creates a Flow for handling creating bookmarks.
29
- *
30
- * @param api - An instance of the `IBookmarkClient` interface, which provides the necessary API methods for creating bookmarks.
31
- * @returns A flow that listens for `createBookmark` actions, creates a new bookmark using the provided API.
32
- */
33
- export declare const handleCreateBookmark: (api: IBookmarkClient) => Flow<BookmarkActions>;
34
- /**
35
- * Creates a Flow for handling updating bookmarks.
36
- *
37
- * @param api - An instance of the `IBookmarkClient` interface, which provides the necessary API methods for updating bookmarks.
38
- * @returns A flow that listens for `updateBookmark` actions, updates the bookmark using the provided API.
39
- */
40
- export declare const handleUpdateBookmark: (api: IBookmarkClient) => Flow<BookmarkActions>;
41
- /**
42
- * Creates a flow for handling deleting bookmarks.
43
- *
44
- * @param api - An instance of the `IBookmarkClient` interface, which provides the necessary API methods for deleting bookmarks.
45
- * @returns A flow that listens for `deleteBookmark` actions, deletes the bookmark using the provided API.
46
- */
47
- export declare const handleDeleteBookmark: (api: IBookmarkClient) => Flow<BookmarkActions>;
48
- /**
49
- * Creates a Flow for handling deleting bookmarks.
50
- *
51
- * @param api - An instance of the `IBookmarkClient` interface, which provides the necessary API methods for managing favorite bookmarks.
52
- * @returns A flow that listens for `removeBookmarkAsFavorite` actions, removes the bookmark from the user's favorites using the provided API.
53
- */
54
- export declare const handleRemoveBookmarkFromFavorites: (api: IBookmarkClient) => Flow<BookmarkActions>;
55
- /**
56
- * Handles the flow of adding a bookmark as a favorite.
57
- *
58
- * @param api - The `IBookmarkClient` API instance to use for interacting with bookmarks.
59
- * @returns An RxJS `Flow` that handles the `addBookmarkAsFavourite` action.
60
- */
61
- export declare const handleAddBookmarkAsFavorite: (api: IBookmarkClient) => Flow<BookmarkActions>;
62
- /**
63
- * Creates a flow for handling bookmark removal.
64
- *
65
- * This function observes actions related to deleting a bookmark or removing a bookmark as a favorite, and dispatches the appropriate actions for each case.
66
- * If the bookmark is currently a favorite, it will dispatch an action to remove it as a favorite.
67
- * If the bookmark is not a favorite, it will dispatch an action to delete it.
68
- *
69
- * @param api - An instance of the `IBookmarkClient` interface, which provides the necessary API methods checking if bookmarks are favorites.
70
- * @returns A flow that handles the removal of a bookmark.
71
- */
72
- export declare const handleRemoveBookmark: (api: IBookmarkClient) => Flow<BookmarkActions>;
73
- /**
74
- * Defines a set of flows that handle various bookmark-related actions, such as fetching, creating, updating, and deleting bookmarks.
75
- *
76
- * @param api - An instance of `IBookmarkClient` that provides the necessary API methods for interacting with the bookmark service.
77
- * @returns A combined flow that handles all the bookmark-related actions.
78
- */
79
- export declare const bookmarkApiFlows: (api: IBookmarkClient) => Flow<BookmarkActions, BookmarkState>;
@@ -1,421 +0,0 @@
1
- import { from, of, merge, concat } from 'rxjs';
2
- import {
3
- switchMap,
4
- map,
5
- catchError,
6
- filter,
7
- concatMap,
8
- mergeMap,
9
- groupBy,
10
- throttleTime,
11
- last,
12
- } from 'rxjs/operators';
13
-
14
- import { type Flow, type Observable, getBaseType } from '@equinor/fusion-observable';
15
-
16
- import { bookmarkActions as actions, type BookmarkActions } from './BookmarkProvider.actions';
17
- import type { IBookmarkClient } from './BookmarkClient.interface';
18
- import type { BookmarkState } from './BookmarkProvider.store';
19
- import { BookmarkFlowError } from './BookmarkProvider.error';
20
-
21
- const defaultThrottleTime = 200;
22
-
23
- /**
24
- * Handles the fetch bookmark action by making an API request to get the bookmark by its ID.
25
- *
26
- * @param api - The bookmark client API.
27
- * @returns A flow that handles the fetch bookmark action.
28
- */
29
- export const handleFetchBookmark =
30
- (api: IBookmarkClient): Flow<BookmarkActions> =>
31
- (action$) => {
32
- /**
33
- * Observable that represents the flow of fetching bookmarks.
34
- * It listens for `fetchBookmark` actions, makes an API request to get the bookmark by ID,
35
- * and emits corresponding success or failure actions based on the API response.
36
- */
37
- const flow$ = action$.pipe(
38
- filter(actions.fetchBookmark.match),
39
- groupBy((action) => action.payload),
40
- mergeMap((group) => group.pipe(throttleTime(defaultThrottleTime))),
41
- switchMap((action) =>
42
- from(api.getBookmarkById(action.payload)).pipe(
43
- last(),
44
- map((bookmark) => actions.fetchBookmark.success(bookmark, action.meta)),
45
- catchError((error) =>
46
- of(
47
- actions.fetchBookmark.failure(
48
- new BookmarkFlowError('Failed to fetch bookmark', action, {
49
- cause: error,
50
- }),
51
- action.meta,
52
- ),
53
- ),
54
- ),
55
- ),
56
- ),
57
- );
58
- return flow$;
59
- };
60
-
61
- /**
62
- * Creates a flow for handling fetching bookmark data.
63
- *
64
- * @param api - The bookmark client API.
65
- * @returns A flow of bookmark actions.
66
- */
67
- export const handleFetchBookmarkData =
68
- (api: IBookmarkClient): Flow<BookmarkActions> =>
69
- (action$) => {
70
- const flow$ = action$.pipe(
71
- filter(actions.fetchBookmarkData.match),
72
- groupBy((action) => JSON.stringify(action.payload)),
73
- mergeMap((group) => group.pipe(throttleTime(defaultThrottleTime))),
74
- switchMap((action) => {
75
- return from(api.getBookmarkData(action.payload)).pipe(
76
- last(),
77
- map((data) => actions.fetchBookmarkData.success(action.payload, data, action.meta)),
78
- catchError((error) =>
79
- of(
80
- actions.fetchBookmarkData.failure(
81
- new BookmarkFlowError('Failed to fetch bookmark payload', action, {
82
- cause: error,
83
- }),
84
- action.meta,
85
- ),
86
- ),
87
- ),
88
- );
89
- }),
90
- );
91
- return flow$;
92
- };
93
-
94
- /**
95
- * Creates a Flow for handling fetching users bookmarks.
96
- *
97
- * @param api - The bookmark API client to use for fetching bookmarks.
98
- * @param options - Optional configuration options, including a `throttle` value in milliseconds.
99
- * @returns An observable that emits the result of the bookmark fetch operation.
100
- */
101
- export const handleFetchAllBookmark =
102
- (api: IBookmarkClient): Flow<BookmarkActions> =>
103
- (action$: Observable<BookmarkActions>) => {
104
- /**
105
- * This flow is triggered by the `fetchBookmarks` action and uses the `throttleTime` operator
106
- * to limit the number of requests made to the API. The `switchMap` operator is used to make
107
- * the API call and map the response to the appropriate action (success or failure).
108
- */
109
- const flow$ = action$.pipe(
110
- filter(actions.fetchBookmarks.match),
111
- groupBy((action) => JSON.stringify(action.payload)),
112
- switchMap((group) => group.pipe(throttleTime(defaultThrottleTime))),
113
- switchMap((action) =>
114
- from(api.getAllBookmarks(action.payload)).pipe(
115
- last(),
116
- map((value) => actions.fetchBookmarks.success(value, action.meta)),
117
- catchError((error) =>
118
- of(
119
- actions.fetchBookmarks.failure(
120
- new BookmarkFlowError('Failed to fetch all bookmarks', action, {
121
- cause: error,
122
- }),
123
- action.meta,
124
- ),
125
- ),
126
- ),
127
- ),
128
- ),
129
- );
130
- return flow$;
131
- };
132
-
133
- /**
134
- * Creates a Flow for handling creating bookmarks.
135
- *
136
- * @param api - An instance of the `IBookmarkClient` interface, which provides the necessary API methods for creating bookmarks.
137
- * @returns A flow that listens for `createBookmark` actions, creates a new bookmark using the provided API.
138
- */
139
- export const handleCreateBookmark =
140
- (api: IBookmarkClient): Flow<BookmarkActions> =>
141
- (action$: Observable<BookmarkActions>) => {
142
- /**
143
- * This flow listens for the `createBookmark` action, and then uses the `api.createBookmark` function to create a new bookmark.
144
- * If the bookmark is created successfully, it dispatches the `createBookmark.success` action with the new bookmark.
145
- * If there is an error creating the bookmark, it dispatches the `fetchBookmark.failure` action with an error.
146
- *
147
- * The `concatMap` operator is used to prevent aborting the request if a new `createBookmark` action is dispatched while the previous request is in flight.
148
- */
149
- const flow$ = action$.pipe(
150
- filter(actions.createBookmark.match),
151
- concatMap((action) => {
152
- return from(api.createBookmark(action.payload)).pipe(
153
- last(),
154
- map((bookmark) => actions.createBookmark.success(bookmark, action.meta)),
155
- catchError((error) =>
156
- of(
157
- actions.createBookmark.failure(
158
- new BookmarkFlowError('Failed to create new bookmark', action, {
159
- cause: error,
160
- }),
161
- action.meta,
162
- ),
163
- ),
164
- ),
165
- );
166
- }),
167
- );
168
- return flow$;
169
- };
170
-
171
- /**
172
- * Creates a Flow for handling updating bookmarks.
173
- *
174
- * @param api - An instance of the `IBookmarkClient` interface, which provides the necessary API methods for updating bookmarks.
175
- * @returns A flow that listens for `updateBookmark` actions, updates the bookmark using the provided API.
176
- */
177
- export const handleUpdateBookmark =
178
- (api: IBookmarkClient): Flow<BookmarkActions> =>
179
- (action$: Observable<BookmarkActions>) => {
180
- /**
181
- * This flow listens for `updateBookmark` actions, then calls the `api.updateBookmark` function with the action payload.
182
- * If the update is successful, it dispatches a `updateBookmark.success` action with the updated bookmark.
183
- * If there is an error, it dispatches a `fetchBookmark.failure` action with the error.
184
- *
185
- * The `concatMap` operator is used to prevent aborting the request if a new `updateBookmark` action is dispatched while the previous request is in flight.
186
- */
187
- const flow$ = action$.pipe(
188
- filter(actions.updateBookmark.match),
189
- concatMap((action) => {
190
- const { bookmarkId, updates } = action.payload;
191
- return from(api.updateBookmark(bookmarkId, updates)).pipe(
192
- last(),
193
- map((bookmark) => actions.updateBookmark.success(bookmark, action.meta)),
194
- catchError((error) =>
195
- of(
196
- actions.updateBookmark.failure(
197
- new BookmarkFlowError('Failed to update bookmark', action, {
198
- cause: error,
199
- }),
200
- action.meta,
201
- ),
202
- ),
203
- ),
204
- );
205
- }),
206
- );
207
- return flow$;
208
- };
209
-
210
- /**
211
- * Creates a flow for handling deleting bookmarks.
212
- *
213
- * @param api - An instance of the `IBookmarkClient` interface, which provides the necessary API methods for deleting bookmarks.
214
- * @returns A flow that listens for `deleteBookmark` actions, deletes the bookmark using the provided API.
215
- */
216
- export const handleDeleteBookmark =
217
- (api: IBookmarkClient): Flow<BookmarkActions> =>
218
- (action$: Observable<BookmarkActions>) => {
219
- /**
220
- * - Listens for the `deleteBookmark` action.
221
- * - Calls the `api.deleteBookmark` function to delete the bookmark.
222
- * - Maps the successful response to the `deleteBookmark.success` action.
223
- * - Handles errors by dispatching the `fetchBookmark.failure` action with a `BookmarkFlowError`.
224
- * - Uses `concatMap` to prevent aborting the request if a new action is dispatched while the previous request is in flight.
225
- */
226
- const flow$ = action$.pipe(
227
- filter(actions.deleteBookmark.match),
228
- concatMap((action) =>
229
- from(api.deleteBookmark(action.payload)).pipe(
230
- last(),
231
- map(() => actions.deleteBookmark.success(action.payload, action.meta)),
232
- catchError((error) =>
233
- of(
234
- actions.deleteBookmark.failure(
235
- new BookmarkFlowError('Failed to delete bookmark', action, {
236
- cause: error,
237
- }),
238
- action.meta,
239
- ),
240
- ),
241
- ),
242
- ),
243
- ),
244
- );
245
- return flow$;
246
- };
247
-
248
- /**
249
- * Creates a Flow for handling deleting bookmarks.
250
- *
251
- * @param api - An instance of the `IBookmarkClient` interface, which provides the necessary API methods for managing favorite bookmarks.
252
- * @returns A flow that listens for `removeBookmarkAsFavorite` actions, removes the bookmark from the user's favorites using the provided API.
253
- */
254
- export const handleRemoveBookmarkFromFavorites =
255
- (api: IBookmarkClient): Flow<BookmarkActions> =>
256
- (action$: Observable<BookmarkActions>) => {
257
- /**
258
- * - Listens for the `removeBookmarkAsFavourite` action.
259
- * - Calls the `api.removeBookmarkFromFavorites` function to remove the bookmark from favorites.
260
- * - On success, dispatches the `removeBookmarkAsFavourite.success` action.
261
- * - On error, dispatches the `fetchBookmark.failure` action with a `BookmarkFlowError`.
262
- * - Uses `concatMap` to prevent aborting the request if a new action is dispatched while the previous request is in flight.
263
- */
264
- const flow$ = action$.pipe(
265
- filter(actions.removeBookmarkAsFavourite.match),
266
- // use concat to prevent aborting the request if a new action is dispatched while the previous request is in flight
267
- concatMap((action) =>
268
- from(api.removeBookmarkFromFavorites(action.payload)).pipe(
269
- last(),
270
- map(() => actions.removeBookmarkAsFavourite.success(action.payload, action.meta)),
271
- catchError((error) =>
272
- of(
273
- actions.removeBookmarkAsFavourite.failure(
274
- new BookmarkFlowError('Failed to remove bookmark as favorite', action, {
275
- cause: error,
276
- }),
277
- action.meta,
278
- ),
279
- ),
280
- ),
281
- ),
282
- ),
283
- );
284
-
285
- return flow$;
286
- };
287
-
288
- /**
289
- * Handles the flow of adding a bookmark as a favorite.
290
- *
291
- * @param api - The `IBookmarkClient` API instance to use for interacting with bookmarks.
292
- * @returns An RxJS `Flow` that handles the `addBookmarkAsFavourite` action.
293
- */
294
- export const handleAddBookmarkAsFavorite =
295
- (api: IBookmarkClient): Flow<BookmarkActions> =>
296
- (action$: Observable<BookmarkActions>) => {
297
- /**
298
- * Handles the flow for adding a bookmark as a favorite.
299
- *
300
- * This flow listens for the `addBookmarkAsFavourite` action, and then:
301
- * 1. Calls the `addBookmarkToFavorites` API to add the bookmark to the user's favorites.
302
- * 2. If the API call is successful, dispatches the `addBookmarkAsFavourite.success` action.
303
- * 3. If the API call fails, dispatches the `addBookmarkAsFavourite.failure` action with an error.
304
- */
305
- const flow$ = action$.pipe(
306
- filter(actions.addBookmarkAsFavourite.match),
307
- concatMap((action) =>
308
- from(api.addBookmarkToFavorites(action.payload)).pipe(
309
- last(),
310
- map(() => actions.addBookmarkAsFavourite.success(action.payload, action.meta)),
311
- catchError((error) =>
312
- of(
313
- actions.addBookmarkAsFavourite.failure(
314
- new BookmarkFlowError('Failed to add bookmark as favorite', action, {
315
- cause: error,
316
- }),
317
- action.meta,
318
- ),
319
- ),
320
- ),
321
- ),
322
- ),
323
- );
324
-
325
- return flow$;
326
- };
327
-
328
- /**
329
- * Creates a flow for handling bookmark removal.
330
- *
331
- * This function observes actions related to deleting a bookmark or removing a bookmark as a favorite, and dispatches the appropriate actions for each case.
332
- * If the bookmark is currently a favorite, it will dispatch an action to remove it as a favorite.
333
- * If the bookmark is not a favorite, it will dispatch an action to delete it.
334
- *
335
- * @param api - An instance of the `IBookmarkClient` interface, which provides the necessary API methods checking if bookmarks are favorites.
336
- * @returns A flow that handles the removal of a bookmark.
337
- */
338
- export const handleRemoveBookmark =
339
- (api: IBookmarkClient): Flow<BookmarkActions> =>
340
- (action$) => {
341
- /**
342
- * Handles the removal of a bookmark from the application.
343
- *
344
- * This function is responsible for determining whether the bookmark being removed is a favorite or not,
345
- * and then dispatching the appropriate action to either remove the bookmark as a favorite or delete the bookmark entirely.
346
- *
347
- * Uses `concatMap` to prevent aborting the request if a new action is dispatched while the previous request is in flight.
348
- */
349
- const dispatch$ = action$.pipe(
350
- filter(actions.removeBookmark.match),
351
- concatMap(({ payload: bookmarkId }) => {
352
- return from(api.isBookmarkFavorite(bookmarkId)).pipe(
353
- map((isFavorite) => {
354
- const action = isFavorite ? actions.removeBookmarkAsFavourite : actions.deleteBookmark;
355
- return action(bookmarkId);
356
- }),
357
- );
358
- }),
359
- );
360
-
361
- /**
362
- * Handles failures for deleting a bookmark or removing a bookmark as a favorite.
363
- */
364
- const failure$ = merge(
365
- action$.pipe(filter(actions.deleteBookmark.failure.match)),
366
- action$.pipe(filter(actions.removeBookmarkAsFavourite.failure.match)),
367
- ).pipe(map((action) => actions.removeBookmark.failure(action.payload, action.meta)));
368
-
369
- /**
370
- * Handles the success of deleting a bookmark or removing a bookmark as a favorite.
371
- */
372
- const success$ = merge(
373
- action$.pipe(filter(actions.deleteBookmark.success.match)),
374
- action$.pipe(filter(actions.removeBookmarkAsFavourite.success.match)),
375
- ).pipe(
376
- map((action) =>
377
- actions.removeBookmark.success(
378
- {
379
- type: getBaseType(action.type),
380
- bookmarkId: action.payload,
381
- },
382
- action.meta,
383
- ),
384
- ),
385
- );
386
-
387
- /**
388
- * First dispatch either deletions of bookmark or removals of bookmarks as favorites.
389
- * Then observer the results of those actions and dispatch the appropriate actions.
390
- */
391
- const flow$ = concat(dispatch$, merge(failure$, success$));
392
-
393
- return flow$;
394
- };
395
-
396
- /**
397
- * Defines a set of flows that handle various bookmark-related actions, such as fetching, creating, updating, and deleting bookmarks.
398
- *
399
- * @param api - An instance of `IBookmarkClient` that provides the necessary API methods for interacting with the bookmark service.
400
- * @returns A combined flow that handles all the bookmark-related actions.
401
- */
402
- export const bookmarkApiFlows = (api: IBookmarkClient): Flow<BookmarkActions, BookmarkState> => {
403
- // return handleFetchAllBookmark(api);
404
- /**
405
- * Combines multiple Bookmark-related observable flows into a single observable stream.
406
- * The resulting observable stream emits the combined effects of these flows,
407
- * which can be used to update the application state.
408
- */
409
- return (actions$: Observable<BookmarkActions>, state$: Observable<BookmarkState>) =>
410
- of(
411
- handleFetchBookmark,
412
- handleFetchBookmarkData,
413
- handleFetchAllBookmark,
414
- handleCreateBookmark,
415
- handleUpdateBookmark,
416
- handleDeleteBookmark,
417
- handleRemoveBookmark,
418
- handleRemoveBookmarkFromFavorites,
419
- handleAddBookmarkAsFavorite,
420
- ).pipe(mergeMap((flow) => flow(api)(actions$, state$)));
421
- };
@@ -1,144 +0,0 @@
1
- import {
2
- createReducer,
3
- getBaseType,
4
- isCompleteAction,
5
- isFailureAction,
6
- isRequestAction,
7
- isSuccessAction,
8
- type ActionBaseType,
9
- } from '@equinor/fusion-observable';
10
-
11
- import { bookmarkActions, type BookmarkActions } from './BookmarkProvider.actions';
12
- import type { BookmarkState } from './BookmarkProvider.store';
13
- import type { BookmarkFlowError } from './BookmarkProvider.error';
14
- import type { BookmarkWithoutData } from './types';
15
- import { enableMapSet } from 'immer';
16
-
17
- enableMapSet();
18
-
19
- /**
20
- * Utility function that extracts the base action type from a given action object.
21
- */
22
- const getBookmarkBaseAction = <T extends BookmarkActions>(action: T): ActionBaseType<T> => {
23
- return getBaseType(action.type) as ActionBaseType<T>;
24
- };
25
-
26
- /**
27
- * The default initial state for the BookmarkProvider reducer.
28
- */
29
- const defaultInitialState: BookmarkState = {
30
- status: new Set<ActionBaseType<BookmarkActions>>(),
31
- errors: {} as Record<ActionBaseType<BookmarkActions>, BookmarkFlowError>,
32
- bookmarks: {},
33
- };
34
-
35
- /**
36
- * Creates a reducer for managing the state of bookmarks.
37
- *
38
- * @todo add fast-deep-equal to compare bookmarks
39
- *
40
- * @param initialState - The initial state of the bookmarks.
41
- * @returns A reducer function for managing the bookmarks state.
42
- */
43
- export const createBookmarkReducer = (initialState?: Partial<BookmarkState>) =>
44
- createReducer<BookmarkState, BookmarkActions>(
45
- { ...defaultInitialState, ...initialState },
46
- (builder) => {
47
- builder
48
- .addCase(bookmarkActions.fetchBookmark.success, (state, action) => {
49
- if (action.payload.id in state.bookmarks) {
50
- state.bookmarks[action.payload.id] = action.payload;
51
- }
52
- })
53
- .addCase(bookmarkActions.fetchBookmarkData.success, (state, action) => {
54
- const { bookmarkId, data } = action.payload;
55
- if (state.currentBookmark?.id === bookmarkId) {
56
- state.currentBookmark.payload = data;
57
- }
58
- })
59
- .addCase(bookmarkActions.fetchBookmarks.success, (state, action) => {
60
- // normalize the bookmarks array into a record
61
- state.bookmarks = action.payload.reduce(
62
- (acc, bookmark) => {
63
- acc[bookmark.id] = bookmark;
64
- return acc;
65
- },
66
- {} as Record<string, BookmarkWithoutData>,
67
- );
68
- })
69
- .addCase(bookmarkActions.setBookmark, (state, action) => {
70
- const bookmarkId = action.payload.id;
71
- if (bookmarkId in state.bookmarks) {
72
- state.bookmarks[bookmarkId] = action.payload;
73
- }
74
- if (state.currentBookmark?.id === bookmarkId) {
75
- state.currentBookmark = action.payload;
76
- }
77
- })
78
- .addCase(bookmarkActions.setCurrentBookmark, (state, action) => {
79
- state.currentBookmark = action.payload;
80
- })
81
- .addCase(bookmarkActions.createBookmark.success, (state, action) => {
82
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
83
- const { payload, ...bookmark } = action.payload;
84
- state.bookmarks[bookmark.id] = bookmark;
85
- })
86
- .addCase(bookmarkActions.updateBookmark.success, (state, action) => {
87
- const bookmarkId = action.payload.id;
88
- const hasBookmark = bookmarkId in state.bookmarks;
89
- const isCurrent = state.currentBookmark?.id === bookmarkId;
90
-
91
- // get the current bookmark
92
- const current = hasBookmark
93
- ? state.bookmarks[bookmarkId]
94
- : isCurrent
95
- ? state.currentBookmark
96
- : null;
97
-
98
- // merge the current bookmark with the new data
99
- const next = { ...current, ...action.payload };
100
-
101
- // if the bookmark is in the current state, update it
102
- if (hasBookmark) {
103
- state.bookmarks[bookmarkId] = next;
104
- }
105
-
106
- // if the bookmark is the selected bookmark, update it
107
- if (isCurrent) {
108
- state.currentBookmark = next;
109
- }
110
- })
111
-
112
- /** removal of bookmarks */
113
- .addCase(bookmarkActions.deleteBookmark.success, (state, action) => {
114
- delete state.bookmarks[action.payload];
115
- })
116
- .addCase(bookmarkActions.removeBookmarkAsFavourite.success, (state, action) => {
117
- delete state.bookmarks[action.payload];
118
- })
119
-
120
- /** when a request is made, add the action type to the status object */
121
- .addMatcher(isRequestAction, (state, action) => {
122
- const actionName = getBookmarkBaseAction(action);
123
- state.status.add(actionName);
124
- })
125
-
126
- /** when a request succeeds, remove the error from the errors object */
127
- .addMatcher(isSuccessAction, (state, action) => {
128
- const actionName = getBookmarkBaseAction(action);
129
- delete state.errors[actionName];
130
- })
131
-
132
- /** when a request fails, add the error to the errors object */
133
- .addMatcher(isFailureAction, (state, action) => {
134
- const actionName = getBookmarkBaseAction(action);
135
- state.errors[actionName] = action.payload;
136
- })
137
-
138
- /** when a request is complete, remove the status from the status set */
139
- .addMatcher(isCompleteAction, (state, action) => {
140
- const actionName = getBookmarkBaseAction(action);
141
- state.status.delete(actionName);
142
- });
143
- },
144
- );