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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/README.md +1 -0
  2. package/dist/auth/AuthManager.d.ts +14 -5
  3. package/dist/auth/AuthManager.js +59 -23
  4. package/dist/auth/storage.d.ts +8 -8
  5. package/dist/auth/storage.js +19 -14
  6. package/dist/client/AvroQueryClientProvider.d.ts +14 -0
  7. package/dist/client/AvroQueryClientProvider.js +32 -0
  8. package/dist/client/QueryClient.d.ts +529 -16
  9. package/dist/client/QueryClient.js +397 -214
  10. package/dist/client/core/fetch.d.ts +1 -0
  11. package/dist/client/core/fetch.js +64 -0
  12. package/dist/client/core/utils.d.ts +1 -0
  13. package/dist/client/core/utils.js +14 -0
  14. package/dist/client/core/xhr.d.ts +1 -0
  15. package/dist/client/core/xhr.js +90 -0
  16. package/dist/client/hooks/analytics.d.ts +1 -0
  17. package/dist/client/hooks/analytics.js +26 -0
  18. package/dist/client/hooks/avro.d.ts +1 -0
  19. package/dist/client/hooks/avro.js +9 -0
  20. package/dist/client/hooks/bills.d.ts +1 -0
  21. package/dist/client/hooks/bills.js +165 -0
  22. package/dist/client/hooks/catalog_items.d.ts +1 -0
  23. package/dist/client/hooks/catalog_items.js +90 -0
  24. package/dist/client/hooks/chats.d.ts +1 -0
  25. package/dist/client/hooks/chats.js +37 -0
  26. package/dist/client/hooks/companies.d.ts +1 -0
  27. package/dist/client/hooks/companies.js +185 -0
  28. package/dist/client/hooks/events.d.ts +1 -0
  29. package/dist/client/hooks/events.js +308 -0
  30. package/dist/client/hooks/groups.d.ts +1 -0
  31. package/dist/client/hooks/groups.js +130 -0
  32. package/dist/client/hooks/jobs.d.ts +1 -0
  33. package/dist/client/hooks/jobs.js +296 -0
  34. package/dist/client/hooks/labels.d.ts +1 -0
  35. package/dist/client/hooks/labels.js +130 -0
  36. package/dist/client/hooks/messages.d.ts +1 -0
  37. package/dist/client/hooks/messages.js +30 -0
  38. package/dist/client/hooks/months.d.ts +1 -0
  39. package/dist/client/hooks/months.js +93 -0
  40. package/dist/client/hooks/plans.d.ts +1 -0
  41. package/dist/client/hooks/plans.js +8 -0
  42. package/dist/client/hooks/proposal.d.ts +1 -0
  43. package/dist/client/hooks/proposal.js +22 -0
  44. package/dist/client/hooks/root.d.ts +1 -0
  45. package/dist/client/hooks/root.js +8 -0
  46. package/dist/client/hooks/routes.d.ts +1 -0
  47. package/dist/client/hooks/routes.js +174 -0
  48. package/dist/client/hooks/sessions.d.ts +1 -0
  49. package/dist/client/hooks/sessions.js +175 -0
  50. package/dist/client/hooks/skills.d.ts +1 -0
  51. package/dist/client/hooks/skills.js +123 -0
  52. package/dist/client/hooks/teams.d.ts +1 -0
  53. package/dist/client/hooks/teams.js +128 -0
  54. package/dist/client/hooks/users.d.ts +1 -0
  55. package/dist/client/hooks/users.js +118 -0
  56. package/dist/index.d.ts +27 -1
  57. package/dist/index.js +27 -1
  58. package/dist/types/api.d.ts +243 -53
  59. package/dist/types/api.js +57 -1
  60. package/dist/types/auth.d.ts +6 -5
  61. package/dist/types/auth.js +5 -1
  62. package/dist/types/cache.d.ts +9 -0
  63. package/dist/types/cache.js +1 -0
  64. package/package.json +6 -4
