@go-avro/avro-js 0.0.37 → 0.0.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.
Files changed (59) hide show
  1. package/dist/auth/AuthManager.d.ts +2 -2
  2. package/dist/auth/AuthManager.js +37 -32
  3. package/dist/auth/storage.d.ts +1 -1
  4. package/dist/auth/storage.js +6 -6
  5. package/dist/client/AvroQueryClientProvider.js +1 -1
  6. package/dist/client/QueryClient.d.ts +35 -17
  7. package/dist/client/QueryClient.js +505 -391
  8. package/dist/client/core/fetch.js +16 -11
  9. package/dist/client/core/utils.js +5 -5
  10. package/dist/client/core/xhr.js +28 -23
  11. package/dist/client/hooks/analytics.js +14 -14
  12. package/dist/client/hooks/avro.js +2 -2
  13. package/dist/client/hooks/bills.js +66 -30
  14. package/dist/client/hooks/catalog_items.js +57 -22
  15. package/dist/client/hooks/chats.js +4 -4
  16. package/dist/client/hooks/companies.js +96 -39
  17. package/dist/client/hooks/email.js +1 -1
  18. package/dist/client/hooks/events.js +174 -63
  19. package/dist/client/hooks/groups.js +37 -22
  20. package/dist/client/hooks/jobs.js +69 -18
  21. package/dist/client/hooks/labels.js +36 -21
  22. package/dist/client/hooks/messages.js +9 -6
  23. package/dist/client/hooks/months.js +42 -22
  24. package/dist/client/hooks/plans.js +2 -2
  25. package/dist/client/hooks/prepayments.js +42 -22
  26. package/dist/client/hooks/proposal.js +21 -5
  27. package/dist/client/hooks/root.js +4 -4
  28. package/dist/client/hooks/routes.js +77 -32
  29. package/dist/client/hooks/sessions.js +66 -34
  30. package/dist/client/hooks/skills.js +33 -18
  31. package/dist/client/hooks/teams.js +36 -21
  32. package/dist/client/hooks/timecards.js +6 -0
  33. package/dist/client/hooks/users.js +61 -29
  34. package/dist/client/hooks/waivers.js +41 -19
  35. package/dist/index.d.ts +38 -38
  36. package/dist/index.js +37 -37
  37. package/dist/types/api/Bill.d.ts +1 -1
  38. package/dist/types/api/Bill.js +1 -1
  39. package/dist/types/api/Job.d.ts +1 -1
  40. package/dist/types/api/Job.js +14 -14
  41. package/dist/types/api/LineItem.d.ts +3 -3
  42. package/dist/types/api/LineItem.js +5 -2
  43. package/dist/types/api/PaymentType.d.ts +1 -1
  44. package/dist/types/api/Prepayment.d.ts +1 -1
  45. package/dist/types/api/Route.d.ts +3 -3
  46. package/dist/types/api/Route.js +4 -2
  47. package/dist/types/api/RouteJob.d.ts +1 -1
  48. package/dist/types/api/Task.d.ts +2 -2
  49. package/dist/types/api/Task.js +12 -7
  50. package/dist/types/api/Timecard.d.ts +1 -1
  51. package/dist/types/api/TimecardAction.d.ts +1 -1
  52. package/dist/types/api/UserCompanyAssociation.d.ts +2 -2
  53. package/dist/types/api/UserCompanyAssociation.js +1 -1
  54. package/dist/types/api/_Event.d.ts +1 -1
  55. package/dist/types/api/_Event.js +1 -1
  56. package/dist/types/api.d.ts +1 -1
  57. package/dist/types/auth.d.ts +1 -1
  58. package/dist/types/client.d.ts +1 -1
  59. package/package.json +3 -2
