@quesmed/types 2.2.91 → 2.2.93

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.
@@ -1,6 +1,6 @@
1
1
  import { gql } from '@apollo/client';
2
2
  import { EQuestionLike, EQuestionType, } from '../../../models';
3
- import { MARKSHEET, } from '../../query/restricted';
3
+ import { MARKSHEET, QUESTION, } from '../../query/restricted';
4
4
  export const getLikeData = (like, item) => {
5
5
  let { likes = 0, dislikes = 0, isLikedByMe } = item;
6
6
  if (isLikedByMe === EQuestionLike.LIKE) {
@@ -38,6 +38,155 @@ export const getQuestionTypeName = (typeId) => {
38
38
  return 'QuestionSBA';
39
39
  }
40
40
  };
41
+ export const updatedCommentsOnAdd = (newComment, comments, parentId) => {
42
+ if (parentId) {
43
+ const commentIndex = comments.findIndex(({ id }) => Number(id) === parentId);
44
+ const { replies = [], ...commentRest } = comments[commentIndex];
45
+ return [
46
+ ...comments.slice(0, commentIndex),
47
+ { ...commentRest, replies: [...replies, newComment] },
48
+ ...comments.slice(commentIndex + 1),
49
+ ];
50
+ }
51
+ return [...comments, newComment];
52
+ };
53
+ export const updateCommentsOnRemove = (comments, commentId, parentId) => {
54
+ if (parentId) {
55
+ const commentIndex = comments.findIndex(({ id }) => Number(id) === Number(parentId));
56
+ const { replies = [], ...commentRest } = comments[commentIndex];
57
+ return [
58
+ ...comments.slice(0, commentIndex),
59
+ {
60
+ ...commentRest,
61
+ replies: replies.filter(({ id }) => Number(id) !== Number(commentId)),
62
+ },
63
+ ...comments.slice(commentIndex + 1),
64
+ ];
65
+ }
66
+ return comments.filter(({ id }) => Number(id) !== Number(commentId));
67
+ };
68
+ export const updatedCommentsInMarksheetQuestion = (cache, questionComments, marksheetId, questionId, parentId) => {
69
+ const marksheetData = cache.readQuery({
70
+ variables: { id: marksheetId },
71
+ query: MARKSHEET,
72
+ });
73
+ if (marksheetData) {
74
+ const { marksheet } = marksheetData.restricted || {};
75
+ const { marks, ...marksheetRest } = marksheet;
76
+ const index = marks.findIndex(({ question }) => Number(question.id) === Number(questionId));
77
+ const { question, ...markRest } = marks[index];
78
+ const { comments = [], ...questionRest } = question;
79
+ cache.writeQuery({
80
+ query: MARKSHEET,
81
+ variables: { id: marksheetId },
82
+ data: {
83
+ ...marksheetData,
84
+ restricted: {
85
+ ...marksheetData.restricted,
86
+ marksheet: {
87
+ ...marksheetRest,
88
+ marks: [
89
+ ...marks.slice(0, index),
90
+ {
91
+ ...markRest,
92
+ question: {
93
+ ...questionRest,
94
+ comments: updatedCommentsOnAdd(questionComments, comments, parentId),
95
+ },
96
+ },
97
+ ...marks.slice(index + 1),
98
+ ],
99
+ },
100
+ },
101
+ },
102
+ });
103
+ }
104
+ };
105
+ export const updatedCommentsInQuestion = (cache, questionComments, questionId, parentId) => {
106
+ const questionData = cache.readQuery({
107
+ variables: { id: questionId },
108
+ query: QUESTION,
109
+ });
110
+ if (questionData) {
111
+ const { question } = questionData.restricted || {};
112
+ const { comments = [], ...questionRest } = question;
113
+ cache.writeQuery({
114
+ query: QUESTION,
115
+ variables: { id: questionId },
116
+ data: {
117
+ ...questionData,
118
+ restricted: {
119
+ ...questionData.restricted,
120
+ question: {
121
+ ...questionRest,
122
+ comments: updatedCommentsOnAdd(questionComments, comments, parentId),
123
+ },
124
+ },
125
+ },
126
+ });
127
+ }
128
+ };
129
+ export const updatedRemoveCommentsInMarksheetQuestion = (cache, marksheetId, questionId, commentId, parentId) => {
130
+ const marksheetData = cache.readQuery({
131
+ variables: { id: marksheetId },
132
+ query: MARKSHEET,
133
+ });
134
+ if (marksheetData) {
135
+ const { marksheet } = marksheetData.restricted || {};
136
+ const { marks, ...marksheetRest } = marksheet;
137
+ const index = marks.findIndex(({ question }) => Number(question.id) === Number(questionId));
138
+ const { question, ...markRest } = marks[index];
139
+ const { comments = [], ...questionRest } = question;
140
+ cache.writeQuery({
141
+ query: MARKSHEET,
142
+ variables: { id: marksheetId },
143
+ data: {
144
+ ...marksheetData,
145
+ restricted: {
146
+ ...marksheetData.restricted,
147
+ marksheet: {
148
+ ...marksheetRest,
149
+ marks: [
150
+ ...marks.slice(0, index),
151
+ {
152
+ ...markRest,
153
+ question: {
154
+ ...questionRest,
155
+ comments: updateCommentsOnRemove(comments, commentId, parentId),
156
+ },
157
+ },
158
+ ...marks.slice(index + 1),
159
+ ],
160
+ },
161
+ },
162
+ },
163
+ });
164
+ }
165
+ };
166
+ export const updatedRemoveCommentsInQuestion = (cache, questionId, commentId, parentId) => {
167
+ const questionData = cache.readQuery({
168
+ variables: { id: questionId },
169
+ query: QUESTION,
170
+ });
171
+ if (questionData) {
172
+ const { question } = questionData.restricted || {};
173
+ const { comments = [], ...questionRest } = question;
174
+ cache.writeQuery({
175
+ query: QUESTION,
176
+ variables: { id: questionId },
177
+ data: {
178
+ ...questionData,
179
+ restricted: {
180
+ ...questionData.restricted,
181
+ question: {
182
+ ...questionRest,
183
+ comments: updateCommentsOnRemove(comments, commentId, parentId),
184
+ },
185
+ },
186
+ },
187
+ });
188
+ }
189
+ };
41
190
  export const QUESTION_LIKE = gql `
42
191
  mutation QuestionLike($questionId: Int!, $like: Int!) {
43
192
  restricted {
@@ -51,16 +200,25 @@ export const QUESTION_LIKE = gql `
51
200
  }
