@quesmed/types 2.2.94 → 2.2.95

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,4 +1,4 @@
1
- import { ApolloCache, ApolloClient } from '@apollo/client';
1
+ import { ApolloCache, DocumentNode } from '@apollo/client';
2
2
  import { ApolloUpdateOptions, ApolloUpdateResultRestricted } from '../..';
3
3
  import { EQuestionLike, EQuestionType, Id, IQuestion, IQuestionComment } from '../../../models';
4
4
  import { graphqlNormalize, RestrictedData } from '../../types';
@@ -7,23 +7,21 @@ interface LikeData {
7
7
  dislikes?: number;
8
8
  isLikedByMe?: EQuestionLike;
9
9
  }
10
+ export declare const QUESTION_SBA_COMMENT_FIELDS: DocumentNode;
11
+ export declare const QUESTION_QA_COMMENT_FIELDS: DocumentNode;
12
+ export declare const QUESTION_MULTIA_COMMENT_FIELDS: DocumentNode;
13
+ export declare const QUESTION_PRESCRIPTION_COMMENT_FIELDS: DocumentNode;
10
14
  export declare const getLikeData: (like: EQuestionLike, item: LikeData) => LikeData;
11
15
  export declare const getQuestionTypeName: (typeId: EQuestionType) => string;
12
- export declare const updatedCommentsOnAdd: (newComment: IQuestionComment, comments: IQuestionComment[], parentId?: number | undefined) => IQuestionComment[];
13
- export declare const updateCommentsOnRemove: (comments: IQuestionComment[], commentId: number, parentId?: number | undefined) => IQuestionComment[];
14
- export declare const updatedCommentsInMarksheetQuestion: (cache: ApolloCache<any>, questionComments: IQuestionComment, marksheetId: number, questionId: number, parentId?: number | undefined) => void;
15
- export declare const updatedCommentsInQuestion: (cache: ApolloCache<any>, questionComments: IQuestionComment, questionId: number, parentId?: number | undefined) => void;
16
- export declare const updatedRemoveCommentsInMarksheetQuestion: (cache: ApolloCache<any>, marksheetId: number, questionId: number, commentId: number, parentId?: number | undefined) => void;
17
- export declare const updatedRemoveCommentsInQuestion: (cache: ApolloCache<any>, questionId: number, commentId: number, parentId?: number | undefined) => void;
18
- export declare const QUESTION_LIKE: import("@apollo/client").DocumentNode;
19
- export declare const optimisticQuestionLike: (client: ApolloClient<any>, input: IQuestionLikeVar, marksheetId?: number | undefined) => IQuestionLikeData;
16
+ export declare const QUESTION_LIKE: DocumentNode;
17
+ export declare const optimisticQuestionLike: (input: IQuestionLikeVar, question: IQuestion) => IQuestionLikeData;
20
18
  export interface IQuestionLikeVar {
21
19
  questionId: Id;
22
20
  like: EQuestionLike;
23
21
  }
24
22
  export declare type IQuestionLikeData = RestrictedData<graphqlNormalize & IQuestion, 'questionLike'>;
