@go-avro/avro-js 0.0.2-beta.37 → 0.0.2-beta.39
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/dist/client/QueryClient.d.ts +71 -12
- package/dist/client/hooks/bills.js +105 -1
- package/dist/client/hooks/companies.js +36 -1
- package/dist/client/hooks/events.js +107 -1
- package/dist/client/hooks/months.js +59 -1
- package/dist/client/hooks/routes.js +45 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +1 -1
|
@@ -33,7 +33,7 @@ declare module '../client/QueryClient' {
|
|
|
33
33
|
billed?: boolean;
|
|
34
34
|
paid?: boolean;
|
|
35
35
|
jobId?: string;
|
|
36
|
-
}): UseInfiniteQueryResult<InfiniteData<_Event[], unknown>,
|
|
36
|
+
}): UseInfiniteQueryResult<InfiniteData<_Event[], unknown>, StandardError>;
|
|
37
37
|
useGetMonths(companyGuid: string, body: {
|
|
38
38
|
amt?: number;
|
|
39
39
|
known_ids?: string[];
|
|
@@ -43,14 +43,14 @@ declare module '../client/QueryClient' {
|
|
|
43
43
|
billed?: boolean;
|
|
44
44
|
paid?: boolean;
|
|
45
45
|
jobId?: string;
|
|
46
|
-
}): UseInfiniteQueryResult<InfiniteData<ServiceMonth[], unknown>,
|
|
46
|
+
}): UseInfiniteQueryResult<InfiniteData<ServiceMonth[], unknown>, StandardError>;
|
|
47
47
|
useGetBills(companyGuid: string, body: {
|
|
48
48
|
amt?: number;
|
|
49
49
|
known_ids?: string[];
|
|
50
50
|
unknown_ids?: string[];
|
|
51
51
|
query?: string;
|
|
52
52
|
paid?: boolean;
|
|
53
|
-
}): UseInfiniteQueryResult<InfiniteData<Bill[], unknown>,
|
|
53
|
+
}): UseInfiniteQueryResult<InfiniteData<Bill[], unknown>, StandardError>;
|
|
54
54
|
useGetCompanies(options?: {}): UseQueryResult<{
|
|
55
55
|
name: string;
|
|
56
56
|
id: string;
|
|
@@ -61,19 +61,78 @@ declare module '../client/QueryClient' {
|
|
|
61
61
|
useGetUser(userId: string): UseQueryResult<User, StandardError>;
|
|
62
62
|
useGetSelf(): UseQueryResult<User, StandardError>;
|
|
63
63
|
useGetBill(billId: string): UseQueryResult<Bill, StandardError>;
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
},
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
64
|
+
useCreateBill(): ReturnType<typeof useMutation<{
|
|
65
|
+
id: string;
|
|
66
|
+
invoice_id: number;
|
|
67
|
+
}, StandardError, {
|
|
68
|
+
companyId: string;
|
|
69
|
+
data: {
|
|
70
|
+
line_items: LineItem[];
|
|
71
|
+
due_date: number;
|
|
72
|
+
users: string[];
|
|
73
|
+
custom_emails: [string, string][];
|
|
74
|
+
};
|
|
75
|
+
}>>;
|
|
71
76
|
useCreateJob(): ReturnType<typeof useMutation<{
|
|
72
77
|
id: string;
|
|
73
|
-
},
|
|
78
|
+
}, StandardError, {
|
|
74
79
|
companyId: string;
|
|
75
80
|
jobData: Partial<Job>;
|
|
76
|
-
}
|
|
81
|
+
}>>;
|
|
82
|
+
useUpdateJob(): ReturnType<typeof useMutation<{
|
|
83
|
+
msg: string;
|
|
84
|
+
}, StandardError, {
|
|
85
|
+
jobId: string;
|
|
86
|
+
updates: Partial<Job>;
|
|
87
|
+
}>>;
|
|
88
|
+
useUpdateBill(): ReturnType<typeof useMutation<{
|
|
89
|
+
msg: string;
|
|
90
|
+
}, StandardError, {
|
|
91
|
+
billId: string;
|
|
92
|
+
updates: Partial<Bill>;
|
|
93
|
+
}>>;
|
|
94
|
+
useUpdateEvents(): ReturnType<typeof useMutation<void, StandardError, {
|
|
95
|
+
companyId: string;
|
|
96
|
+
events: (_Event & {
|
|
97
|
+
page?: number;
|
|
98
|
+
})[];
|
|
99
|
+
action: "billed" | "paid";
|
|
100
|
+
}>>;
|
|
101
|
+
useUpdateMonths(): ReturnType<typeof useMutation<void, StandardError, {
|
|
102
|
+
companyId: string;
|
|
103
|
+
months: (ServiceMonth & {
|
|
104
|
+
page?: number;
|
|
105
|
+
})[];
|
|
106
|
+
action: "billed" | "paid";
|
|
107
|
+
}>>;
|
|
108
|
+
useDeleteJob(): ReturnType<typeof useMutation<{
|
|
109
|
+
msg: string;
|
|
110
|
+
}, StandardError, {
|
|
111
|
+
jobId: string;
|
|
112
|
+
}>>;
|
|
113
|
+
useUpdateCompany(): ReturnType<typeof useMutation<{
|
|
114
|
+
msg: string;
|
|
115
|
+
}, StandardError, {
|
|
116
|
+
companyId: string;
|
|
117
|
+
companyData: Partial<Company | {
|
|
118
|
+
logo: File | null;
|
|
119
|
+
}>;
|
|
120
|
+
}>>;
|
|
121
|
+
useDeleteEvent(): ReturnType<typeof useMutation<{
|
|
122
|
+
msg: string;
|
|
123
|
+
}, StandardError, {
|
|
124
|
+
eventId: string;
|
|
125
|
+
}>>;
|
|
126
|
+
useDeleteBill(): ReturnType<typeof useMutation<{
|
|
127
|
+
msg: string;
|
|
128
|
+
}, StandardError, {
|
|
129
|
+
billId: string;
|
|
130
|
+
}>>;
|
|
131
|
+
useDeleteRoute(): ReturnType<typeof useMutation<{
|
|
132
|
+
msg: string;
|
|
133
|
+
}, StandardError, {
|
|
134
|
+
routeId: string;
|
|
135
|
+
}>>;
|
|
77
136
|
}
|
|
78
137
|
}
|
|
79
138
|
export declare class AvroQueryClient {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AvroQueryClient } from '../../client/QueryClient';
|
|
2
|
-
import { useQueryClient, useInfiniteQuery, useQuery } from '@tanstack/react-query';
|
|
2
|
+
import { useQueryClient, useInfiniteQuery, useQuery, useMutation } from '@tanstack/react-query';
|
|
3
3
|
AvroQueryClient.prototype.useGetBills = function (companyGuid, body) {
|
|
4
4
|
const queryClient = useQueryClient();
|
|
5
5
|
const result = useInfiniteQuery({
|
|
@@ -35,3 +35,107 @@ AvroQueryClient.prototype.useGetBill = function (billId) {
|
|
|
35
35
|
enabled: Boolean(billId),
|
|
36
36
|
});
|
|
37
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
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AvroQueryClient } from '../../client/QueryClient';
|
|
2
|
-
import { useQuery } from '@tanstack/react-query';
|
|
2
|
+
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
3
3
|
AvroQueryClient.prototype.useGetCompanies = function (options = {}) {
|
|
4
4
|
return useQuery({
|
|
5
5
|
queryKey: ['/company/list'],
|
|
@@ -14,3 +14,38 @@ AvroQueryClient.prototype.useGetCompany = function (companyId) {
|
|
|
14
14
|
enabled: Boolean(companyId),
|
|
15
15
|
});
|
|
16
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
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AvroQueryClient } from '../../client/QueryClient';
|
|
2
|
-
import { useQueryClient, useInfiniteQuery, useQuery } from '@tanstack/react-query';
|
|
2
|
+
import { useQueryClient, useInfiniteQuery, useQuery, useMutation } from '@tanstack/react-query';
|
|
3
3
|
AvroQueryClient.prototype.useGetEvents = function (companyGuid, body) {
|
|
4
4
|
const queryClient = useQueryClient();
|
|
5
5
|
const result = useInfiniteQuery({
|
|
@@ -39,3 +39,109 @@ AvroQueryClient.prototype.useGetEvent = function (eventId) {
|
|
|
39
39
|
enabled: Boolean(eventId),
|
|
40
40
|
});
|
|
41
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
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AvroQueryClient } from '../../client/QueryClient';
|
|
2
|
-
import { useQueryClient, useInfiniteQuery } from '@tanstack/react-query';
|
|
2
|
+
import { useQueryClient, useInfiniteQuery, useMutation } from '@tanstack/react-query';
|
|
3
3
|
AvroQueryClient.prototype.useGetMonths = function (companyGuid, body) {
|
|
4
4
|
const queryClient = useQueryClient();
|
|
5
5
|
const result = useInfiniteQuery({
|
|
@@ -32,3 +32,61 @@ AvroQueryClient.prototype.useGetMonths = function (companyGuid, body) {
|
|
|
32
32
|
}
|
|
33
33
|
return result;
|
|
34
34
|
};
|
|
35
|
+
AvroQueryClient.prototype.useUpdateMonths = function () {
|
|
36
|
+
const queryClient = useQueryClient();
|
|
37
|
+
return useMutation({
|
|
38
|
+
mutationFn: async ({ companyId, months, action, }) => {
|
|
39
|
+
const monthIds = months.map(month => month.id);
|
|
40
|
+
return this.put(`/company/${companyId}/months`, JSON.stringify({
|
|
41
|
+
months: monthIds,
|
|
42
|
+
billed: true,
|
|
43
|
+
paid: action === "paid",
|
|
44
|
+
}), undefined, {
|
|
45
|
+
"Content-Type": "application/json",
|
|
46
|
+
});
|
|
47
|
+
},
|
|
48
|
+
onMutate: async ({ months, action }) => {
|
|
49
|
+
await queryClient.cancelQueries({ queryKey: ['months'] });
|
|
50
|
+
await queryClient.cancelQueries({ queryKey: ['month'] });
|
|
51
|
+
const previousMonths = queryClient.getQueryData(['months']);
|
|
52
|
+
const previousMonthObjs = months.map(month => queryClient.getQueryData(['month', month.id]));
|
|
53
|
+
const monthIds = months.map(month => month.id);
|
|
54
|
+
monthIds.forEach((monthId, idx) => {
|
|
55
|
+
queryClient.setQueryData(['month', monthId], (oldData) => {
|
|
56
|
+
return oldData
|
|
57
|
+
? { ...oldData, billed: true, paid: action === "paid" }
|
|
58
|
+
: oldData;
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
queryClient.setQueriesData({ queryKey: ['months'] }, (oldData) => {
|
|
62
|
+
if (!oldData)
|
|
63
|
+
return oldData;
|
|
64
|
+
if (oldData.pages) {
|
|
65
|
+
const updatedPages = oldData.pages.map((page) => page.map((month) => monthIds.includes(month.id)
|
|
66
|
+
? { ...month, billed: true, paid: action === "paid" }
|
|
67
|
+
: month));
|
|
68
|
+
return { ...oldData, pages: updatedPages };
|
|
69
|
+
}
|
|
70
|
+
if (Array.isArray(oldData)) {
|
|
71
|
+
return oldData.map((month) => monthIds.includes(month.id)
|
|
72
|
+
? { ...month, billed: true, paid: action === "paid" }
|
|
73
|
+
: month);
|
|
74
|
+
}
|
|
75
|
+
return oldData;
|
|
76
|
+
});
|
|
77
|
+
return { previousMonths, previousMonthObjs };
|
|
78
|
+
},
|
|
79
|
+
onError: (err, variables, context) => {
|
|
80
|
+
if (context) {
|
|
81
|
+
queryClient.setQueryData(['months'], context.previousMonths);
|
|
82
|
+
context.previousMonthObjs.forEach((monthObj) => {
|
|
83
|
+
queryClient.setQueryData(['month', monthObj.id], monthObj);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
onSettled: () => {
|
|
88
|
+
queryClient.invalidateQueries({ queryKey: ['months'] });
|
|
89
|
+
queryClient.invalidateQueries({ queryKey: ['month'] });
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AvroQueryClient } from '../../client/QueryClient';
|
|
2
|
-
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
|
2
|
+
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
|
3
3
|
AvroQueryClient.prototype.useGetRoutes = function (companyGuid, body, total = 0, onProgress) {
|
|
4
4
|
const queryClient = useQueryClient();
|
|
5
5
|
return useQuery({
|
|
@@ -29,3 +29,47 @@ AvroQueryClient.prototype.useGetRoutes = function (companyGuid, body, total = 0,
|
|
|
29
29
|
enabled: Boolean(companyGuid) && companyGuid.length > 0 && Boolean(total) && total >= 0,
|
|
30
30
|
});
|
|
31
31
|
};
|
|
32
|
+
AvroQueryClient.prototype.useDeleteRoute = function () {
|
|
33
|
+
const queryClient = useQueryClient();
|
|
34
|
+
return useMutation({
|
|
35
|
+
mutationFn: async ({ routeId, }) => {
|
|
36
|
+
return this.delete(`/route/${routeId}`, undefined, {
|
|
37
|
+
"Content-Type": "application/json",
|
|
38
|
+
});
|
|
39
|
+
},
|
|
40
|
+
onMutate: async ({ routeId }) => {
|
|
41
|
+
await queryClient.cancelQueries({ queryKey: ['routes'] });
|
|
42
|
+
await queryClient.cancelQueries({ queryKey: ['route', routeId] });
|
|
43
|
+
const previousRoutes = queryClient.getQueryData(['routes']);
|
|
44
|
+
const previousRoute = queryClient.getQueryData(['route', routeId]);
|
|
45
|
+
queryClient.setQueryData(['route', routeId], undefined);
|
|
46
|
+
queryClient.setQueriesData({ queryKey: ['routes'] }, (oldData) => {
|
|
47
|
+
if (!oldData)
|
|
48
|
+
return oldData;
|
|
49
|
+
if (oldData.pages) {
|
|
50
|
+
const updatedPages = oldData.pages.map((page) => page.filter((route) => route.id !== routeId));
|
|
51
|
+
return { ...oldData, pages: updatedPages };
|
|
52
|
+
}
|
|
53
|
+
if (Array.isArray(oldData)) {
|
|
54
|
+
return oldData.filter((route) => route.id !== routeId);
|
|
55
|
+
}
|
|
56
|
+
return oldData;
|
|
57
|
+
});
|
|
58
|
+
return { previousRoutes, previousRoute };
|
|
59
|
+
},
|
|
60
|
+
onError: (_err, variables, context) => {
|
|
61
|
+
const { routeId } = variables;
|
|
62
|
+
if (context?.previousRoutes) {
|
|
63
|
+
queryClient.setQueryData(['routes'], context.previousRoutes);
|
|
64
|
+
}
|
|
65
|
+
if (context?.previousRoute) {
|
|
66
|
+
queryClient.setQueryData(['route', routeId], context.previousRoute);
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
onSettled: (_data, _error, variables) => {
|
|
70
|
+
const { routeId } = variables;
|
|
71
|
+
queryClient.invalidateQueries({ queryKey: ['routes'] });
|
|
72
|
+
queryClient.invalidateQueries({ queryKey: ['route', routeId] });
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ import './client/hooks/events';
|
|
|
11
11
|
import './client/hooks/months';
|
|
12
12
|
import './client/hooks/bills';
|
|
13
13
|
import './client/hooks/companies';
|
|
14
|
+
import './client/hooks/users';
|
|
14
15
|
export * from './types/api';
|
|
15
16
|
export * from './types/error';
|
|
16
17
|
export * from './types/client';
|
package/dist/index.js
CHANGED
|
@@ -11,6 +11,7 @@ import './client/hooks/events';
|
|
|
11
11
|
import './client/hooks/months';
|
|
12
12
|
import './client/hooks/bills';
|
|
13
13
|
import './client/hooks/companies';
|
|
14
|
+
import './client/hooks/users';
|
|
14
15
|
export * from './types/api';
|
|
15
16
|
export * from './types/error';
|
|
16
17
|
export * from './types/client';
|