@go-avro/avro-js 0.0.2-beta.9 → 0.0.2-beta.91

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 +18 -2
  4. package/dist/client/QueryClient.d.ts +349 -15
  5. package/dist/client/QueryClient.js +303 -195
  6. package/dist/client/core/fetch.d.ts +1 -0
  7. package/dist/client/core/fetch.js +62 -0
  8. package/dist/client/core/utils.d.ts +1 -0
  9. package/dist/client/core/utils.js +14 -0
  10. package/dist/client/core/xhr.d.ts +1 -0
  11. package/dist/client/core/xhr.js +84 -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 +185 -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,185 @@
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 ?? "", body.routeId ?? ""],
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
+ route_id: body.routeId,
18
+ offset: i * (body.amt ?? 0),
19
+ }));
20
+ const trackedPromises = promises.map((promise) => promise.then((result) => {
21
+ completed++;
22
+ const fraction = completed / pageCount;
23
+ onProgress?.(fraction);
24
+ return result;
25
+ }));
26
+ const pages = await Promise.all(trackedPromises);
27
+ const jobs = pages.flat();
28
+ jobs.forEach((job) => {
29
+ job.last_event = job.tasks.reduce((latest, task) => {
30
+ return task.last_event && (!latest || task.last_event.time_started > latest.time_started) ? task.last_event : latest;
31
+ }, null);
32
+ job.last_completed_event = job.tasks.reduce((latest, task) => {
33
+ return task.last_completed_event && (!latest || task.last_completed_event.time_started > latest.time_started) ? task.last_completed_event : latest;
34
+ }, null);
35
+ job.overdue_time = job.tasks.reduce((maxOverdue, task) => {
36
+ return task.overdue_time && task.overdue_time > maxOverdue ? task.overdue_time : maxOverdue;
37
+ }, 0);
38
+ queryClient.setQueryData(['job', job.id], job);
39
+ });
40
+ if (isMainLoad) {
41
+ queryClient.setQueryData(['jobs', companyGuid, body.amt ?? 50, body.query ?? ""], jobs);
42
+ }
43
+ return jobs;
44
+ },
45
+ enabled: Boolean(companyGuid) && companyGuid.length > 0 && Boolean(total) && total >= 0,
46
+ ...isMainLoad ? { staleTime: Infinity, cacheTime: Infinity } : {},
47
+ });
48
+ };
49
+ AvroQueryClient.prototype.useGetJob = function (jobId) {
50
+ return useQuery({
51
+ queryKey: ['job', jobId],
52
+ queryFn: () => this.get(`/job/${jobId}`),
53
+ enabled: Boolean(jobId),
54
+ });
55
+ };
56
+ AvroQueryClient.prototype.useCreateJob = function () {
57
+ const queryClient = useQueryClient();
58
+ return useMutation({
59
+ mutationFn: ({ companyId, jobData }) => {
60
+ return this.post(`/company/${companyId}/job`, JSON.stringify(jobData), undefined, {
61
+ "Content-Type": "application/json",
62
+ });
63
+ },
64
+ onMutate: async ({ companyId, jobData }) => {
65
+ await queryClient.cancelQueries({ queryKey: ['jobs'] });
66
+ const previousJobs = queryClient.getQueryData(['jobs']);
67
+ queryClient.setQueryData(['jobs'], (oldData) => {
68
+ if (!oldData)
69
+ return [jobData];
70
+ if (oldData.pages) {
71
+ const firstPage = oldData.pages[0] || [];
72
+ return {
73
+ ...oldData,
74
+ pages: [[jobData, ...firstPage], ...oldData.pages.slice(1)],
75
+ };
76
+ }
77
+ if (Array.isArray(oldData)) {
78
+ return [jobData, ...oldData];
79
+ }
80
+ return oldData;
81
+ });
82
+ return { previousJobs };
83
+ },
84
+ onError: (err, variables, context) => {
85
+ if (context?.previousJobs) {
86
+ queryClient.setQueryData(['jobs'], context.previousJobs);
87
+ }
88
+ },
89
+ onSettled: (data, error, variables) => {
90
+ const { id: jobId } = data ?? {};
91
+ queryClient.invalidateQueries({ queryKey: ['jobs'] });
92
+ queryClient.invalidateQueries({ queryKey: ['job', jobId] });
93
+ },
94
+ });
95
+ };
96
+ AvroQueryClient.prototype.useUpdateJob = function () {
97
+ const queryClient = useQueryClient();
98
+ return useMutation({
99
+ mutationFn: ({ jobId, updates }) => {
100
+ return this.put(`/job/${jobId}`, JSON.stringify(updates), undefined, {
101
+ "Content-Type": "application/json",
102
+ });
103
+ },
104
+ onMutate: async ({ jobId, updates }) => {
105
+ await queryClient.cancelQueries({ queryKey: ['jobs'] });
106
+ await queryClient.cancelQueries({ queryKey: ['job', jobId] });
107
+ const previousJobs = queryClient.getQueryData(['jobs']);
108
+ const previousJob = queryClient.getQueryData(['job', jobId]);
109
+ queryClient.setQueryData(['job', jobId], (oldData) => oldData ? { ...oldData, ...updates } : undefined);
110
+ queryClient.setQueriesData({ queryKey: ['jobs'] }, (oldData) => {
111
+ if (!oldData)
112
+ return oldData;
113
+ if (oldData.pages) {
114
+ return {
115
+ ...oldData,
116
+ pages: oldData.pages.map((page) => page.map((job) => job.id === jobId ? { ...job, ...updates } : job)),
117
+ };
118
+ }
119
+ if (Array.isArray(oldData)) {
120
+ return oldData.map((job) => job.id === jobId ? { ...job, ...updates } : job);
121
+ }
122
+ return oldData;
123
+ });
124
+ return { previousJobs, previousJob };
125
+ },
126
+ onError: (err, variables, context) => {
127
+ const { jobId } = variables;
128
+ if (context?.previousJobs) {
129
+ queryClient.setQueryData(['jobs'], context.previousJobs);
130
+ }
131
+ if (context?.previousJob) {
132
+ queryClient.setQueryData(['job', jobId], context.previousJob);
133
+ }
134
+ },
135
+ onSettled: (data, error, variables) => {
136
+ const { jobId } = variables;
137
+ queryClient.invalidateQueries({ queryKey: ['jobs'] });
138
+ queryClient.invalidateQueries({ queryKey: ['job', jobId] });
139
+ },
140
+ });
141
+ };
142
+ AvroQueryClient.prototype.useDeleteJob = function () {
143
+ const queryClient = useQueryClient();
144
+ return useMutation({
145
+ mutationFn: async ({ jobId }) => {
146
+ return this.delete(`/job/${jobId}`, undefined, {
147
+ "Content-Type": "application/json",
148
+ });
149
+ },
150
+ onMutate: async ({ jobId }) => {
151
+ await queryClient.cancelQueries({ queryKey: ['jobs'] });
152
+ await queryClient.cancelQueries({ queryKey: ['job', jobId] });
153
+ const previousJobs = queryClient.getQueryData(['jobs']);
154
+ const previousJob = queryClient.getQueryData(['job', jobId]);
155
+ queryClient.setQueryData(['job', jobId], undefined);
156
+ queryClient.setQueriesData({ queryKey: ['jobs'] }, (oldData) => {
157
+ if (!oldData)
158
+ return oldData;
159
+ if (oldData.pages) {
160
+ const updatedPages = oldData.pages.map((page) => page.filter((job) => job.id !== jobId));
161
+ return { ...oldData, pages: updatedPages };
162
+ }
163
+ if (Array.isArray(oldData)) {
164
+ return oldData.filter((job) => job.id !== jobId);
165
+ }
166
+ return oldData;
167
+ });
168
+ return { previousJobs, previousJob };
169
+ },
170
+ onError: (_err, variables, context) => {
171
+ const { jobId } = variables;
172
+ if (context?.previousJobs) {
173
+ queryClient.setQueryData(['jobs'], context.previousJobs);
174
+ }
175
+ if (context?.previousJob) {
176
+ queryClient.setQueryData(['job', jobId], context.previousJob);
177
+ }
178
+ },
179
+ onSettled: (_data, _error, variables) => {
180
+ const { jobId } = variables;
181
+ queryClient.invalidateQueries({ queryKey: ['jobs'] });
182
+ queryClient.invalidateQueries({ queryKey: ['job', jobId] });
183
+ },
184
+ });
185
+ };
@@ -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 {};