52
201
  }
53
202
  `;
54
- export const optimisticQuestionLike = (marksheetId, client, input) => {
203
+ export const optimisticQuestionLike = (client, input, marksheetId) => {
55
204
  const { questionId, like } = input;
56
- const data = client.readQuery({
57
- variables: { id: marksheetId },
58
- query: MARKSHEET,
59
- });
60
- const { marksheet } = data?.restricted || {};
61
- const { marks = [] } = marksheet || {};
62
- const { question } = marks.find(({ question }) => Number(question.id) === Number(questionId)) ||
63
- {};
205
+ let question;
206
+ if (marksheetId) {
207
+ const data = client.readQuery({
208
+ variables: { id: marksheetId },
209
+ query: MARKSHEET,
210
+ });
211
+ const { marksheet } = data?.restricted || {};
212
+ const { marks = [] } = marksheet || {};
213
+ question = marks.find(({ question }) => Number(question.id) === Number(questionId))?.question;
214
+ }
215
+ else {
216
+ const data = client.readQuery({
217
+ variables: { id: Number(questionId) },
218
+ query: QUESTION,
219
+ });
220
+ question = data?.restricted.question;
221
+ }
64
222
  if (question) {
65
223
  const { typeId } = question;
66
224
  return {
@@ -125,50 +283,11 @@ export const updateQuestionComments = (marksheetId) => (cache, result, options)
125
283
  }
126
284
  const { questionId, parentId } = variables;
127
285
  try {
128
- const prevData = cache.readQuery({
129
- variables: { id: marksheetId },
130
- query: MARKSHEET,
131
- });
132
- if (prevData) {
133
- const { marksheet } = prevData.restricted || {};
134
- const { marks, ...marksheetRest } = marksheet;
135
- const index = marks.findIndex(({ question }) => Number(question.id) === Number(questionId));
136
- const { question, ...markRest } = marks[index];
137
- const { comments = [], ...questionRest } = question;
138
- let newComments;
139
- if (parentId) {
140
- const commentIndex = comments.findIndex(({ id }) => Number(id) === parentId);
141
- const { replies = [], ...commentRest } = comments[commentIndex];
142
- newComments = [
143
- ...comments.slice(0, commentIndex),
144
- { ...commentRest, replies: [...replies, questionComments] },
145
- ...comments.slice(commentIndex + 1),
146
- ];
147
- }
148
- else {
149
- newComments = [...comments, questionComments];
150
- }
151
- cache.writeQuery({
152
- query: MARKSHEET,
153
- variables: { id: marksheetId },
154
- data: {
155
- ...prevData,
156
- restricted: {
157
- ...prevData.restricted,
158
- marksheet: {
159
- ...marksheetRest,
160
- marks: [
161
- ...marks.slice(0, index),
162
- {
163
- ...markRest,
164
- question: { ...questionRest, comments: newComments },
165
- },
166
- ...marks.slice(index + 1),
167
- ],
168
- },
169
- },
170
- },
171
- });
286
+ if (marksheetId) {
287
+ updatedCommentsInMarksheetQuestion(cache, questionComments, marksheetId, questionId, parentId);
288
+ }
289
+ else {
290
+ updatedCommentsInQuestion(cache, questionComments, questionId, parentId);
172
291
  }
173
292
  }
174
293
  catch (error) {
@@ -290,53 +409,11 @@ export const updateQuestionCommentsRemove = (marksheetId) => (cache, result, opt
290
409
  }
291
410
  const { questionId, id: commentId, parentId } = questionCommentRemove;
292
411
  try {
293
- const prevData = cache.readQuery({
294
- variables: { id: marksheetId },
295
- query: MARKSHEET,
296
- });
297
- if (prevData) {
298
- const { marksheet } = prevData.restricted || {};
299
- const { marks, ...marksheetRest } = marksheet;
300
- const index = marks.findIndex(({ question }) => Number(question.id) === Number(questionId));
301
- const { question, ...markRest } = marks[index];
302
- const { comments = [], ...questionRest } = question;
303
- let newComments;
304
- if (parentId) {
305
- const commentIndex = comments.findIndex(({ id }) => Number(id) === Number(parentId));
306
- const { replies = [], ...commentRest } = comments[commentIndex];
307
- newComments = [
308
- ...comments.slice(0, commentIndex),
309
- {
310
- ...commentRest,
311
- replies: replies.filter(({ id }) => Number(id) !== Number(commentId)),
312
- },
313
- ...comments.slice(commentIndex + 1),
314
- ];
315
- }
316
- else {
317
- newComments = comments.filter(({ id }) => Number(id) !== Number(commentId));
318
- }
319
- cache.writeQuery({
320
- query: MARKSHEET,
321
- variables: { id: marksheetId },
322
- data: {
323
- ...prevData,
324
- restricted: {
325
- ...prevData.restricted,
326
- marksheet: {
327
- ...marksheetRest,
328
- marks: [
329
- ...marks.slice(0, index),
330
- {
331
- ...markRest,
332
- question: { ...questionRest, comments: newComments },
333
- },
334
- ...marks.slice(index + 1),
335
- ],
336
- },
337
- },
338
- },
339
- });
412
+ if (marksheetId) {
413
+ updatedRemoveCommentsInMarksheetQuestion(cache, marksheetId, questionId, commentId, parentId);
414
+ }
415
+ else {
416
+ updatedRemoveCommentsInQuestion(cache, questionId, commentId, parentId);
340
417
  }
341
418
  }
342
419
  catch (error) {
@@ -93,6 +93,7 @@ export const MARKSHEET = gql `
93
93
  id
