@reltio/components 1.4.2154 → 1.4.2156

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.
@@ -118,6 +118,7 @@ var setUp = function (_a) {
118
118
  return (React.createElement(MdmModuleProvider, { values: mdmValues },
119
119
  React.createElement(CollaborationContext.Provider, { value: {
120
120
  commentsMap: {},
121
+ totalCommentsCount: null,
121
122
  currentComment: currentComment,
122
123
  createComment: function () { return Promise.resolve(); },
123
124
  editComment: function () { return Promise.resolve(); },
@@ -126,6 +126,7 @@ describe('Comment tests', function () {
126
126
  };
127
127
  var defaultCollaborationContextValue = {
128
128
  commentsMap: defaultCommentsMap,
129
+ totalCommentsCount: null,
129
130
  currentComment: defaultCurrentComment,
130
131
  createComment: function () { return Promise.resolve(); },
131
132
  editComment: function () { return Promise.resolve(); },
@@ -65,7 +65,7 @@ describe('SendMessageArea tests', function () {
65
65
  };
66
66
  var createCollaborationContext = function (props) {
67
67
  if (props === void 0) { props = {}; }
68
- return __assign({ comments: [], commentsMap: null, currentComment: null, getComment: identity, getComments: function () { return new Promise(identity); }, getCommentsCount: identity, resolveThread: identity, reOpenThread: identity, clearCurrentComment: identity, deleteComment: identity, sending: false, loading: false, pageToken: null, deleteReply: identity, createReply: function () { return new Promise(function (res) { return res(); }); }, createComment: function () { return new Promise(function (res) { return res(); }); }, editComment: function () { return new Promise(function (res) { return res(); }); }, editReply: function () { return new Promise(function (res) { return res(); }); }, getCommentState: function () { return ({}); }, clearCommentState: function () { }, updateCommentState: function () { } }, props);
68
+ return __assign({ comments: [], commentsMap: null, totalCommentsCount: null, currentComment: null, getComment: identity, getComments: function () { return new Promise(identity); }, getCommentsCount: identity, resolveThread: identity, reOpenThread: identity, clearCurrentComment: identity, deleteComment: identity, sending: false, loading: false, pageToken: null, deleteReply: identity, createReply: function () { return new Promise(function (res) { return res(); }); }, createComment: function () { return new Promise(function (res) { return res(); }); }, editComment: function () { return new Promise(function (res) { return res(); }); }, editReply: function () { return new Promise(function (res) { return res(); }); }, getCommentState: function () { return ({}); }, clearCommentState: function () { }, updateCommentState: function () { } }, props);
69
69
  };
70
70
  var createProps = function (comment, extraProps) {
71
71
  if (extraProps === void 0) { extraProps = {}; }
@@ -123,6 +123,7 @@ var setUp = function (_a) {
123
123
  return (react_1.default.createElement(MdmModuleContext_1.MdmModuleProvider, { values: mdmValues },
124
124
  react_1.default.createElement(CollaborationContext_1.CollaborationContext.Provider, { value: {
125
125
  commentsMap: {},
126
+ totalCommentsCount: null,
126
127
  currentComment: currentComment,
127
128
  createComment: function () { return Promise.resolve(); },
128
129
  editComment: function () { return Promise.resolve(); },
@@ -131,6 +131,7 @@ describe('Comment tests', function () {
131
131
  };
132
132
  var defaultCollaborationContextValue = {
133
133
  commentsMap: defaultCommentsMap,
134
+ totalCommentsCount: null,
134
135
  currentComment: defaultCurrentComment,
135
136
  createComment: function () { return Promise.resolve(); },
136
137
  editComment: function () { return Promise.resolve(); },
@@ -70,7 +70,7 @@ describe('SendMessageArea tests', function () {
70
70
  };
71
71
  var createCollaborationContext = function (props) {
72
72
  if (props === void 0) { props = {}; }
73
- return __assign({ comments: [], commentsMap: null, currentComment: null, getComment: ramda_1.identity, getComments: function () { return new Promise(ramda_1.identity); }, getCommentsCount: ramda_1.identity, resolveThread: ramda_1.identity, reOpenThread: ramda_1.identity, clearCurrentComment: ramda_1.identity, deleteComment: ramda_1.identity, sending: false, loading: false, pageToken: null, deleteReply: ramda_1.identity, createReply: function () { return new Promise(function (res) { return res(); }); }, createComment: function () { return new Promise(function (res) { return res(); }); }, editComment: function () { return new Promise(function (res) { return res(); }); }, editReply: function () { return new Promise(function (res) { return res(); }); }, getCommentState: function () { return ({}); }, clearCommentState: function () { }, updateCommentState: function () { } }, props);
73
+ return __assign({ comments: [], commentsMap: null, totalCommentsCount: null, currentComment: null, getComment: ramda_1.identity, getComments: function () { return new Promise(ramda_1.identity); }, getCommentsCount: ramda_1.identity, resolveThread: ramda_1.identity, reOpenThread: ramda_1.identity, clearCurrentComment: ramda_1.identity, deleteComment: ramda_1.identity, sending: false, loading: false, pageToken: null, deleteReply: ramda_1.identity, createReply: function () { return new Promise(function (res) { return res(); }); }, createComment: function () { return new Promise(function (res) { return res(); }); }, editComment: function () { return new Promise(function (res) { return res(); }); }, editReply: function () { return new Promise(function (res) { return res(); }); }, getCommentState: function () { return ({}); }, clearCommentState: function () { }, updateCommentState: function () { } }, props);
74
74
  };
75
75
  var createProps = function (comment, extraProps) {
76
76
  if (extraProps === void 0) { extraProps = {}; }
@@ -22,6 +22,7 @@ export declare const InitialCollaborationContextValue: {
22
22
  pageToken: any;
23
23
  deleteReply: any;
24
24
  editReply: () => Promise<void>;
25
+ totalCommentsCount: any;
25
26
  };
26
27
  export declare const CollaborationContext: React.Context<{
27
28
  objectTypes?: CollaborationObjectTypes[];
@@ -67,6 +68,7 @@ export declare const CollaborationContext: React.Context<{
67
68
  sending: boolean;
68
69
  loading: boolean;
69
70
  pageToken: string;
71
+ totalCommentsCount: number | null;
70
72
  }>;
71
73
  export declare const CollaborationContextProvider: ({ collaboration, children }: {
72
74
  collaboration: any;
@@ -29,7 +29,8 @@ exports.InitialCollaborationContextValue = {
29
29
  loading: false,
30
30
  pageToken: null,
31
31
  deleteReply: ramda_1.identity,
32
- editReply: function () { return new Promise(ramda_1.identity); }
32
+ editReply: function () { return new Promise(ramda_1.identity); },
33
+ totalCommentsCount: null
33
34
  };
34
35
  exports.CollaborationContext = react_1.default.createContext(exports.InitialCollaborationContextValue);
35
36
  exports.CollaborationContext.displayName = 'CollaborationContext';
@@ -3,8 +3,9 @@ type Props = {
3
3
  objectIds?: string[];
4
4
  objectTypes?: CollaborationObjectTypes[];
5
5
  enabled?: boolean;
6
+ needsTotalCount?: boolean;
6
7
  };
7
- export declare const useCollaboration: ({ objectIds, objectTypes, enabled }: Props) => {
8
+ export declare const useCollaboration: ({ objectIds, objectTypes, enabled, needsTotalCount }: Props) => {
8
9
  clearCurrentComment: () => void;
9
10
  comments: Comment[];
10
11
  commentsMap: CommentsMap;
@@ -27,5 +28,6 @@ export declare const useCollaboration: ({ objectIds, objectTypes, enabled }: Pro
27
28
  sending: boolean;
28
29
  deleteReply: ({ uri, commentId, reply }: any) => void;
29
30
  editReply: ({ content, namedUsers, commentId, replyId }: any) => Promise<void>;
31
+ totalCommentsCount: number;
30
32
  };
31
33
  export {};
@@ -38,35 +38,57 @@ var getUpdatedReplies = function (comment, reply) {
38
38
  var defaultObjectIds = [];
39
39
  var catchErrorInRequest = function (e) { return console.warn('Collaboration error', e); };
40
40
  var useCollaboration = function (_a) {
41
- var _b = _a.objectIds, objectIds = _b === void 0 ? defaultObjectIds : _b, objectTypes = _a.objectTypes, _c = _a.enabled, enabled = _c === void 0 ? true : _c;
41
+ var _b = _a.objectIds, objectIds = _b === void 0 ? defaultObjectIds : _b, objectTypes = _a.objectTypes, _c = _a.enabled, enabled = _c === void 0 ? true : _c, _d = _a.needsTotalCount, needsTotalCount = _d === void 0 ? false : _d;
42
42
  var entity = (0, MdmModuleContext_1.useMdmEntity)();
43
43
  var tenant = (0, MdmModuleContext_1.useMdmTenant)();
44
44
  var collaborationPath = (0, MdmModuleContext_1.useMdmCollaborationPath)();
45
45
  var isCollaborationEnabled = (0, MdmModuleContext_1.useMdmIsCollaborationEnabled)() && enabled;
46
- var _d = (0, react_1.useState)(null), pageToken = _d[0], setPageToken = _d[1];
47
- var _e = (0, react_1.useState)([]), comments = _e[0], setComments = _e[1];
48
- var _f = (0, react_1.useState)(null), commentsMap = _f[0], setCommentsMap = _f[1];
49
- var _g = (0, react_1.useState)(null), currentComment = _g[0], setCurrentComment = _g[1];
50
- var _h = (0, react_1.useState)(false), sending = _h[0], setSending = _h[1];
51
- var _j = (0, react_1.useState)(false), loading = _j[0], setLoading = _j[1];
52
- var _k = (0, react_1.useState)({}), collaborationStateMap = _k[0], setCollaborationStateMap = _k[1];
46
+ var _e = (0, react_1.useState)(null), pageToken = _e[0], setPageToken = _e[1];
47
+ var _f = (0, react_1.useState)([]), comments = _f[0], setComments = _f[1];
48
+ var _g = (0, react_1.useState)(null), commentsMap = _g[0], setCommentsMap = _g[1];
49
+ var _h = (0, react_1.useState)(null), totalCommentsCount = _h[0], setTotalCommentsCount = _h[1];
50
+ var _j = (0, react_1.useState)(null), currentComment = _j[0], setCurrentComment = _j[1];
51
+ var _k = (0, react_1.useState)(false), sending = _k[0], setSending = _k[1];
52
+ var _l = (0, react_1.useState)(false), loading = _l[0], setLoading = _l[1];
53
+ var _m = (0, react_1.useState)({}), collaborationStateMap = _m[0], setCollaborationStateMap = _m[1];
53
54
  var addCommentsToCommentsMap = (0, react_1.useCallback)(function (commentsMap) {
54
55
  setCommentsMap(function (currentCommentsMap) { return (__assign(__assign({}, currentCommentsMap), commentsMap)); });
55
56
  }, []);
56
57
  var entityUri = entity === null || entity === void 0 ? void 0 : entity.uri;
58
+ var isCollaborationEnabledForEntity = isCollaborationEnabled && entityUri && !(0, mdm_sdk_1.isTempUri)(entityUri);
57
59
  var getCommentsCount = (0, react_1.useCallback)(function (objectIds, shouldResetCommentsMap) {
58
60
  if (shouldResetCommentsMap === void 0) { shouldResetCommentsMap = false; }
59
- if (isCollaborationEnabled && entityUri && !(0, mdm_sdk_1.isTempUri)(entityUri)) {
61
+ if (!isCollaborationEnabledForEntity) {
62
+ setCommentsMap(null);
63
+ return;
64
+ }
65
+ var onError = function (e) {
66
+ console.warn('Collaboration error', e);
67
+ setCommentsMap(null);
68
+ };
69
+ (0, ramda_1.pipe)(mdm_sdk_1.getCommentsCount, (0, ramda_1.andThen)((0, ramda_1.map)((0, ramda_1.props)(['objectId', 'comments']))), (0, ramda_1.andThen)(ramda_1.fromPairs), (0, ramda_1.andThen)(shouldResetCommentsMap ? setCommentsMap : addCommentsToCommentsMap), (0, ramda_1.otherwise)(onError))({ uris: objectIds, tenant: tenant, collaborationPath: collaborationPath });
70
+ }, [addCommentsToCommentsMap, collaborationPath, isCollaborationEnabledForEntity, tenant]);
71
+ var getTotalCommentsCount = (0, react_1.useCallback)(function () {
72
+ if (isCollaborationEnabledForEntity) {
60
73
  var onError = function (e) {
61
74
  console.warn('Collaboration error', e);
62
- setCommentsMap(null);
75
+ setTotalCommentsCount(null);
63
76
  };
64
- (0, ramda_1.pipe)(mdm_sdk_1.getCommentsCount, (0, ramda_1.andThen)((0, ramda_1.map)((0, ramda_1.props)(['objectId', 'comments']))), (0, ramda_1.andThen)(ramda_1.fromPairs), (0, ramda_1.andThen)(shouldResetCommentsMap ? setCommentsMap : addCommentsToCommentsMap), (0, ramda_1.otherwise)(onError))({ uris: objectIds, tenant: tenant, collaborationPath: collaborationPath });
77
+ (0, ramda_1.pipe)(mdm_sdk_1.getTotalCommentsCount, (0, ramda_1.andThen)((0, ramda_1.prop)('total')), (0, ramda_1.andThen)(setTotalCommentsCount), (0, ramda_1.otherwise)(onError))({
78
+ relatedObjectUri: entityUri,
79
+ collaborationPath: collaborationPath,
80
+ tenant: tenant
81
+ });
65
82
  }
66
83
  else {
67
- setCommentsMap(null);
84
+ setTotalCommentsCount(null);
68
85
  }
69
- }, [addCommentsToCommentsMap, collaborationPath, entityUri, isCollaborationEnabled, tenant]);
86
+ }, [isCollaborationEnabledForEntity, collaborationPath, entityUri, tenant]);
87
+ (0, react_1.useEffect)(function () {
88
+ if (needsTotalCount) {
89
+ getTotalCommentsCount();
90
+ }
91
+ }, [getTotalCommentsCount, needsTotalCount]);
70
92
  (0, react_1.useEffect)(function () {
71
93
  getCommentsCount(objectIds, true);
72
94
  }, [objectIds, getCommentsCount]);
@@ -98,21 +120,29 @@ var useCollaboration = function (_a) {
98
120
  return __assign(__assign({}, prevCommentsMap), (_a = {}, _a[uri] = __spreadArray(__spreadArray([], (prevCommentsMap[uri] || []), true), [{ commentId: comment.commentId, replies: 0, status: 'open' }], false), _a));
99
121
  });
100
122
  setComments(function (prevComments) { return __spreadArray([comment], prevComments, true); });
123
+ setTotalCommentsCount(function (prevCount) { return (prevCount || 0) + 1; });
101
124
  };
102
125
  var updateCommentInState = function (comment) {
103
126
  setCurrentComment(comment);
104
127
  setComments((0, ramda_1.map)((0, ramda_1.when)((0, ramda_1.propEq)('commentId', comment.commentId), (0, ramda_1.always)(comment))));
105
128
  };
106
129
  var deleteCommentFromState = function (commentId, uri) {
130
+ var repliesCountFromComments;
131
+ var repliesCountFromCommentsMap;
107
132
  setCurrentComment(null);
108
133
  setCommentsMap(function (prevCommentsMap) {
109
134
  var _a;
135
+ var _b, _c;
136
+ repliesCountFromCommentsMap = (_c = (_b = prevCommentsMap === null || prevCommentsMap === void 0 ? void 0 : prevCommentsMap[uri]) === null || _b === void 0 ? void 0 : _b.find(function (c) { return c.commentId === commentId; })) === null || _c === void 0 ? void 0 : _c.replies;
110
137
  var comments = prevCommentsMap[uri] || [];
111
138
  return __assign(__assign({}, prevCommentsMap), (_a = {}, _a[uri] = comments.filter(function (c) { return c.commentId !== commentId; }), _a));
112
139
  });
113
140
  setComments(function (prevComments) {
141
+ var _a;
142
+ repliesCountFromComments = (_a = prevComments.find(function (comment) { return comment.commentId === commentId; })) === null || _a === void 0 ? void 0 : _a.replies.length;
114
143
  return prevComments.filter(function (comment) { return comment.commentId !== commentId; });
115
144
  });
145
+ setTotalCommentsCount(function (prevCount) { var _a; return Math.max((prevCount || 0) - (1 + ((_a = repliesCountFromComments !== null && repliesCountFromComments !== void 0 ? repliesCountFromComments : repliesCountFromCommentsMap) !== null && _a !== void 0 ? _a : 0)), 0); });
116
146
  };
117
147
  var createComment = (0, react_1.useCallback)(function (_a) {
118
148
  var content = _a.content, namedUsers = _a.namedUsers, objectType = _a.objectType, uri = _a.uri, relatedObjectUris = _a.relatedObjectUris;
@@ -198,6 +228,12 @@ var useCollaboration = function (_a) {
198
228
  return comment;
199
229
  });
200
230
  });
231
+ if (isDeleteReply) {
232
+ setTotalCommentsCount(function (prevCount) { return Math.max((prevCount || 0) - 1, 0); });
233
+ }
234
+ else {
235
+ setTotalCommentsCount(function (prevCount) { return (prevCount || 0) + 1; });
236
+ }
201
237
  }, []);
202
238
  var deleteReply = (0, react_1.useCallback)(function (_a) {
203
239
  var uri = _a.uri, commentId = _a.commentId, reply = _a.reply;
@@ -292,7 +328,8 @@ var useCollaboration = function (_a) {
292
328
  resolveThread: resolveThread,
293
329
  sending: sending,
294
330
  deleteReply: deleteReply,
295
- editReply: editReply
331
+ editReply: editReply,
332
+ totalCommentsCount: totalCommentsCount
296
333
  }); }, [
297
334
  clearCurrentComment,
298
335
  comments,
@@ -315,7 +352,8 @@ var useCollaboration = function (_a) {
315
352
  resolveThread,
316
353
  sending,
317
354
  deleteReply,
318
- editReply
355
+ editReply,
356
+ totalCommentsCount
319
357
  ]);
320
358
  };
321
359
  exports.useCollaboration = useCollaboration;
@@ -64,7 +64,7 @@ var react_hooks_1 = require("@testing-library/react-hooks");
64
64
  var mdm_sdk_1 = require("@reltio/mdm-sdk");
65
65
  var MdmModuleContext_1 = require("../../contexts/MdmModuleContext");
66
66
  var useCollaboration_1 = require("./useCollaboration");
67
- jest.mock('@reltio/mdm-sdk', function () { return (__assign(__assign({}, jest.requireActual('@reltio/mdm-sdk')), { getCommentsCount: jest.fn(), getComment: jest.fn(), createComment: jest.fn(), createReply: jest.fn(), updateComment: jest.fn(), deleteComment: jest.fn(), getComments: jest.fn(), deleteReply: jest.fn(), updateReply: jest.fn() })); });
67
+ jest.mock('@reltio/mdm-sdk', function () { return (__assign(__assign({}, jest.requireActual('@reltio/mdm-sdk')), { getCommentsCount: jest.fn(), getTotalCommentsCount: jest.fn(), getComment: jest.fn(), createComment: jest.fn(), createReply: jest.fn(), updateComment: jest.fn(), deleteComment: jest.fn(), getComments: jest.fn(), deleteReply: jest.fn(), updateReply: jest.fn() })); });
68
68
  describe('useCollaboration behaviour', function () {
69
69
  var defaultMdmValues = {
70
70
  uiPath: 'https://localhost:3000/ui/alenat',
@@ -82,15 +82,20 @@ describe('useCollaboration behaviour', function () {
82
82
  return (0, react_hooks_1.renderHook)(useCollaboration_1.useCollaboration, { initialProps: props, wrapper: Providers });
83
83
  };
84
84
  beforeEach(function () {
85
- jest.clearAllMocks();
86
85
  mdm_sdk_1.getComment.mockResolvedValue({ commentId: 'commentId' });
87
86
  mdm_sdk_1.getCommentsCount.mockResolvedValue([{ objectId: 'uri1', comments: [] }]);
87
+ mdm_sdk_1.getTotalCommentsCount.mockResolvedValue({ total: 1 });
88
+ });
89
+ afterEach(function () {
90
+ jest.resetAllMocks();
88
91
  });
89
92
  it('should not call getCommentsCount if collaboration is not enabled', function () {
90
93
  var mdmValues = __assign(__assign({}, defaultMdmValues), { collaborationPath: undefined, isCollaborationEnabled: false });
91
94
  var result = setUp({ mdmValues: mdmValues }).result;
92
95
  expect(result.current.commentsMap).toBe(null);
96
+ expect(result.current.totalCommentsCount).toBe(null);
93
97
  expect(mdm_sdk_1.getCommentsCount).not.toHaveBeenCalled();
98
+ expect(mdm_sdk_1.getTotalCommentsCount).not.toHaveBeenCalled();
94
99
  });
95
100
  it('should call getCommentsCount if collaboration is enabled and uris is undefined and update commentsMap value', function () { return __awaiter(void 0, void 0, void 0, function () {
96
101
  var result;
@@ -98,12 +103,8 @@ describe('useCollaboration behaviour', function () {
98
103
  switch (_a.label) {
99
104
  case 0:
100
105
  mdm_sdk_1.getCommentsCount.mockResolvedValueOnce([]);
106
+ mdm_sdk_1.getTotalCommentsCount.mockResolvedValueOnce({ total: 1 });
101
107
  result = setUp().result;
102
- expect(mdm_sdk_1.getCommentsCount).toHaveBeenCalledWith({
103
- uris: [],
104
- tenant: 'alenat',
105
- collaborationPath: defaultMdmValues.collaborationPath
106
- });
107
108
  return [4 /*yield*/, (0, react_hooks_1.act)(function () { return __awaiter(void 0, void 0, void 0, function () {
108
109
  return __generator(this, function (_a) {
109
110
  switch (_a.label) {
@@ -116,6 +117,11 @@ describe('useCollaboration behaviour', function () {
116
117
  }); })];
117
118
  case 1:
118
119
  _a.sent();
120
+ expect(mdm_sdk_1.getCommentsCount).toHaveBeenCalledWith({
121
+ uris: [],
122
+ tenant: 'alenat',
123
+ collaborationPath: defaultMdmValues.collaborationPath
124
+ });
119
125
  expect(result.current.commentsMap).toEqual({});
120
126
  return [2 /*return*/];
121
127
  }
@@ -1235,4 +1241,90 @@ describe('useCollaboration behaviour', function () {
1235
1241
  }
1236
1242
  });
1237
1243
  }); });
1244
+ describe('totalCommentsCount behavior', function () {
1245
+ it('should not call getTotalCommentsCount if collaboration is not enabled', function () {
1246
+ var mdmValues = __assign(__assign({}, defaultMdmValues), { collaborationPath: undefined, isCollaborationEnabled: false });
1247
+ var result = setUp({ mdmValues: mdmValues }).result;
1248
+ expect(result.current.totalCommentsCount).toBe(null);
1249
+ expect(mdm_sdk_1.getTotalCommentsCount).not.toHaveBeenCalled();
1250
+ });
1251
+ it('should not call getTotalCommentsCount and update totalCommentsCount value if collaboration is enabled but needsTotalCount is false', function () { return __awaiter(void 0, void 0, void 0, function () {
1252
+ return __generator(this, function (_a) {
1253
+ switch (_a.label) {
1254
+ case 0:
1255
+ mdm_sdk_1.getTotalCommentsCount.mockResolvedValueOnce({ total: 15 });
1256
+ setUp();
1257
+ expect(mdm_sdk_1.getTotalCommentsCount).not.toHaveBeenCalled();
1258
+ return [4 /*yield*/, (0, react_hooks_1.act)(function () { return __awaiter(void 0, void 0, void 0, function () {
1259
+ return __generator(this, function (_a) {
1260
+ switch (_a.label) {
1261
+ case 0: return [4 /*yield*/, Promise.resolve()];
1262
+ case 1:
1263
+ _a.sent();
1264
+ return [2 /*return*/];
1265
+ }
1266
+ });
1267
+ }); })];
1268
+ case 1:
1269
+ _a.sent();
1270
+ expect(mdm_sdk_1.getTotalCommentsCount).not.toHaveBeenCalled();
1271
+ return [2 /*return*/];
1272
+ }
1273
+ });
1274
+ }); });
1275
+ it('should call getTotalCommentsCount and update totalCommentsCount value if collaboration is enabled and needsTotalCount is true', function () { return __awaiter(void 0, void 0, void 0, function () {
1276
+ var result;
1277
+ return __generator(this, function (_a) {
1278
+ switch (_a.label) {
1279
+ case 0:
1280
+ mdm_sdk_1.getTotalCommentsCount.mockResolvedValueOnce({ total: 15 });
1281
+ result = setUp({ props: { needsTotalCount: true } }).result;
1282
+ expect(mdm_sdk_1.getTotalCommentsCount).toHaveBeenCalledWith({
1283
+ relatedObjectUri: '1',
1284
+ collaborationPath: '123',
1285
+ tenant: 'alenat'
1286
+ });
1287
+ return [4 /*yield*/, (0, react_hooks_1.act)(function () { return __awaiter(void 0, void 0, void 0, function () {
1288
+ return __generator(this, function (_a) {
1289
+ switch (_a.label) {
1290
+ case 0: return [4 /*yield*/, Promise.resolve()];
1291
+ case 1:
1292
+ _a.sent();
1293
+ return [2 /*return*/];
1294
+ }
1295
+ });
1296
+ }); })];
1297
+ case 1:
1298
+ _a.sent();
1299
+ expect(result.current.totalCommentsCount).toBe(15);
1300
+ return [2 /*return*/];
1301
+ }
1302
+ });
1303
+ }); });
1304
+ it('should set totalCommentsCount as null in case of unsuccessful request', function () { return __awaiter(void 0, void 0, void 0, function () {
1305
+ var result;
1306
+ return __generator(this, function (_a) {
1307
+ switch (_a.label) {
1308
+ case 0:
1309
+ mdm_sdk_1.getTotalCommentsCount.mockRejectedValueOnce(new Error('Network error'));
1310
+ result = setUp({ props: { needsTotalCount: true } }).result;
1311
+ expect(mdm_sdk_1.getTotalCommentsCount).toHaveBeenCalled();
1312
+ return [4 /*yield*/, (0, react_hooks_1.act)(function () { return __awaiter(void 0, void 0, void 0, function () {
1313
+ return __generator(this, function (_a) {
1314
+ switch (_a.label) {
1315
+ case 0: return [4 /*yield*/, Promise.resolve()];
1316
+ case 1:
1317
+ _a.sent();
1318
+ return [2 /*return*/];
1319
+ }
1320
+ });
1321
+ }); })];
1322
+ case 1:
1323
+ _a.sent();
1324
+ expect(result.current.totalCommentsCount).toBe(null);
1325
+ return [2 /*return*/];
1326
+ }
1327
+ });
1328
+ }); });
1329
+ });
1238
1330
  });
@@ -22,6 +22,7 @@ export declare const InitialCollaborationContextValue: {
22
22
  pageToken: any;
23
23
  deleteReply: any;
24
24
  editReply: () => Promise<void>;
25
+ totalCommentsCount: any;
25
26
  };
26
27
  export declare const CollaborationContext: React.Context<{
27
28
  objectTypes?: CollaborationObjectTypes[];
@@ -67,6 +68,7 @@ export declare const CollaborationContext: React.Context<{
67
68
  sending: boolean;
68
69
  loading: boolean;
69
70
  pageToken: string;
71
+ totalCommentsCount: number | null;
70
72
  }>;
71
73
  export declare const CollaborationContextProvider: ({ collaboration, children }: {
72
74
  collaboration: any;
@@ -23,7 +23,8 @@ export var InitialCollaborationContextValue = {
23
23
  loading: false,
24
24
  pageToken: null,
25
25
  deleteReply: identity,
26
- editReply: function () { return new Promise(identity); }
26
+ editReply: function () { return new Promise(identity); },
27
+ totalCommentsCount: null
27
28
  };
28
29
  export var CollaborationContext = React.createContext(InitialCollaborationContextValue);
29
30
  CollaborationContext.displayName = 'CollaborationContext';
@@ -3,8 +3,9 @@ type Props = {
3
3
  objectIds?: string[];
4
4
  objectTypes?: CollaborationObjectTypes[];
5
5
  enabled?: boolean;
6
+ needsTotalCount?: boolean;
6
7
  };
7
- export declare const useCollaboration: ({ objectIds, objectTypes, enabled }: Props) => {
8
+ export declare const useCollaboration: ({ objectIds, objectTypes, enabled, needsTotalCount }: Props) => {
8
9
  clearCurrentComment: () => void;
9
10
  comments: Comment[];
10
11
  commentsMap: CommentsMap;
@@ -27,5 +28,6 @@ export declare const useCollaboration: ({ objectIds, objectTypes, enabled }: Pro
27
28
  sending: boolean;
28
29
  deleteReply: ({ uri, commentId, reply }: any) => void;
29
30
  editReply: ({ content, namedUsers, commentId, replyId }: any) => Promise<void>;
31
+ totalCommentsCount: number;
30
32
  };
31
33
  export {};
@@ -20,7 +20,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
20
20
  };
21
21
  import { useCallback, useEffect, useState, useMemo } from 'react';
22
22
  import { andThen, fromPairs, map, otherwise, pipe, props, uniqBy, prop, propEq, when, always, assocPath, dissocPath, path, pathOr, mergeRight } from 'ramda';
23
- import { createComment as createCommentRequest, createReply as createReplyRequest, getComment as getCommentRequest, getCommentsCount as getCommentsCountRequest, updateComment as updateCommentRequest, updateReply as updateReplyRequest, deleteComment as deleteCommentRequest, deleteReply as deleteReplyRequest, getComments as getCommentsRequest, isTempUri } from '@reltio/mdm-sdk';
23
+ import { createComment as createCommentRequest, createReply as createReplyRequest, getComment as getCommentRequest, getCommentsCount as getCommentsCountRequest, getTotalCommentsCount as getTotalCommentsCountRequest, updateComment as updateCommentRequest, updateReply as updateReplyRequest, deleteComment as deleteCommentRequest, deleteReply as deleteReplyRequest, getComments as getCommentsRequest, isTempUri } from '@reltio/mdm-sdk';
24
24
  import { useMdmCollaborationPath, useMdmEntity, useMdmIsCollaborationEnabled, useMdmTenant } from '../../contexts/MdmModuleContext';
25
25
  var filterReplies = function (replyId, replies) { return replies.filter(function (reply) { return reply.replyId !== replyId; }); };
26
26
  var getUpdatedReplies = function (comment, reply) {
@@ -35,35 +35,57 @@ var getUpdatedReplies = function (comment, reply) {
35
35
  var defaultObjectIds = [];
36
36
  var catchErrorInRequest = function (e) { return console.warn('Collaboration error', e); };
37
37
  export var useCollaboration = function (_a) {
38
- var _b = _a.objectIds, objectIds = _b === void 0 ? defaultObjectIds : _b, objectTypes = _a.objectTypes, _c = _a.enabled, enabled = _c === void 0 ? true : _c;
38
+ var _b = _a.objectIds, objectIds = _b === void 0 ? defaultObjectIds : _b, objectTypes = _a.objectTypes, _c = _a.enabled, enabled = _c === void 0 ? true : _c, _d = _a.needsTotalCount, needsTotalCount = _d === void 0 ? false : _d;
39
39
  var entity = useMdmEntity();
40
40
  var tenant = useMdmTenant();
41
41
  var collaborationPath = useMdmCollaborationPath();
42
42
  var isCollaborationEnabled = useMdmIsCollaborationEnabled() && enabled;
43
- var _d = useState(null), pageToken = _d[0], setPageToken = _d[1];
44
- var _e = useState([]), comments = _e[0], setComments = _e[1];
45
- var _f = useState(null), commentsMap = _f[0], setCommentsMap = _f[1];
46
- var _g = useState(null), currentComment = _g[0], setCurrentComment = _g[1];
47
- var _h = useState(false), sending = _h[0], setSending = _h[1];
48
- var _j = useState(false), loading = _j[0], setLoading = _j[1];
49
- var _k = useState({}), collaborationStateMap = _k[0], setCollaborationStateMap = _k[1];
43
+ var _e = useState(null), pageToken = _e[0], setPageToken = _e[1];
44
+ var _f = useState([]), comments = _f[0], setComments = _f[1];
45
+ var _g = useState(null), commentsMap = _g[0], setCommentsMap = _g[1];
46
+ var _h = useState(null), totalCommentsCount = _h[0], setTotalCommentsCount = _h[1];
47
+ var _j = useState(null), currentComment = _j[0], setCurrentComment = _j[1];
48
+ var _k = useState(false), sending = _k[0], setSending = _k[1];
49
+ var _l = useState(false), loading = _l[0], setLoading = _l[1];
50
+ var _m = useState({}), collaborationStateMap = _m[0], setCollaborationStateMap = _m[1];
50
51
  var addCommentsToCommentsMap = useCallback(function (commentsMap) {
51
52
  setCommentsMap(function (currentCommentsMap) { return (__assign(__assign({}, currentCommentsMap), commentsMap)); });
52
53
  }, []);
53
54
  var entityUri = entity === null || entity === void 0 ? void 0 : entity.uri;
55
+ var isCollaborationEnabledForEntity = isCollaborationEnabled && entityUri && !isTempUri(entityUri);
54
56
  var getCommentsCount = useCallback(function (objectIds, shouldResetCommentsMap) {
55
57
  if (shouldResetCommentsMap === void 0) { shouldResetCommentsMap = false; }
56
- if (isCollaborationEnabled && entityUri && !isTempUri(entityUri)) {
58
+ if (!isCollaborationEnabledForEntity) {
59
+ setCommentsMap(null);
60
+ return;
61
+ }
62
+ var onError = function (e) {
63
+ console.warn('Collaboration error', e);
64
+ setCommentsMap(null);
65
+ };
66
+ pipe(getCommentsCountRequest, andThen(map(props(['objectId', 'comments']))), andThen(fromPairs), andThen(shouldResetCommentsMap ? setCommentsMap : addCommentsToCommentsMap), otherwise(onError))({ uris: objectIds, tenant: tenant, collaborationPath: collaborationPath });
67
+ }, [addCommentsToCommentsMap, collaborationPath, isCollaborationEnabledForEntity, tenant]);
68
+ var getTotalCommentsCount = useCallback(function () {
69
+ if (isCollaborationEnabledForEntity) {
57
70
  var onError = function (e) {
58
71
  console.warn('Collaboration error', e);
59
- setCommentsMap(null);
72
+ setTotalCommentsCount(null);
60
73
  };
61
- pipe(getCommentsCountRequest, andThen(map(props(['objectId', 'comments']))), andThen(fromPairs), andThen(shouldResetCommentsMap ? setCommentsMap : addCommentsToCommentsMap), otherwise(onError))({ uris: objectIds, tenant: tenant, collaborationPath: collaborationPath });
74
+ pipe(getTotalCommentsCountRequest, andThen(prop('total')), andThen(setTotalCommentsCount), otherwise(onError))({
75
+ relatedObjectUri: entityUri,
76
+ collaborationPath: collaborationPath,
77
+ tenant: tenant
78
+ });
62
79
  }
63
80
  else {
64
- setCommentsMap(null);
81
+ setTotalCommentsCount(null);
65
82
  }
66
- }, [addCommentsToCommentsMap, collaborationPath, entityUri, isCollaborationEnabled, tenant]);
83
+ }, [isCollaborationEnabledForEntity, collaborationPath, entityUri, tenant]);
84
+ useEffect(function () {
85
+ if (needsTotalCount) {
86
+ getTotalCommentsCount();
87
+ }
88
+ }, [getTotalCommentsCount, needsTotalCount]);
67
89
  useEffect(function () {
68
90
  getCommentsCount(objectIds, true);
69
91
  }, [objectIds, getCommentsCount]);
@@ -95,21 +117,29 @@ export var useCollaboration = function (_a) {
95
117
  return __assign(__assign({}, prevCommentsMap), (_a = {}, _a[uri] = __spreadArray(__spreadArray([], (prevCommentsMap[uri] || []), true), [{ commentId: comment.commentId, replies: 0, status: 'open' }], false), _a));
96
118
  });
97
119
  setComments(function (prevComments) { return __spreadArray([comment], prevComments, true); });
120
+ setTotalCommentsCount(function (prevCount) { return (prevCount || 0) + 1; });
98
121
  };
99
122
  var updateCommentInState = function (comment) {
100
123
  setCurrentComment(comment);
101
124
  setComments(map(when(propEq('commentId', comment.commentId), always(comment))));
102
125
  };
103
126
  var deleteCommentFromState = function (commentId, uri) {
127
+ var repliesCountFromComments;
128
+ var repliesCountFromCommentsMap;
104
129
  setCurrentComment(null);
105
130
  setCommentsMap(function (prevCommentsMap) {
106
131
  var _a;
132
+ var _b, _c;
133
+ repliesCountFromCommentsMap = (_c = (_b = prevCommentsMap === null || prevCommentsMap === void 0 ? void 0 : prevCommentsMap[uri]) === null || _b === void 0 ? void 0 : _b.find(function (c) { return c.commentId === commentId; })) === null || _c === void 0 ? void 0 : _c.replies;
107
134
  var comments = prevCommentsMap[uri] || [];
108
135
  return __assign(__assign({}, prevCommentsMap), (_a = {}, _a[uri] = comments.filter(function (c) { return c.commentId !== commentId; }), _a));
109
136
  });
110
137
  setComments(function (prevComments) {
138
+ var _a;
139
+ repliesCountFromComments = (_a = prevComments.find(function (comment) { return comment.commentId === commentId; })) === null || _a === void 0 ? void 0 : _a.replies.length;
111
140
  return prevComments.filter(function (comment) { return comment.commentId !== commentId; });
112
141
  });
142
+ setTotalCommentsCount(function (prevCount) { var _a; return Math.max((prevCount || 0) - (1 + ((_a = repliesCountFromComments !== null && repliesCountFromComments !== void 0 ? repliesCountFromComments : repliesCountFromCommentsMap) !== null && _a !== void 0 ? _a : 0)), 0); });
113
143
  };
114
144
  var createComment = useCallback(function (_a) {
115
145
  var content = _a.content, namedUsers = _a.namedUsers, objectType = _a.objectType, uri = _a.uri, relatedObjectUris = _a.relatedObjectUris;
@@ -195,6 +225,12 @@ export var useCollaboration = function (_a) {
195
225
  return comment;
196
226
  });
197
227
  });
228
+ if (isDeleteReply) {
229
+ setTotalCommentsCount(function (prevCount) { return Math.max((prevCount || 0) - 1, 0); });
230
+ }
231
+ else {
232
+ setTotalCommentsCount(function (prevCount) { return (prevCount || 0) + 1; });
233
+ }
198
234
  }, []);
199
235
  var deleteReply = useCallback(function (_a) {
200
236
  var uri = _a.uri, commentId = _a.commentId, reply = _a.reply;
@@ -289,7 +325,8 @@ export var useCollaboration = function (_a) {
289
325
  resolveThread: resolveThread,
290
326
  sending: sending,
291
327
  deleteReply: deleteReply,
292
- editReply: editReply
328
+ editReply: editReply,
329
+ totalCommentsCount: totalCommentsCount
293
330
  }); }, [
294
331
  clearCurrentComment,
295
332
  comments,
@@ -312,6 +349,7 @@ export var useCollaboration = function (_a) {
312
349
  resolveThread,
313
350
  sending,
314
351
  deleteReply,
315
- editReply
352
+ editReply,
353
+ totalCommentsCount
316
354
  ]);
317
355
  };
@@ -56,10 +56,10 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
56
56
  };
57
57
  import React from 'react';
58
58
  import { renderHook, act } from '@testing-library/react-hooks';
59
- import { getCommentsCount, getComment, createComment, createReply, deleteComment, updateComment, getComments, deleteReply, updateReply } from '@reltio/mdm-sdk';
59
+ import { getCommentsCount, getTotalCommentsCount, getComment, createComment, createReply, deleteComment, updateComment, getComments, deleteReply, updateReply } from '@reltio/mdm-sdk';
60
60
  import { MdmModuleProvider } from '../../contexts/MdmModuleContext';
61
61
  import { useCollaboration } from './useCollaboration';
62
- jest.mock('@reltio/mdm-sdk', function () { return (__assign(__assign({}, jest.requireActual('@reltio/mdm-sdk')), { getCommentsCount: jest.fn(), getComment: jest.fn(), createComment: jest.fn(), createReply: jest.fn(), updateComment: jest.fn(), deleteComment: jest.fn(), getComments: jest.fn(), deleteReply: jest.fn(), updateReply: jest.fn() })); });
62
+ jest.mock('@reltio/mdm-sdk', function () { return (__assign(__assign({}, jest.requireActual('@reltio/mdm-sdk')), { getCommentsCount: jest.fn(), getTotalCommentsCount: jest.fn(), getComment: jest.fn(), createComment: jest.fn(), createReply: jest.fn(), updateComment: jest.fn(), deleteComment: jest.fn(), getComments: jest.fn(), deleteReply: jest.fn(), updateReply: jest.fn() })); });
63
63
  describe('useCollaboration behaviour', function () {
64
64
  var defaultMdmValues = {
65
65
  uiPath: 'https://localhost:3000/ui/alenat',
@@ -77,15 +77,20 @@ describe('useCollaboration behaviour', function () {
77
77
  return renderHook(useCollaboration, { initialProps: props, wrapper: Providers });
78
78
  };
79
79
  beforeEach(function () {
80
- jest.clearAllMocks();
81
80
  getComment.mockResolvedValue({ commentId: 'commentId' });
82
81
  getCommentsCount.mockResolvedValue([{ objectId: 'uri1', comments: [] }]);
82
+ getTotalCommentsCount.mockResolvedValue({ total: 1 });
83
+ });
84
+ afterEach(function () {
85
+ jest.resetAllMocks();
83
86
  });
84
87
  it('should not call getCommentsCount if collaboration is not enabled', function () {
85
88
  var mdmValues = __assign(__assign({}, defaultMdmValues), { collaborationPath: undefined, isCollaborationEnabled: false });
86
89
  var result = setUp({ mdmValues: mdmValues }).result;
87
90
  expect(result.current.commentsMap).toBe(null);
91
+ expect(result.current.totalCommentsCount).toBe(null);
88
92
  expect(getCommentsCount).not.toHaveBeenCalled();
93
+ expect(getTotalCommentsCount).not.toHaveBeenCalled();
89
94
  });
90
95
  it('should call getCommentsCount if collaboration is enabled and uris is undefined and update commentsMap value', function () { return __awaiter(void 0, void 0, void 0, function () {
91
96
  var result;
@@ -93,12 +98,8 @@ describe('useCollaboration behaviour', function () {
93
98
  switch (_a.label) {
94
99
  case 0:
95
100
  getCommentsCount.mockResolvedValueOnce([]);
101
+ getTotalCommentsCount.mockResolvedValueOnce({ total: 1 });
96
102
  result = setUp().result;
97
- expect(getCommentsCount).toHaveBeenCalledWith({
98
- uris: [],
99
- tenant: 'alenat',
100
- collaborationPath: defaultMdmValues.collaborationPath
101
- });
102
103
  return [4 /*yield*/, act(function () { return __awaiter(void 0, void 0, void 0, function () {
103
104
  return __generator(this, function (_a) {
104
105
  switch (_a.label) {
@@ -111,6 +112,11 @@ describe('useCollaboration behaviour', function () {
111
112
  }); })];
112
113
  case 1:
113
114
  _a.sent();
115
+ expect(getCommentsCount).toHaveBeenCalledWith({
116
+ uris: [],
117
+ tenant: 'alenat',
118
+ collaborationPath: defaultMdmValues.collaborationPath
119
+ });
114
120
  expect(result.current.commentsMap).toEqual({});
115
121
  return [2 /*return*/];
116
122
  }
@@ -1230,4 +1236,90 @@ describe('useCollaboration behaviour', function () {
1230
1236
  }
1231
1237
  });
1232
1238
  }); });
1239
+ describe('totalCommentsCount behavior', function () {
1240
+ it('should not call getTotalCommentsCount if collaboration is not enabled', function () {
1241
+ var mdmValues = __assign(__assign({}, defaultMdmValues), { collaborationPath: undefined, isCollaborationEnabled: false });
1242
+ var result = setUp({ mdmValues: mdmValues }).result;
1243
+ expect(result.current.totalCommentsCount).toBe(null);
1244
+ expect(getTotalCommentsCount).not.toHaveBeenCalled();
1245
+ });
1246
+ it('should not call getTotalCommentsCount and update totalCommentsCount value if collaboration is enabled but needsTotalCount is false', function () { return __awaiter(void 0, void 0, void 0, function () {
1247
+ return __generator(this, function (_a) {
1248
+ switch (_a.label) {
1249
+ case 0:
1250
+ getTotalCommentsCount.mockResolvedValueOnce({ total: 15 });
1251
+ setUp();
1252
+ expect(getTotalCommentsCount).not.toHaveBeenCalled();
1253
+ return [4 /*yield*/, act(function () { return __awaiter(void 0, void 0, void 0, function () {
1254
+ return __generator(this, function (_a) {
1255
+ switch (_a.label) {
1256
+ case 0: return [4 /*yield*/, Promise.resolve()];
1257
+ case 1:
1258
+ _a.sent();
1259
+ return [2 /*return*/];
1260
+ }
1261
+ });
1262
+ }); })];
1263
+ case 1:
1264
+ _a.sent();
1265
+ expect(getTotalCommentsCount).not.toHaveBeenCalled();
1266
+ return [2 /*return*/];
1267
+ }
1268
+ });
1269
+ }); });
1270
+ it('should call getTotalCommentsCount and update totalCommentsCount value if collaboration is enabled and needsTotalCount is true', function () { return __awaiter(void 0, void 0, void 0, function () {
1271
+ var result;
1272
+ return __generator(this, function (_a) {
1273
+ switch (_a.label) {
1274
+ case 0:
1275
+ getTotalCommentsCount.mockResolvedValueOnce({ total: 15 });
1276
+ result = setUp({ props: { needsTotalCount: true } }).result;
1277
+ expect(getTotalCommentsCount).toHaveBeenCalledWith({
1278
+ relatedObjectUri: '1',
1279
+ collaborationPath: '123',
1280
+ tenant: 'alenat'
1281
+ });
1282
+ return [4 /*yield*/, act(function () { return __awaiter(void 0, void 0, void 0, function () {
1283
+ return __generator(this, function (_a) {
1284
+ switch (_a.label) {
1285
+ case 0: return [4 /*yield*/, Promise.resolve()];
1286
+ case 1:
1287
+ _a.sent();
1288
+ return [2 /*return*/];
1289
+ }
1290
+ });
1291
+ }); })];
1292
+ case 1:
1293
+ _a.sent();
1294
+ expect(result.current.totalCommentsCount).toBe(15);
1295
+ return [2 /*return*/];
1296
+ }
1297
+ });
1298
+ }); });
1299
+ it('should set totalCommentsCount as null in case of unsuccessful request', function () { return __awaiter(void 0, void 0, void 0, function () {
1300
+ var result;
1301
+ return __generator(this, function (_a) {
1302
+ switch (_a.label) {
1303
+ case 0:
1304
+ getTotalCommentsCount.mockRejectedValueOnce(new Error('Network error'));
1305
+ result = setUp({ props: { needsTotalCount: true } }).result;
1306
+ expect(getTotalCommentsCount).toHaveBeenCalled();
1307
+ return [4 /*yield*/, act(function () { return __awaiter(void 0, void 0, void 0, function () {
1308
+ return __generator(this, function (_a) {
1309
+ switch (_a.label) {
1310
+ case 0: return [4 /*yield*/, Promise.resolve()];
1311
+ case 1:
1312
+ _a.sent();
1313
+ return [2 /*return*/];
1314
+ }
1315
+ });
1316
+ }); })];
1317
+ case 1:
1318
+ _a.sent();
1319
+ expect(result.current.totalCommentsCount).toBe(null);
1320
+ return [2 /*return*/];
1321
+ }
1322
+ });
1323
+ }); });
1324
+ });
1233
1325
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reltio/components",
3
- "version": "1.4.2154",
3
+ "version": "1.4.2156",
4
4
  "license": "SEE LICENSE IN LICENSE FILE",
5
5
  "main": "./cjs/index.js",
6
6
  "module": "./index.js",
@@ -11,7 +11,7 @@
11
11
  "@fluentui/react-context-selector": "^9.1.26",
12
12
  "@googlemaps/markerclusterer": "^2.5.3",
13
13
  "@react-sigma/core": "3.4.0",
14
- "@reltio/mdm-sdk": "^1.4.1975",
14
+ "@reltio/mdm-sdk": "^1.4.1977",
15
15
  "@vis.gl/react-google-maps": "^1.3.0",
16
16
  "d3-cloud": "^1.2.5",
17
17
  "d3-geo": "^2.0.1",