@go-avro/avro-js 0.0.2-beta.17 → 0.0.2-beta.170

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.
Files changed (64) hide show
  1. package/README.md +1 -0
  2. package/dist/auth/AuthManager.d.ts +14 -5
  3. package/dist/auth/AuthManager.js +59 -23
  4. package/dist/auth/storage.d.ts +8 -8
  5. package/dist/auth/storage.js +19 -14
  6. package/dist/client/AvroQueryClientProvider.d.ts +14 -0
  7. package/dist/client/AvroQueryClientProvider.js +32 -0
  8. package/dist/client/QueryClient.d.ts +529 -16
  9. package/dist/client/QueryClient.js +397 -214
  10. package/dist/client/core/fetch.d.ts +1 -0
  11. package/dist/client/core/fetch.js +64 -0
  12. package/dist/client/core/utils.d.ts +1 -0
  13. package/dist/client/core/utils.js +14 -0
  14. package/dist/client/core/xhr.d.ts +1 -0
  15. package/dist/client/core/xhr.js +90 -0
  16. package/dist/client/hooks/analytics.d.ts +1 -0
  17. package/dist/client/hooks/analytics.js +26 -0
  18. package/dist/client/hooks/avro.d.ts +1 -0
  19. package/dist/client/hooks/avro.js +9 -0
  20. package/dist/client/hooks/bills.d.ts +1 -0
  21. package/dist/client/hooks/bills.js +165 -0
  22. package/dist/client/hooks/catalog_items.d.ts +1 -0
  23. package/dist/client/hooks/catalog_items.js +90 -0
  24. package/dist/client/hooks/chats.d.ts +1 -0
  25. package/dist/client/hooks/chats.js +37 -0
  26. package/dist/client/hooks/companies.d.ts +1 -0
  27. package/dist/client/hooks/companies.js +185 -0
  28. package/dist/client/hooks/events.d.ts +1 -0
  29. package/dist/client/hooks/events.js +308 -0
  30. package/dist/client/hooks/groups.d.ts +1 -0
  31. package/dist/client/hooks/groups.js +130 -0
  32. package/dist/client/hooks/jobs.d.ts +1 -0
  33. package/dist/client/hooks/jobs.js +296 -0
  34. package/dist/client/hooks/labels.d.ts +1 -0
  35. package/dist/client/hooks/labels.js +130 -0
  36. package/dist/client/hooks/messages.d.ts +1 -0
  37. package/dist/client/hooks/messages.js +30 -0
  38. package/dist/client/hooks/months.d.ts +1 -0
  39. package/dist/client/hooks/months.js +93 -0
  40. package/dist/client/hooks/plans.d.ts +1 -0
  41. package/dist/client/hooks/plans.js +8 -0
  42. package/dist/client/hooks/proposal.d.ts +1 -0
  43. package/dist/client/hooks/proposal.js +22 -0
  44. package/dist/client/hooks/root.d.ts +1 -0
  45. package/dist/client/hooks/root.js +8 -0
  46. package/dist/client/hooks/routes.d.ts +1 -0
  47. package/dist/client/hooks/routes.js +174 -0
  48. package/dist/client/hooks/sessions.d.ts +1 -0
  49. package/dist/client/hooks/sessions.js +175 -0
  50. package/dist/client/hooks/skills.d.ts +1 -0
  51. package/dist/client/hooks/skills.js +123 -0
  52. package/dist/client/hooks/teams.d.ts +1 -0
  53. package/dist/client/hooks/teams.js +128 -0
  54. package/dist/client/hooks/users.d.ts +1 -0
  55. package/dist/client/hooks/users.js +118 -0
  56. package/dist/index.d.ts +27 -1
  57. package/dist/index.js +27 -1
  58. package/dist/types/api.d.ts +243 -53
  59. package/dist/types/api.js +57 -1
  60. package/dist/types/auth.d.ts +6 -5
  61. package/dist/types/auth.js +5 -1
  62. package/dist/types/cache.d.ts +9 -0
  63. package/dist/types/cache.js +1 -0
  64. package/package.json +6 -4