25
- export declare const QUESTION_COMMENTS: import("@apollo/client").DocumentNode;
26
- export declare const updateQuestionComments: (marksheetId?: number | undefined) => (cache: ApolloCache<any>, result: ApolloUpdateResultRestricted<IQuestionCommentsData>, options: ApolloUpdateOptions) => void;
23
+ export declare const QUESTION_COMMENTS: DocumentNode;
24
+ export declare const updateQuestionComments: (typeId: EQuestionType) => (cache: ApolloCache<any>, result: ApolloUpdateResultRestricted<IQuestionCommentsData>, options: ApolloUpdateOptions) => void;
27
25
  export declare const optimisticQuestionComment: (id: number, user: {
28
26
  id: number;
29
27
  displayName: string;
@@ -34,15 +32,15 @@ export interface IQuestionCommentsVar {
34
32
  comment: string;
35
33
  }
36
34
  export declare type IQuestionCommentsData = RestrictedData<graphqlNormalize & IQuestionComment, 'questionComments'>;
37
- export declare const QUESTION_COMMENT_LIKE: import("@apollo/client").DocumentNode;
35
+ export declare const QUESTION_COMMENT_LIKE: DocumentNode;
38
36
  export declare const optimisticCommentLike: (comments: IQuestionComment[], input: IQuestionCommentsLikeVar, parentId: number | null) => IQuestionCommentsLikeData;
39
37
  export interface IQuestionCommentsLikeVar {
40
38
  commentId: Id;
41
39
  like: EQuestionLike;
42
40
  }
43
41
  export declare type IQuestionCommentsLikeData = RestrictedData<graphqlNormalize & IQuestionComment, 'questionCommentLike'>;
44
- export declare const QUESTION_COMMENT_REMOVE: import("@apollo/client").DocumentNode;
45
- export declare const updateQuestionCommentsRemove: (marksheetId?: number | undefined) => (cache: ApolloCache<any>, result: ApolloUpdateResultRestricted<IQuestionCommentRemoveData>, options: ApolloUpdateOptions) => void;
42
+ export declare const QUESTION_COMMENT_REMOVE: DocumentNode;
43
+ export declare const updateQuestionCommentsRemove: (typeId: EQuestionType) => (cache: ApolloCache<any>, result: ApolloUpdateResultRestricted<IQuestionCommentRemoveData>, options: ApolloUpdateOptions) => void;
46
44
  export declare const optimisticQuestionCommentRemove: (comments: IQuestionComment[], input: IQuestionCommentRemoveVar, parentId?: number | undefined) => IQuestionCommentRemoveData;
47
45
  export interface IQuestionCommentRemoveVar {
48
46
  commentId: Id;
@@ -1,9 +1,152 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.optimisticQuestionCommentRemove = exports.updateQuestionCommentsRemove = exports.QUESTION_COMMENT_REMOVE = exports.optimisticCommentLike = exports.QUESTION_COMMENT_LIKE = exports.optimisticQuestionComment = exports.updateQuestionComments = exports.QUESTION_COMMENTS = exports.optimisticQuestionLike = exports.QUESTION_LIKE = exports.updatedRemoveCommentsInQuestion = exports.updatedRemoveCommentsInMarksheetQuestion = exports.updatedCommentsInQuestion = exports.updatedCommentsInMarksheetQuestion = exports.updateCommentsOnRemove = exports.updatedCommentsOnAdd = exports.getQuestionTypeName = exports.getLikeData = void 0;
3
+ exports.optimisticQuestionCommentRemove = exports.updateQuestionCommentsRemove = exports.QUESTION_COMMENT_REMOVE = exports.optimisticCommentLike = exports.QUESTION_COMMENT_LIKE = exports.optimisticQuestionComment = exports.updateQuestionComments = exports.QUESTION_COMMENTS = exports.optimisticQuestionLike = exports.QUESTION_LIKE = exports.getQuestionTypeName = exports.getLikeData = exports.QUESTION_PRESCRIPTION_COMMENT_FIELDS = exports.QUESTION_MULTIA_COMMENT_FIELDS = exports.QUESTION_QA_COMMENT_FIELDS = exports.QUESTION_SBA_COMMENT_FIELDS = void 0;
4
4
  const client_1 = require("@apollo/client");
5
5
  const models_1 = require("../../../models");
6
- const restricted_1 = require("../../query/restricted");
6
+ exports.QUESTION_SBA_COMMENT_FIELDS = (0, client_1.gql) `
7
+ fragment QuestionSBAComments on QuestionSBA {
8
+ id
9
+ likes
10
+ dislikes
11
+ isLikedByMe
12
+ comments {
13
+ id
14
+ createdAt
15
+ comment
16
+ parentId
17
+ likes
18
+ user {
19
+ id
20
+ displayName
21
+ }
22
+ dislikes
23
+ isLikedByMe
24
+ questionId
25
+ replies {
26
+ id
27
+ createdAt
28
+ comment
29
+ parentId
30
+ user {
31
+ id
32
+ displayName
33
+ }
34
+ likes
35
+ dislikes
36
+ isLikedByMe
37
+ questionId
38
+ }
39
+ }
40
+ }
41
+ `;
42
+ exports.QUESTION_QA_COMMENT_FIELDS = (0, client_1.gql) `
43
+ fragment QuestionQAComments on QuestionQA {
44
+ id
45
+ likes
46
+ dislikes
47
+ isLikedByMe
48
+ comments {
49
+ id
50
+ createdAt
51
+ comment
52
+ parentId
53
+ likes
54
+ user {
55
+ id
56
+ displayName
57
+ }
58
+ dislikes
59
+ isLikedByMe
60
+ questionId
61
+ replies {
62
+ id
63
+ createdAt
64
+ comment
65
+ parentId
66
+ user {
67
+ id
68
+ displayName
69
+ }
70
+ likes
71
+ dislikes
72
+ isLikedByMe
73
+ questionId
74
+ }
75
+ }
76
+ }
77
+ `;
78
+ exports.QUESTION_MULTIA_COMMENT_FIELDS = (0, client_1.gql) `
79
+ fragment QuestionMultiAComments on QuestionMultiA {
80
+ id
81
+ likes
82
+ dislikes
83
+ isLikedByMe
84
+ comments {
85
+ id
86
+ createdAt
87
+ comment
88
+ parentId
89
+ likes
90
+ user {
91
+ id
92
+ displayName
93
+ }
94
+ dislikes
95
+ isLikedByMe
96
+ questionId
97
+ replies {
98
+ id
99
+ createdAt
100
+ comment
101
+ parentId
102
+ user {
103
+ id
104
+ displayName
105
+ }
106
+ likes
107
+ dislikes
108
+ isLikedByMe
109
+ questionId
110
+ }
111
+ }
112
+ }
113
+ `;
114
+ exports.QUESTION_PRESCRIPTION_COMMENT_FIELDS = (0, client_1.gql) `
115
+ fragment QuestionPrescriptionComments on QuestionPrescription {
116
+ id
117
+ likes
118
+ dislikes
119
+ isLikedByMe
120
+ comments {
121
+ id
122
+ createdAt
123
+ comment
124
+ parentId
125
+ likes
126
+ user {
127
+ id
128
+ displayName
129
+ }
130
+ dislikes
131
+ isLikedByMe
132
+ questionId
133
+ replies {
134
+ id
135
+ createdAt
136
+ comment
137
+ parentId
138
+ user {
139
+ id
140
+ displayName
141
+ }
142
+ likes
143
+ dislikes
144
+ isLikedByMe
145
+ questionId
146
+ }
147
+ }
148
+ }
149
+ `;
7
150
  const getLikeData = (like, item) => {
8
151
  let { likes = 0, dislikes = 0, isLikedByMe } = item;
9
152
  if (isLikedByMe === models_1.EQuestionLike.LIKE) {
@@ -43,7 +186,20 @@ const getQuestionTypeName = (typeId) => {
43
186
  }
44
187
  };
45
188
  exports.getQuestionTypeName = getQuestionTypeName;
46
- const updatedCommentsOnAdd = (newComment, comments, parentId) => {
189
+ const getQuestionFragment = (typeId) => {
190
+ switch (typeId) {
191
+ case models_1.EQuestionType.QUESTION_ANSWER:
192
+ return exports.QUESTION_QA_COMMENT_FIELDS;
193
+ case models_1.EQuestionType.PRESCRIPTION_ANSWER:
194
+ return exports.QUESTION_PRESCRIPTION_COMMENT_FIELDS;
195
+ case models_1.EQuestionType.MULTIPLE_ANSWERS:
196
+ return exports.QUESTION_MULTIA_COMMENT_FIELDS;
197
+ case models_1.EQuestionType.SINGLE_BEST_ANSWER:
198
+ default:
199
+ return exports.QUESTION_SBA_COMMENT_FIELDS;
200
+ }
201
+ };
202
+ const addCommentToDiscussion = (newComment, comments, parentId) => {
47
203
  if (parentId) {
48
204
  const commentIndex = comments.findIndex(({ id }) => Number(id) === parentId);
49
205
  const { replies = [], ...commentRest } = comments[commentIndex];
@@ -55,8 +211,7 @@ const updatedCommentsOnAdd = (newComment, comments, parentId) => {
55
211
  }
56
212
  return [...comments, newComment];
57
213
  };
58
- exports.updatedCommentsOnAdd = updatedCommentsOnAdd;
59
- const updateCommentsOnRemove = (comments, commentId, parentId) => {
214
+ const removeCommnetFromDiscussion = (comments, commentId, parentId) => {
60
215
  if (parentId) {
61
216
  const commentIndex = comments.findIndex(({ id }) => Number(id) === Number(parentId));
62
217
  const { replies = [], ...commentRest } = comments[commentIndex];
@@ -71,133 +226,6 @@ const updateCommentsOnRemove = (comments, commentId, parentId) => {
71
226
  }
72
227
  return comments.filter(({ id }) => Number(id) !== Number(commentId));
73
228
  };
74
- exports.updateCommentsOnRemove = updateCommentsOnRemove;
75
- const updatedCommentsInMarksheetQuestion = (cache, questionComments, marksheetId, questionId, parentId) => {
76
- const marksheetData = cache.readQuery({
77
- variables: { id: marksheetId },
78
- query: restricted_1.MARKSHEET,
79
- });
80
- if (marksheetData) {
81
- const { marksheet } = marksheetData.restricted || {};
82
- const { marks, ...marksheetRest } = marksheet;
83
- const index = marks.findIndex(({ question }) => Number(question.id) === Number(questionId));
84
- const { question, ...markRest } = marks[index];
85
- const { comments = [], ...questionRest } = question;
86
- cache.writeQuery({
87
- query: restricted_1.MARKSHEET,
88
- variables: { id: marksheetId },
89
- data: {
90
- ...marksheetData,
91
- restricted: {
92
- ...marksheetData.restricted,
93
- marksheet: {
94
- ...marksheetRest,
95
- marks: [
96
- ...marks.slice(0, index),
97
- {
98
- ...markRest,
99
- question: {
100
- ...questionRest,
101
- comments: (0, exports.updatedCommentsOnAdd)(questionComments, comments, parentId),
102
- },
103
- },
104
- ...marks.slice(index + 1),
105
- ],
106
- },
107
- },
108
- },
109
- });
110
- }
111
- };
112
- exports.updatedCommentsInMarksheetQuestion = updatedCommentsInMarksheetQuestion;
113
- const updatedCommentsInQuestion = (cache, questionComments, questionId, parentId) => {
114
- const questionData = cache.readQuery({
115
- variables: { id: questionId },
116
- query: restricted_1.QUESTION,
117
- });
118
- if (questionData) {
119
- const { question } = questionData.restricted || {};
120
- const { comments = [], ...questionRest } = question;
121
- cache.writeQuery({
122
- query: restricted_1.QUESTION,
123
- variables: { id: questionId },
124
- data: {
125
- ...questionData,
126
- restricted: {
127
- ...questionData.restricted,
128
- question: {
129
- ...questionRest,
130
- comments: (0, exports.updatedCommentsOnAdd)(questionComments, comments, parentId),
131
- },
132
- },
133
- },
134
- });
135
- }
136
- };
137
- exports.updatedCommentsInQuestion = updatedCommentsInQuestion;
138
- const updatedRemoveCommentsInMarksheetQuestion = (cache, marksheetId, questionId, commentId, parentId) => {
139
- const marksheetData = cache.readQuery({
140
- variables: { id: marksheetId },
141
- query: restricted_1.MARKSHEET,
142
- });
143
- if (marksheetData) {
144
- const { marksheet } = marksheetData.restricted || {};
145
- const { marks, ...marksheetRest } = marksheet;
146
- const index = marks.findIndex(({ question }) => Number(question.id) === Number(questionId));
147
- const { question, ...markRest } = marks[index];
148
- const { comments = [], ...questionRest } = question;
149
- cache.writeQuery({
150
- query: restricted_1.MARKSHEET,
151
- variables: { id: marksheetId },
152
- data: {
153
- ...marksheetData,
154
- restricted: {
155
- ...marksheetData.restricted,
156
- marksheet: {
157
- ...marksheetRest,
158
- marks: [
159
- ...marks.slice(0, index),
160
- {
161
- ...markRest,
162
- question: {
163
- ...questionRest,
164
- comments: (0, exports.updateCommentsOnRemove)(comments, commentId, parentId),
165
- },
166
- },
167
- ...marks.slice(index + 1),
168
- ],
169
- },
170
- },
171
- },
172
- });
173
- }
174
- };
175
- exports.updatedRemoveCommentsInMarksheetQuestion = updatedRemoveCommentsInMarksheetQuestion;
176
- const updatedRemoveCommentsInQuestion = (cache, questionId, commentId, parentId) => {
177
- const questionData = cache.readQuery({
178
- variables: { id: questionId },
179
- query: restricted_1.QUESTION,
180
- });
181
- if (questionData) {
182
- const { question } = questionData.restricted || {};
183
- const { comments = [], ...questionRest } = question;
184
- cache.writeQuery({
185
- query: restricted_1.QUESTION,
186
- variables: { id: questionId },
187
- data: {
188
- ...questionData,
189
- restricted: {
190
- ...questionData.restricted,
191
- question: {
192
- ...questionRest,
193
- comments: (0, exports.updateCommentsOnRemove)(comments, commentId, parentId),
194
- },
195
- },
196
- },
197
- });
198
- }
199
- };
200
- exports.updatedRemoveCommentsInQuestion = updatedRemoveCommentsInQuestion;
201
229
  exports.QUESTION_LIKE = (0, client_1.gql) `
202
230
  mutation QuestionLike($questionId: Int!, $like: Int!) {
203
231
  restricted {
@@ -211,38 +239,16 @@ exports.QUESTION_LIKE = (0, client_1.gql) `
211
239
  }
212
240
  }
213
241
  `;
214
- const optimisticQuestionLike = (client, input, marksheetId) => {
215
- const { questionId, like } = input;
216
- let question;
217
- if (marksheetId) {
218
- const data = client.readQuery({
219
- variables: { id: marksheetId },
220
- query: restricted_1.MARKSHEET,
221
- });
222
- const { marksheet } = data?.restricted || {};
223
- const { marks = [] } = marksheet || {};
224
- question = marks.find(({ question }) => Number(question.id) === Number(questionId))?.question;
225
- }
226
- else {
227
- const data = client.readQuery({
228
- variables: { id: Number(questionId) },
229
- query: restricted_1.QUESTION,
230
- });
231
- question = data?.restricted.question;
232
- }
233
- if (question) {
234
- const { typeId } = question;
235
- return {
236
- restricted: {
237
- questionLike: {
238
- ...question,
239
- __typename: (0, exports.getQuestionTypeName)(typeId),
240
- ...(0, exports.getLikeData)(like, question),
241
- },
242
+ const optimisticQuestionLike = (input, question) => {
243
+ const { like } = input;
244
+ return {
245
+ restricted: {
246
+ questionLike: {
247
+ ...question,
248
+ ...(0, exports.getLikeData)(like, question),
242
249
  },
243
- };
244
- }
245
- return {};
250
+ },
251
+ };
246
252
  };
247
253
  exports.optimisticQuestionLike = optimisticQuestionLike;
248
254
  exports.QUESTION_COMMENTS = (0, client_1.gql) `
@@ -287,7 +293,7 @@ exports.QUESTION_COMMENTS = (0, client_1.gql) `
287
293
  }
288
294
  }
289
295
  `;
290
- const updateQuestionComments = (marksheetId) => (cache, result, options) => {
296
+ const updateQuestionComments = (typeId) => (cache, result, options) => {
291
297
  const { questionComments } = result?.data?.restricted || {};
292
298
  const { variables } = options || {};
293
299
  if (!variables || !questionComments) {
@@ -295,11 +301,26 @@ const updateQuestionComments = (marksheetId) => (cache, result, options) => {
295
301
  }
296
302
  const { questionId, parentId } = variables;
297
303
  try {
298
- if (marksheetId) {
299
- (0, exports.updatedCommentsInMarksheetQuestion)(cache, questionComments, marksheetId, questionId, parentId);
300
- }
301
- else {
302
- (0, exports.updatedCommentsInQuestion)(cache, questionComments, questionId, parentId);
304
+ const dataFragment = cache.readFragment({
305
+ id: cache.identify({
306
+ id: questionId,
307
+ __typename: (0, exports.getQuestionTypeName)(typeId),
308
+ }),
309
+ fragment: getQuestionFragment(typeId),
310
+ });
311
+ if (dataFragment) {
312
+ const { comments = [] } = dataFragment;
313
+ cache.writeFragment({
314
+ id: cache.identify({
315
+ id: questionId,
316
+ __typename: (0, exports.getQuestionTypeName)(typeId),
317
+ }),
318
+ data: {
319
+ ...dataFragment,
320
+ comments: addCommentToDiscussion(questionComments, comments, parentId),
321
+ },
322
+ fragment: getQuestionFragment(typeId),
323
+ });
303
324
  }
304
325
  }
305
326
  catch (error) {
@@ -420,7 +441,7 @@ exports.QUESTION_COMMENT_REMOVE = (0, client_1.gql) `
420
441
  }
421
442
  }
422
443
  `;
423
- const updateQuestionCommentsRemove = (marksheetId) => (cache, result, options) => {
444
+ const updateQuestionCommentsRemove = (typeId) => (cache, result, options) => {
424
445
  const { questionCommentRemove } = result?.data?.restricted || {};
425
446
  const { variables } = options || {};
426
447
  if (!variables || !questionCommentRemove) {
@@ -428,11 +449,26 @@ const updateQuestionCommentsRemove = (marksheetId) => (cache, result, options) =
428
449
  }
429
450
  const { questionId, id: commentId, parentId } = questionCommentRemove;
430
451
  try {
431
- if (marksheetId) {
432
- (0, exports.updatedRemoveCommentsInMarksheetQuestion)(cache, marksheetId, questionId, commentId, parentId);
433
- }
434
- else {
435
- (0, exports.updatedRemoveCommentsInQuestion)(cache, questionId, commentId, parentId);
452
+ const dataFragment = cache.readFragment({
453
+ id: cache.identify({
454
+ id: questionId,
455
+ __typename: (0, exports.getQuestionTypeName)(typeId),
456
+ }),
457
+ fragment: getQuestionFragment(typeId),
458
+ });
459
+ if (dataFragment) {
460
+ const { comments = [] } = dataFragment;
461
+ cache.writeFragment({
462
+ id: cache.identify({
463
+ id: questionId,
464
+ __typename: (0, exports.getQuestionTypeName)(typeId),
465
+ }),
466
+ data: {
467
+ ...dataFragment,
468
+ comments: removeCommnetFromDiscussion(comments, commentId, parentId),
469
+ },
470
+ fragment: getQuestionFragment(typeId),
471
+ });
436
472
  }
437
473
  }
438
474
  catch (error) {
@@ -1,4 +1,4 @@
1
- import { ApolloCache, ApolloClient } from '@apollo/client';
1
+ import { ApolloCache, DocumentNode } from '@apollo/client';
2
2
  import { ApolloUpdateOptions, ApolloUpdateResultRestricted } from '../..';
3
3
  import { EQuestionLike, EQuestionType, Id, IQuestion, IQuestionComment } from '../../../models';
4
4
  import { graphqlNormalize, RestrictedData } from '../../types';
@@ -7,23 +7,21 @@ interface LikeData {
7
7
  dislikes?: number;
8
8
  isLikedByMe?: EQuestionLike;
9
9
  }
10
+ export declare const QUESTION_SBA_COMMENT_FIELDS: DocumentNode;
11
+ export declare const QUESTION_QA_COMMENT_FIELDS: DocumentNode;
12
+ export declare const QUESTION_MULTIA_COMMENT_FIELDS: DocumentNode;
13
+ export declare const QUESTION_PRESCRIPTION_COMMENT_FIELDS: DocumentNode;
10
14
  export declare const getLikeData: (like: EQuestionLike, item: LikeData) => LikeData;
11
15
  export declare const getQuestionTypeName: (typeId: EQuestionType) => string;
12
- export declare const updatedCommentsOnAdd: (newComment: IQuestionComment, comments: IQuestionComment[], parentId?: number | undefined) => IQuestionComment[];
13
- export declare const updateCommentsOnRemove: (comments: IQuestionComment[], commentId: number, parentId?: number | undefined) => IQuestionComment[];
14
- export declare const updatedCommentsInMarksheetQuestion: (cache: ApolloCache<any>, questionComments: IQuestionComment, marksheetId: number, questionId: number, parentId?: number | undefined) => void;
15
- export declare const updatedCommentsInQuestion: (cache: ApolloCache<any>, questionComments: IQuestionComment, questionId: number, parentId?: number | undefined) => void;
16
- export declare const updatedRemoveCommentsInMarksheetQuestion: (cache: ApolloCache<any>, marksheetId: number, questionId: number, commentId: number, parentId?: number | undefined) => void;
17
- export declare const updatedRemoveCommentsInQuestion: (cache: ApolloCache<any>, questionId: number, commentId: number, parentId?: number | undefined) => void;
18
- export declare const QUESTION_LIKE: import("@apollo/client").DocumentNode;
19
- export declare const optimisticQuestionLike: (client: ApolloClient<any>, input: IQuestionLikeVar, marksheetId?: number | undefined) => IQuestionLikeData;
16
+ export declare const QUESTION_LIKE: DocumentNode;
17
+ export declare const optimisticQuestionLike: (input: IQuestionLikeVar, question: IQuestion) => IQuestionLikeData;
20
18
  export interface IQuestionLikeVar {
21
19
  questionId: Id;
22
20
  like: EQuestionLike;
23
21
  }
24
22
  export declare type IQuestionLikeData = RestrictedData<graphqlNormalize & IQuestion, 'questionLike'>;
25
- export declare const QUESTION_COMMENTS: import("@apollo/client").DocumentNode;
26
- export declare const updateQuestionComments: (marksheetId?: number | undefined) => (cache: ApolloCache<any>, result: ApolloUpdateResultRestricted<IQuestionCommentsData>, options: ApolloUpdateOptions) => void;
23
+ export declare const QUESTION_COMMENTS: DocumentNode;
24
+ export declare const updateQuestionComments: (typeId: EQuestionType) => (cache: ApolloCache<any>, result: ApolloUpdateResultRestricted<IQuestionCommentsData>, options: ApolloUpdateOptions) => void;
27
25
  export declare const optimisticQuestionComment: (id: number, user: {
28
26
  id: number;
29
27
  displayName: string;
@@ -34,15 +32,15 @@ export interface IQuestionCommentsVar {
34
32
  comment: string;
35
33
  }
36
34
  export declare type IQuestionCommentsData = RestrictedData<graphqlNormalize & IQuestionComment, 'questionComments'>;
37
- export declare const QUESTION_COMMENT_LIKE: import("@apollo/client").DocumentNode;
35
+ export declare const QUESTION_COMMENT_LIKE: DocumentNode;
38
36
  export declare const optimisticCommentLike: (comments: IQuestionComment[], input: IQuestionCommentsLikeVar, parentId: number | null) => IQuestionCommentsLikeData;
39
37
  export interface IQuestionCommentsLikeVar {
40
38
  commentId: Id;
41
39
  like: EQuestionLike;
42
40
  }
43
41
  export declare type IQuestionCommentsLikeData = RestrictedData<graphqlNormalize & IQuestionComment, 'questionCommentLike'>;
44
- export declare const QUESTION_COMMENT_REMOVE: import("@apollo/client").DocumentNode;
45
- export declare const updateQuestionCommentsRemove: (marksheetId?: number | undefined) => (cache: ApolloCache<any>, result: ApolloUpdateResultRestricted<IQuestionCommentRemoveData>, options: ApolloUpdateOptions) => void;
42
+ export declare const QUESTION_COMMENT_REMOVE: DocumentNode;
43
+ export declare const updateQuestionCommentsRemove: (typeId: EQuestionType) => (cache: ApolloCache<any>, result: ApolloUpdateResultRestricted<IQuestionCommentRemoveData>, options: ApolloUpdateOptions) => void;
46
44
  export declare const optimisticQuestionCommentRemove: (comments: IQuestionComment[], input: IQuestionCommentRemoveVar, parentId?: number | undefined) => IQuestionCommentRemoveData;
47
45
  export interface IQuestionCommentRemoveVar {
48
46
  commentId: Id;
@@ -1,6 +1,149 @@
1
1
  import { gql } from '@apollo/client';
2
2
  import { EQuestionLike, EQuestionType, } from '../../../models';
3
- import { MARKSHEET, QUESTION, } from '../../query/restricted';
3
+ export const QUESTION_SBA_COMMENT_FIELDS = gql `
4
+ fragment QuestionSBAComments on QuestionSBA {
5
+ id
6
+ likes
7
+ dislikes
8
+ isLikedByMe
9
+ comments {
10
+ id
11
+ createdAt
12
+ comment
13
+ parentId
14
+ likes
15
+ user {
16
+ id
17
+ displayName
18
+ }
19
+ dislikes
20
+ isLikedByMe
21
+ questionId
22
+ replies {
23
+ id
24
+ createdAt
25
+ comment
26
+ parentId
27
+ user {
28
+ id
29
+ displayName
30
+ }
31
+ likes
32
+ dislikes
33
+ isLikedByMe
34
+ questionId
35
+ }
36
+ }
37
+ }
38
+ `;
39
+ export const QUESTION_QA_COMMENT_FIELDS = gql `
40
+ fragment QuestionQAComments on QuestionQA {
41
+ id
42
+ likes
43
+ dislikes
44
+ isLikedByMe
45
+ comments {
46
+ id
47
+ createdAt
48
+ comment
49
+ parentId
50
+ likes
51
+ user {
52
+ id
53
+ displayName
54
+ }
55
+ dislikes
56
+ isLikedByMe
57
+ questionId
58
+ replies {
59
+ id
60
+ createdAt
61
+ comment
62
+ parentId
63
+ user {
64
+ id
65
+ displayName
66
+ }
67
+ likes
68
+ dislikes
69
+ isLikedByMe
70
+ questionId
71
+ }
72
+ }
73
+ }
74
+ `;
75
+ export const QUESTION_MULTIA_COMMENT_FIELDS = gql `
76
+ fragment QuestionMultiAComments on QuestionMultiA {
77
+ id
78
+ likes
79
+ dislikes
80
+ isLikedByMe
81
+ comments {
82
+ id
83
+ createdAt
84
+ comment
85
+ parentId
86
+ likes
87
+ user {
88
+ id
89
+ displayName
90
+ }
91
+ dislikes
92
+ isLikedByMe
93
+ questionId
94
+ replies {
95
+ id
96
+ createdAt
97
+ comment
98
+ parentId
99
+ user {
100
+ id
101
+ displayName
102
+ }
103
+ likes
104
+ dislikes
105
+ isLikedByMe
106
+ questionId
107
+ }
108
+ }
109
+ }
110
+ `;
111
+ export const QUESTION_PRESCRIPTION_COMMENT_FIELDS = gql `
112
+ fragment QuestionPrescriptionComments on QuestionPrescription {
113
+ id
114
+ likes
115
+ dislikes
116
+ isLikedByMe
117
+ comments {
118
+ id
119
+ createdAt
120
+ comment
121
+ parentId
122
+ likes
123
+ user {
124
+ id
125
+ displayName
126
+ }
127
+ dislikes
128
+ isLikedByMe
129
+ questionId
130
+ replies {
131
+ id
132
+ createdAt
133
+ comment
134
+ parentId
135
+ user {
136
+ id
137
+ displayName
138
+ }
139
+ likes
140
+ dislikes
141
+ isLikedByMe
142
+ questionId
143
+ }
144
+ }
145
+ }
146
+ `;
4
147
  export const getLikeData = (like, item) => {
5
148
  let { likes = 0, dislikes = 0, isLikedByMe } = item;
6
149
  if (isLikedByMe === EQuestionLike.LIKE) {
@@ -38,7 +181,20 @@ export const getQuestionTypeName = (typeId) => {
38
181
  return 'QuestionSBA';
39
182
  }
40
183
  };
41
- export const updatedCommentsOnAdd = (newComment, comments, parentId) => {
184
+ const getQuestionFragment = (typeId) => {
185
+ switch (typeId) {
186
+ case EQuestionType.QUESTION_ANSWER:
187
+ return QUESTION_QA_COMMENT_FIELDS;
188
+ case EQuestionType.PRESCRIPTION_ANSWER:
189
+ return QUESTION_PRESCRIPTION_COMMENT_FIELDS;
190
+ case EQuestionType.MULTIPLE_ANSWERS:
191
+ return QUESTION_MULTIA_COMMENT_FIELDS;
192
+ case EQuestionType.SINGLE_BEST_ANSWER:
193
+ default:
194
+ return QUESTION_SBA_COMMENT_FIELDS;
195
+ }
196
+ };
197
+ const addCommentToDiscussion = (newComment, comments, parentId) => {
42
198
  if (parentId) {
43
199
  const commentIndex = comments.findIndex(({ id }) => Number(id) === parentId);
44
200
  const { replies = [], ...commentRest } = comments[commentIndex];
@@ -50,7 +206,7 @@ export const updatedCommentsOnAdd = (newComment, comments, parentId) => {
50
206
  }
51
207
  return [...comments, newComment];
52
208
  };
53
- export const updateCommentsOnRemove = (comments, commentId, parentId) => {
209
+ const removeCommnetFromDiscussion = (comments, commentId, parentId) => {
54
210
  if (parentId) {
55
211
  const commentIndex = comments.findIndex(({ id }) => Number(id) === Number(parentId));
56
212
  const { replies = [], ...commentRest } = comments[commentIndex];
@@ -65,128 +221,6 @@ export const updateCommentsOnRemove = (comments, commentId, parentId) => {
65
221
  }
66
222
  return comments.filter(({ id }) => Number(id) !== Number(commentId));
67
223
  };
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
- };
190
224
  export const QUESTION_LIKE = gql `
191
225
  mutation QuestionLike($questionId: Int!, $like: Int!) {
192
226
  restricted {
@@ -200,38 +234,16 @@ export const QUESTION_LIKE = gql `
200
234
  }
201
235
  }
202
236
  `;
203
- export const optimisticQuestionLike = (client, input, marksheetId) => {
204
- const { questionId, like } = input;
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
- }
222
- if (question) {
223
- const { typeId } = question;
224
- return {
225
- restricted: {
226
- questionLike: {
227
- ...question,
228
- __typename: getQuestionTypeName(typeId),
229
- ...getLikeData(like, question),
230
- },
237
+ export const optimisticQuestionLike = (input, question) => {
238
+ const { like } = input;
239
+ return {
240
+ restricted: {
241
+ questionLike: {
242
+ ...question,
243
+ ...getLikeData(like, question),
231
244
  },
232
- };
233
- }
234
- return {};
245
+ },
246
+ };
235
247
  };
236
248
  export const QUESTION_COMMENTS = gql `
237
249
  mutation QuestionComments(
@@ -275,7 +287,7 @@ export const QUESTION_COMMENTS = gql `
275
287
  }
276
288
  }
277
289
  `;
278
- export const updateQuestionComments = (marksheetId) => (cache, result, options) => {
290
+ export const updateQuestionComments = (typeId) => (cache, result, options) => {
279
291
  const { questionComments } = result?.data?.restricted || {};
280
292
  const { variables } = options || {};
281
293
  if (!variables || !questionComments) {
@@ -283,11 +295,26 @@ export const updateQuestionComments = (marksheetId) => (cache, result, options)
283
295
  }
284
296
  const { questionId, parentId } = variables;
285
297
  try {
286
- if (marksheetId) {
287
- updatedCommentsInMarksheetQuestion(cache, questionComments, marksheetId, questionId, parentId);
288
- }
289
- else {
290
- updatedCommentsInQuestion(cache, questionComments, questionId, parentId);
298
+ const dataFragment = cache.readFragment({
299
+ id: cache.identify({
300
+ id: questionId,
301
+ __typename: getQuestionTypeName(typeId),
302
+ }),
303
+ fragment: getQuestionFragment(typeId),
304
+ });
305
+ if (dataFragment) {
306
+ const { comments = [] } = dataFragment;
307
+ cache.writeFragment({
308
+ id: cache.identify({
309
+ id: questionId,
310
+ __typename: getQuestionTypeName(typeId),
311
+ }),
312
+ data: {
313
+ ...dataFragment,
314
+ comments: addCommentToDiscussion(questionComments, comments, parentId),
315
+ },
316
+ fragment: getQuestionFragment(typeId),
317
+ });
291
318
  }
292
319
  }
293
320
  catch (error) {
@@ -405,7 +432,7 @@ export const QUESTION_COMMENT_REMOVE = gql `
405
432
  }
406
433
  }
407
434
  `;
408
- export const updateQuestionCommentsRemove = (marksheetId) => (cache, result, options) => {
435
+ export const updateQuestionCommentsRemove = (typeId) => (cache, result, options) => {
409
436
  const { questionCommentRemove } = result?.data?.restricted || {};
410
437
  const { variables } = options || {};
411
438
  if (!variables || !questionCommentRemove) {
@@ -413,11 +440,26 @@ export const updateQuestionCommentsRemove = (marksheetId) => (cache, result, opt
413
440
  }
414
441
  const { questionId, id: commentId, parentId } = questionCommentRemove;
415
442
  try {
416
- if (marksheetId) {
417
- updatedRemoveCommentsInMarksheetQuestion(cache, marksheetId, questionId, commentId, parentId);
418
- }
419
- else {
420
- updatedRemoveCommentsInQuestion(cache, questionId, commentId, parentId);
443
+ const dataFragment = cache.readFragment({
444
+ id: cache.identify({
445
+ id: questionId,
446
+ __typename: getQuestionTypeName(typeId),
447
+ }),
448
+ fragment: getQuestionFragment(typeId),
449
+ });
450
+ if (dataFragment) {
451
+ const { comments = [] } = dataFragment;
452
+ cache.writeFragment({
453
+ id: cache.identify({
454
+ id: questionId,
455
+ __typename: getQuestionTypeName(typeId),
456
+ }),
457
+ data: {
458
+ ...dataFragment,
459
+ comments: removeCommnetFromDiscussion(comments, commentId, parentId),
460
+ },
461
+ fragment: getQuestionFragment(typeId),
462
+ });
421
463
  }
422
464
  }
423
465
  catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quesmed/types",
3
- "version": "2.2.94",
3
+ "version": "2.2.95",
4
4
  "description": "Typescript types for Quesmed",
5
5
  "keywords": [
6
6
  "quesmed",