@go-avro/avro-js 0.0.2-beta.13 → 0.0.2-beta.131

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 (53) hide show
  1. package/README.md +1 -0
  2. package/dist/auth/AuthManager.d.ts +12 -3
  3. package/dist/auth/AuthManager.js +56 -12
  4. package/dist/auth/storage.d.ts +8 -8
  5. package/dist/auth/storage.js +12 -10
  6. package/dist/client/QueryClient.d.ts +409 -15
  7. package/dist/client/QueryClient.js +343 -203
  8. package/dist/client/core/fetch.d.ts +1 -0
  9. package/dist/client/core/fetch.js +62 -0
  10. package/dist/client/core/utils.d.ts +1 -0
  11. package/dist/client/core/utils.js +14 -0
  12. package/dist/client/core/xhr.d.ts +1 -0
  13. package/dist/client/core/xhr.js +84 -0
  14. package/dist/client/hooks/analytics.d.ts +1 -0
  15. package/dist/client/hooks/analytics.js +26 -0
  16. package/dist/client/hooks/avro.d.ts +1 -0
  17. package/dist/client/hooks/avro.js +9 -0
  18. package/dist/client/hooks/bills.d.ts +1 -0
  19. package/dist/client/hooks/bills.js +165 -0
  20. package/dist/client/hooks/chats.d.ts +1 -0
  21. package/dist/client/hooks/chats.js +37 -0
  22. package/dist/client/hooks/companies.d.ts +1 -0
  23. package/dist/client/hooks/companies.js +137 -0
  24. package/dist/client/hooks/events.d.ts +1 -0
  25. package/dist/client/hooks/events.js +308 -0
  26. package/dist/client/hooks/jobs.d.ts +1 -0
  27. package/dist/client/hooks/jobs.js +215 -0
  28. package/dist/client/hooks/messages.d.ts +1 -0
  29. package/dist/client/hooks/messages.js +30 -0
  30. package/dist/client/hooks/months.d.ts +1 -0
  31. package/dist/client/hooks/months.js +93 -0
  32. package/dist/client/hooks/plans.d.ts +1 -0
  33. package/dist/client/hooks/plans.js +8 -0
  34. package/dist/client/hooks/root.d.ts +1 -0
  35. package/dist/client/hooks/root.js +8 -0
  36. package/dist/client/hooks/routes.d.ts +1 -0
  37. package/dist/client/hooks/routes.js +167 -0
  38. package/dist/client/hooks/sessions.d.ts +1 -0
  39. package/dist/client/hooks/sessions.js +175 -0
  40. package/dist/client/hooks/skills.d.ts +1 -0
  41. package/dist/client/hooks/skills.js +123 -0
  42. package/dist/client/hooks/teams.d.ts +1 -0
  43. package/dist/client/hooks/teams.js +128 -0
  44. package/dist/client/hooks/users.d.ts +1 -0
  45. package/dist/client/hooks/users.js +104 -0
  46. package/dist/index.d.ts +22 -1
  47. package/dist/index.js +22 -1
  48. package/dist/types/api.d.ts +124 -32
  49. package/dist/types/api.js +10 -1
  50. package/dist/types/auth.d.ts +0 -5
  51. package/dist/types/cache.d.ts +9 -0
  52. package/dist/types/cache.js +1 -0
  53. package/package.json +6 -4