@@ -1,8 +1,8 @@
1
- import { useQuery } from '@tanstack/react-query';
2
- import { AvroQueryClient } from '../../client/QueryClient';
1
+ import { useQuery } from "@tanstack/react-query";
2
+ import { AvroQueryClient } from "../../client/QueryClient";
3
3
  AvroQueryClient.prototype.useGetRoot = function () {
4
4
  return useQuery({
5
- queryKey: ['health'],
6
- queryFn: () => this.get({ path: '/' }),
5
+ queryKey: ["health"],
6
+ queryFn: () => this.get({ path: "/" }),
7
7
  });
8
8
  };
@@ -1,9 +1,15 @@
1
- import { useMutation, useQuery } from '@tanstack/react-query';
2
- import { AvroQueryClient } from '../../client/QueryClient';
3
- import { Route } from '../../types/api';
1
+ import { useMutation, useQuery } from "@tanstack/react-query";
2
+ import { AvroQueryClient } from "../../client/QueryClient";
3
+ import { Route } from "../../types/api";
4
4
  AvroQueryClient.prototype.useGetRoutes = function (body, total, onProgress) {
5
5
  return useQuery({
6
- queryKey: ['routes', this.companyId, body.amt ?? 50, body.query ?? "", total ?? "all"],
6
+ queryKey: [
7
+ "routes",
8
+ this.companyId,
9
+ body.amt ?? 50,
10
+ body.query ?? "",
11
+ total ?? "all",
12
+ ],
7
13
  queryFn: async () => {
8
14
  if (typeof total !== "number") {
9
15
  const routes = await this.fetchRoutes({ ...body, offset: 0 });
@@ -31,7 +37,7 @@ AvroQueryClient.prototype.useGetRoutes = function (body, total, onProgress) {
31
37
  AvroQueryClient.prototype.useGetRoute = function (routeId) {
32
38
  const queryClient = this.getQueryClient();
33
39
  return useQuery({
34
- queryKey: ['routes', routeId],
40
+ queryKey: ["routes", routeId],
35
41
  queryFn: async () => {
36
42
  const route = await this.get({ path: `/route/${routeId}` });
37
43
  return new Route(route);
@@ -46,47 +52,69 @@ AvroQueryClient.prototype.useCreateRoute = function () {
46
52
  return this.post({
47
53
  path: `/company/${this.companyId}/route`,
48
54
  data: JSON.stringify(routeData),
49
- headers: { "Content-Type": "application/json" }
55
+ headers: { "Content-Type": "application/json" },
50
56
  });
51
57
  },
52
58
  onMutate: async ({ routeData }) => {
53
- await queryClient.cancelQueries({ queryKey: ['routes'] });
54
- const previousRoutes = queryClient.getQueryData(['routes']);
55
- queryClient.setQueriesData({ queryKey: ['routes'] }, (oldData) => {
59
+ await queryClient.cancelQueries({ queryKey: ["routes"] });
60
+ const previousRoutes = queryClient.getQueryData(["routes"]);
61
+ queryClient.setQueriesData({ queryKey: ["routes"] }, (oldData) => {
56
62
  if (!oldData)
57
63
  return oldData;
58
64
  if (oldData.pages) {
59
65
  return {
60
66
  ...oldData,
61
67
  pages: [
62
- [new Route({ ...routeData, id: 'temp-id' })],
68
+ [
69
+ new Route({
70
+ ...routeData,
71
+ id: "temp-id",
72
+ }),
73
+ ],
63
74
  ...oldData.pages,
64
75
  ],
65
76
  };
66
77
  }
67
78
  if (Array.isArray(oldData)) {
68
- return [new Route({ ...routeData, id: 'temp-id' }), ...oldData];
79
+ return [
80
+ new Route({ ...routeData, id: "temp-id" }),
81
+ ...oldData,
82
+ ];
69
83
  }
70
84
  return oldData;
71
85
  });
72
86
  return { previousRoutes };
73
87
  },
88
+ onSuccess: async (result) => {
89
+ await this._syncEntity(queryClient, {
90
+ action: "create",
91
+ entityKey: "routes",
92
+ id: result.id,
93
+ fetchPath: `/route/${result.id}`,
94
+ });
95
+ },
74
96
  onError: (_err, _variables, context) => {
75
97
  if (context?.previousRoutes) {
76
- queryClient.setQueryData(['routes'], context.previousRoutes);
98
+ queryClient.setQueryData(["routes"], context.previousRoutes);
77
99
  }
78
100
  },
79
101
  });
80
102
  };
81
103
  AvroQueryClient.prototype.useScheduleRoutes = function () {
104
+ const queryClient = this.getQueryClient();
82
105
  return useMutation({
83
106
  mutationFn: async ({ schedule }) => {
84
107
  return this.post({
85
108
  path: `/company/${this.companyId}/schedule`,
86
109
  data: JSON.stringify(schedule),
87
- headers: { "Content-Type": "application/json" }
110
+ headers: { "Content-Type": "application/json" },
88
111
  });
89
112
  },
113
+ onSuccess: () => {
114
+ queryClient.invalidateQueries({ queryKey: ["routes"] });
115
+ queryClient.invalidateQueries({ queryKey: ["jobs"] });
116
+ queryClient.invalidateQueries({ queryKey: ["infinite", "jobs"] });
117
+ },
90
118
  });
91
119
  };
92
120
  AvroQueryClient.prototype.useUpdateRoute = function () {
@@ -96,22 +124,24 @@ AvroQueryClient.prototype.useUpdateRoute = function () {
96
124
  return this.put({
97
125
  path: `/route/${routeId}`,
98
126
  data: JSON.stringify(updates),
99
- headers: { "Content-Type": "application/json" }
127
+ headers: { "Content-Type": "application/json" },
100
128
  });
101
129
  },
102
130
  onMutate: async ({ routeId, updates }) => {
103
- await queryClient.cancelQueries({ queryKey: ['routes', routeId] });
104
- await queryClient.cancelQueries({ queryKey: ['routes'] });
105
- const previousRoute = queryClient.getQueryData(['routes', routeId]);
106
- const previousRoutes = queryClient.getQueryData(['routes']);
107
- queryClient.setQueryData(['routes', routeId], (oldData) => oldData ? new Route({ ...oldData, ...updates }) : undefined);
108
- queryClient.setQueriesData({ queryKey: ['routes'] }, (oldData) => {
131
+ await queryClient.cancelQueries({ queryKey: ["routes", routeId] });
132
+ await queryClient.cancelQueries({ queryKey: ["routes"] });
133
+ const previousRoute = queryClient.getQueryData(["routes", routeId]);
134
+ const previousRoutes = queryClient.getQueryData(["routes"]);
135
+ queryClient.setQueryData(["routes", routeId], (oldData) => oldData ? new Route({ ...oldData, ...updates }) : undefined);
136
+ queryClient.setQueriesData({ queryKey: ["routes"] }, (oldData) => {
109
137
  if (!oldData)
110
138
  return oldData;
111
139
  if (oldData.pages) {
112
140
  return {
113
141
  ...oldData,
114
- pages: oldData.pages.map((page) => page.map((route) => route.id === routeId ? new Route({ ...route, ...updates }) : route)),
142
+ pages: oldData.pages.map((page) => page.map((route) => route.id === routeId
143
+ ? new Route({ ...route, ...updates })
144
+ : route)),
115
145
  };
116
146
  }
117
147
  if (Array.isArray(oldData)) {
@@ -121,13 +151,21 @@ AvroQueryClient.prototype.useUpdateRoute = function () {
121
151
  });
122
152
  return { previousRoute, previousRoutes };
123
153
  },
154
+ onSuccess: async (_result, { routeId }) => {
155
+ await this._syncEntity(queryClient, {
156
+ action: "update",
157
+ entityKey: "routes",
158
+ id: routeId,
159
+ fetchPath: `/route/${routeId}`,
160
+ });
161
+ },
124
162
  onError: (_err, variables, context) => {
125
163
  const { routeId } = variables;
126
164
  if (context?.previousRoute) {
127
- queryClient.setQueryData(['routes', routeId], context.previousRoute);
165
+ queryClient.setQueryData(["routes", routeId], context.previousRoute);
128
166
  }
129
167
  if (context?.previousRoutes) {
130
- queryClient.setQueryData(['routes'], context.previousRoutes);
168
+ queryClient.setQueryData(["routes"], context.previousRoutes);
131
169
  }
132
170
  },
133
171
  });
@@ -135,16 +173,16 @@ AvroQueryClient.prototype.useUpdateRoute = function () {
135
173
  AvroQueryClient.prototype.useDeleteRoute = function () {
136
174
  const queryClient = this.getQueryClient();
137
175
  return useMutation({
138
- mutationFn: async ({ routeId, }) => {
176
+ mutationFn: async ({ routeId }) => {
139
177
  return this.delete({ path: `/route/${routeId}` });
140
178
  },
141
179
  onMutate: async ({ routeId }) => {
142
- await queryClient.cancelQueries({ queryKey: ['routes'] });
143
- await queryClient.cancelQueries({ queryKey: ['routes', routeId] });
144
- const previousRoutes = queryClient.getQueryData(['routes']);
145
- const previousRoute = queryClient.getQueryData(['routes', routeId]);
146
- queryClient.setQueryData(['routes', routeId], undefined);
147
- queryClient.setQueriesData({ queryKey: ['routes'] }, (oldData) => {
180
+ await queryClient.cancelQueries({ queryKey: ["routes"] });
181
+ await queryClient.cancelQueries({ queryKey: ["routes", routeId] });
182
+ const previousRoutes = queryClient.getQueryData(["routes"]);
183
+ const previousRoute = queryClient.getQueryData(["routes", routeId]);
184
+ queryClient.setQueryData(["routes", routeId], undefined);
185
+ queryClient.setQueriesData({ queryKey: ["routes"] }, (oldData) => {
148
186
  if (!oldData)
149
187
  return oldData;
150
188
  if (oldData.pages) {
@@ -158,13 +196,20 @@ AvroQueryClient.prototype.useDeleteRoute = function () {
158
196
  });
159
197
  return { previousRoutes, previousRoute };
160
198
  },
199
+ onSuccess: async (_result, { routeId }) => {
200
+ await this._syncEntity(queryClient, {
201
+ action: "delete",
202
+ entityKey: "routes",
203
+ id: routeId,
204
+ });
205
+ },
161
206
  onError: (_err, variables, context) => {
162
207
  const { routeId } = variables;
163
208
  if (context?.previousRoutes) {
164
- queryClient.setQueryData(['routes'], context.previousRoutes);
209
+ queryClient.setQueryData(["routes"], context.previousRoutes);
165
210
  }
166
211
  if (context?.previousRoute) {
167
- queryClient.setQueryData(['routes', routeId], context.previousRoute);
212
+ queryClient.setQueryData(["routes", routeId], context.previousRoute);
168
213
  }
169
214
  },
170
215
  });
@@ -2,8 +2,8 @@ import { useMutation, useQuery, useInfiniteQuery } from "@tanstack/react-query";
2
2
  import { AvroQueryClient } from "../../client/QueryClient";
3
3
  AvroQueryClient.prototype.useGetUserSessions = function () {
4
4
  return useQuery({
5
- queryKey: ['sessions', this.companyId],
6
- queryFn: () => this.get({ path: '/session' }),
5
+ queryKey: ["sessions", this.companyId],
6
+ queryFn: () => this.get({ path: "/session" }),
7
7
  enabled: Boolean(this.companyId),
8
8
  });
9
9
  };
@@ -14,13 +14,18 @@ AvroQueryClient.prototype.useCreateUserSession = function () {
14
14
  return this.post({
15
15
  path: `/company/${this.companyId}/session`,
16
16
  data: JSON.stringify(sessionData),
17
- headers: { "Content-Type": "application/json" }
17
+ headers: { "Content-Type": "application/json" },
18
18
  });
19
19
  },
20
20
  onMutate: async ({ sessionData }) => {
21
- await queryClient.cancelQueries({ queryKey: ['sessions', this.companyId] });
22
- const previousSessions = queryClient.getQueryData(['sessions', this.companyId]);
23
- queryClient.setQueryData(['sessions', this.companyId], (oldData) => {
21
+ await queryClient.cancelQueries({
22
+ queryKey: ["sessions", this.companyId],
23
+ });
24
+ const previousSessions = queryClient.getQueryData([
25
+ "sessions",
26
+ this.companyId,
27
+ ]);
28
+ queryClient.setQueryData(["sessions", this.companyId], (oldData) => {
24
29
  if (!oldData)
25
30
  return [];
26
31
  if (Array.isArray(oldData)) {
@@ -30,16 +35,19 @@ AvroQueryClient.prototype.useCreateUserSession = function () {
30
35
  ...sessionData,
31
36
  id: Math.random().toString(36).substr(2, 9),
32
37
  company_id: this.companyId,
33
- }
38
+ },
34
39
  ];
35
40
  }
36
41
  return oldData;
37
42
  });
38
43
  return { previousSessions };
39
44
  },
45
+ onSuccess: () => {
46
+ queryClient.invalidateQueries({ queryKey: ["sessions"] });
47
+ },
40
48
  onError: (err, variables, context) => {
41
49
  if (context?.previousSessions) {
42
- queryClient.setQueryData(['sessions', this.companyId], context.previousSessions);
50
+ queryClient.setQueryData(["sessions", this.companyId], context.previousSessions);
43
51
  }
44
52
  },
45
53
  });
@@ -47,17 +55,22 @@ AvroQueryClient.prototype.useCreateUserSession = function () {
47
55
  AvroQueryClient.prototype.useCreateSessionBreak = function () {
48
56
  const queryClient = this.getQueryClient();
49
57
  return useMutation({
50
- mutationFn: ({ sessionId, breakData }) => {
58
+ mutationFn: ({ sessionId, breakData, }) => {
51
59
  return this.post({
52
60
  path: `/session/${sessionId}/break`,
53
61
  data: JSON.stringify(breakData),
54
- headers: { "Content-Type": "application/json" }
62
+ headers: { "Content-Type": "application/json" },
55
63
  });
56
64
  },
57
- onMutate: async ({ sessionId, breakData }) => {
58
- await queryClient.cancelQueries({ queryKey: ['sessions', this.companyId] });
59
- const previousSessions = queryClient.getQueryData(['sessions', this.companyId]);
60
- queryClient.setQueryData(['sessions', this.companyId], (oldData) => {
65
+ onMutate: async ({ sessionId, breakData, }) => {
66
+ await queryClient.cancelQueries({
67
+ queryKey: ["sessions", this.companyId],
68
+ });
69
+ const previousSessions = queryClient.getQueryData([
70
+ "sessions",
71
+ this.companyId,
72
+ ]);
73
+ queryClient.setQueryData(["sessions", this.companyId], (oldData) => {
61
74
  if (!oldData)
62
75
  return oldData;
63
76
  if (Array.isArray(oldData)) {
@@ -70,8 +83,8 @@ AvroQueryClient.prototype.useCreateSessionBreak = function () {
70
83
  ...breakData,
71
84
  id: Math.random().toString(36).substr(2, 9),
72
85
  session_id: sessionId,
73
- }
74
- ]
86
+ },
87
+ ],
75
88
  }
76
89
  : session);
77
90
  }
@@ -79,9 +92,12 @@ AvroQueryClient.prototype.useCreateSessionBreak = function () {
79
92
  });
80
93
  return { previousSessions };
81
94
  },
95
+ onSuccess: () => {
96
+ queryClient.invalidateQueries({ queryKey: ["sessions"] });
97
+ },
82
98
  onError: (err, variables, context) => {
83
99
  if (context?.previousSessions) {
84
- queryClient.setQueryData(['sessions', this.companyId], context.previousSessions);
100
+ queryClient.setQueryData(["sessions", this.companyId], context.previousSessions);
85
101
  }
86
102
  },
87
103
  });
@@ -89,17 +105,22 @@ AvroQueryClient.prototype.useCreateSessionBreak = function () {
89
105
  AvroQueryClient.prototype.useUpdateUserSession = function () {
90
106
  const queryClient = this.getQueryClient();
91
107
  return useMutation({
92
- mutationFn: ({ sessionId, updates }) => {
108
+ mutationFn: ({ sessionId, updates, }) => {
93
109
  return this.put({
94
110
  path: `/session/${sessionId}`,
95
111
  data: JSON.stringify(updates),
96
- headers: { "Content-Type": "application/json" }
112
+ headers: { "Content-Type": "application/json" },
97
113
  });
98
114
  },
99
- onMutate: async ({ sessionId, updates }) => {
100
- await queryClient.cancelQueries({ queryKey: ['sessions', this.companyId] });
101
- const previousSessions = queryClient.getQueryData(['sessions', this.companyId]);
102
- queryClient.setQueryData(['sessions', this.companyId], (oldData) => {
115
+ onMutate: async ({ sessionId, updates, }) => {
116
+ await queryClient.cancelQueries({
117
+ queryKey: ["sessions", this.companyId],
118
+ });
119
+ const previousSessions = queryClient.getQueryData([
120
+ "sessions",
121
+ this.companyId,
122
+ ]);
123
+ queryClient.setQueryData(["sessions", this.companyId], (oldData) => {
103
124
  if (!oldData)
104
125
  return oldData;
105
126
  if (Array.isArray(oldData)) {
@@ -109,9 +130,12 @@ AvroQueryClient.prototype.useUpdateUserSession = function () {
109
130
  });
110
131
  return { previousSessions };
111
132
  },
133
+ onSuccess: () => {
134
+ queryClient.invalidateQueries({ queryKey: ["sessions"] });
135
+ },
112
136
  onError: (err, variables, context) => {
113
137
  if (context?.previousSessions) {
114
- queryClient.setQueryData(['sessions', this.companyId], context.previousSessions);
138
+ queryClient.setQueryData(["sessions", this.companyId], context.previousSessions);
115
139
  }
116
140
  },
117
141
  });
@@ -119,32 +143,40 @@ AvroQueryClient.prototype.useUpdateUserSession = function () {
119
143
  AvroQueryClient.prototype.useUpdateSessionBreak = function () {
120
144
  const queryClient = this.getQueryClient();
121
145
  return useMutation({
122
- mutationFn: ({ breakId, updates }) => {
146
+ mutationFn: ({ breakId, updates, }) => {
123
147
  return this.put({
124
148
  path: `/break/${breakId}`,
125
149
  data: JSON.stringify(updates),
126
- headers: { "Content-Type": "application/json" }
150
+ headers: { "Content-Type": "application/json" },
127
151
  });
128
152
  },
129
- onMutate: async ({ breakId, updates }) => {
130
- await queryClient.cancelQueries({ queryKey: ['sessions', this.companyId] });
131
- const previousSessions = queryClient.getQueryData(['sessions', this.companyId]);
132
- queryClient.setQueryData(['sessions', this.companyId], (oldData) => {
153
+ onMutate: async ({ breakId, updates, }) => {
154
+ await queryClient.cancelQueries({
155
+ queryKey: ["sessions", this.companyId],
156
+ });
157
+ const previousSessions = queryClient.getQueryData([
158
+ "sessions",
159
+ this.companyId,
160
+ ]);
161
+ queryClient.setQueryData(["sessions", this.companyId], (oldData) => {
133
162
  if (!oldData)
134
163
  return oldData;
135
164
  if (Array.isArray(oldData)) {
136
165
  return oldData.map((session) => ({
137
166
  ...session,
138
- breaks: session.breaks?.map(b => b.id === breakId ? { ...b, ...updates } : b)
167
+ breaks: session.breaks?.map((b) => b.id === breakId ? { ...b, ...updates } : b),
139
168
  }));
140
169
  }
141
170
  return oldData;
142
171
  });
143
172
  return { previousSessions };
144
173
  },
174
+ onSuccess: () => {
175
+ queryClient.invalidateQueries({ queryKey: ["sessions"] });
176
+ },
145
177
  onError: (err, variables, context) => {
146
178
  if (context?.previousSessions) {
147
- queryClient.setQueryData(['sessions', this.companyId], context.previousSessions);
179
+ queryClient.setQueryData(["sessions", this.companyId], context.previousSessions);
148
180
  }
149
181
  },
150
182
  });
@@ -152,7 +184,7 @@ AvroQueryClient.prototype.useUpdateSessionBreak = function () {
152
184
  AvroQueryClient.prototype.useGetSessions = function (body) {
153
185
  const queryClient = this.getQueryClient();
154
186
  const result = useInfiniteQuery({
155
- queryKey: ['sessions', this.companyId, body.amt ?? 50, body.query ?? ""],
187
+ queryKey: ["sessions", this.companyId, body.amt ?? 50, body.query ?? ""],
156
188
  initialPageParam: 0,
157
189
  getNextPageParam: (lastPage, allPages) => {
158
190
  if (lastPage.length < (body.amt ?? 50))
@@ -164,7 +196,7 @@ AvroQueryClient.prototype.useGetSessions = function (body) {
164
196
  if (result.data) {
165
197
  result.data.pages.forEach((data_page) => {
166
198
  data_page.forEach((session) => {
167
- queryClient.setQueryData(['session', session.id], session);
199
+ queryClient.setQueryData(["session", session.id], session);
168
200
  });
169
201
  });
170
202
  }
@@ -1,5 +1,5 @@
1
- import { useMutation, useQuery } from '@tanstack/react-query';
2
- import { AvroQueryClient } from '../../client/QueryClient';
1
+ import { useMutation, useQuery } from "@tanstack/react-query";
2
+ import { AvroQueryClient } from "../../client/QueryClient";
3
3
  AvroQueryClient.prototype.useCreateSkill = function () {
4
4
  const queryClient = this.getQueryClient();
5
5
  return useMutation({
@@ -7,14 +7,23 @@ AvroQueryClient.prototype.useCreateSkill = function () {
7
7
  return this.post({
8
8
  path: `/company/${this.companyId}/skill`,
9
9
  data: JSON.stringify(skillData),
10
- headers: { "Content-Type": "application/json" }
10
+ headers: { "Content-Type": "application/json" },
11
11
  });
12
12
  },
13
+ onSuccess: () => {
14
+ queryClient.invalidateQueries({ queryKey: ["skills"] });
15
+ },
13
16
  });
14
17
  };
15
18
  AvroQueryClient.prototype.useGetSkills = function (body, total, onProgress) {
16
19
  return useQuery({
17
- queryKey: ['skills', this.companyId, body.amt ?? 50, body.query ?? "", total ?? "all"],
20
+ queryKey: [
21
+ "skills",
22
+ this.companyId,
23
+ body.amt ?? 50,
24
+ body.query ?? "",
25
+ total ?? "all",
26
+ ],
18
27
  queryFn: async () => {
19
28
  if (typeof total !== "number") {
20
29
  return this.fetchSkills({ ...body, offset: 0 });
@@ -41,24 +50,24 @@ AvroQueryClient.prototype.useGetSkills = function (body, total, onProgress) {
41
50
  AvroQueryClient.prototype.useUpdateSkill = function () {
42
51
  const queryClient = this.getQueryClient();
43
52
  return useMutation({
44
- mutationFn: async ({ skillId, updates }) => {
53
+ mutationFn: async ({ skillId, updates, }) => {
45
54
  return this.put({
46
55
  path: `/skill/${skillId}`,
47
56
  data: JSON.stringify(updates),
48
- headers: { "Content-Type": "application/json" }
57
+ headers: { "Content-Type": "application/json" },
49
58
  });
50
59
  },
51
60
  onMutate: async ({ skillId, updates }) => {
52
- await queryClient.cancelQueries({ queryKey: ['skills'] });
53
- await queryClient.cancelQueries({ queryKey: ['skills', skillId] });
54
- const previousSkills = queryClient.getQueryData(['skills']);
55
- const previousSkill = queryClient.getQueryData(['skills', skillId]);
56
- queryClient.setQueryData(['skills', skillId], (oldData) => {
61
+ await queryClient.cancelQueries({ queryKey: ["skills"] });
62
+ await queryClient.cancelQueries({ queryKey: ["skills", skillId] });
63
+ const previousSkills = queryClient.getQueryData(["skills"]);
64
+ const previousSkill = queryClient.getQueryData(["skills", skillId]);
65
+ queryClient.setQueryData(["skills", skillId], (oldData) => {
57
66
  if (!oldData)
58
67
  return oldData;
59
68
  return { ...oldData, ...updates };
60
69
  });
61
- queryClient.setQueriesData({ queryKey: ['skills'] }, (oldData) => {
70
+ queryClient.setQueriesData({ queryKey: ["skills"] }, (oldData) => {
62
71
  if (!oldData)
63
72
  return oldData;
64
73
  if (oldData.pages) {
@@ -72,13 +81,16 @@ AvroQueryClient.prototype.useUpdateSkill = function () {
72
81
  });
73
82
  return { previousSkills, previousSkill };
74
83
  },
84
+ onSuccess: () => {
85
+ queryClient.invalidateQueries({ queryKey: ["skills"] });
86
+ },
75
87
  onError: (_err, variables, context) => {
76
88
  const { skillId } = variables;
77
89
  if (context?.previousSkills) {
78
- queryClient.setQueryData(['skills'], context.previousSkills);
90
+ queryClient.setQueryData(["skills"], context.previousSkills);
79
91
  }
80
92
  if (context?.previousSkill) {
81
- queryClient.setQueryData(['skills', skillId], context.previousSkill);
93
+ queryClient.setQueryData(["skills", skillId], context.previousSkill);
82
94
  }
83
95
  },
84
96
  });
@@ -90,9 +102,9 @@ AvroQueryClient.prototype.useDeleteSkill = function () {
90
102
  return this.delete({ path: `/skill/${skillId}` });
91
103
  },
92
104
  onMutate: async ({ skillId }) => {
93
- await queryClient.cancelQueries({ queryKey: ['skills'] });
94
- const previousSkills = queryClient.getQueryData(['skills']);
95
- queryClient.setQueriesData({ queryKey: ['skills'] }, (oldData) => {
105
+ await queryClient.cancelQueries({ queryKey: ["skills"] });
106
+ const previousSkills = queryClient.getQueryData(["skills"]);
107
+ queryClient.setQueriesData({ queryKey: ["skills"] }, (oldData) => {
96
108
  if (!oldData)
97
109
  return oldData;
98
110
  if (oldData.pages) {
@@ -106,9 +118,12 @@ AvroQueryClient.prototype.useDeleteSkill = function () {
106
118
  });
107
119
  return { previousSkills };
108
120
  },
121
+ onSuccess: () => {
122
+ queryClient.invalidateQueries({ queryKey: ["skills"] });
123
+ },
109
124
  onError: (_err, variables, context) => {
110
125
  if (context?.previousSkills) {
111
- queryClient.setQueryData(['skills'], context.previousSkills);
126
+ queryClient.setQueryData(["skills"], context.previousSkills);
112
127
  }
113
128
  },
114
129
  });