@fluid-topics/ft-app-context 1.3.39 → 1.3.40

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.
@@ -15,6 +15,7 @@ export interface FtUserAssetsState {
15
15
  assetCounts: AssetCounts;
16
16
  assetLabels: Array<FtUserAssetLabel>;
17
17
  }
18
+ type FtUserAsset = FtBookmark | FtMySearch;
18
19
  declare const reducers: {
19
20
  setAssetCount: (state: FtUserAssetsState, action: {
20
21
  payload: {
@@ -34,11 +35,18 @@ declare const reducers: {
34
35
  addAsset: (state: FtUserAssetsState, action: {
35
36
  payload: {
36
37
  assetType: FtUserAssetType;
37
- asset: FtBookmark | FtMySearch;
38
+ asset: FtUserAsset;
38
39
  mapId?: string | undefined;
39
40
  };
40
41
  type: string;
41
42
  }) => void;
43
+ editAsset: (state: FtUserAssetsState, action: {
44
+ payload: {
45
+ assetType: FtUserAssetType;
46
+ asset: FtUserAsset;
47
+ };
48
+ type: string;
49
+ }) => void;
42
50
  removeAsset: (state: FtUserAssetsState, action: {
43
51
  payload: {
44
52
  assetType: FtUserAssetType;
@@ -25,58 +25,60 @@ const reducers = {
25
25
  },
26
26
  addAsset: (state, action) => {
27
27
  const { assetType, mapId, asset } = action.payload;
28
- switch (assetType) {
29
- case FtUserAssetType.BOOKMARKS:
30
- if (state.bookmarks) {
31
- state.bookmarks.push(asset);
32
- }
33
- else {
34
- state.bookmarks = [asset];
35
- }
36
- break;
37
- case FtUserAssetType.SEARCHES:
38
- if (state.savedSearches) {
39
- state.savedSearches.push(asset);
40
- }
41
- else {
42
- state.savedSearches = [asset];
43
- }
44
- break;
45
- }
46
- const currentCount = state.assetCounts.allAsset[assetType];
47
- if (currentCount !== undefined) {
48
- state.assetCounts.allAsset[assetType] = currentCount + 1;
49
- }
50
- if (assetType === FtUserAssetType.BOOKMARKS && mapId !== undefined) {
51
- const currentMapCount = state.assetCounts.bookmarkByMap[mapId];
52
- if (currentMapCount !== undefined) {
53
- state.assetCounts.bookmarkByMap[mapId] = currentMapCount + 1;
54
- }
55
- }
28
+ updateAssetsByType(state, assetType, [...getAssetsByType(state, assetType), asset]);
29
+ updateAssetCount(state, assetType, +1, mapId);
30
+ updateAssetLabels(state, asset);
31
+ },
32
+ editAsset: (state, action) => {
33
+ const { assetType, asset } = action.payload;
34
+ updateAssetsByType(state, assetType, getAssetsByType(state, assetType).map((a) => a.id === asset.id ? asset : a));
35
+ updateAssetLabels(state, asset);
56
36
  },
57
37
  removeAsset: (state, action) => {
58
- var _a, _b;
59
38
  const { assetType, mapId, assetId } = action.payload;
60
- switch (assetType) {
61
- case FtUserAssetType.BOOKMARKS:
62
- state.bookmarks = (_a = state.bookmarks) === null || _a === void 0 ? void 0 : _a.filter((b) => b.id !== assetId);
63
- break;
64
- case FtUserAssetType.SEARCHES:
65
- state.savedSearches = (_b = state.savedSearches) === null || _b === void 0 ? void 0 : _b.filter((s) => s.id !== assetId);
66
- break;
67
- }
68
- const currentCount = state.assetCounts.allAsset[assetType];
69
- if (currentCount !== undefined && currentCount > 0) {
70
- state.assetCounts.allAsset[assetType] = currentCount - 1;
71
- }
72
- if (assetType === FtUserAssetType.BOOKMARKS && mapId !== undefined) {
73
- const currentMapCount = state.assetCounts.bookmarkByMap[mapId];
74
- if (currentMapCount !== undefined && currentMapCount > 0) {
75
- state.assetCounts.bookmarkByMap[mapId] = currentMapCount - 1;
76
- }
77
- }
39
+ updateAssetsByType(state, assetType, getAssetsByType(state, assetType).filter((a) => a.id !== assetId));
40
+ updateAssetCount(state, assetType, -1, mapId);
78
41
  },
79
42
  };
43
+ const assetTypeToStateKey = {
44
+ // Refactor Optional<UserAssetsKey> to UserAssetsKey as soon as all asset arrays are present in store
45
+ [FtUserAssetType.SEARCHES]: "savedSearches",
46
+ [FtUserAssetType.BOOKMARKS]: "bookmarks",
47
+ [FtUserAssetType.BOOKS]: undefined,
48
+ [FtUserAssetType.COLLECTIONS]: undefined,
49
+ };
50
+ const getAssetsByType = (state, assetType) => {
51
+ var _a;
52
+ const stateKey = assetTypeToStateKey[assetType];
53
+ return stateKey
54
+ ? (_a = state[stateKey]) !== null && _a !== void 0 ? _a : []
55
+ : [];
56
+ };
57
+ const updateAssetsByType = (state, assetType, assets) => {
58
+ const stateKey = assetTypeToStateKey[assetType];
59
+ if (stateKey) {
60
+ state[stateKey] = assets;
61
+ }
62
+ };
63
+ const updateAssetCount = (state, assetType, delta, mapId) => {
64
+ const currentCount = state.assetCounts.allAsset[assetType];
65
+ if (currentCount === undefined) {
66
+ return;
67
+ }
68
+ // We use the Math.max to ensure we don’t go under 0 whether we increment or decrement
69
+ state.assetCounts.allAsset[assetType] = Math.max(0, currentCount + delta);
70
+ if (assetType === FtUserAssetType.BOOKMARKS && mapId) {
71
+ const currentMapCount = state.assetCounts.bookmarkByMap[mapId];
72
+ state.assetCounts.bookmarkByMap[mapId] = Math.max(0, currentMapCount + delta);
73
+ }
74
+ };
75
+ const updateAssetLabels = (state, asset) => {
76
+ const existingLabels = state.assetLabels.map((l) => l.title);
77
+ const newLabels = asset.labels
78
+ .filter((l) => !existingLabels.includes(l))
79
+ .map((l) => ({ title: l }));
80
+ state.assetLabels.push(...newLabels);
81
+ };
80
82
  export const ftUserAssetsStore = FtReduxStore.get({
81
83
  name: FtUserAssetsStoreName,
82
84
  reducers: reducers,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fluid-topics/ft-app-context",
3
- "version": "1.3.39",
3
+ "version": "1.3.40",
4
4
  "description": "Global application context for Fluid Topics integrations",
5
5
  "keywords": [
6
6
  "Lit"
@@ -19,11 +19,11 @@
19
19
  "url": "ssh://git@scm.mrs.antidot.net:2222/fluidtopics/ft-web-components.git"
20
20
  },
21
21
  "dependencies": {
22
- "@fluid-topics/ft-wc-utils": "1.3.39",
22
+ "@fluid-topics/ft-wc-utils": "1.3.40",
23
23
  "lit": "3.1.0"
24
24
  },
25
25
  "devDependencies": {
26
26
  "@fluid-topics/public-api": "1.0.107"
27
27
  },
28
- "gitHead": "a8e8e4b1852a7e32bc2c197ee75925b2b8a64306"
28
+ "gitHead": "81f9bbdb2550069fadb6b401f5bea60a453c2dc0"
29
29
  }