@go-avro/avro-js 0.0.2-beta.4 → 0.0.2-beta.41
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.
- package/README.md +1 -0
- package/dist/auth/AuthManager.d.ts +1 -1
- package/dist/auth/AuthManager.js +33 -2
- package/dist/client/QueryClient.d.ts +181 -5
- package/dist/client/QueryClient.js +96 -164
- package/dist/client/core/fetch.d.ts +1 -0
- package/dist/client/core/fetch.js +67 -0
- package/dist/client/core/utils.d.ts +1 -0
- package/dist/client/core/utils.js +13 -0
- package/dist/client/core/xhr.d.ts +1 -0
- package/dist/client/core/xhr.js +80 -0
- package/dist/client/hooks/bills.d.ts +1 -0
- package/dist/client/hooks/bills.js +141 -0
- package/dist/client/hooks/companies.d.ts +1 -0
- package/dist/client/hooks/companies.js +51 -0
- package/dist/client/hooks/events.d.ts +1 -0
- package/dist/client/hooks/events.js +147 -0
- package/dist/client/hooks/jobs.d.ts +1 -0
- package/dist/client/hooks/jobs.js +184 -0
- package/dist/client/hooks/months.d.ts +1 -0
- package/dist/client/hooks/months.js +92 -0
- package/dist/client/hooks/root.d.ts +1 -0
- package/dist/client/hooks/root.js +8 -0
- package/dist/client/hooks/routes.d.ts +1 -0
- package/dist/client/hooks/routes.js +75 -0
- package/dist/client/hooks/sessions.d.ts +1 -0
- package/dist/client/hooks/sessions.js +8 -0
- package/dist/client/hooks/users.d.ts +1 -0
- package/dist/client/hooks/users.js +16 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +12 -0
- package/dist/types/api.d.ts +88 -20
- package/package.json +5 -1
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { AvroQueryClient } from '../../client/QueryClient';
|
|
2
|
+
import { StandardError } from '../../types/error';
|
|
3
|
+
AvroQueryClient.prototype._xhr = function (method, path, body, cancelToken, headers = {}, isIdempotent = false, retryCount = 0) {
|
|
4
|
+
const checkCancelled = () => {
|
|
5
|
+
if (cancelToken?.isCancelled()) {
|
|
6
|
+
return new StandardError(0, 'Request cancelled');
|
|
7
|
+
}
|
|
8
|
+
return null;
|
|
9
|
+
};
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
this.config.authManager.accessToken().then(token => {
|
|
12
|
+
const cancelErr = checkCancelled();
|
|
13
|
+
if (cancelErr)
|
|
14
|
+
return reject(cancelErr);
|
|
15
|
+
const xhr = new XMLHttpRequest();
|
|
16
|
+
const url = this.config.baseUrl + path;
|
|
17
|
+
xhr.open(method, url, true);
|
|
18
|
+
if (token)
|
|
19
|
+
xhr.setRequestHeader('Authorization', `Bearer ${token}`);
|
|
20
|
+
Object.entries(headers).forEach(([key, value]) => xhr.setRequestHeader(key, value));
|
|
21
|
+
xhr.onload = () => {
|
|
22
|
+
const cancelErr = checkCancelled();
|
|
23
|
+
if (cancelErr)
|
|
24
|
+
return reject(cancelErr);
|
|
25
|
+
if (xhr.status === 401 && this.config.authManager.refreshTokens && retryCount === 0) {
|
|
26
|
+
this.config.authManager
|
|
27
|
+
.refreshTokens()
|
|
28
|
+
.then(() => {
|
|
29
|
+
this._xhr(method, path, body, cancelToken, headers, isIdempotent, retryCount + 1).then(resolve, reject);
|
|
30
|
+
})
|
|
31
|
+
.catch(() => reject(new StandardError(401, 'Unauthorized (refresh failed)')));
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
if (xhr.status >= 200 && xhr.status < 300) {
|
|
35
|
+
try {
|
|
36
|
+
resolve(JSON.parse(xhr.responseText));
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
resolve(xhr.responseText);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
if (retryCount < this.config.maxRetries) {
|
|
44
|
+
const delay = this.getDelay(this.config.retryStrategy, retryCount);
|
|
45
|
+
setTimeout(() => {
|
|
46
|
+
this._xhr(method, path, body, cancelToken, headers, isIdempotent, retryCount + 1).then(resolve, reject);
|
|
47
|
+
}, delay);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
let msg = xhr.statusText;
|
|
51
|
+
try {
|
|
52
|
+
const parsed = JSON.parse(xhr.responseText);
|
|
53
|
+
msg = parsed.msg || msg;
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
console.warn('Failed to parse error response:', xhr.responseText);
|
|
57
|
+
}
|
|
58
|
+
reject(new StandardError(xhr.status, msg));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
xhr.onerror = () => {
|
|
63
|
+
if (retryCount < this.config.maxRetries) {
|
|
64
|
+
const delay = this.getDelay(this.config.retryStrategy, retryCount);
|
|
65
|
+
setTimeout(() => {
|
|
66
|
+
this._xhr(method, path, body, cancelToken, headers, isIdempotent, retryCount + 1).then(resolve, reject);
|
|
67
|
+
}, delay);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
reject(new StandardError(0, 'Network Error'));
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
if (this.config.timeout) {
|
|
74
|
+
xhr.timeout = this.config.timeout;
|
|
75
|
+
xhr.ontimeout = () => reject(new StandardError(0, 'Request timed out'));
|
|
76
|
+
}
|
|
77
|
+
xhr.send(body);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { AvroQueryClient } from '../../client/QueryClient';
|
|
2
|
+
import { useQueryClient, useInfiniteQuery, useQuery, useMutation } from '@tanstack/react-query';
|
|
3
|
+
AvroQueryClient.prototype.useGetBills = function (companyGuid, body) {
|
|
4
|
+
const queryClient = useQueryClient();
|
|
5
|
+
const result = useInfiniteQuery({
|
|
6
|
+
queryKey: [
|
|
7
|
+
'bills',
|
|
8
|
+
companyGuid,
|
|
9
|
+
body.query ?? "",
|
|
10
|
+
body.known_ids ?? [],
|
|
11
|
+
body.unknown_ids ?? [],
|
|
12
|
+
body.paid ?? false,
|
|
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.fetchBills(companyGuid, { ...body, offset: pageParam }),
|
|
21
|
+
});
|
|
22
|
+
if (result.data) {
|
|
23
|
+
result.data.pages.forEach((data_page) => {
|
|
24
|
+
data_page.forEach((bill) => {
|
|
25
|
+
queryClient.setQueryData(['bill', bill.id], bill);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
return result;
|
|
30
|
+
};
|
|
31
|
+
AvroQueryClient.prototype.useGetBill = function (billId) {
|
|
32
|
+
return useQuery({
|
|
33
|
+
queryKey: ['bill', billId],
|
|
34
|
+
queryFn: () => this.get(`/bill/${billId}`),
|
|
35
|
+
enabled: Boolean(billId),
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
AvroQueryClient.prototype.useCreateBill = function () {
|
|
39
|
+
const queryClient = useQueryClient();
|
|
40
|
+
return useMutation({
|
|
41
|
+
mutationFn: async ({ companyId, data, }) => {
|
|
42
|
+
const body = {
|
|
43
|
+
events: data.line_items.filter(item => item.line_item_type === 'EVENT').map(item => item.id),
|
|
44
|
+
months: data.line_items.filter(item => item.line_item_type === 'SERVICE_MONTH').map(item => item.id),
|
|
45
|
+
line_items: data.line_items.filter(item => item.line_item_type === 'CUSTOM'),
|
|
46
|
+
manual_emails: data.custom_emails,
|
|
47
|
+
users: data.users,
|
|
48
|
+
due_date: data.due_date,
|
|
49
|
+
};
|
|
50
|
+
return this.post(`/company/${companyId}/bill`, JSON.stringify(body), undefined, {
|
|
51
|
+
'Content-Type': 'application/json',
|
|
52
|
+
});
|
|
53
|
+
},
|
|
54
|
+
onMutate: async ({ companyId }) => {
|
|
55
|
+
await queryClient.cancelQueries({ queryKey: ['bills', companyId] });
|
|
56
|
+
const previousBills = queryClient.getQueryData(['bills', companyId]);
|
|
57
|
+
// TODO: Create a fake bill object for optimistic update and update events and months accordingly
|
|
58
|
+
return { previousBills };
|
|
59
|
+
},
|
|
60
|
+
onError: (_err, _variables, context) => {
|
|
61
|
+
if (context?.previousBills) {
|
|
62
|
+
queryClient.setQueryData(['bills'], context.previousBills);
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
onSettled: (_data, _error, variables) => {
|
|
66
|
+
queryClient.invalidateQueries({ queryKey: ['bills', variables.companyId] });
|
|
67
|
+
queryClient.invalidateQueries({ queryKey: ['events', variables.companyId] });
|
|
68
|
+
queryClient.invalidateQueries({ queryKey: ['months', variables.companyId] });
|
|
69
|
+
},
|
|
70
|
+
});
|
|
71
|
+
};
|
|
72
|
+
AvroQueryClient.prototype.useUpdateBill = function () {
|
|
73
|
+
const queryClient = useQueryClient();
|
|
74
|
+
return useMutation({
|
|
75
|
+
mutationFn: ({ billId, updates }) => {
|
|
76
|
+
return this.put(`/bill/${billId}`, JSON.stringify(updates), undefined, {
|
|
77
|
+
"Content-Type": "application/json",
|
|
78
|
+
});
|
|
79
|
+
},
|
|
80
|
+
onMutate: async ({ billId, updates }) => {
|
|
81
|
+
await queryClient.cancelQueries({ queryKey: ['bills'] });
|
|
82
|
+
await queryClient.cancelQueries({ queryKey: ['bill', billId] });
|
|
83
|
+
const previousBills = queryClient.getQueryData(['bills']);
|
|
84
|
+
const previousBill = queryClient.getQueryData(['bill', billId]);
|
|
85
|
+
queryClient.setQueryData(['bill', billId], (oldData) => oldData ? { ...oldData, ...updates } : undefined);
|
|
86
|
+
queryClient.setQueriesData({ queryKey: ['bills'] }, (oldData) => {
|
|
87
|
+
if (!oldData)
|
|
88
|
+
return oldData;
|
|
89
|
+
if (oldData.pages) {
|
|
90
|
+
return {
|
|
91
|
+
...oldData,
|
|
92
|
+
pages: oldData.pages.map((page) => page.map((bill) => bill.id === billId ? { ...bill, ...updates } : bill)),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
if (Array.isArray(oldData)) {
|
|
96
|
+
return oldData.map((bill) => bill.id === billId ? { ...bill, ...updates } : bill);
|
|
97
|
+
}
|
|
98
|
+
return oldData;
|
|
99
|
+
});
|
|
100
|
+
return { previousBills, previousBill };
|
|
101
|
+
},
|
|
102
|
+
onError: (err, variables, context) => {
|
|
103
|
+
const { billId } = variables;
|
|
104
|
+
if (context?.previousBills) {
|
|
105
|
+
queryClient.setQueryData(['bills'], context.previousBills);
|
|
106
|
+
}
|
|
107
|
+
if (context?.previousBill) {
|
|
108
|
+
queryClient.setQueryData(['bill', billId], context.previousBill);
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
onSettled: (data, error, variables) => {
|
|
112
|
+
const { billId } = variables;
|
|
113
|
+
queryClient.invalidateQueries({ queryKey: ['bills'] });
|
|
114
|
+
queryClient.invalidateQueries({ queryKey: ['bill', billId] });
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
};
|
|
118
|
+
AvroQueryClient.prototype.useDeleteBill = function () {
|
|
119
|
+
const queryClient = useQueryClient();
|
|
120
|
+
return useMutation({
|
|
121
|
+
mutationFn: async ({ billId }) => {
|
|
122
|
+
return this.delete(`/bill/${billId}`);
|
|
123
|
+
},
|
|
124
|
+
onMutate: async ({ billId }) => {
|
|
125
|
+
await queryClient.cancelQueries({ queryKey: ['bills'] });
|
|
126
|
+
const previousBills = queryClient.getQueryData(['bills']);
|
|
127
|
+
// TODO: Create a fake bill object for optimistic update and update events and months accordingly
|
|
128
|
+
return { previousBills };
|
|
129
|
+
},
|
|
130
|
+
onError: (_err, _variables, context) => {
|
|
131
|
+
if (context?.previousBills) {
|
|
132
|
+
queryClient.setQueryData(['bills'], context.previousBills);
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
onSettled: (_data, _error) => {
|
|
136
|
+
queryClient.invalidateQueries({ queryKey: ['bills'] });
|
|
137
|
+
queryClient.invalidateQueries({ queryKey: ['events'] });
|
|
138
|
+
queryClient.invalidateQueries({ queryKey: ['months'] });
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { AvroQueryClient } from '../../client/QueryClient';
|
|
2
|
+
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
3
|
+
AvroQueryClient.prototype.useGetCompanies = function (options = {}) {
|
|
4
|
+
return useQuery({
|
|
5
|
+
queryKey: ['/company/list'],
|
|
6
|
+
queryFn: () => this.get('/company/list'),
|
|
7
|
+
...options,
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
AvroQueryClient.prototype.useGetCompany = function (companyId) {
|
|
11
|
+
return useQuery({
|
|
12
|
+
queryKey: ['company', companyId],
|
|
13
|
+
queryFn: () => this.get(`/company/${companyId}`),
|
|
14
|
+
enabled: Boolean(companyId),
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
AvroQueryClient.prototype.useUpdateCompany = function () {
|
|
18
|
+
const queryClient = useQueryClient();
|
|
19
|
+
return useMutation({
|
|
20
|
+
mutationFn: async ({ companyId, companyData, }) => {
|
|
21
|
+
return this.put(`/company/${companyId}`, JSON.stringify(companyData), undefined, { "Content-Type": "application/json" });
|
|
22
|
+
},
|
|
23
|
+
onMutate: async ({ companyId, companyData }) => {
|
|
24
|
+
await queryClient.cancelQueries({ queryKey: ['company', companyId] });
|
|
25
|
+
await queryClient.cancelQueries({ queryKey: ['/company/list'] });
|
|
26
|
+
const previousCompany = queryClient.getQueryData(['company', companyId]);
|
|
27
|
+
const previousCompanyList = queryClient.getQueryData(['/company/list']);
|
|
28
|
+
queryClient.setQueryData(['company', companyId], (oldData) => oldData ? { ...oldData, ...companyData } : undefined);
|
|
29
|
+
queryClient.setQueryData(['/company/list'], (oldList) => {
|
|
30
|
+
if (!oldList)
|
|
31
|
+
return oldList;
|
|
32
|
+
return oldList.map((company) => company.id === companyId ? { ...company, ...companyData } : company);
|
|
33
|
+
});
|
|
34
|
+
return { previousCompany, previousCompanyList };
|
|
35
|
+
},
|
|
36
|
+
onError: (_err, variables, context) => {
|
|
37
|
+
const { companyId } = variables;
|
|
38
|
+
if (context?.previousCompany) {
|
|
39
|
+
queryClient.setQueryData(['company', companyId], context.previousCompany);
|
|
40
|
+
}
|
|
41
|
+
if (context?.previousCompanyList) {
|
|
42
|
+
queryClient.setQueryData(['/company/list'], context.previousCompanyList);
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
onSettled: (_data, _error, variables) => {
|
|
46
|
+
const { companyId } = variables;
|
|
47
|
+
queryClient.invalidateQueries({ queryKey: ['company', companyId] });
|
|
48
|
+
queryClient.invalidateQueries({ queryKey: ['/company/list'] });
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { AvroQueryClient } from '../../client/QueryClient';
|
|
2
|
+
import { useQueryClient, useInfiniteQuery, useQuery, useMutation } from '@tanstack/react-query';
|
|
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.useUpdateEvents = function () {
|
|
43
|
+
const queryClient = useQueryClient();
|
|
44
|
+
return useMutation({
|
|
45
|
+
mutationFn: async ({ companyId, events, action, }) => {
|
|
46
|
+
const eventIds = events.map(event => event.id);
|
|
47
|
+
return this.put(`/company/${companyId}/events`, JSON.stringify({
|
|
48
|
+
events: eventIds,
|
|
49
|
+
billed: true,
|
|
50
|
+
paid: action === "paid",
|
|
51
|
+
}), undefined, {
|
|
52
|
+
"Content-Type": "application/json",
|
|
53
|
+
});
|
|
54
|
+
},
|
|
55
|
+
onMutate: async ({ events, action }) => {
|
|
56
|
+
await queryClient.cancelQueries({ queryKey: ['events'] });
|
|
57
|
+
await queryClient.cancelQueries({ queryKey: ['event'] });
|
|
58
|
+
const previousEvents = queryClient.getQueryData(['events']);
|
|
59
|
+
const previousEventObjs = events.map(event => queryClient.getQueryData(['event', event.id]));
|
|
60
|
+
const eventIds = events.map(event => event.id);
|
|
61
|
+
// Optimistically update individual event cache
|
|
62
|
+
eventIds.forEach((eventId, idx) => {
|
|
63
|
+
queryClient.setQueryData(['event', eventId], (oldData) => {
|
|
64
|
+
return oldData
|
|
65
|
+
? { ...oldData, billed: true, paid: action === "paid" }
|
|
66
|
+
: oldData;
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
// Optimistically update events list cache
|
|
70
|
+
queryClient.setQueriesData({ queryKey: ['events'] }, (oldData) => {
|
|
71
|
+
if (!oldData)
|
|
72
|
+
return oldData;
|
|
73
|
+
if (oldData.pages) {
|
|
74
|
+
const updatedPages = oldData.pages.map((page) => page.map((event) => eventIds.includes(event.id)
|
|
75
|
+
? { ...event, billed: true, paid: action === "paid" }
|
|
76
|
+
: event));
|
|
77
|
+
return { ...oldData, pages: updatedPages };
|
|
78
|
+
}
|
|
79
|
+
if (Array.isArray(oldData)) {
|
|
80
|
+
return oldData.map((event) => eventIds.includes(event.id)
|
|
81
|
+
? { ...event, billed: true, paid: action === "paid" }
|
|
82
|
+
: event);
|
|
83
|
+
}
|
|
84
|
+
return oldData;
|
|
85
|
+
});
|
|
86
|
+
return { previousEvents, previousEventObjs };
|
|
87
|
+
},
|
|
88
|
+
onError: (err, variables, context) => {
|
|
89
|
+
if (context?.previousEvents) {
|
|
90
|
+
queryClient.setQueryData(['events'], context.previousEvents);
|
|
91
|
+
}
|
|
92
|
+
if (context?.previousEventObjs) {
|
|
93
|
+
context.previousEventObjs.forEach((eventObj) => {
|
|
94
|
+
queryClient.setQueryData(['event', eventObj.id], eventObj);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
onSettled: () => {
|
|
99
|
+
queryClient.invalidateQueries({ queryKey: ['events'] });
|
|
100
|
+
queryClient.invalidateQueries({ queryKey: ['event'] });
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
AvroQueryClient.prototype.useDeleteEvent = function () {
|
|
105
|
+
const queryClient = useQueryClient();
|
|
106
|
+
return useMutation({
|
|
107
|
+
mutationFn: async ({ eventId, }) => {
|
|
108
|
+
return this.delete(`/event/${eventId}`, undefined, {
|
|
109
|
+
"Content-Type": "application/json",
|
|
110
|
+
});
|
|
111
|
+
},
|
|
112
|
+
onMutate: async ({ eventId }) => {
|
|
113
|
+
await queryClient.cancelQueries({ queryKey: ['events'] });
|
|
114
|
+
await queryClient.cancelQueries({ queryKey: ['event', eventId] });
|
|
115
|
+
const previousEvents = queryClient.getQueryData(['events']);
|
|
116
|
+
const previousEvent = queryClient.getQueryData(['event', eventId]);
|
|
117
|
+
queryClient.setQueryData(['event', eventId], undefined);
|
|
118
|
+
queryClient.setQueriesData({ queryKey: ['events'] }, (oldData) => {
|
|
119
|
+
if (!oldData)
|
|
120
|
+
return oldData;
|
|
121
|
+
if (oldData.pages) {
|
|
122
|
+
const updatedPages = oldData.pages.map((page) => page.filter((event) => event.id !== eventId));
|
|
123
|
+
return { ...oldData, pages: updatedPages };
|
|
124
|
+
}
|
|
125
|
+
if (Array.isArray(oldData)) {
|
|
126
|
+
return oldData.filter((event) => event.id !== eventId);
|
|
127
|
+
}
|
|
128
|
+
return oldData;
|
|
129
|
+
});
|
|
130
|
+
return { previousEvents, previousEvent };
|
|
131
|
+
},
|
|
132
|
+
onError: (_err, variables, context) => {
|
|
133
|
+
const { eventId } = variables;
|
|
134
|
+
if (context?.previousEvents) {
|
|
135
|
+
queryClient.setQueryData(['events'], context.previousEvents);
|
|
136
|
+
}
|
|
137
|
+
if (context?.previousEvent) {
|
|
138
|
+
queryClient.setQueryData(['event', eventId], context.previousEvent);
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
onSettled: (_data, _error, variables) => {
|
|
142
|
+
const { eventId } = variables;
|
|
143
|
+
queryClient.invalidateQueries({ queryKey: ['events'] });
|
|
144
|
+
queryClient.invalidateQueries({ queryKey: ['event', eventId] });
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { AvroQueryClient } from '../../client/QueryClient';
|
|
2
|
+
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
3
|
+
AvroQueryClient.prototype.useGetJobs = function (companyGuid, body, total = 0, onProgress, isMainLoad = false) {
|
|
4
|
+
const queryClient = useQueryClient();
|
|
5
|
+
return useQuery({
|
|
6
|
+
queryKey: [isMainLoad ? 'main' : 'jobs', companyGuid, body.amt ?? 50, body.query ?? ""],
|
|
7
|
+
queryFn: async () => {
|
|
8
|
+
if (total === 0) {
|
|
9
|
+
onProgress?.(1);
|
|
10
|
+
return [];
|
|
11
|
+
}
|
|
12
|
+
onProgress?.(0);
|
|
13
|
+
const pageCount = body.amt ? Math.ceil(total / body.amt) : 0;
|
|
14
|
+
let completed = 0;
|
|
15
|
+
const promises = Array.from({ length: pageCount }, (_, i) => this.fetchJobs(companyGuid, {
|
|
16
|
+
...body,
|
|
17
|
+
offset: i * (body.amt ?? 0),
|
|
18
|
+
}));
|
|
19
|
+
const trackedPromises = promises.map((promise) => promise.then((result) => {
|
|
20
|
+
completed++;
|
|
21
|
+
const fraction = completed / pageCount;
|
|
22
|
+
onProgress?.(fraction);
|
|
23
|
+
return result;
|
|
24
|
+
}));
|
|
25
|
+
const pages = await Promise.all(trackedPromises);
|
|
26
|
+
const jobs = pages.flat();
|
|
27
|
+
jobs.forEach((job) => {
|
|
28
|
+
job.last_event = job.tasks.reduce((latest, task) => {
|
|
29
|
+
return task.last_event && (!latest || task.last_event.time_started > latest.time_started) ? task.last_event : latest;
|
|
30
|
+
}, null);
|
|
31
|
+
job.last_completed_event = job.tasks.reduce((latest, task) => {
|
|
32
|
+
return task.last_completed_event && (!latest || task.last_completed_event.time_started > latest.time_started) ? task.last_completed_event : latest;
|
|
33
|
+
}, null);
|
|
34
|
+
job.overdue_time = job.tasks.reduce((maxOverdue, task) => {
|
|
35
|
+
return task.overdue_time && task.overdue_time > maxOverdue ? task.overdue_time : maxOverdue;
|
|
36
|
+
}, 0);
|
|
37
|
+
queryClient.setQueryData(['job', job.id], job);
|
|
38
|
+
});
|
|
39
|
+
if (isMainLoad) {
|
|
40
|
+
queryClient.setQueryData(['jobs', companyGuid, body.amt ?? 50, body.query ?? ""], jobs);
|
|
41
|
+
}
|
|
42
|
+
return jobs;
|
|
43
|
+
},
|
|
44
|
+
enabled: Boolean(companyGuid) && companyGuid.length > 0 && Boolean(total) && total >= 0,
|
|
45
|
+
...isMainLoad ? { staleTime: Infinity, cacheTime: Infinity } : {},
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
AvroQueryClient.prototype.useGetJob = function (jobId) {
|
|
49
|
+
return useQuery({
|
|
50
|
+
queryKey: ['job', jobId],
|
|
51
|
+
queryFn: () => this.get(`/job/${jobId}`),
|
|
52
|
+
enabled: Boolean(jobId),
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
AvroQueryClient.prototype.useCreateJob = function () {
|
|
56
|
+
const queryClient = useQueryClient();
|
|
57
|
+
return useMutation({
|
|
58
|
+
mutationFn: ({ companyId, jobData }) => {
|
|
59
|
+
return this.post(`/company/${companyId}/job`, JSON.stringify(jobData), undefined, {
|
|
60
|
+
"Content-Type": "application/json",
|
|
61
|
+
});
|
|
62
|
+
},
|
|
63
|
+
onMutate: async ({ companyId, jobData }) => {
|
|
64
|
+
await queryClient.cancelQueries({ queryKey: ['jobs'] });
|
|
65
|
+
const previousJobs = queryClient.getQueryData(['jobs']);
|
|
66
|
+
queryClient.setQueryData(['jobs'], (oldData) => {
|
|
67
|
+
if (!oldData)
|
|
68
|
+
return [jobData];
|
|
69
|
+
if (oldData.pages) {
|
|
70
|
+
const firstPage = oldData.pages[0] || [];
|
|
71
|
+
return {
|
|
72
|
+
...oldData,
|
|
73
|
+
pages: [[jobData, ...firstPage], ...oldData.pages.slice(1)],
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
if (Array.isArray(oldData)) {
|
|
77
|
+
return [jobData, ...oldData];
|
|
78
|
+
}
|
|
79
|
+
return oldData;
|
|
80
|
+
});
|
|
81
|
+
return { previousJobs };
|
|
82
|
+
},
|
|
83
|
+
onError: (err, variables, context) => {
|
|
84
|
+
if (context?.previousJobs) {
|
|
85
|
+
queryClient.setQueryData(['jobs'], context.previousJobs);
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
onSettled: (data, error, variables) => {
|
|
89
|
+
const { id: jobId } = data ?? {};
|
|
90
|
+
queryClient.invalidateQueries({ queryKey: ['jobs'] });
|
|
91
|
+
queryClient.invalidateQueries({ queryKey: ['job', jobId] });
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
};
|
|
95
|
+
AvroQueryClient.prototype.useUpdateJob = function () {
|
|
96
|
+
const queryClient = useQueryClient();
|
|
97
|
+
return useMutation({
|
|
98
|
+
mutationFn: ({ jobId, updates }) => {
|
|
99
|
+
return this.put(`/job/${jobId}`, JSON.stringify(updates), undefined, {
|
|
100
|
+
"Content-Type": "application/json",
|
|
101
|
+
});
|
|
102
|
+
},
|
|
103
|
+
onMutate: async ({ jobId, updates }) => {
|
|
104
|
+
await queryClient.cancelQueries({ queryKey: ['jobs'] });
|
|
105
|
+
await queryClient.cancelQueries({ queryKey: ['job', jobId] });
|
|
106
|
+
const previousJobs = queryClient.getQueryData(['jobs']);
|
|
107
|
+
const previousJob = queryClient.getQueryData(['job', jobId]);
|
|
108
|
+
queryClient.setQueryData(['job', jobId], (oldData) => oldData ? { ...oldData, ...updates } : undefined);
|
|
109
|
+
queryClient.setQueriesData({ queryKey: ['jobs'] }, (oldData) => {
|
|
110
|
+
if (!oldData)
|
|
111
|
+
return oldData;
|
|
112
|
+
if (oldData.pages) {
|
|
113
|
+
return {
|
|
114
|
+
...oldData,
|
|
115
|
+
pages: oldData.pages.map((page) => page.map((job) => job.id === jobId ? { ...job, ...updates } : job)),
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
if (Array.isArray(oldData)) {
|
|
119
|
+
return oldData.map((job) => job.id === jobId ? { ...job, ...updates } : job);
|
|
120
|
+
}
|
|
121
|
+
return oldData;
|
|
122
|
+
});
|
|
123
|
+
return { previousJobs, previousJob };
|
|
124
|
+
},
|
|
125
|
+
onError: (err, variables, context) => {
|
|
126
|
+
const { jobId } = variables;
|
|
127
|
+
if (context?.previousJobs) {
|
|
128
|
+
queryClient.setQueryData(['jobs'], context.previousJobs);
|
|
129
|
+
}
|
|
130
|
+
if (context?.previousJob) {
|
|
131
|
+
queryClient.setQueryData(['job', jobId], context.previousJob);
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
onSettled: (data, error, variables) => {
|
|
135
|
+
const { jobId } = variables;
|
|
136
|
+
queryClient.invalidateQueries({ queryKey: ['jobs'] });
|
|
137
|
+
queryClient.invalidateQueries({ queryKey: ['job', jobId] });
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
};
|
|
141
|
+
AvroQueryClient.prototype.useDeleteJob = function () {
|
|
142
|
+
const queryClient = useQueryClient();
|
|
143
|
+
return useMutation({
|
|
144
|
+
mutationFn: async ({ jobId }) => {
|
|
145
|
+
return this.delete(`/job/${jobId}`, undefined, {
|
|
146
|
+
"Content-Type": "application/json",
|
|
147
|
+
});
|
|
148
|
+
},
|
|
149
|
+
onMutate: async ({ jobId }) => {
|
|
150
|
+
await queryClient.cancelQueries({ queryKey: ['jobs'] });
|
|
151
|
+
await queryClient.cancelQueries({ queryKey: ['job', jobId] });
|
|
152
|
+
const previousJobs = queryClient.getQueryData(['jobs']);
|
|
153
|
+
const previousJob = queryClient.getQueryData(['job', jobId]);
|
|
154
|
+
queryClient.setQueryData(['job', jobId], undefined);
|
|
155
|
+
queryClient.setQueriesData({ queryKey: ['jobs'] }, (oldData) => {
|
|
156
|
+
if (!oldData)
|
|
157
|
+
return oldData;
|
|
158
|
+
if (oldData.pages) {
|
|
159
|
+
const updatedPages = oldData.pages.map((page) => page.filter((job) => job.id !== jobId));
|
|
160
|
+
return { ...oldData, pages: updatedPages };
|
|
161
|
+
}
|
|
162
|
+
if (Array.isArray(oldData)) {
|
|
163
|
+
return oldData.filter((job) => job.id !== jobId);
|
|
164
|
+
}
|
|
165
|
+
return oldData;
|
|
166
|
+
});
|
|
167
|
+
return { previousJobs, previousJob };
|
|
168
|
+
},
|
|
169
|
+
onError: (_err, variables, context) => {
|
|
170
|
+
const { jobId } = variables;
|
|
171
|
+
if (context?.previousJobs) {
|
|
172
|
+
queryClient.setQueryData(['jobs'], context.previousJobs);
|
|
173
|
+
}
|
|
174
|
+
if (context?.previousJob) {
|
|
175
|
+
queryClient.setQueryData(['job', jobId], context.previousJob);
|
|
176
|
+
}
|
|
177
|
+
},
|
|
178
|
+
onSettled: (_data, _error, variables) => {
|
|
179
|
+
const { jobId } = variables;
|
|
180
|
+
queryClient.invalidateQueries({ queryKey: ['jobs'] });
|
|
181
|
+
queryClient.invalidateQueries({ queryKey: ['job', jobId] });
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|