@@ -0,0 +1,185 @@
1
+ import { useMutation, useQuery } from '@tanstack/react-query';
2
+ import { AvroQueryClient } from '../../client/QueryClient';
3
+ import { AuthState } from '../../types/auth';
4
+ AvroQueryClient.prototype.useGetCompanies = function (options = {}) {
5
+ return useQuery({
6
+ queryKey: ['/company/list'],
7
+ queryFn: () => {
8
+ const companiesPromise = this.get(`/company/list`);
9
+ companiesPromise.then((companies) => {
10
+ if (!companies.find((c) => c.id === this.companyId) && companies.length > 0) {
11
+ this.companyId = companies[0].id;
12
+ }
13
+ });
14
+ return companiesPromise;
15
+ },
16
+ enabled: this.getAuthState() === AuthState.AUTHENTICATED,
17
+ ...options,
18
+ });
19
+ };
20
+ AvroQueryClient.prototype.useGetCompany = function (companyId) {
21
+ return useQuery({
22
+ queryKey: ['company', companyId],
23
+ queryFn: () => this.get(`/company/${companyId}`),
24
+ enabled: Boolean(companyId),
25
+ });
26
+ };
27
+ AvroQueryClient.prototype.useGetCurrentCompany = function () {
28
+ return useQuery({
29
+ queryKey: ['company', 'current'],
30
+ queryFn: async () => {
31
+ if (!this.companyId) {
32
+ this.companyId = await this.config.authManager.getCompanyId();
33
+ }
34
+ if (!this.companyId) {
35
+ const companyList = await this.get(`/company/list`);
36
+ if (companyList.length > 0) {
37
+ this.companyId = companyList[0].id;
38
+ }
39
+ else {
40
+ throw new Error("No company ID set and no companies available");
41
+ }
42
+ }
43
+ this.company = await this.get(`/company/${this.companyId}`);
44
+ return this.company;
45
+ },
46
+ enabled: this.getAuthState() === AuthState.AUTHENTICATED,
47
+ });
48
+ };
49
+ AvroQueryClient.prototype.useCreateCompany = function () {
50
+ const queryClient = this.getQueryClient();
51
+ return useMutation({
52
+ mutationFn: async ({ companyData }) => {
53
+ return this.post(`/company`, JSON.stringify(companyData), undefined, { "Content-Type": "application/json" });
54
+ },
55
+ onSettled: () => {
56
+ queryClient.invalidateQueries({ queryKey: ['/company/list'] });
57
+ },
58
+ });
59
+ };
60
+ AvroQueryClient.prototype.useUpdateCompany = function () {
61
+ const queryClient = this.getQueryClient();
62
+ return useMutation({
63
+ mutationFn: async ({ companyId, companyData, }) => {
64
+ return this.put(`/company/${companyId}`, JSON.stringify(companyData), undefined, { "Content-Type": "application/json" });
65
+ },
66
+ onMutate: async ({ companyId, companyData }) => {
67
+ await queryClient.cancelQueries({ queryKey: ['company', companyId] });
68
+ await queryClient.cancelQueries({ queryKey: ['/company/list'] });
69
+ const previousCompany = queryClient.getQueryData(['company', companyId]);
70
+ const previousCompanyList = queryClient.getQueryData(['/company/list']);
71
+ queryClient.setQueryData(['company', companyId], (oldData) => oldData ? { ...oldData, ...companyData } : undefined);
72
+ queryClient.setQueryData(['/company/list'], (oldList) => {
73
+ if (!oldList)
74
+ return oldList;
75
+ return oldList.map((company) => company?.id === companyId ? { ...company, ...companyData } : company);
76
+ });
77
+ return { previousCompany, previousCompanyList };
78
+ },
79
+ onError: (_err, variables, context) => {
80
+ const { companyId } = variables;
81
+ if (context?.previousCompany) {
82
+ queryClient.setQueryData(['company', companyId], context.previousCompany);
83
+ }
84
+ if (context?.previousCompanyList) {
85
+ queryClient.setQueryData(['/company/list'], context.previousCompanyList);
86
+ }
87
+ },
88
+ onSettled: (_data, _error, variables) => {
89
+ const { companyId } = variables;
90
+ queryClient.invalidateQueries({ queryKey: ['company', companyId] });
91
+ queryClient.invalidateQueries({ queryKey: ['/company/list'] });
92
+ },
93
+ });
94
+ };
95
+ AvroQueryClient.prototype.useCreateUserCompany = function () {
96
+ const queryClient = this.getQueryClient();
97
+ return useMutation({
98
+ mutationFn: async ({ user_id, data }) => {
99
+ if (!user_id) {
100
+ throw new Error("Both userId and companyId are required");
101
+ }
102
+ return this.post(`/company/${this.companyId}/user/${user_id}`, JSON.stringify(data), undefined, { "Content-Type": "application/json" });
103
+ },
104
+ onMutate: async ({ user_id }) => {
105
+ await queryClient.cancelQueries({ queryKey: ['company', this.companyId] });
106
+ const previousCompany = queryClient.getQueryData(['company', this.companyId]);
107
+ queryClient.setQueryData(['company', this.companyId], (oldData) => {
108
+ if (!oldData)
109
+ return oldData;
110
+ return {
111
+ ...oldData,
112
+ users: [...(oldData.users || []), { id: user_id }],
113
+ };
114
+ });
115
+ return { previousCompany };
116
+ },
117
+ onError: (err, variables, context) => {
118
+ if (context?.previousCompany) {
119
+ queryClient.setQueryData(['company', this.companyId], context.previousCompany);
120
+ }
121
+ },
122
+ onSettled: (_data, _error, variables) => {
123
+ queryClient.invalidateQueries({ queryKey: ['company', this.companyId] });
124
+ queryClient.invalidateQueries({ queryKey: ['/company/list'] });
125
+ queryClient.invalidateQueries({ queryKey: ['company', 'current'] });
126
+ },
127
+ });
128
+ };
129
+ AvroQueryClient.prototype.useRemoveUserCompany = function () {
130
+ const queryClient = this.getQueryClient();
131
+ return useMutation({
132
+ mutationFn: async ({ userId }) => {
133
+ return this.delete(`/company/${this.companyId}/user/${userId}`);
134
+ },
135
+ onMutate: async ({ userId }) => {
136
+ await queryClient.cancelQueries({ queryKey: ['company', this.companyId] });
137
+ const previousCompany = queryClient.getQueryData(['company', this.companyId]);
138
+ queryClient.setQueryData(['company', this.companyId], (oldData) => {
139
+ if (!oldData)
140
+ return oldData;
141
+ return {
142
+ ...oldData,
143
+ users: oldData.users.filter((user) => user.id !== userId),
144
+ };
145
+ });
146
+ return { previousCompany };
147
+ },
148
+ onError: (err, variables, context) => {
149
+ if (context?.previousCompany) {
150
+ queryClient.setQueryData(['company', this.companyId], context.previousCompany);
151
+ }
152
+ },
153
+ onSettled: (_data, _error, variables) => {
154
+ queryClient.invalidateQueries({ queryKey: ['company', this.companyId] });
155
+ },
156
+ });
157
+ };
158
+ AvroQueryClient.prototype.useDeleteCompany = function () {
159
+ const queryClient = this.getQueryClient();
160
+ return useMutation({
161
+ mutationFn: async ({ companyId }) => {
162
+ return this.delete(`/company/${companyId}`);
163
+ },
164
+ onMutate: async ({ companyId }) => {
165
+ await queryClient.cancelQueries({ queryKey: ['/company/list'] });
166
+ await queryClient.cancelQueries({ queryKey: ['company', companyId] });
167
+ const previousCompanyList = queryClient.getQueryData(['/company/list']);
168
+ queryClient.setQueryData(['/company/list'], (oldList) => {
169
+ if (!oldList)
170
+ return oldList;
171
+ return oldList.filter((company) => company?.id !== companyId);
172
+ });
173
+ return { previousCompanyList };
174
+ },
175
+ onError: (err, companyId, context) => {
176
+ if (context?.previousCompanyList) {
177
+ queryClient.setQueryData(['/company/list'], context.previousCompanyList);
178
+ }
179
+ },
180
+ onSettled: (_data, _error, companyId) => {
181
+ queryClient.invalidateQueries({ queryKey: ['/company/list'] });
182
+ queryClient.invalidateQueries({ queryKey: ['company', companyId] });
183
+ },
184
+ });
185
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,308 @@
1
+ import { useInfiniteQuery, useQuery, useMutation } from '@tanstack/react-query';
2
+ import { AvroQueryClient } from '../../client/QueryClient';
3
+ AvroQueryClient.prototype.useGetEvents = function (body) {
4
+ const queryClient = this.getQueryClient();
5
+ const result = useInfiniteQuery({
6
+ queryKey: [
7
+ 'events',
8
+ this.companyId,
9
+ body.amt ?? 50,
10
+ body.known_ids ?? [],
11
+ body.unknown_ids ?? [],
12
+ body.query ?? '',
13
+ body.unbilled ?? true,
14
+ body.billed ?? true,
15
+ body.paid ?? true,
16
+ body.jobId ?? '',
17
+ ],
18
+ initialPageParam: 0,
19
+ getNextPageParam: (lastPage, allPages) => {
20
+ if (lastPage.length < (body.amt ?? 50))
21
+ return undefined;
22
+ return allPages.flat().length; // next offset
23
+ },
24
+ queryFn: ({ pageParam = 0 }) => this.fetchEvents({ ...body, offset: pageParam }),
25
+ });
26
+ if (result.data) {
27
+ result.data.pages.forEach((data_page) => {
28
+ data_page.forEach((event) => {
29
+ queryClient.setQueryData(['event', event.id], event);
30
+ });
31
+ });
32
+ }
33
+ return result;
34
+ };
35
+ AvroQueryClient.prototype.useGetEvent = function (eventId) {
36
+ return useQuery({
37
+ queryKey: ['event', eventId],
38
+ queryFn: () => this.get(`/event/${eventId}`),
39
+ enabled: Boolean(eventId),
40
+ });
41
+ };
42
+ AvroQueryClient.prototype.useCreateEvent = function () {
43
+ const queryClient = this.getQueryClient();
44
+ return useMutation({
45
+ mutationFn: ({ eventData }) => {
46
+ return this.post(`/company/${this.companyId}/event`, JSON.stringify(eventData), undefined, {
47
+ "Content-Type": "application/json",
48
+ });
49
+ },
50
+ onMutate: async ({ eventData }) => {
51
+ await queryClient.cancelQueries({ queryKey: ['events'] });
52
+ const previousEvents = queryClient.getQueryData(['events']);
53
+ const previousJob = queryClient.getQueryData(['job', eventData.job_id]);
54
+ const previousJobs = queryClient.getQueryData(['jobs']);
55
+ if (previousJob) {
56
+ const updatedJob = {
57
+ ...previousJob,
58
+ current_event: eventData,
59
+ last_completed_event: (eventData.time_ended ?? -1) > -1 ? eventData : previousJob.last_completed_event,
60
+ };
61
+ updatedJob.tasks = previousJob.tasks.map((task) => {
62
+ if (eventData.tasks?.includes(task.id)) {
63
+ return {
64
+ ...task,
65
+ current_event: eventData,
66
+ last_completed_event: (eventData.time_ended ?? -1) > -1 ? eventData : task.last_completed_event,
67
+ };
68
+ }
69
+ return task;
70
+ });
71
+ const updatedJobs = previousJobs?.map((job) => job.id === updatedJob.id ? updatedJob : job);
72
+ queryClient.setQueryData(['job', previousJob.id], updatedJob);
73
+ queryClient.setQueryData(['jobs'], updatedJobs);
74
+ }
75
+ queryClient.setQueryData(['events'], (oldData) => {
76
+ if (!oldData)
77
+ return [];
78
+ if (oldData.pages) {
79
+ const firstPage = oldData.pages[0] || [];
80
+ return {
81
+ ...oldData,
82
+ pages: [
83
+ [
84
+ {
85
+ ...eventData,
86
+ id: Math.random().toString(36).substring(2, 11),
87
+ company_id: this.companyId,
88
+ },
89
+ ...firstPage,
90
+ ],
91
+ ...oldData.pages.slice(1),
92
+ ],
93
+ };
94
+ }
95
+ if (Array.isArray(oldData)) {
96
+ return [
97
+ {
98
+ ...eventData,
99
+ id: Math.random().toString(36).substring(2, 11),
100
+ company_id: this.companyId,
101
+ },
102
+ ...oldData,
103
+ ];
104
+ }
105
+ return oldData;
106
+ });
107
+ return { previousEvents, previousJob, previousJobs };
108
+ },
109
+ onError: (err, variables, context) => {
110
+ if (context?.previousEvents) {
111
+ queryClient.setQueryData(['events'], context.previousEvents);
112
+ }
113
+ if (context?.previousJob) {
114
+ queryClient.setQueryData(['job', context.previousJob.id], context.previousJob);
115
+ }
116
+ if (context?.previousJobs) {
117
+ queryClient.setQueryData(['jobs'], context.previousJobs);
118
+ }
119
+ },
120
+ onSettled: () => {
121
+ queryClient.invalidateQueries({ queryKey: ['events'] });
122
+ queryClient.invalidateQueries({ queryKey: ['job'] });
123
+ queryClient.invalidateQueries({ queryKey: ['jobs'] });
124
+ queryClient.invalidateQueries({ queryKey: ['company'] });
125
+ },
126
+ });
127
+ };
128
+ AvroQueryClient.prototype.useUpdateEvent = function () {
129
+ const queryClient = this.getQueryClient();
130
+ return useMutation({
131
+ mutationFn: ({ eventId, updates }) => {
132
+ return this.put(`/event/${eventId}`, JSON.stringify(updates), undefined, {
133
+ "Content-Type": "application/json",
134
+ });
135
+ },
136
+ onMutate: async ({ eventId, updates }) => {
137
+ await queryClient.cancelQueries({ queryKey: ['event', eventId] });
138
+ await queryClient.cancelQueries({ queryKey: ['events'] });
139
+ const previousEvent = queryClient.getQueryData(['event', eventId]);
140
+ const previousEvents = queryClient.getQueryData(['events']);
141
+ const previousJob = queryClient.getQueryData(['job', previousEvent?.job_id]);
142
+ const previousJobs = queryClient.getQueryData(['jobs']);
143
+ if (previousJob) {
144
+ const updatedJob = {
145
+ ...previousJob,
146
+ current_event: previousEvent ? { ...previousEvent, ...updates } : previousJob.current_event,
147
+ last_completed_event: (updates.time_ended ?? -1) > -1 && previousEvent ? { ...previousEvent, ...updates } : previousJob.last_completed_event,
148
+ };
149
+ updatedJob.tasks = previousJob.tasks.map((task) => {
150
+ if (updates.tasks?.includes(task.id)) {
151
+ return {
152
+ ...task,
153
+ current_event: previousEvent ? { ...previousEvent, ...updates } : task.current_event,
154
+ last_completed_event: (updates.time_ended ?? -1) > -1 ? (previousEvent ? { ...previousEvent, ...updates } : task.last_completed_event) : task.last_completed_event,
155
+ overdue_time: -task.frequency - task.delay,
156
+ };
157
+ }
158
+ return task;
159
+ });
160
+ const updatedJobs = previousJobs?.map((job) => job.id === updatedJob.id ? updatedJob : job);
161
+ queryClient.setQueryData(['job', previousJob.id], updatedJob);
162
+ queryClient.setQueryData(['jobs'], updatedJobs);
163
+ }
164
+ queryClient.setQueryData(['event', eventId], (oldData) => oldData ? { ...oldData, ...updates } : undefined);
165
+ queryClient.setQueriesData({ queryKey: ['events'] }, (oldData) => {
166
+ if (!oldData)
167
+ return oldData;
168
+ if (oldData.pages) {
169
+ const updatedPages = oldData.pages.map((page) => page.map((event) => event.id === eventId ? { ...event, ...updates } : event));
170
+ return { ...oldData, pages: updatedPages };
171
+ }
172
+ if (Array.isArray(oldData)) {
173
+ return oldData.map((event) => event.id === eventId ? { ...event, ...updates } : event);
174
+ }
175
+ return oldData;
176
+ });
177
+ return { previousEvent, previousEvents, previousJob, previousJobs };
178
+ },
179
+ onError: (_err, variables, context) => {
180
+ const { eventId } = variables;
181
+ if (context?.previousEvent) {
182
+ queryClient.setQueryData(['event', eventId], context.previousEvent);
183
+ }
184
+ if (context?.previousEvents) {
185
+ queryClient.setQueryData(['events'], context.previousEvents);
186
+ }
187
+ if (context?.previousJob) {
188
+ queryClient.setQueryData(['job', context.previousJob.id], context.previousJob);
189
+ }
190
+ if (context?.previousJobs) {
191
+ queryClient.setQueryData(['jobs'], context.previousJobs);
192
+ }
193
+ },
194
+ onSettled: (_data, _error, variables) => {
195
+ const { eventId } = variables;
196
+ queryClient.invalidateQueries({ queryKey: ['event', eventId] });
197
+ queryClient.invalidateQueries({ queryKey: ['events'] });
198
+ queryClient.invalidateQueries({ queryKey: ['job'] });
199
+ queryClient.invalidateQueries({ queryKey: ['jobs'] });
200
+ },
201
+ });
202
+ };
203
+ AvroQueryClient.prototype.useUpdateEvents = function () {
204
+ const queryClient = this.getQueryClient();
205
+ return useMutation({
206
+ mutationFn: async ({ events, action, }) => {
207
+ const eventIds = events.map(event => event.id);
208
+ return this.put(`/company/${this.companyId}/events`, JSON.stringify({
209
+ events: eventIds,
210
+ billed: true,
211
+ paid: action === "paid",
212
+ }), undefined, {
213
+ "Content-Type": "application/json",
214
+ });
215
+ },
216
+ onMutate: async ({ events, action }) => {
217
+ await queryClient.cancelQueries({ queryKey: ['events'] });
218
+ await queryClient.cancelQueries({ queryKey: ['event'] });
219
+ const previousEvents = queryClient.getQueryData(['events']);
220
+ const previousEventObjs = events.map(event => queryClient.getQueryData(['event', event.id]));
221
+ const eventIds = events.map(event => event.id);
222
+ // Optimistically update individual event cache
223
+ eventIds.forEach((eventId, idx) => {
224
+ queryClient.setQueryData(['event', eventId], (oldData) => {
225
+ return oldData
226
+ ? { ...oldData, billed: true, paid: action === "paid" }
227
+ : oldData;
228
+ });
229
+ });
230
+ // Optimistically update events list cache
231
+ queryClient.setQueriesData({ queryKey: ['events'] }, (oldData) => {
232
+ if (!oldData)
233
+ return oldData;
234
+ if (oldData.pages) {
235
+ const updatedPages = oldData.pages.map((page) => page.map((event) => eventIds.includes(event.id)
236
+ ? { ...event, billed: true, paid: action === "paid" }
237
+ : event));
238
+ return { ...oldData, pages: updatedPages };
239
+ }
240
+ if (Array.isArray(oldData)) {
241
+ return oldData.map((event) => eventIds.includes(event.id)
242
+ ? { ...event, billed: true, paid: action === "paid" }
243
+ : event);
244
+ }
245
+ return oldData;
246
+ });
247
+ return { previousEvents, previousEventObjs };
248
+ },
249
+ onError: (err, variables, context) => {
250
+ if (context?.previousEvents) {
251
+ queryClient.setQueryData(['events'], context.previousEvents);
252
+ }
253
+ if (context?.previousEventObjs) {
254
+ context.previousEventObjs.forEach((eventObj) => {
255
+ queryClient.setQueryData(['event', eventObj.id], eventObj);
256
+ });
257
+ }
258
+ },
259
+ onSettled: () => {
260
+ queryClient.invalidateQueries({ queryKey: ['events'] });
261
+ queryClient.invalidateQueries({ queryKey: ['event'] });
262
+ },
263
+ });
264
+ };
265
+ AvroQueryClient.prototype.useDeleteEvent = function () {
266
+ const queryClient = this.getQueryClient();
267
+ return useMutation({
268
+ mutationFn: async ({ eventId, }) => {
269
+ return this.delete(`/event/${eventId}`, undefined, {
270
+ "Content-Type": "application/json",
271
+ });
272
+ },
273
+ onMutate: async ({ eventId }) => {
274
+ await queryClient.cancelQueries({ queryKey: ['events'] });
275
+ await queryClient.cancelQueries({ queryKey: ['event', eventId] });
276
+ const previousEvents = queryClient.getQueryData(['events']);
277
+ const previousEvent = queryClient.getQueryData(['event', eventId]);
278
+ queryClient.setQueryData(['event', eventId], undefined);
279
+ queryClient.setQueriesData({ queryKey: ['events'] }, (oldData) => {
280
+ if (!oldData)
281
+ return oldData;
282
+ if (oldData.pages) {
283
+ const updatedPages = oldData.pages.map((page) => page.filter((event) => event.id !== eventId));
284
+ return { ...oldData, pages: updatedPages };
285
+ }
286
+ if (Array.isArray(oldData)) {
287
+ return oldData.filter((event) => event.id !== eventId);
288
+ }
289
+ return oldData;
290
+ });
291
+ return { previousEvents, previousEvent };
292
+ },
293
+ onError: (_err, variables, context) => {
294
+ const { eventId } = variables;
295
+ if (context?.previousEvents) {
296
+ queryClient.setQueryData(['events'], context.previousEvents);
297
+ }
298
+ if (context?.previousEvent) {
299
+ queryClient.setQueryData(['event', eventId], context.previousEvent);
300
+ }
301
+ },
302
+ onSettled: (_data, _error, variables) => {
303
+ const { eventId } = variables;
304
+ queryClient.invalidateQueries({ queryKey: ['events'] });
305
+ queryClient.invalidateQueries({ queryKey: ['event', eventId] });
306
+ },
307
+ });
308
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,130 @@
1
+ import { useMutation, useQuery } from "@tanstack/react-query";
2
+ import { AvroQueryClient } from "../../client/QueryClient";
3
+ AvroQueryClient.prototype.useGetGroups = function (body, total, onProgress) {
4
+ return useQuery({
5
+ queryKey: ['groups', this.companyId, body.amt ?? 50, body.query ?? "", total ?? "all"],
6
+ queryFn: async () => {
7
+ if (typeof total !== "number") {
8
+ return this.fetchGroups({ ...body, offset: 0 });
9
+ }
10
+ onProgress?.(0);
11
+ const pageCount = body.amt ? Math.ceil(total / body.amt) + 1 : 1;
12
+ let completed = 0;
13
+ const promises = Array.from({ length: pageCount }, (_, i) => this.fetchGroups({
14
+ ...body,
15
+ offset: i * (body.amt ?? 1),
16
+ }));
17
+ const trackedPromises = promises.map((promise) => promise.then((result) => {
18
+ completed++;
19
+ const fraction = completed / pageCount;
20
+ onProgress?.(fraction);
21
+ return result;
22
+ }));
23
+ const pages = await Promise.all(trackedPromises);
24
+ const groups = pages.flat();
25
+ return groups;
26
+ },
27
+ });
28
+ };
29
+ AvroQueryClient.prototype.useCreateGroup = function () {
30
+ const queryClient = this.getQueryClient();
31
+ return useMutation({
32
+ mutationFn: async ({ groupData }) => {
33
+ return this.post(`/company/${this.companyId}/group`, JSON.stringify(groupData), undefined, { "Content-Type": "application/json" });
34
+ },
35
+ onSettled: () => {
36
+ queryClient.invalidateQueries({ queryKey: ['company'] });
37
+ queryClient.invalidateQueries({ queryKey: ['groups'] });
38
+ },
39
+ });
40
+ };
41
+ AvroQueryClient.prototype.useUpdateGroup = function () {
42
+ const queryClient = this.getQueryClient();
43
+ return useMutation({
44
+ mutationFn: async ({ groupId, groupData }) => {
45
+ return this.put(`/group/${groupId}`, JSON.stringify(groupData), undefined, { "Content-Type": "application/json" });
46
+ },
47
+ onMutate: async ({ groupId, groupData }) => {
48
+ await queryClient.cancelQueries({ queryKey: ['groups'] });
49
+ await queryClient.cancelQueries({ queryKey: ['group', groupId] });
50
+ const previousGroups = queryClient.getQueryData(['groups']);
51
+ const previousGroup = queryClient.getQueryData(['group', groupId]);
52
+ queryClient.setQueryData(['group', groupId], (oldData) => {
53
+ if (!oldData)
54
+ return oldData;
55
+ return { ...oldData, ...groupData };
56
+ });
57
+ queryClient.setQueriesData({ queryKey: ['groups'] }, (oldData) => {
58
+ if (!oldData)
59
+ return oldData;
60
+ if (oldData.pages) {
61
+ const updatedPages = oldData.pages.map((page) => page.map((group) => group.id === groupId ? { ...group, ...groupData } : group));
62
+ return { ...oldData, pages: updatedPages };
63
+ }
64
+ if (Array.isArray(oldData)) {
65
+ return oldData.map((group) => group.id === groupId ? { ...group, ...groupData } : group);
66
+ }
67
+ return oldData;
68
+ });
69
+ return { previousGroups, previousGroup };
70
+ },
71
+ onError: (_err, variables, context) => {
72
+ const { groupId } = variables;
73
+ if (context?.previousGroups) {
74
+ queryClient.setQueryData(['groups'], context.previousGroups);
75
+ }
76
+ if (context?.previousGroup) {
77
+ queryClient.setQueryData(['group', groupId], context.previousGroup);
78
+ }
79
+ },
80
+ onSettled: (_data, _error, variables) => {
81
+ const { groupId } = variables;
82
+ queryClient.invalidateQueries({ queryKey: ['groups'] });
83
+ queryClient.invalidateQueries({ queryKey: ['group', groupId] });
84
+ queryClient.invalidateQueries({ queryKey: ['company'] });
85
+ },
86
+ });
87
+ };
88
+ AvroQueryClient.prototype.useDeleteGroup = function () {
89
+ const queryClient = this.getQueryClient();
90
+ return useMutation({
91
+ mutationFn: async ({ groupId, }) => {
92
+ return this.delete(`/group/${groupId}`);
93
+ },
94
+ onMutate: async ({ groupId }) => {
95
+ await queryClient.cancelQueries({ queryKey: ['groups'] });
96
+ await queryClient.cancelQueries({ queryKey: ['group', groupId] });
97
+ const previousGroups = queryClient.getQueryData(['groups']);
98
+ const previousGroup = queryClient.getQueryData(['group', groupId]);
99
+ queryClient.setQueryData(['group', groupId], undefined);
100
+ queryClient.setQueriesData({ queryKey: ['groups'] }, (oldData) => {
101
+ if (!oldData)
102
+ return oldData;
103
+ if (oldData.pages) {
104
+ const updatedPages = oldData.pages.map((page) => page.filter((group) => group.id !== groupId));
105
+ return { ...oldData, pages: updatedPages };
106
+ }
107
+ if (Array.isArray(oldData)) {
108
+ return oldData.filter((group) => group.id !== groupId);
109
+ }
110
+ return oldData;
111
+ });
112
+ return { previousGroups, previousGroup };
113
+ },
114
+ onError: (_err, variables, context) => {
115
+ const { groupId } = variables;
116
+ if (context?.previousGroups) {
117
+ queryClient.setQueryData(['groups'], context.previousGroups);
118
+ }
119
+ if (context?.previousGroup) {
120
+ queryClient.setQueryData(['group', groupId], context.previousGroup);
121
+ }
122
+ },
123
+ onSettled: (_data, _error, variables) => {
124
+ const { groupId } = variables;
125
+ queryClient.invalidateQueries({ queryKey: ['groups'] });
126
+ queryClient.invalidateQueries({ queryKey: ['group', groupId] });
127
+ queryClient.invalidateQueries({ queryKey: ['company'] });
128
+ },
129
+ });
130
+ };
@@ -0,0 +1 @@
1
+ export {};