94
94
  createdAt
95
95
  comment
96
+ parentId
96
97
  likes
97
98
  user {
98
99
  id
@@ -105,6 +106,7 @@ export const MARKSHEET = gql `
105
106
  id
106
107
  createdAt
107
108
  comment
109
+ parentId
108
110
  user {
109
111
  id
110
112
  displayName
@@ -210,6 +212,7 @@ export const MARKSHEET = gql `
210
212
  id
211
213
  createdAt
212
214
  comment
215
+ parentId
213
216
  likes
214
217
  user {
215
218
  id
@@ -222,6 +225,7 @@ export const MARKSHEET = gql `
222
225
  id
223
226
  createdAt
224
227
  comment
228
+ parentId
225
229
  user {
226
230
  id
227
231
  displayName
@@ -330,6 +334,7 @@ export const MARKSHEET = gql `
330
334
  id
331
335
  createdAt
332
336
  comment
337
+ parentId
333
338
  likes
334
339
  user {
335
340
  id
@@ -342,6 +347,7 @@ export const MARKSHEET = gql `
342
347
  id
343
348
  createdAt
344
349
  comment
350
+ parentId
345
351
  user {
346
352
  id
347
353
  displayName
@@ -447,6 +453,7 @@ export const MARKSHEET = gql `
447
453
  id
448
454
  createdAt
449
455
  comment
456
+ parentId
450
457
  likes
451
458
  user {
452
459
  id
@@ -459,6 +466,7 @@ export const MARKSHEET = gql `
459
466
  id
460
467
  createdAt
461
468
  comment
469
+ parentId
462
470
  user {
463
471
  id
464
472
  displayName
@@ -644,6 +652,7 @@ export const FLAGGED_QUESTIONS = gql `
644
652
  id
645
653
  createdAt
646
654
  comment
655
+ parentId
647
656
  likes
648
657
  user {
649
658
  id
@@ -656,6 +665,7 @@ export const FLAGGED_QUESTIONS = gql `
656
665
  id
657
666
  createdAt
658
667
  comment
668
+ parentId
659
669
  user {
660
670
  id
661
671
  displayName
@@ -768,6 +778,7 @@ export const FLAGGED_QUESTIONS = gql `
768
778
  id
769
779
  createdAt
770
780
  comment
781
+ parentId
771
782
  likes
772
783
  user {
773
784
  id
@@ -780,6 +791,7 @@ export const FLAGGED_QUESTIONS = gql `
780
791
  id
781
792
  createdAt
782
793
  comment
794
+ parentId
783
795
  user {
784
796
  id
785
797
  displayName
@@ -885,6 +897,7 @@ export const FLAGGED_QUESTIONS = gql `
885
897
  id
886
898
  createdAt
887
899
  comment
900
+ parentId
888
901
  likes
889
902
  user {
890
903
  id
@@ -897,6 +910,7 @@ export const FLAGGED_QUESTIONS = gql `
897
910
  id
898
911
  createdAt
899
912
  comment
913
+ parentId
900
914
  user {
901
915
  id
902
916
  displayName
@@ -1005,6 +1019,7 @@ export const FLAGGED_QUESTIONS = gql `
1005
1019
  id
1006
1020
  createdAt
1007
1021
  comment
1022
+ parentId
1008
1023
  likes
1009
1024
  user {
1010
1025
  id
@@ -1017,6 +1032,7 @@ export const FLAGGED_QUESTIONS = gql `
1017
1032
  id
1018
1033
  createdAt
1019
1034
  comment
1035
+ parentId
1020
1036
  user {
1021
1037
  id
1022
1038
  displayName
@@ -1122,6 +1138,7 @@ export const FLAGGED_QUESTIONS = gql `
1122
1138
  id
1123
1139
  createdAt
1124
1140
  comment
1141
+ parentId
1125
1142
  likes
1126
1143
  user {
1127
1144
  id
@@ -1134,6 +1151,7 @@ export const FLAGGED_QUESTIONS = gql `
1134
1151
  id
1135
1152
  createdAt
1136
1153
  comment
1154
+ parentId
1137
1155
  user {
1138
1156
  id
1139
1157
  displayName
@@ -1327,6 +1345,7 @@ export const FLAGGED_QUESTIONS_MARKSHEET = gql `
1327
1345
  id
1328
1346
  createdAt
1329
1347
  comment
1348
+ parentId
1330
1349
  likes
1331
1350
  user {
1332
1351
  id
@@ -1339,6 +1358,7 @@ export const FLAGGED_QUESTIONS_MARKSHEET = gql `
1339
1358
  id
1340
1359
  createdAt
1341
1360
  comment
1361
+ parentId
1342
1362
  user {
1343
1363
  id
1344
1364
  displayName
@@ -1444,6 +1464,7 @@ export const FLAGGED_QUESTIONS_MARKSHEET = gql `
1444
1464
  id
1445
1465
  createdAt
1446
1466
  comment
1467
+ parentId
1447
1468
  likes
1448
1469
  user {
1449
1470
  id
@@ -1456,6 +1477,7 @@ export const FLAGGED_QUESTIONS_MARKSHEET = gql `
1456
1477
  id
1457
1478
  createdAt
1458
1479
  comment
1480
+ parentId
1459
1481
  user {
1460
1482
  id
1461
1483
  displayName
@@ -1564,6 +1586,7 @@ export const FLAGGED_QUESTIONS_MARKSHEET = gql `
1564
1586
  id
1565
1587
  createdAt
1566
1588
  comment
1589
+ parentId
1567
1590
  likes
1568
1591
  user {
1569
1592
  id
@@ -1576,6 +1599,7 @@ export const FLAGGED_QUESTIONS_MARKSHEET = gql `
1576
1599
  id
1577
1600
  createdAt
1578
1601
  comment
1602
+ parentId
1579
1603
  user {
1580
1604
  id
1581
1605
  displayName
@@ -1681,6 +1705,7 @@ export const FLAGGED_QUESTIONS_MARKSHEET = gql `
1681
1705
  id
1682
1706
  createdAt
1683
1707
  comment
1708
+ parentId
1684
1709
  likes
1685
1710
  user {
1686
1711
  id
@@ -1693,6 +1718,7 @@ export const FLAGGED_QUESTIONS_MARKSHEET = gql `
1693
1718
  id
1694
1719
  createdAt
1695
1720
  comment
1721
+ parentId
1696
1722
  user {
1697
1723
  id
1698
1724
  displayName
@@ -36,6 +36,7 @@ export const QUESTION = gql `
36
36
  id
37
37
  createdAt
38
38
  comment
39
+ parentId
39
40
  likes
40
41
  user {
41
42
  id
@@ -48,6 +49,7 @@ export const QUESTION = gql `
48
49
  id
49
50
  createdAt
50
51
  comment
52
+ parentId
51
53
  user {
52
54
  id
53
55
  displayName
@@ -153,6 +155,7 @@ export const QUESTION = gql `
153
155
  id
154
156
  createdAt
155
157
  comment
158
+ parentId
156
159
  likes
157
160
  user {
158
161
  id
@@ -165,6 +168,7 @@ export const QUESTION = gql `
165
168
  id
166
169
  createdAt
167
170
  comment
171
+ parentId
168
172
  user {
169
173
  id
170
174
  displayName
@@ -273,6 +277,7 @@ export const QUESTION = gql `
273
277
  id
274
278
  createdAt
275
279
  comment
280
+ parentId
276
281
  likes
277
282
  user {
278
283
  id
@@ -285,6 +290,7 @@ export const QUESTION = gql `
285
290
  id
286
291
  createdAt
287
292
  comment
293
+ parentId
288
294
  user {
289
295
  id
290
296
  displayName
@@ -390,6 +396,7 @@ export const QUESTION = gql `
390
396
  id
391
397
  createdAt
392
398
  comment
399
+ parentId
393
400
  likes
394
401
  user {
395
402
  id
@@ -402,6 +409,7 @@ export const QUESTION = gql `
402
409
  id
403
410
  createdAt
404
411
  comment
412
+ parentId
405
413
  user {
406
414
  id
407
415
  displayName
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quesmed/types",
3
- "version": "2.2.91",
3
+ "version": "2.2.93",
4
4
  "description": "Typescript types for Quesmed",
5
5
  "keywords": [
6
6
  "quesmed",