@go-avro/avro-js 0.0.2-beta.49 → 0.0.2-beta.50
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.
|
@@ -63,6 +63,12 @@ declare module '../client/QueryClient' {
|
|
|
63
63
|
useGetBill(billId: string): UseQueryResult<Bill, StandardError>;
|
|
64
64
|
useGetRoute(routeId: string): UseQueryResult<Route, StandardError>;
|
|
65
65
|
useGetUserSessions(): UseQueryResult<Session[], StandardError>;
|
|
66
|
+
useCreateEvent(): ReturnType<typeof useMutation<{
|
|
67
|
+
id: string;
|
|
68
|
+
}, StandardError, {
|
|
69
|
+
companyId: string;
|
|
70
|
+
eventData: Partial<_Event>;
|
|
71
|
+
}>>;
|
|
66
72
|
useCreateUserSession(): ReturnType<typeof useMutation<{
|
|
67
73
|
id: string;
|
|
68
74
|
}, StandardError, {
|
|
@@ -87,6 +93,12 @@ declare module '../client/QueryClient' {
|
|
|
87
93
|
companyId: string;
|
|
88
94
|
jobData: Partial<Job>;
|
|
89
95
|
}>>;
|
|
96
|
+
useUpdateEvent(): ReturnType<typeof useMutation<{
|
|
97
|
+
msg: string;
|
|
98
|
+
}, StandardError, {
|
|
99
|
+
eventId: string;
|
|
100
|
+
updates: Partial<_Event>;
|
|
101
|
+
}>>;
|
|
90
102
|
useUpdateUserSession(): ReturnType<typeof useMutation<{
|
|
91
103
|
msg: string;
|
|
92
104
|
}, StandardError, {
|
|
@@ -39,6 +39,105 @@ AvroQueryClient.prototype.useGetEvent = function (eventId) {
|
|
|
39
39
|
enabled: Boolean(eventId),
|
|
40
40
|
});
|
|
41
41
|
};
|
|
42
|
+
AvroQueryClient.prototype.useCreateEvent = function () {
|
|
43
|
+
const queryClient = useQueryClient();
|
|
44
|
+
return useMutation({
|
|
45
|
+
mutationFn: ({ companyId, eventData }) => {
|
|
46
|
+
return this.post(`/company/${companyId}/event`, JSON.stringify(eventData), undefined, {
|
|
47
|
+
"Content-Type": "application/json",
|
|
48
|
+
});
|
|
49
|
+
},
|
|
50
|
+
onMutate: async ({ companyId, eventData }) => {
|
|
51
|
+
await queryClient.cancelQueries({ queryKey: ['events'] });
|
|
52
|
+
const previousEvents = queryClient.getQueryData(['events']);
|
|
53
|
+
queryClient.setQueryData(['events'], (oldData) => {
|
|
54
|
+
if (!oldData)
|
|
55
|
+
return [];
|
|
56
|
+
if (oldData.pages) {
|
|
57
|
+
const firstPage = oldData.pages[0] || [];
|
|
58
|
+
return {
|
|
59
|
+
...oldData,
|
|
60
|
+
pages: [
|
|
61
|
+
[
|
|
62
|
+
{
|
|
63
|
+
...eventData,
|
|
64
|
+
id: Math.random().toString(36).substring(2, 11),
|
|
65
|
+
company_id: companyId,
|
|
66
|
+
},
|
|
67
|
+
...firstPage,
|
|
68
|
+
],
|
|
69
|
+
...oldData.pages.slice(1),
|
|
70
|
+
],
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
if (Array.isArray(oldData)) {
|
|
74
|
+
return [
|
|
75
|
+
{
|
|
76
|
+
...eventData,
|
|
77
|
+
id: Math.random().toString(36).substring(2, 11),
|
|
78
|
+
company_id: companyId,
|
|
79
|
+
},
|
|
80
|
+
...oldData,
|
|
81
|
+
];
|
|
82
|
+
}
|
|
83
|
+
return oldData;
|
|
84
|
+
});
|
|
85
|
+
return { previousEvents };
|
|
86
|
+
},
|
|
87
|
+
onError: (err, variables, context) => {
|
|
88
|
+
if (context?.previousEvents) {
|
|
89
|
+
queryClient.setQueryData(['events'], context.previousEvents);
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
onSettled: () => {
|
|
93
|
+
queryClient.invalidateQueries({ queryKey: ['events'] });
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
};
|
|
97
|
+
AvroQueryClient.prototype.useUpdateEvent = function () {
|
|
98
|
+
const queryClient = useQueryClient();
|
|
99
|
+
return useMutation({
|
|
100
|
+
mutationFn: ({ eventId, updates }) => {
|
|
101
|
+
return this.put(`/event/${eventId}`, JSON.stringify(updates), undefined, {
|
|
102
|
+
"Content-Type": "application/json",
|
|
103
|
+
});
|
|
104
|
+
},
|
|
105
|
+
onMutate: async ({ eventId, updates }) => {
|
|
106
|
+
await queryClient.cancelQueries({ queryKey: ['event', eventId] });
|
|
107
|
+
await queryClient.cancelQueries({ queryKey: ['events'] });
|
|
108
|
+
const previousEvent = queryClient.getQueryData(['event', eventId]);
|
|
109
|
+
const previousEvents = queryClient.getQueryData(['events']);
|
|
110
|
+
queryClient.setQueryData(['event', eventId], (oldData) => oldData ? { ...oldData, ...updates } : undefined);
|
|
111
|
+
queryClient.setQueriesData({ queryKey: ['events'] }, (oldData) => {
|
|
112
|
+
if (!oldData)
|
|
113
|
+
return oldData;
|
|
114
|
+
if (oldData.pages) {
|
|
115
|
+
const updatedPages = oldData.pages.map((page) => page.map((event) => event.id === eventId ? { ...event, ...updates } : event));
|
|
116
|
+
return { ...oldData, pages: updatedPages };
|
|
117
|
+
}
|
|
118
|
+
if (Array.isArray(oldData)) {
|
|
119
|
+
return oldData.map((event) => event.id === eventId ? { ...event, ...updates } : event);
|
|
120
|
+
}
|
|
121
|
+
return oldData;
|
|
122
|
+
});
|
|
123
|
+
return { previousEvent, previousEvents };
|
|
124
|
+
},
|
|
125
|
+
onError: (_err, variables, context) => {
|
|
126
|
+
const { eventId } = variables;
|
|
127
|
+
if (context?.previousEvent) {
|
|
128
|
+
queryClient.setQueryData(['event', eventId], context.previousEvent);
|
|
129
|
+
}
|
|
130
|
+
if (context?.previousEvents) {
|
|
131
|
+
queryClient.setQueryData(['events'], context.previousEvents);
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
onSettled: (_data, _error, variables) => {
|
|
135
|
+
const { eventId } = variables;
|
|
136
|
+
queryClient.invalidateQueries({ queryKey: ['event', eventId] });
|
|
137
|
+
queryClient.invalidateQueries({ queryKey: ['events'] });
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
};
|
|
42
141
|
AvroQueryClient.prototype.useUpdateEvents = function () {
|
|
43
142
|
const queryClient = useQueryClient();
|
|
44
143
|
return useMutation({
|