@go-avro/avro-js 0.0.2-beta.45 → 0.0.2-beta.47

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.
@@ -1,6 +1,6 @@
1
1
  import { InfiniteData, UseInfiniteQueryResult, useMutation, useQuery, UseQueryResult } from '@tanstack/react-query';
2
2
  import { AuthManager } from '../auth/AuthManager';
3
- import { _Event, Bill, Company, Job, LineItem, ServiceMonth, Session, User } from '../types/api';
3
+ import { _Event, Bill, Company, Job, LineItem, Route, ServiceMonth, Session, User } from '../types/api';
4
4
  import { CancelToken, RetryStrategy } from '../types/client';
5
5
  import { StandardError } from '../types/error';
6
6
  export interface AvroQueryClientConfig {
@@ -61,6 +61,7 @@ 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
+ useGetRoute(routeId: string): UseQueryResult<Route, StandardError>;
64
65
  useGetUserSessions(): UseQueryResult<Session[], StandardError>;
65
66
  useCreateUserSession(): ReturnType<typeof useMutation<{
66
67
  id: string;
@@ -86,7 +87,9 @@ declare module '../client/QueryClient' {
86
87
  companyId: string;
87
88
  jobData: Partial<Job>;
88
89
  }>>;
89
- useUpdateUserSession(): ReturnType<typeof useMutation<void, StandardError, {
90
+ useUpdateUserSession(): ReturnType<typeof useMutation<{
91
+ msg: string;
92
+ }, StandardError, {
90
93
  sessionId: string;
91
94
  updates: Partial<Session>;
92
95
  }>>;
@@ -102,6 +105,12 @@ declare module '../client/QueryClient' {
102
105
  billId: string;
103
106
  updates: Partial<Bill>;
104
107
  }>>;
108
+ useUpdateRoute(): ReturnType<typeof useMutation<{
109
+ msg: string;
110
+ }, StandardError, {
111
+ routeId: string;
112
+ updates: Partial<Route>;
113
+ }>>;
105
114
  useUpdateEvents(): ReturnType<typeof useMutation<void, StandardError, {
106
115
  companyId: string;
107
116
  events: (_Event & {
@@ -29,6 +29,58 @@ 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.useGetRoute = function (routeId) {
33
+ const queryClient = useQueryClient();
34
+ return useQuery({
35
+ queryKey: ['route', routeId],
36
+ queryFn: () => this.get(`/route/${routeId}`),
37
+ enabled: Boolean(routeId) && routeId.length > 0,
38
+ });
39
+ };
40
+ AvroQueryClient.prototype.useUpdateRoute = function () {
41
+ const queryClient = useQueryClient();
42
+ return useMutation({
43
+ mutationFn: async ({ routeId, updates, }) => {
44
+ return this.put(`/route/${routeId}`, JSON.stringify(updates), undefined, { "Content-Type": "application/json" });
45
+ },
46
+ onMutate: async ({ routeId, updates }) => {
47
+ await queryClient.cancelQueries({ queryKey: ['route', routeId] });
48
+ await queryClient.cancelQueries({ queryKey: ['routes'] });
49
+ const previousRoute = queryClient.getQueryData(['route', routeId]);
50
+ const previousRoutes = queryClient.getQueryData(['routes']);
51
+ queryClient.setQueryData(['route', routeId], (oldData) => oldData ? { ...oldData, ...updates } : undefined);
52
+ queryClient.setQueriesData({ queryKey: ['routes'] }, (oldData) => {
53
+ if (!oldData)
54
+ return oldData;
55
+ if (oldData.pages) {
56
+ return {
57
+ ...oldData,
58
+ pages: oldData.pages.map((page) => page.map((route) => route.id === routeId ? { ...route, ...updates } : route)),
59
+ };
60
+ }
61
+ if (Array.isArray(oldData)) {
62
+ return oldData.map((route) => route.id === routeId ? { ...route, ...updates } : route);
63
+ }
64
+ return oldData;
65
+ });
66
+ return { previousRoute, previousRoutes };
67
+ },
68
+ onError: (_err, variables, context) => {
69
+ const { routeId } = variables;
70
+ if (context?.previousRoute) {
71
+ queryClient.setQueryData(['route', routeId], context.previousRoute);
72
+ }
73
+ if (context?.previousRoutes) {
74
+ queryClient.setQueryData(['routes'], context.previousRoutes);
75
+ }
76
+ },
77
+ onSettled: (_data, _error, variables) => {
78
+ const { routeId } = variables;
79
+ queryClient.invalidateQueries({ queryKey: ['route', routeId] });
80
+ queryClient.invalidateQueries({ queryKey: ['routes'] });
81
+ },
82
+ });
83
+ };
32
84
  AvroQueryClient.prototype.useDeleteRoute = function () {
33
85
  const queryClient = useQueryClient();
34
86
  return useMutation({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@go-avro/avro-js",
3
- "version": "0.0.2-beta.45",
3
+ "version": "0.0.2-beta.47",
4
4
  "description": "JS client for Avro backend integration.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",