@go-avro/avro-js 0.0.2-beta.8 → 0.0.2-beta.80

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