@@ -0,0 +1,296 @@
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 (params) {
10
+ const queryClient = this.getQueryClient();
11
+ const amt = 50;
12
+ const company_id = params?.companyId ?? this.companyId;
13
+ const num_jobs = params?.numJobs ?? this.company?.num_jobs ?? 0;
14
+ return useQuery({
15
+ queryKey: ['jobs', company_id, num_jobs],
16
+ queryFn: async () => {
17
+ params?.onProgress?.(0);
18
+ const pageCount = amt ? Math.ceil((num_jobs ?? 0) / amt) + 1 : 1;
19
+ let completed = 0;
20
+ const promises = Array.from({ length: pageCount }, (_, i) => this.fetchJobs({ offset: i * (amt ?? 1), amt }, company_id));
21
+ const trackedPromises = promises.map((promise) => promise.then((result) => {
22
+ completed++;
23
+ const fraction = completed / pageCount;
24
+ params?.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
+ }, job.current_event);
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
+ }, job.last_completed_event);
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.useManageJobs = function () {
125
+ const queryClient = this.getQueryClient();
126
+ return useMutation({
127
+ mutationFn: ({ jobs }) => {
128
+ return this.post(`/company/${this.companyId}/jobs/manage`, JSON.stringify({ jobs }), undefined, {
129
+ "Content-Type": "application/json",
130
+ });
131
+ },
132
+ onMutate: async ({ jobs }) => {
133
+ await queryClient.cancelQueries({ queryKey: ['jobs'] });
134
+ const previousJobs = queryClient.getQueryData(['jobs']);
135
+ queryClient.setQueryData(['jobs'], (oldData) => {
136
+ if (!oldData)
137
+ return oldData;
138
+ if (oldData.pages) {
139
+ return {
140
+ ...oldData,
141
+ pages: oldData.pages.map((page) => page.map((job) => {
142
+ const updatedJob = jobs.find((j) => j.id === job.id);
143
+ return updatedJob ? { ...job, ...updatedJob } : job;
144
+ })),
145
+ };
146
+ }
147
+ if (Array.isArray(oldData)) {
148
+ return oldData.map((job) => {
149
+ const updatedJob = jobs.find((j) => j.id === job.id);
150
+ return updatedJob ? { ...job, ...updatedJob } : job;
151
+ });
152
+ }
153
+ return oldData;
154
+ });
155
+ return { previousJobs };
156
+ },
157
+ onError: (err, variables, context) => {
158
+ if (context?.previousJobs) {
159
+ queryClient.setQueryData(['jobs'], context.previousJobs);
160
+ }
161
+ },
162
+ onSettled: (_data, _error, _variables) => {
163
+ queryClient.invalidateQueries({ queryKey: ['jobs'] });
164
+ },
165
+ });
166
+ };
167
+ AvroQueryClient.prototype.useUpdateJob = function () {
168
+ const queryClient = this.getQueryClient();
169
+ return useMutation({
170
+ mutationFn: ({ jobId, updates }) => {
171
+ return this.put(`/job/${jobId}`, JSON.stringify(updates), undefined, {
172
+ "Content-Type": "application/json",
173
+ });
174
+ },
175
+ onMutate: async ({ jobId, updates }) => {
176
+ await queryClient.cancelQueries({ queryKey: ['jobs'] });
177
+ await queryClient.cancelQueries({ queryKey: ['job', jobId] });
178
+ const previousJobs = queryClient.getQueryData(['jobs']);
179
+ const previousJob = queryClient.getQueryData(['job', jobId]);
180
+ queryClient.setQueryData(['job', jobId], (oldData) => oldData ? { ...oldData, ...updates } : undefined);
181
+ queryClient.setQueriesData({ queryKey: ['jobs'] }, (oldData) => {
182
+ if (!oldData)
183
+ return oldData;
184
+ if (oldData.pages) {
185
+ return {
186
+ ...oldData,
187
+ pages: oldData.pages.map((page) => page.map((job) => job.id === jobId ? { ...job, ...updates } : job)),
188
+ };
189
+ }
190
+ if (Array.isArray(oldData)) {
191
+ return oldData.map((job) => job.id === jobId ? { ...job, ...updates } : job);
192
+ }
193
+ return oldData;
194
+ });
195
+ return { previousJobs, previousJob };
196
+ },
197
+ onError: (err, variables, context) => {
198
+ const { jobId } = variables;
199
+ if (context?.previousJobs) {
200
+ queryClient.setQueryData(['jobs'], context.previousJobs);
201
+ }
202
+ if (context?.previousJob) {
203
+ queryClient.setQueryData(['job', jobId], context.previousJob);
204
+ }
205
+ },
206
+ onSettled: (data, error, variables) => {
207
+ const { jobId } = variables;
208
+ queryClient.invalidateQueries({ queryKey: ['jobs'] });
209
+ queryClient.invalidateQueries({ queryKey: ['job', jobId] });
210
+ },
211
+ });
212
+ };
213
+ AvroQueryClient.prototype.useDeleteJobs = function () {
214
+ const queryClient = this.getQueryClient();
215
+ return useMutation({
216
+ mutationFn: ({ ids }) => {
217
+ return this.post(`/company/${this.companyId}/jobs/delete`, JSON.stringify({ ids }), undefined, {
218
+ "Content-Type": "application/json",
219
+ });
220
+ },
221
+ onMutate: async ({ ids }) => {
222
+ await queryClient.cancelQueries({ queryKey: ['jobs'] });
223
+ const previousJobs = queryClient.getQueryData(['jobs']);
224
+ queryClient.setQueryData(['jobs'], (oldData) => {
225
+ if (!oldData)
226
+ return oldData;
227
+ if (oldData.pages) {
228
+ const updatedPages = oldData.pages.map((page) => page.filter((job) => !ids.includes(job.id)));
229
+ return { ...oldData, pages: updatedPages };
230
+ }
231
+ if (Array.isArray(oldData)) {
232
+ return oldData.filter((job) => !ids.includes(job.id));
233
+ }
234
+ return oldData;
235
+ });
236
+ return { previousJobs };
237
+ },
238
+ onError: (_err, variables, context) => {
239
+ if (context?.previousJobs) {
240
+ queryClient.setQueryData(['jobs'], context.previousJobs);
241
+ }
242
+ },
243
+ onSettled: (_data, _error, _variables) => {
244
+ queryClient.invalidateQueries({ queryKey: ['jobs'] });
245
+ queryClient.invalidateQueries({ queryKey: ['routes'] });
246
+ queryClient.invalidateQueries({ queryKey: ['job'] });
247
+ queryClient.invalidateQueries({ queryKey: ['route'] });
248
+ },
249
+ });
250
+ };
251
+ AvroQueryClient.prototype.useDeleteJob = function () {
252
+ const queryClient = this.getQueryClient();
253
+ return useMutation({
254
+ mutationFn: async ({ jobId }) => {
255
+ return this.delete(`/job/${jobId}`, undefined, {
256
+ "Content-Type": "application/json",
257
+ });
258
+ },
259
+ onMutate: async ({ jobId }) => {
260
+ await queryClient.cancelQueries({ queryKey: ['jobs'] });
261
+ await queryClient.cancelQueries({ queryKey: ['job', jobId] });
262
+ const previousJobs = queryClient.getQueryData(['jobs']);
263
+ const previousJob = queryClient.getQueryData(['job', jobId]);
264
+ queryClient.setQueryData(['job', jobId], undefined);
265
+ queryClient.setQueriesData({ queryKey: ['jobs'] }, (oldData) => {
266
+ if (!oldData)
267
+ return oldData;
268
+ if (oldData.pages) {
269
+ const updatedPages = oldData.pages.map((page) => page.filter((job) => job.id !== jobId));
270
+ return { ...oldData, pages: updatedPages };
271
+ }
272
+ if (Array.isArray(oldData)) {
273
+ return oldData.filter((job) => job.id !== jobId);
274
+ }
275
+ return oldData;
276
+ });
277
+ return { previousJobs, previousJob };
278
+ },
279
+ onError: (_err, variables, context) => {
280
+ const { jobId } = variables;
281
+ if (context?.previousJobs) {
282
+ queryClient.setQueryData(['jobs'], context.previousJobs);
283
+ }
284
+ if (context?.previousJob) {
285
+ queryClient.setQueryData(['job', jobId], context.previousJob);
286
+ }
287
+ },
288
+ onSettled: (_data, _error, variables) => {
289
+ const { jobId } = variables;
290
+ queryClient.invalidateQueries({ queryKey: ['jobs'] });
291
+ queryClient.invalidateQueries({ queryKey: ['routes'] });
292
+ queryClient.invalidateQueries({ queryKey: ['job', jobId] });
293
+ queryClient.invalidateQueries({ queryKey: ['route'] });
294
+ },
295
+ });
296
+ };
@@ -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.useGetLabels = function (body, total, onProgress) {
4
+ return useQuery({
5
+ queryKey: ['labels', this.companyId, body.amt ?? 50, body.query ?? "", total ?? "all"],
6
+ queryFn: async () => {
7
+ if (typeof total !== "number") {
8
+ return this.fetchLabels({ ...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.fetchLabels({
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 labels = pages.flat();
25
+ return labels;
26
+ },
27
+ });
28
+ };
29
+ AvroQueryClient.prototype.useCreateLabel = function () {
30
+ const queryClient = this.getQueryClient();
31
+ return useMutation({
32
+ mutationFn: async ({ labelData }) => {
33
+ return this.post(`/company/${this.companyId}/label`, JSON.stringify(labelData), undefined, { "Content-Type": "application/json" });
34
+ },
35
+ onSettled: () => {
36
+ queryClient.invalidateQueries({ queryKey: ['company'] });
37
+ queryClient.invalidateQueries({ queryKey: ['labels'] });
38
+ },
39
+ });
40
+ };
41
+ AvroQueryClient.prototype.useUpdateLabel = function () {
42
+ const queryClient = this.getQueryClient();
43
+ return useMutation({
44
+ mutationFn: async ({ labelId, labelData }) => {
45
+ return this.put(`/label/${labelId}`, JSON.stringify(labelData), undefined, { "Content-Type": "application/json" });
46
+ },
47
+ onMutate: async ({ labelId, labelData }) => {
48
+ await queryClient.cancelQueries({ queryKey: ['labels'] });
49
+ await queryClient.cancelQueries({ queryKey: ['label', labelId] });
50
+ const previousLabels = queryClient.getQueryData(['labels']);
51
+ const previousLabel = queryClient.getQueryData(['label', labelId]);
52
+ queryClient.setQueryData(['label', labelId], (oldData) => {
53
+ if (!oldData)
54
+ return oldData;
55
+ return { ...oldData, ...labelData };
56
+ });
57
+ queryClient.setQueriesData({ queryKey: ['labels'] }, (oldData) => {
58
+ if (!oldData)
59
+ return oldData;
60
+ if (oldData.pages) {
61
+ const updatedPages = oldData.pages.map((page) => page.map((label) => label.id === labelId ? { ...label, ...labelData } : label));
62
+ return { ...oldData, pages: updatedPages };
63
+ }
64
+ if (Array.isArray(oldData)) {
65
+ return oldData.map((label) => label.id === labelId ? { ...label, ...labelData } : label);
66
+ }
67
+ return oldData;
68
+ });
69
+ return { previousLabels, previousLabel };
70
+ },
71
+ onError: (_err, variables, context) => {
72
+ const { labelId } = variables;
73
+ if (context?.previousLabels) {
74
+ queryClient.setQueryData(['labels'], context.previousLabels);
75
+ }
76
+ if (context?.previousLabel) {
77
+ queryClient.setQueryData(['label', labelId], context.previousLabel);
78
+ }
79
+ },
80
+ onSettled: (_data, _error, variables) => {
81
+ const { labelId } = variables;
82
+ queryClient.invalidateQueries({ queryKey: ['labels'] });
83
+ queryClient.invalidateQueries({ queryKey: ['label', labelId] });
84
+ queryClient.invalidateQueries({ queryKey: ['company'] });
85
+ },
86
+ });
87
+ };
88
+ AvroQueryClient.prototype.useDeleteLabel = function () {
89
+ const queryClient = this.getQueryClient();
90
+ return useMutation({
91
+ mutationFn: async ({ labelId, }) => {
92
+ return this.delete(`/label/${labelId}`);
93
+ },
94
+ onMutate: async ({ labelId }) => {
95
+ await queryClient.cancelQueries({ queryKey: ['labels'] });
96
+ await queryClient.cancelQueries({ queryKey: ['label', labelId] });
97
+ const previousLabels = queryClient.getQueryData(['labels']);
98
+ const previousLabel = queryClient.getQueryData(['label', labelId]);
99
+ queryClient.setQueryData(['label', labelId], undefined);
100
+ queryClient.setQueriesData({ queryKey: ['labels'] }, (oldData) => {
101
+ if (!oldData)
102
+ return oldData;
103
+ if (oldData.pages) {
104
+ const updatedPages = oldData.pages.map((page) => page.filter((label) => label.id !== labelId));
105
+ return { ...oldData, pages: updatedPages };
106
+ }
107
+ if (Array.isArray(oldData)) {
108
+ return oldData.filter((label) => label.id !== labelId);
109
+ }
110
+ return oldData;
111
+ });
112
+ return { previousLabels, previousLabel };
113
+ },
114
+ onError: (_err, variables, context) => {
115
+ const { labelId } = variables;
116
+ if (context?.previousLabels) {
117
+ queryClient.setQueryData(['labels'], context.previousLabels);
118
+ }
119
+ if (context?.previousLabel) {
120
+ queryClient.setQueryData(['label', labelId], context.previousLabel);
121
+ }
122
+ },
123
+ onSettled: (_data, _error, variables) => {
124
+ const { labelId } = variables;
125
+ queryClient.invalidateQueries({ queryKey: ['labels'] });
126
+ queryClient.invalidateQueries({ queryKey: ['label', labelId] });
127
+ queryClient.invalidateQueries({ queryKey: ['company'] });
128
+ },
129
+ });
130
+ };
@@ -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,22 @@
1
+ import { useMutation, useQuery } from "@tanstack/react-query";
2
+ import { AvroQueryClient } from "../../client/QueryClient";
3
+ AvroQueryClient.prototype.useCreateProposal = function () {
4
+ return useMutation({
5
+ mutationFn: async ({ job_id, data }) => {
6
+ return this.post(`/job/${job_id}/proposal`, JSON.stringify(data), undefined, { "Content-Type": "application/json" });
7
+ },
8
+ });
9
+ };
10
+ AvroQueryClient.prototype.useAcceptProposal = function (proposal_id) {
11
+ return useMutation({
12
+ mutationFn: async (data) => {
13
+ return this.post(`/proposal/${proposal_id}/accept`, JSON.stringify(data), undefined, { "Content-Type": "application/json" });
14
+ },
15
+ });
16
+ };
17
+ AvroQueryClient.prototype.useGetProposal = function (proposal_id) {
18
+ return useQuery({
19
+ queryKey: ['proposal', proposal_id],
20
+ queryFn: async () => this.get(`/proposal/${proposal_id}`),
21
+ });
22
+ };
@@ -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 {};