@quesmed/types 2.2.90 → 2.2.92

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.
@@ -30,3 +30,33 @@ export declare const updateNotificatonsOnMarkAsRead: (cache: ApolloCache<any>, r
30
30
  export declare type IMarkAllNotificationsAsReadVar = null;
31
31
  export declare type IMarkAllNotificationsAsReadData = RestrictedData<graphqlNormalize & INotification[], 'markAllNotificationsAsRead'>;
32
32
  export declare const MARK_ALL_AS_READ: import("@apollo/client").DocumentNode;
33
+ export declare const optimisticMarkAsRead: (notification: INotification) => () => {
34
+ restricted: {
35
+ __typename: string;
36
+ markNotificationAsRead: {
37
+ read: boolean;
38
+ id: number;
39
+ createdAt: number | Date;
40
+ updatedAt: number | Date;
41
+ deletedAt: number | Date;
42
+ userId: number | null;
43
+ user?: import("../../../models").IUser | undefined;
44
+ fromUserId: number | null;
45
+ fromUser?: import("../../../models").IUser | undefined;
46
+ commentId: number | null;
47
+ comment?: import("../../../models").IQuestionComment | undefined;
48
+ fromUserCommentId: number | null;
49
+ fromUserComment?: import("../../../models").IQuestionComment | undefined;
50
+ description: string;
51
+ actionType: ENotificationActionType;
52
+ type: ENotificationType;
53
+ global: boolean;
54
+ };
55
+ };
56
+ };
57
+ export declare const optimisticMarkAllAsRead: (notifications?: INotification[]) => () => {
58
+ restricted: {
59
+ __typename: string;
60
+ markAllNotificationsAsRead: INotification[];
61
+ };
62
+ };
@@ -1,13 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MARK_ALL_AS_READ = exports.updateNotificatonsOnMarkAsRead = exports.MARK_AS_READ = void 0;
3
+ exports.optimisticMarkAllAsRead = exports.optimisticMarkAsRead = exports.MARK_ALL_AS_READ = exports.updateNotificatonsOnMarkAsRead = exports.MARK_AS_READ = void 0;
4
4
  const client_1 = require("@apollo/client");
5
5
  const restricted_1 = require("../../query/restricted");
6
6
  exports.MARK_AS_READ = (0, client_1.gql) `
7
7
  mutation MarkNotificationAsread($id: Int!) {
8
8
  restricted {
9
9
  markNotificationAsRead(id: $id) {
10
- actionType
11
10
  id
12
11
  read
13
12
  }
@@ -57,10 +56,40 @@ exports.MARK_ALL_AS_READ = (0, client_1.gql) `
57
56
  mutation MarkAllNotificationsAsRead {
58
57
  restricted {
59
58
  markAllNotificationsAsRead {
60
- actionType
61
59
  id
62
60
  read
63
61
  }
64
62
  }
65
63
  }
66
64
  `;
65
+ const optimisticMarkAsRead = (notification) => () => ({
66
+ restricted: {
67
+ __typename: 'RestrictedMutation',
68
+ markNotificationAsRead: {
69
+ ...notification,
70
+ read: true,
71
+ },
72
+ },
73
+ });
74
+ exports.optimisticMarkAsRead = optimisticMarkAsRead;
75
+ const optimisticMarkAllAsRead = (notifications = []) => () => {
76
+ const index = notifications.findIndex(({ read }) => read);
77
+ const updatedNotification = notifications
78
+ .slice(0, index)
79
+ .map((notification) => ({ ...notification, read: true }));
80
+ return {
81
+ restricted: {
82
+ __typename: 'RestrictedMutation',
83
+ markAllNotificationsAsRead: [
84
+ ...updatedNotification,
85
+ ...notifications.slice(index).sort((n1, n2) => {
86
+ if (n1.read === n2.read) {
87
+ return n1.createdAt < n2.createdAt ? -1 : 1;
88
+ }
89
+ return n1.read ? 1 : -1;
90
+ }),
91
+ ],
92
+ },
93
+ };
94
+ };
95
+ exports.optimisticMarkAllAsRead = optimisticMarkAllAsRead;
@@ -9,15 +9,21 @@ interface LikeData {
9
9
  }
10
10
  export declare const getLikeData: (like: EQuestionLike, item: LikeData) => LikeData;
11
11
  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;
12
18
  export declare const QUESTION_LIKE: import("@apollo/client").DocumentNode;
13
- export declare const optimisticQuestionLike: (marksheetId: number, client: ApolloClient<any>, input: IQuestionLikeVar) => IQuestionLikeData;
19
+ export declare const optimisticQuestionLike: (client: ApolloClient<any>, input: IQuestionLikeVar, marksheetId?: number | undefined) => IQuestionLikeData;
14
20
  export interface IQuestionLikeVar {
15
21
  questionId: Id;
16
22
  like: EQuestionLike;
17
23
  }
18
24
  export declare type IQuestionLikeData = RestrictedData<graphqlNormalize & IQuestion, 'questionLike'>;
19
25
  export declare const QUESTION_COMMENTS: import("@apollo/client").DocumentNode;
20
- export declare const updateQuestionComments: (marksheetId: number) => (cache: ApolloCache<any>, result: ApolloUpdateResultRestricted<IQuestionCommentsData>, options: ApolloUpdateOptions) => void;
26
+ export declare const updateQuestionComments: (marksheetId?: number | undefined) => (cache: ApolloCache<any>, result: ApolloUpdateResultRestricted<IQuestionCommentsData>, options: ApolloUpdateOptions) => void;
21
27
  export declare const optimisticQuestionComment: (id: number, user: {
22
28
  id: number;
23
29
  displayName: string;
@@ -36,7 +42,7 @@ export interface IQuestionCommentsLikeVar {
36
42
  }
37
43
  export declare type IQuestionCommentsLikeData = RestrictedData<graphqlNormalize & IQuestionComment, 'questionCommentLike'>;
38
44
  export declare const QUESTION_COMMENT_REMOVE: import("@apollo/client").DocumentNode;
39
- export declare const updateQuestionCommentsRemove: (marksheetId: number) => (cache: ApolloCache<any>, result: ApolloUpdateResultRestricted<IQuestionCommentRemoveData>, options: ApolloUpdateOptions) => void;
45
+ export declare const updateQuestionCommentsRemove: (marksheetId?: number | undefined) => (cache: ApolloCache<any>, result: ApolloUpdateResultRestricted<IQuestionCommentRemoveData>, options: ApolloUpdateOptions) => void;
40
46
  export declare const optimisticQuestionCommentRemove: (comments: IQuestionComment[], input: IQuestionCommentRemoveVar, parentId?: number | undefined) => IQuestionCommentRemoveData;
41
47
  export interface IQuestionCommentRemoveVar {
42
48
  commentId: Id;
@@ -1,6 +1,6 @@
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.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.updatedRemoveCommentsInQuestion = exports.updatedRemoveCommentsInMarksheetQuestion = exports.updatedCommentsInQuestion = exports.updatedCommentsInMarksheetQuestion = exports.updateCommentsOnRemove = exports.updatedCommentsOnAdd = exports.getQuestionTypeName = exports.getLikeData = void 0;
4
4
  const client_1 = require("@apollo/client");
5
5
  const models_1 = require("../../../models");
6
6
  const restricted_1 = require("../../query/restricted");
@@ -43,6 +43,161 @@ const getQuestionTypeName = (typeId) => {
43
43
  }
44
44
  };
45
45
  exports.getQuestionTypeName = getQuestionTypeName;
46
+ const updatedCommentsOnAdd = (newComment, comments, parentId) => {
47
+ if (parentId) {
48
+ const commentIndex = comments.findIndex(({ id }) => Number(id) === parentId);
49
+ const { replies = [], ...commentRest } = comments[commentIndex];
50
+ return [
51
+ ...comments.slice(0, commentIndex),
52
+ { ...commentRest, replies: [...replies, newComment] },
53
+ ...comments.slice(commentIndex + 1),
54
+ ];
55
+ }
56
+ return [...comments, newComment];
57
+ };
58
+ exports.updatedCommentsOnAdd = updatedCommentsOnAdd;
59
+ const updateCommentsOnRemove = (comments, commentId, parentId) => {
60
+ if (parentId) {
61
+ const commentIndex = comments.findIndex(({ id }) => Number(id) === Number(parentId));
62
+ const { replies = [], ...commentRest } = comments[commentIndex];
63
+ return [
64
+ ...comments.slice(0, commentIndex),
65
+ {
66
+ ...commentRest,
67
+ replies: replies.filter(({ id }) => Number(id) !== Number(commentId)),
68
+ },
69
+ ...comments.slice(commentIndex + 1),
70
+ ];
71
+ }
72
+ return comments.filter(({ id }) => Number(id) !== Number(commentId));
73
+ };
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;
46
201
  exports.QUESTION_LIKE = (0, client_1.gql) `
47
202
  mutation QuestionLike($questionId: Int!, $like: Int!) {
48
203
  restricted {
@@ -56,16 +211,25 @@ exports.QUESTION_LIKE = (0, client_1.gql) `
56
211
  }
57
212
  }
58
213
  `;
59
- const optimisticQuestionLike = (marksheetId, client, input) => {
214
+ const optimisticQuestionLike = (client, input, marksheetId) => {
60
215
  const { questionId, like } = input;
61
- const data = client.readQuery({
62
- variables: { id: marksheetId },
63
- query: restricted_1.MARKSHEET,
64
- });
65
- const { marksheet } = data?.restricted || {};
66
- const { marks = [] } = marksheet || {};
67
- const { question } = marks.find(({ question }) => Number(question.id) === Number(questionId)) ||
68
- {};
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
+ }
69
233
  if (question) {
70
234
  const { typeId } = question;
71
235
  return {
@@ -131,50 +295,11 @@ const updateQuestionComments = (marksheetId) => (cache, result, options) => {
131
295
  }
132
296
  const { questionId, parentId } = variables;
133
297
  try {
134
- const prevData = cache.readQuery({
135
- variables: { id: marksheetId },
136
- query: restricted_1.MARKSHEET,
137
- });
138
- if (prevData) {
139
- const { marksheet } = prevData.restricted || {};
140
- const { marks, ...marksheetRest } = marksheet;
141
- const index = marks.findIndex(({ question }) => Number(question.id) === Number(questionId));
142
- const { question, ...markRest } = marks[index];
143
- const { comments = [], ...questionRest } = question;
144
- let newComments;
145
- if (parentId) {
146
- const commentIndex = comments.findIndex(({ id }) => Number(id) === parentId);
147
- const { replies = [], ...commentRest } = comments[commentIndex];
148
- newComments = [
149
- ...comments.slice(0, commentIndex),
150
- { ...commentRest, replies: [...replies, questionComments] },
151
- ...comments.slice(commentIndex + 1),
152
- ];
153
- }
154
- else {
155
- newComments = [...comments, questionComments];
156
- }
157
- cache.writeQuery({
158
- query: restricted_1.MARKSHEET,
159
- variables: { id: marksheetId },
160
- data: {
161
- ...prevData,
162
- restricted: {
163
- ...prevData.restricted,
164
- marksheet: {
165
- ...marksheetRest,
166
- marks: [
167
- ...marks.slice(0, index),
168
- {
169
- ...markRest,
170
- question: { ...questionRest, comments: newComments },
171
- },
172
- ...marks.slice(index + 1),
173
- ],
174
- },
175
- },
176
- },
177
- });
298
+ if (marksheetId) {
299
+ (0, exports.updatedCommentsInMarksheetQuestion)(cache, questionComments, marksheetId, questionId, parentId);
300
+ }
301
+ else {
302
+ (0, exports.updatedCommentsInQuestion)(cache, questionComments, questionId, parentId);
178
303
  }
179
304
  }
180
305
  catch (error) {
@@ -299,53 +424,11 @@ const updateQuestionCommentsRemove = (marksheetId) => (cache, result, options) =
299
424
  }
300
425
  const { questionId, id: commentId, parentId } = questionCommentRemove;
301
426
  try {
302
- const prevData = cache.readQuery({
303
- variables: { id: marksheetId },
304
- query: restricted_1.MARKSHEET,
305
- });
306
- if (prevData) {
307
- const { marksheet } = prevData.restricted || {};
308
- const { marks, ...marksheetRest } = marksheet;
309
- const index = marks.findIndex(({ question }) => Number(question.id) === Number(questionId));
310
- const { question, ...markRest } = marks[index];
311
- const { comments = [], ...questionRest } = question;
312
- let newComments;
313
- if (parentId) {
314
- const commentIndex = comments.findIndex(({ id }) => Number(id) === Number(parentId));
315
- const { replies = [], ...commentRest } = comments[commentIndex];
316
- newComments = [
317
- ...comments.slice(0, commentIndex),
318
- {
319
- ...commentRest,
320
- replies: replies.filter(({ id }) => Number(id) !== Number(commentId)),
321
- },
322
- ...comments.slice(commentIndex + 1),
323
- ];
324
- }
325
- else {
326
- newComments = comments.filter(({ id }) => Number(id) !== Number(commentId));
327
- }
328
- cache.writeQuery({
329
- query: restricted_1.MARKSHEET,
330
- variables: { id: marksheetId },
331
- data: {
332
- ...prevData,
333
- restricted: {
334
- ...prevData.restricted,
335
- marksheet: {
336
- ...marksheetRest,
337
- marks: [
338
- ...marks.slice(0, index),
339
- {
340
- ...markRest,
341
- question: { ...questionRest, comments: newComments },
342
- },
343
- ...marks.slice(index + 1),
344
- ],
345
- },
346
- },
347
- },
348
- });
427
+ if (marksheetId) {
428
+ (0, exports.updatedRemoveCommentsInMarksheetQuestion)(cache, marksheetId, questionId, commentId, parentId);
429
+ }
430
+ else {
431
+ (0, exports.updatedRemoveCommentsInQuestion)(cache, questionId, commentId, parentId);
349
432
  }
350
433
  }
351
434
  catch (error) {
@@ -30,3 +30,33 @@ export declare const updateNotificatonsOnMarkAsRead: (cache: ApolloCache<any>, r
30
30
  export declare type IMarkAllNotificationsAsReadVar = null;
31
31
  export declare type IMarkAllNotificationsAsReadData = RestrictedData<graphqlNormalize & INotification[], 'markAllNotificationsAsRead'>;
32
32
  export declare const MARK_ALL_AS_READ: import("@apollo/client").DocumentNode;
33
+ export declare const optimisticMarkAsRead: (notification: INotification) => () => {
34
+ restricted: {
35
+ __typename: string;
36
+ markNotificationAsRead: {
37
+ read: boolean;
38
+ id: number;
39
+ createdAt: number | Date;
40
+ updatedAt: number | Date;
41
+ deletedAt: number | Date;
42
+ userId: number | null;
43
+ user?: import("../../../models").IUser | undefined;
44
+ fromUserId: number | null;
45
+ fromUser?: import("../../../models").IUser | undefined;
46
+ commentId: number | null;
47
+ comment?: import("../../../models").IQuestionComment | undefined;
48
+ fromUserCommentId: number | null;
49
+ fromUserComment?: import("../../../models").IQuestionComment | undefined;
50
+ description: string;
51
+ actionType: ENotificationActionType;
52
+ type: ENotificationType;
53
+ global: boolean;
54
+ };
55
+ };
56
+ };
57
+ export declare const optimisticMarkAllAsRead: (notifications?: INotification[]) => () => {
58
+ restricted: {
59
+ __typename: string;
60
+ markAllNotificationsAsRead: INotification[];
61
+ };
62
+ };
@@ -4,7 +4,6 @@ export const MARK_AS_READ = gql `
4
4
  mutation MarkNotificationAsread($id: Int!) {
5
5
  restricted {
6
6
  markNotificationAsRead(id: $id) {
7
- actionType
8
7
  id
9
8
  read
10
9
  }
@@ -53,10 +52,38 @@ export const MARK_ALL_AS_READ = gql `
53
52
  mutation MarkAllNotificationsAsRead {
54
53
  restricted {
55
54
  markAllNotificationsAsRead {
56
- actionType
57
55
  id
58
56
  read
59
57
  }
60
58
  }
61
59
  }
62
60
  `;
61
+ export const optimisticMarkAsRead = (notification) => () => ({
62
+ restricted: {
63
+ __typename: 'RestrictedMutation',
64
+ markNotificationAsRead: {
65
+ ...notification,
66
+ read: true,
67
+ },
68
+ },
69
+ });
70
+ export const optimisticMarkAllAsRead = (notifications = []) => () => {
71
+ const index = notifications.findIndex(({ read }) => read);
72
+ const updatedNotification = notifications
73
+ .slice(0, index)
74
+ .map((notification) => ({ ...notification, read: true }));
75
+ return {
76
+ restricted: {
77
+ __typename: 'RestrictedMutation',
78
+ markAllNotificationsAsRead: [
79
+ ...updatedNotification,
80
+ ...notifications.slice(index).sort((n1, n2) => {
81
+ if (n1.read === n2.read) {
82
+ return n1.createdAt < n2.createdAt ? -1 : 1;
83
+ }
84
+ return n1.read ? 1 : -1;
85
+ }),
86
+ ],
87
+ },
88
+ };
89
+ };
@@ -9,15 +9,21 @@ interface LikeData {
9
9
  }
10
10
  export declare const getLikeData: (like: EQuestionLike, item: LikeData) => LikeData;
11
11
  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;
12
18
  export declare const QUESTION_LIKE: import("@apollo/client").DocumentNode;
13
- export declare const optimisticQuestionLike: (marksheetId: number, client: ApolloClient<any>, input: IQuestionLikeVar) => IQuestionLikeData;
19
+ export declare const optimisticQuestionLike: (client: ApolloClient<any>, input: IQuestionLikeVar, marksheetId?: number | undefined) => IQuestionLikeData;
14
20
  export interface IQuestionLikeVar {
15
21
  questionId: Id;
16
22
  like: EQuestionLike;
17
23
  }
18
24
  export declare type IQuestionLikeData = RestrictedData<graphqlNormalize & IQuestion, 'questionLike'>;
19
25
  export declare const QUESTION_COMMENTS: import("@apollo/client").DocumentNode;
20
- export declare const updateQuestionComments: (marksheetId: number) => (cache: ApolloCache<any>, result: ApolloUpdateResultRestricted<IQuestionCommentsData>, options: ApolloUpdateOptions) => void;
26
+ export declare const updateQuestionComments: (marksheetId?: number | undefined) => (cache: ApolloCache<any>, result: ApolloUpdateResultRestricted<IQuestionCommentsData>, options: ApolloUpdateOptions) => void;
21
27
  export declare const optimisticQuestionComment: (id: number, user: {
22
28
  id: number;
23
29
  displayName: string;
@@ -36,7 +42,7 @@ export interface IQuestionCommentsLikeVar {
36
42
  }
37
43
  export declare type IQuestionCommentsLikeData = RestrictedData<graphqlNormalize & IQuestionComment, 'questionCommentLike'>;
38
44
  export declare const QUESTION_COMMENT_REMOVE: import("@apollo/client").DocumentNode;
39
- export declare const updateQuestionCommentsRemove: (marksheetId: number) => (cache: ApolloCache<any>, result: ApolloUpdateResultRestricted<IQuestionCommentRemoveData>, options: ApolloUpdateOptions) => void;
45
+ export declare const updateQuestionCommentsRemove: (marksheetId?: number | undefined) => (cache: ApolloCache<any>, result: ApolloUpdateResultRestricted<IQuestionCommentRemoveData>, options: ApolloUpdateOptions) => void;
40
46
  export declare const optimisticQuestionCommentRemove: (comments: IQuestionComment[], input: IQuestionCommentRemoveVar, parentId?: number | undefined) => IQuestionCommentRemoveData;
41
47
  export interface IQuestionCommentRemoveVar {
42
48
  commentId: Id;
@@ -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) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quesmed/types",
3
- "version": "2.2.90",
3
+ "version": "2.2.92",
4
4
  "description": "Typescript types for Quesmed",
5
5
  "keywords": [
6
6
  "quesmed",