@@ -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,215 @@
1
+ import { useMutation, useQuery, useInfiniteQuery } from '@tanstack/react-query';
2
+ import { AvroQueryClient } from '../../client/QueryClient';
3
+ AvroQueryClient.prototype.getJobsFromCache = function () {
4
+ const queryClient = this.getQueryClient();
5
+ return (queryClient.getQueryData(['jobs', this.companyId, this.company?.num_jobs]) ?? []);
6
+ };
7
+ AvroQueryClient.prototype.useGetJobs = function (onProgress) {
8
+ const queryClient = this.getQueryClient();
9
+ const amt = 50;
10
+ return useQuery({
11
+ queryKey: ['jobs', this.companyId, this.company?.num_jobs],
12
+ queryFn: async () => {
13
+ if (this.company?.num_jobs === 0) {
14
+ onProgress?.(1);
15
+ return [];
16
+ }
17
+ onProgress?.(0);
18
+ const pageCount = amt ? Math.ceil((this.company?.num_jobs ?? 0) / amt) : 0;
19
+ let completed = 0;
20
+ const promises = Array.from({ length: pageCount }, (_, i) => this.fetchJobs({ offset: i * amt, amt }));
21
+ const trackedPromises = promises.map((promise) => promise.then((result) => {
22
+ completed++;
23
+ const fraction = completed / pageCount;
24
+ onProgress?.(fraction);
25
+ return result;
26
+ }));
27
+ const pages = await Promise.all(trackedPromises);
28
+ const jobs = pages.flat();
29
+ jobs.forEach((job) => {
30
+ job.current_event = job.tasks.reduce((latest, task) => {
31
+ return task.current_event && (!latest || task.current_event.time_started > latest.time_started) ? task.current_event : latest;
32
+ }, null);
33
+ job.last_completed_event = job.tasks.reduce((latest, task) => {
34
+ return task.last_completed_event && (!latest || task.last_completed_event.time_started > latest.time_started) ? task.last_completed_event : latest;
35
+ }, null);
36
+ job.overdue_time = job.tasks.reduce((maxOverdue, task) => {
37
+ return task.overdue_time && task.overdue_time > maxOverdue ? task.overdue_time : maxOverdue;
38
+ }, 0);
39
+ queryClient.setQueryData(['job', job.id], job);
40
+ });
41
+ return jobs;
42
+ },
43
+ });
44
+ };
45
+ AvroQueryClient.prototype.useGetInfiniteJobs = function (body, onProgress) {
46
+ const queryClient = this.getQueryClient();
47
+ const result = useInfiniteQuery({
48
+ queryKey: ['infinite', 'jobs', this.companyId, body.amt ?? 50, body.query ?? "", body.routeId ?? ""],
49
+ initialPageParam: 0,
50
+ getNextPageParam: (lastPage, allPages) => {
51
+ if (lastPage.length < (body.amt ?? 50))
52
+ return undefined;
53
+ return allPages.flat().length;
54
+ },
55
+ queryFn: ({ pageParam = 0 }) => {
56
+ onProgress?.(0);
57
+ return this.fetchJobs({
58
+ ...body,
59
+ route_id: body.routeId,
60
+ offset: pageParam,
61
+ });
62
+ },
63
+ });
64
+ if (result.data) {
65
+ result.data.pages.forEach((data_page) => {
66
+ data_page.forEach((job) => {
67
+ job.overdue_time = (job.tasks || []).reduce((maxOverdue, task) => {
68
+ return (task.overdue_time && task.overdue_time > maxOverdue) ? task.overdue_time : maxOverdue;
69
+ }, 0);
70
+ queryClient.setQueryData(['job', job.id], job);
71
+ });
72
+ });
73
+ }
74
+ return result;
75
+ };
76
+ AvroQueryClient.prototype.useGetJob = function (jobId) {
77
+ return useQuery({
78
+ queryKey: ['job', jobId],
79
+ queryFn: () => this.get(`/job/${jobId}`),
80
+ enabled: Boolean(jobId),
81
+ });
82
+ };
83
+ AvroQueryClient.prototype.useCreateJob = function () {
84
+ const queryClient = this.getQueryClient();
85
+ return useMutation({
86
+ mutationFn: ({ jobData }) => {
87
+ return this.post(`/company/${this.companyId}/job`, JSON.stringify(jobData), undefined, {
88
+ "Content-Type": "application/json",
89
+ });
90
+ },
91
+ onMutate: async ({ jobData }) => {
92
+ await queryClient.cancelQueries({ queryKey: ['jobs'] });
93
+ const previousJobs = queryClient.getQueryData(['jobs']);
94
+ queryClient.setQueryData(['jobs'], (oldData) => {
95
+ if (!oldData)
96
+ return [jobData];
97
+ if (oldData.pages) {
98
+ const firstPage = oldData.pages[0] || [];
99
+ return {
100
+ ...oldData,
101
+ pages: [[jobData, ...firstPage], ...oldData.pages.slice(1)],
102
+ };
103
+ }
104
+ if (Array.isArray(oldData)) {
105
+ return [jobData, ...oldData];
106
+ }
107
+ return oldData;
108
+ });
109
+ return { previousJobs };
110
+ },
111
+ onError: (err, variables, context) => {
112
+ if (context?.previousJobs) {
113
+ queryClient.setQueryData(['jobs'], context.previousJobs);
114
+ }
115
+ },
116
+ onSettled: (data, error, variables) => {
117
+ const { id: jobId } = data ?? {};
118
+ queryClient.invalidateQueries({ queryKey: ['jobs'] });
119
+ queryClient.invalidateQueries({ queryKey: ['job', jobId] });
120
+ queryClient.invalidateQueries({ queryKey: ['company'] });
121
+ },
122
+ });
123
+ };
124
+ AvroQueryClient.prototype.useUpdateJob = function () {
125
+ const queryClient = this.getQueryClient();
126
+ return useMutation({
127
+ mutationFn: ({ jobId, updates }) => {
128
+ return this.put(`/job/${jobId}`, JSON.stringify(updates), undefined, {
129
+ "Content-Type": "application/json",
130
+ });
131
+ },
132
+ onMutate: async ({ jobId, updates }) => {
133
+ await queryClient.cancelQueries({ queryKey: ['jobs'] });
134
+ await queryClient.cancelQueries({ queryKey: ['job', jobId] });
135
+ const previousJobs = queryClient.getQueryData(['jobs']);
136
+ const previousJob = queryClient.getQueryData(['job', jobId]);
137
+ queryClient.setQueryData(['job', jobId], (oldData) => oldData ? { ...oldData, ...updates } : undefined);
138
+ queryClient.setQueriesData({ queryKey: ['jobs'] }, (oldData) => {
139
+ if (!oldData)
140
+ return oldData;
141
+ if (oldData.pages) {
142
+ return {
143
+ ...oldData,
144
+ pages: oldData.pages.map((page) => page.map((job) => job.id === jobId ? { ...job, ...updates } : job)),
145
+ };
146
+ }
147
+ if (Array.isArray(oldData)) {
148
+ return oldData.map((job) => job.id === jobId ? { ...job, ...updates } : job);
149
+ }
150
+ return oldData;
151
+ });
152
+ return { previousJobs, previousJob };
153
+ },
154
+ onError: (err, variables, context) => {
155
+ const { jobId } = variables;
156
+ if (context?.previousJobs) {
157
+ queryClient.setQueryData(['jobs'], context.previousJobs);
158
+ }
159
+ if (context?.previousJob) {
160
+ queryClient.setQueryData(['job', jobId], context.previousJob);
161
+ }
162
+ },
163
+ onSettled: (data, error, variables) => {
164
+ const { jobId } = variables;
165
+ queryClient.invalidateQueries({ queryKey: ['jobs'] });
166
+ queryClient.invalidateQueries({ queryKey: ['job', jobId] });
167
+ },
168
+ });
169
+ };
170
+ AvroQueryClient.prototype.useDeleteJob = function () {
171
+ const queryClient = this.getQueryClient();
172
+ return useMutation({
173
+ mutationFn: async ({ jobId }) => {
174
+ return this.delete(`/job/${jobId}`, undefined, {
175
+ "Content-Type": "application/json",
176
+ });
177
+ },
178
+ onMutate: async ({ jobId }) => {
179
+ await queryClient.cancelQueries({ queryKey: ['jobs'] });
180
+ await queryClient.cancelQueries({ queryKey: ['job', jobId] });
181
+ const previousJobs = queryClient.getQueryData(['jobs']);
182
+ const previousJob = queryClient.getQueryData(['job', jobId]);
183
+ queryClient.setQueryData(['job', jobId], undefined);
184
+ queryClient.setQueriesData({ queryKey: ['jobs'] }, (oldData) => {
185
+ if (!oldData)
186
+ return oldData;
187
+ if (oldData.pages) {
188
+ const updatedPages = oldData.pages.map((page) => page.filter((job) => job.id !== jobId));
189
+ return { ...oldData, pages: updatedPages };
190
+ }
191
+ if (Array.isArray(oldData)) {
192
+ return oldData.filter((job) => job.id !== jobId);
193
+ }
194
+ return oldData;
195
+ });
196
+ return { previousJobs, previousJob };
197
+ },
198
+ onError: (_err, variables, context) => {
199
+ const { jobId } = variables;
200
+ if (context?.previousJobs) {
201
+ queryClient.setQueryData(['jobs'], context.previousJobs);
202
+ }
203
+ if (context?.previousJob) {
204
+ queryClient.setQueryData(['job', jobId], context.previousJob);
205
+ }
206
+ },
207
+ onSettled: (_data, _error, variables) => {
208
+ const { jobId } = variables;
209
+ queryClient.invalidateQueries({ queryKey: ['jobs'] });
210
+ queryClient.invalidateQueries({ queryKey: ['routes'] });
211
+ queryClient.invalidateQueries({ queryKey: ['job', jobId] });
212
+ queryClient.invalidateQueries({ queryKey: ['route'] });
213
+ },
214
+ });
215
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,30 @@
1
+ import { useInfiniteQuery } from "@tanstack/react-query";
2
+ import { AvroQueryClient } from "../../client/QueryClient";
3
+ AvroQueryClient.prototype.useGetMessages = function (chatId, body) {
4
+ const queryClient = this.getQueryClient();
5
+ const result = useInfiniteQuery({
6
+ queryKey: [
7
+ 'messages',
8
+ chatId,
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(chatId, { ...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,93 @@
1
+ import { useInfiniteQuery, useMutation } from '@tanstack/react-query';
2
+ import { AvroQueryClient } from '../../client/QueryClient';
3
+ AvroQueryClient.prototype.useGetMonths = function (body) {
4
+ const queryClient = this.getQueryClient();
5
+ const result = useInfiniteQuery({
6
+ queryKey: [
7
+ 'months',
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.fetchMonths({ ...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 = this.getQueryClient();
37
+ return useMutation({
38
+ mutationFn: async ({ months, action, }) => {
39
+ const monthIds = months.map(month => month.id);
40
+ return this.put(`/company/${this.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
+ queryClient.invalidateQueries({ queryKey: ['company'] });
91
+ },
92
+ });
93
+ };
@@ -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.useGetPlans = function (code) {
4
+ return useQuery({
5
+ queryKey: ['plans', code],
6
+ queryFn: async () => this.get(`/plans${code ? `?code=${code}` : ''}`),
7
+ });
8
+ };
@@ -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
+ };