@go-avro/avro-js 0.0.2-beta.15 → 0.0.2-beta.150

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