@go-avro/avro-js 0.0.2-beta.14 → 0.0.2-beta.140

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 (57) hide show
  1. package/README.md +1 -0
  2. package/dist/auth/AuthManager.d.ts +12 -3
  3. package/dist/auth/AuthManager.js +56 -12
  4. package/dist/auth/storage.d.ts +8 -8
  5. package/dist/auth/storage.js +19 -14
  6. package/dist/client/AvroQueryClientProvider.d.ts +14 -0
  7. package/dist/client/AvroQueryClientProvider.js +32 -0
  8. package/dist/client/QueryClient.d.ts +447 -15
  9. package/dist/client/QueryClient.js +405 -201
  10. package/dist/client/core/fetch.d.ts +1 -0
  11. package/dist/client/core/fetch.js +62 -0
  12. package/dist/client/core/utils.d.ts +1 -0
  13. package/dist/client/core/utils.js +14 -0
  14. package/dist/client/core/xhr.d.ts +1 -0
  15. package/dist/client/core/xhr.js +84 -0
  16. package/dist/client/hooks/analytics.d.ts +1 -0
  17. package/dist/client/hooks/analytics.js +26 -0
  18. package/dist/client/hooks/avro.d.ts +1 -0
  19. package/dist/client/hooks/avro.js +9 -0
  20. package/dist/client/hooks/bills.d.ts +1 -0
  21. package/dist/client/hooks/bills.js +165 -0
  22. package/dist/client/hooks/chats.d.ts +1 -0
  23. package/dist/client/hooks/chats.js +37 -0
  24. package/dist/client/hooks/companies.d.ts +1 -0
  25. package/dist/client/hooks/companies.js +140 -0
  26. package/dist/client/hooks/events.d.ts +1 -0
  27. package/dist/client/hooks/events.js +308 -0
  28. package/dist/client/hooks/groups.d.ts +1 -0
  29. package/dist/client/hooks/groups.js +130 -0
  30. package/dist/client/hooks/jobs.d.ts +1 -0
  31. package/dist/client/hooks/jobs.js +213 -0
  32. package/dist/client/hooks/messages.d.ts +1 -0
  33. package/dist/client/hooks/messages.js +30 -0
  34. package/dist/client/hooks/months.d.ts +1 -0
  35. package/dist/client/hooks/months.js +93 -0
  36. package/dist/client/hooks/plans.d.ts +1 -0
  37. package/dist/client/hooks/plans.js +8 -0
  38. package/dist/client/hooks/root.d.ts +1 -0
  39. package/dist/client/hooks/root.js +8 -0
  40. package/dist/client/hooks/routes.d.ts +1 -0
  41. package/dist/client/hooks/routes.js +167 -0
  42. package/dist/client/hooks/sessions.d.ts +1 -0
  43. package/dist/client/hooks/sessions.js +175 -0
  44. package/dist/client/hooks/skills.d.ts +1 -0
  45. package/dist/client/hooks/skills.js +123 -0
  46. package/dist/client/hooks/teams.d.ts +1 -0
  47. package/dist/client/hooks/teams.js +128 -0
  48. package/dist/client/hooks/users.d.ts +1 -0
  49. package/dist/client/hooks/users.js +104 -0
  50. package/dist/index.d.ts +24 -1
  51. package/dist/index.js +24 -1
  52. package/dist/types/api.d.ts +138 -34
  53. package/dist/types/api.js +10 -1
  54. package/dist/types/auth.d.ts +0 -5
  55. package/dist/types/cache.d.ts +9 -0
  56. package/dist/types/cache.js +1 -0
  57. package/package.json +6 -4
@@ -0,0 +1,30 @@
1
+ import { useInfiniteQuery } from "@tanstack/react-query";
2
+ import { AvroQueryClient } from "../../client/QueryClient";
3
+ AvroQueryClient.prototype.useGetMessages = function (chatId, body) {
4
+ const queryClient = this.getQueryClient();
5
+ const result = useInfiniteQuery({
6
+ queryKey: [
7
+ 'messages',
8
+ chatId,
9
+ body.amt ?? 50,
10
+ body.known_ids ?? [],
11
+ body.unknown_ids ?? [],
12
+ body.query ?? '',
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.fetchMessages(chatId, { ...body, offset: pageParam }),
21
+ });
22
+ if (result.data) {
23
+ result.data.pages.forEach((data_page) => {
24
+ data_page.forEach((chat) => {
25
+ queryClient.setQueryData(['chat', chat.id], chat);
26
+ });
27
+ });
28
+ }
29
+ return result;
30
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,93 @@
1
+ import { useInfiniteQuery, useMutation } from '@tanstack/react-query';
2
+ import { AvroQueryClient } from '../../client/QueryClient';
3
+ AvroQueryClient.prototype.useGetMonths = function (body) {
4
+ const queryClient = this.getQueryClient();
5
+ const result = useInfiniteQuery({
6
+ queryKey: [
7
+ 'months',
8
+ this.companyId,
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.fetchMonths({ ...body, offset: pageParam }),
25
+ });
26
+ if (result.data) {
27
+ result.data.pages.forEach((data_page) => {
28
+ data_page.forEach((month) => {
29
+ queryClient.setQueryData(['month', month.id], month);
30
+ });
31
+ });
32
+ }
33
+ return result;
34
+ };
35
+ AvroQueryClient.prototype.useUpdateMonths = function () {
36
+ const queryClient = this.getQueryClient();
37
+ return useMutation({
38
+ mutationFn: async ({ months, action, }) => {
39
+ const monthIds = months.map(month => month.id);
40
+ return this.put(`/company/${this.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
+ queryClient.invalidateQueries({ queryKey: ['company'] });
91
+ },
92
+ });
93
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ import { useQuery } from "@tanstack/react-query";
2
+ import { AvroQueryClient } from "../../client/QueryClient";
3
+ AvroQueryClient.prototype.useGetPlans = function (code) {
4
+ return useQuery({
5
+ queryKey: ['plans', code],
6
+ queryFn: async () => this.get(`/plans${code ? `?code=${code}` : ''}`),
7
+ });
8
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ import { useQuery } from '@tanstack/react-query';
2
+ import { AvroQueryClient } from '../../client/QueryClient';
3
+ AvroQueryClient.prototype.useGetRoot = function () {
4
+ return useQuery({
5
+ queryKey: ['health'],
6
+ queryFn: () => this.get('/'),
7
+ });
8
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,167 @@
1
+ import { useMutation, useQuery } from '@tanstack/react-query';
2
+ import { AvroQueryClient } from '../../client/QueryClient';
3
+ AvroQueryClient.prototype.useGetRoutes = function (body, total, onProgress) {
4
+ return useQuery({
5
+ queryKey: ['routes', this.companyId, body.amt ?? 50, body.query ?? "", total ?? "all"],
6
+ queryFn: async () => {
7
+ if (typeof total !== "number") {
8
+ return this.fetchRoutes({ ...body, offset: 0 });
9
+ }
10
+ onProgress?.(0);
11
+ const pageCount = body.amt ? Math.ceil(total / body.amt) + 1 : 1;
12
+ let completed = 0;
13
+ const promises = Array.from({ length: pageCount }, (_, i) => this.fetchRoutes({
14
+ ...body,
15
+ offset: i * (body.amt ?? 1),
16
+ }));
17
+ const trackedPromises = promises.map((promise) => promise.then((result) => {
18
+ completed++;
19
+ const fraction = completed / pageCount;
20
+ onProgress?.(fraction);
21
+ return result;
22
+ }));
23
+ const pages = await Promise.all(trackedPromises);
24
+ const routes = pages.flat();
25
+ return routes;
26
+ },
27
+ });
28
+ };
29
+ AvroQueryClient.prototype.useGetRoute = function (routeId) {
30
+ const queryClient = this.getQueryClient();
31
+ return useQuery({
32
+ queryKey: ['route', routeId],
33
+ queryFn: () => this.get(`/route/${routeId}`),
34
+ enabled: Boolean(routeId) && routeId.length > 0,
35
+ });
36
+ };
37
+ AvroQueryClient.prototype.useCreateRoute = function () {
38
+ const queryClient = this.getQueryClient();
39
+ return useMutation({
40
+ mutationFn: async ({ routeData }) => {
41
+ return this.post(`/company/${this.companyId}/route`, JSON.stringify(routeData), undefined, { "Content-Type": "application/json" });
42
+ },
43
+ onMutate: async ({ routeData }) => {
44
+ await queryClient.cancelQueries({ queryKey: ['routes'] });
45
+ const previousRoutes = queryClient.getQueryData(['routes']);
46
+ queryClient.setQueriesData({ queryKey: ['routes'] }, (oldData) => {
47
+ if (!oldData)
48
+ return oldData;
49
+ if (oldData.pages) {
50
+ return {
51
+ ...oldData,
52
+ pages: [
53
+ [{ ...routeData, id: 'temp-id' }],
54
+ ...oldData.pages,
55
+ ],
56
+ };
57
+ }
58
+ if (Array.isArray(oldData)) {
59
+ return [{ ...routeData, id: 'temp-id' }, ...oldData];
60
+ }
61
+ return oldData;
62
+ });
63
+ return { previousRoutes };
64
+ },
65
+ onError: (_err, _variables, context) => {
66
+ if (context?.previousRoutes) {
67
+ queryClient.setQueryData(['routes'], context.previousRoutes);
68
+ }
69
+ },
70
+ onSettled: (_data, _error, _variables) => {
71
+ queryClient.invalidateQueries({ queryKey: ['routes'] });
72
+ queryClient.invalidateQueries({ queryKey: ['jobs'] });
73
+ queryClient.invalidateQueries({ queryKey: ['job'] });
74
+ queryClient.invalidateQueries({ queryKey: ['company'] });
75
+ },
76
+ });
77
+ };
78
+ AvroQueryClient.prototype.useUpdateRoute = function () {
79
+ const queryClient = this.getQueryClient();
80
+ return useMutation({
81
+ mutationFn: async ({ routeId, updates, }) => {
82
+ return this.put(`/route/${routeId}`, JSON.stringify(updates), undefined, { "Content-Type": "application/json" });
83
+ },
84
+ onMutate: async ({ routeId, updates }) => {
85
+ await queryClient.cancelQueries({ queryKey: ['route', routeId] });
86
+ await queryClient.cancelQueries({ queryKey: ['routes'] });
87
+ const previousRoute = queryClient.getQueryData(['route', routeId]);
88
+ const previousRoutes = queryClient.getQueryData(['routes']);
89
+ queryClient.setQueryData(['route', routeId], (oldData) => oldData ? { ...oldData, ...updates } : undefined);
90
+ queryClient.setQueriesData({ queryKey: ['routes'] }, (oldData) => {
91
+ if (!oldData)
92
+ return oldData;
93
+ if (oldData.pages) {
94
+ return {
95
+ ...oldData,
96
+ pages: oldData.pages.map((page) => page.map((route) => route.id === routeId ? { ...route, ...updates } : route)),
97
+ };
98
+ }
99
+ if (Array.isArray(oldData)) {
100
+ return oldData.map((route) => route.id === routeId ? { ...route, ...updates } : route);
101
+ }
102
+ return oldData;
103
+ });
104
+ return { previousRoute, previousRoutes };
105
+ },
106
+ onError: (_err, variables, context) => {
107
+ const { routeId } = variables;
108
+ if (context?.previousRoute) {
109
+ queryClient.setQueryData(['route', routeId], context.previousRoute);
110
+ }
111
+ if (context?.previousRoutes) {
112
+ queryClient.setQueryData(['routes'], context.previousRoutes);
113
+ }
114
+ },
115
+ onSettled: (_data, _error, variables) => {
116
+ const { routeId } = variables;
117
+ queryClient.invalidateQueries({ queryKey: ['routes'] });
118
+ queryClient.invalidateQueries({ queryKey: ['jobs'] });
119
+ queryClient.invalidateQueries({ queryKey: ['route', routeId] });
120
+ queryClient.invalidateQueries({ queryKey: ['job'] });
121
+ },
122
+ });
123
+ };
124
+ AvroQueryClient.prototype.useDeleteRoute = function () {
125
+ const queryClient = this.getQueryClient();
126
+ return useMutation({
127
+ mutationFn: async ({ routeId, }) => {
128
+ return this.delete(`/route/${routeId}`);
129
+ },
130
+ onMutate: async ({ routeId }) => {
131
+ await queryClient.cancelQueries({ queryKey: ['routes'] });
132
+ await queryClient.cancelQueries({ queryKey: ['route', routeId] });
133
+ const previousRoutes = queryClient.getQueryData(['routes']);
134
+ const previousRoute = queryClient.getQueryData(['route', routeId]);
135
+ queryClient.setQueryData(['route', routeId], undefined);
136
+ queryClient.setQueriesData({ queryKey: ['routes'] }, (oldData) => {
137
+ if (!oldData)
138
+ return oldData;
139
+ if (oldData.pages) {
140
+ const updatedPages = oldData.pages.map((page) => page.filter((route) => route.id !== routeId));
141
+ return { ...oldData, pages: updatedPages };
142
+ }
143
+ if (Array.isArray(oldData)) {
144
+ return oldData.filter((route) => route.id !== routeId);
145
+ }
146
+ return oldData;
147
+ });
148
+ return { previousRoutes, previousRoute };
149
+ },
150
+ onError: (_err, variables, context) => {
151
+ const { routeId } = variables;
152
+ if (context?.previousRoutes) {
153
+ queryClient.setQueryData(['routes'], context.previousRoutes);
154
+ }
155
+ if (context?.previousRoute) {
156
+ queryClient.setQueryData(['route', routeId], context.previousRoute);
157
+ }
158
+ },
159
+ onSettled: (_data, _error, variables) => {
160
+ const { routeId } = variables;
161
+ queryClient.invalidateQueries({ queryKey: ['routes'] });
162
+ queryClient.invalidateQueries({ queryKey: ['jobs'] });
163
+ queryClient.invalidateQueries({ queryKey: ['route', routeId] });
164
+ queryClient.invalidateQueries({ queryKey: ['job'] });
165
+ },
166
+ });
167
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,175 @@
1
+ import { useMutation, useQuery, useInfiniteQuery } from "@tanstack/react-query";
2
+ import { AvroQueryClient } from "../../client/QueryClient";
3
+ AvroQueryClient.prototype.useGetUserSessions = function () {
4
+ return useQuery({
5
+ queryKey: ['sessions'],
6
+ queryFn: () => this.get('/session'),
7
+ });
8
+ };
9
+ AvroQueryClient.prototype.useCreateUserSession = function () {
10
+ const queryClient = this.getQueryClient();
11
+ return useMutation({
12
+ mutationFn: ({ sessionData }) => {
13
+ return this.post(`/company/${this.companyId}/session`, JSON.stringify(sessionData), undefined, {
14
+ "Content-Type": "application/json",
15
+ });
16
+ },
17
+ onMutate: async ({ sessionData }) => {
18
+ await queryClient.cancelQueries({ queryKey: ['sessions'] });
19
+ const previousSessions = queryClient.getQueryData(['sessions']);
20
+ queryClient.setQueryData(['sessions'], (oldData) => {
21
+ if (!oldData)
22
+ return [];
23
+ if (Array.isArray(oldData)) {
24
+ return [
25
+ ...oldData,
26
+ {
27
+ ...sessionData,
28
+ id: Math.random().toString(36).substr(2, 9),
29
+ company_id: this.companyId,
30
+ }
31
+ ];
32
+ }
33
+ return oldData;
34
+ });
35
+ return { previousSessions };
36
+ },
37
+ onError: (err, variables, context) => {
38
+ if (context?.previousSessions) {
39
+ queryClient.setQueryData(['sessions'], context.previousSessions);
40
+ }
41
+ },
42
+ onSettled: () => {
43
+ queryClient.invalidateQueries({ queryKey: ['sessions'] });
44
+ },
45
+ });
46
+ };
47
+ AvroQueryClient.prototype.useCreateSessionBreak = function () {
48
+ const queryClient = this.getQueryClient();
49
+ return useMutation({
50
+ mutationFn: ({ sessionId, breakData }) => {
51
+ return this.post(`/session/${sessionId}/break`, JSON.stringify(breakData), undefined, {
52
+ "Content-Type": "application/json",
53
+ });
54
+ },
55
+ onMutate: async ({ sessionId, breakData }) => {
56
+ await queryClient.cancelQueries({ queryKey: ['sessions'] });
57
+ const previousSessions = queryClient.getQueryData(['sessions']);
58
+ queryClient.setQueryData(['sessions'], (oldData) => {
59
+ if (!oldData)
60
+ return oldData;
61
+ if (Array.isArray(oldData)) {
62
+ return oldData.map((session) => session.id === sessionId
63
+ ? {
64
+ ...session,
65
+ breaks: [
66
+ ...(session.breaks || []),
67
+ {
68
+ ...breakData,
69
+ id: Math.random().toString(36).substr(2, 9),
70
+ session_id: sessionId,
71
+ }
72
+ ]
73
+ }
74
+ : session);
75
+ }
76
+ return oldData;
77
+ });
78
+ return { previousSessions };
79
+ },
80
+ onError: (err, variables, context) => {
81
+ if (context?.previousSessions) {
82
+ queryClient.setQueryData(['sessions'], context.previousSessions);
83
+ }
84
+ },
85
+ onSettled: () => {
86
+ queryClient.invalidateQueries({ queryKey: ['sessions'] });
87
+ },
88
+ });
89
+ };
90
+ AvroQueryClient.prototype.useUpdateUserSession = function () {
91
+ const queryClient = this.getQueryClient();
92
+ return useMutation({
93
+ mutationFn: ({ sessionId, updates }) => {
94
+ return this.put(`/session/${sessionId}`, JSON.stringify(updates), undefined, {
95
+ "Content-Type": "application/json",
96
+ });
97
+ },
98
+ onMutate: async ({ sessionId, updates }) => {
99
+ await queryClient.cancelQueries({ queryKey: ['sessions'] });
100
+ const previousSessions = queryClient.getQueryData(['sessions']);
101
+ queryClient.setQueryData(['sessions'], (oldData) => {
102
+ if (!oldData)
103
+ return oldData;
104
+ if (Array.isArray(oldData)) {
105
+ return oldData.map((session) => session.id === sessionId ? { ...session, ...updates } : session);
106
+ }
107
+ return oldData;
108
+ });
109
+ return { previousSessions };
110
+ },
111
+ onError: (err, variables, context) => {
112
+ if (context?.previousSessions) {
113
+ queryClient.setQueryData(['sessions'], context.previousSessions);
114
+ }
115
+ },
116
+ onSettled: () => {
117
+ queryClient.invalidateQueries({ queryKey: ['sessions'] });
118
+ },
119
+ });
120
+ };
121
+ AvroQueryClient.prototype.useUpdateSessionBreak = function () {
122
+ const queryClient = this.getQueryClient();
123
+ return useMutation({
124
+ mutationFn: ({ breakId, updates }) => {
125
+ return this.put(`/break/${breakId}`, JSON.stringify(updates), undefined, {
126
+ "Content-Type": "application/json",
127
+ });
128
+ },
129
+ onMutate: async ({ breakId, updates }) => {
130
+ await queryClient.cancelQueries({ queryKey: ['sessions'] });
131
+ const previousSessions = queryClient.getQueryData(['sessions']);
132
+ queryClient.setQueryData(['sessions'], (oldData) => {
133
+ if (!oldData)
134
+ return oldData;
135
+ if (Array.isArray(oldData)) {
136
+ return oldData.map((session) => ({
137
+ ...session,
138
+ breaks: session.breaks?.map(b => b.id === breakId ? { ...b, ...updates } : b)
139
+ }));
140
+ }
141
+ return oldData;
142
+ });
143
+ return { previousSessions };
144
+ },
145
+ onError: (err, variables, context) => {
146
+ if (context?.previousSessions) {
147
+ queryClient.setQueryData(['sessions'], context.previousSessions);
148
+ }
149
+ },
150
+ onSettled: () => {
151
+ queryClient.invalidateQueries({ queryKey: ['sessions'] });
152
+ },
153
+ });
154
+ };
155
+ AvroQueryClient.prototype.useGetSessions = function (body) {
156
+ const queryClient = this.getQueryClient();
157
+ const result = useInfiniteQuery({
158
+ queryKey: ['sessions', this.companyId, body.amt ?? 50, body.query ?? ""],
159
+ initialPageParam: 0,
160
+ getNextPageParam: (lastPage, allPages) => {
161
+ if (lastPage.length < (body.amt ?? 50))
162
+ return undefined;
163
+ return allPages.flat().length; // next offset
164
+ },
165
+ queryFn: ({ pageParam = 0 }) => this.fetchSessions({ ...body, offset: pageParam }),
166
+ });
167
+ if (result.data) {
168
+ result.data.pages.forEach((data_page) => {
169
+ data_page.forEach((session) => {
170
+ queryClient.setQueryData(['session', session.id], session);
171
+ });
172
+ });
173
+ }
174
+ return result;
175
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,123 @@
1
+ import { useMutation, useQuery } from '@tanstack/react-query';
2
+ import { AvroQueryClient } from '../../client/QueryClient';
3
+ AvroQueryClient.prototype.useCreateSkill = function () {
4
+ const queryClient = this.getQueryClient();
5
+ return useMutation({
6
+ mutationFn: async ({ skillData }) => {
7
+ return this.post(`/company/${this.companyId}/skill`, JSON.stringify(skillData), undefined, {
8
+ "Content-Type": "application/json",
9
+ });
10
+ },
11
+ onSettled: () => {
12
+ queryClient.invalidateQueries({ queryKey: ['company'] });
13
+ queryClient.invalidateQueries({ queryKey: ['skills'] });
14
+ },
15
+ });
16
+ };
17
+ AvroQueryClient.prototype.useGetSkills = function (body, total, onProgress) {
18
+ return useQuery({
19
+ queryKey: ['skills', this.companyId, body.amt ?? 50, body.query ?? "", total ?? "all"],
20
+ queryFn: async () => {
21
+ if (typeof total !== "number") {
22
+ return this.fetchSkills({ ...body, offset: 0 });
23
+ }
24
+ onProgress?.(0);
25
+ const pageCount = body.amt ? Math.ceil(total / body.amt) + 1 : 1;
26
+ let completed = 0;
27
+ const promises = Array.from({ length: pageCount }, (_, i) => this.fetchSkills({
28
+ ...body,
29
+ offset: i * (body.amt ?? 1),
30
+ }));
31
+ const trackedPromises = promises.map((promise) => promise.then((result) => {
32
+ completed++;
33
+ const fraction = completed / pageCount;
34
+ onProgress?.(fraction);
35
+ return result;
36
+ }));
37
+ const pages = await Promise.all(trackedPromises);
38
+ const skills = pages.flat();
39
+ return skills;
40
+ },
41
+ });
42
+ };
43
+ AvroQueryClient.prototype.useUpdateSkill = function () {
44
+ const queryClient = this.getQueryClient();
45
+ return useMutation({
46
+ mutationFn: async ({ skillId, updates }) => {
47
+ return this.put(`/skill/${skillId}`, JSON.stringify(updates), undefined, {
48
+ "Content-Type": "application/json",
49
+ });
50
+ },
51
+ onMutate: async ({ skillId, updates }) => {
52
+ await queryClient.cancelQueries({ queryKey: ['skills'] });
53
+ await queryClient.cancelQueries({ queryKey: ['skill', skillId] });
54
+ const previousSkills = queryClient.getQueryData(['skills']);
55
+ const previousSkill = queryClient.getQueryData(['skill', skillId]);
56
+ queryClient.setQueryData(['skill', skillId], (oldData) => {
57
+ if (!oldData)
58
+ return oldData;
59
+ return { ...oldData, ...updates };
60
+ });
61
+ queryClient.setQueriesData({ queryKey: ['skills'] }, (oldData) => {
62
+ if (!oldData)
63
+ return oldData;
64
+ if (oldData.pages) {
65
+ const updatedPages = oldData.pages.map((page) => page.map((skill) => skill.id === skillId ? { ...skill, ...updates } : skill));
66
+ return { ...oldData, pages: updatedPages };
67
+ }
68
+ if (Array.isArray(oldData)) {
69
+ return oldData.map((skill) => skill.id === skillId ? { ...skill, ...updates } : skill);
70
+ }
71
+ return oldData;
72
+ });
73
+ return { previousSkills, previousSkill };
74
+ },
75
+ onError: (_err, variables, context) => {
76
+ const { skillId } = variables;
77
+ if (context?.previousSkills) {
78
+ queryClient.setQueryData(['skills'], context.previousSkills);
79
+ }
80
+ if (context?.previousSkill) {
81
+ queryClient.setQueryData(['skill', skillId], context.previousSkill);
82
+ }
83
+ },
84
+ onSettled: (_data, _error, variables) => {
85
+ const { skillId } = variables;
86
+ queryClient.invalidateQueries({ queryKey: ['skills'] });
87
+ queryClient.invalidateQueries({ queryKey: ['skill', skillId] });
88
+ },
89
+ });
90
+ };
91
+ AvroQueryClient.prototype.useDeleteSkill = function () {
92
+ const queryClient = this.getQueryClient();
93
+ return useMutation({
94
+ mutationFn: async ({ skillId }) => {
95
+ return this.delete(`/skill/${skillId}`);
96
+ },
97
+ onMutate: async ({ skillId }) => {
98
+ await queryClient.cancelQueries({ queryKey: ['skills'] });
99
+ const previousSkills = queryClient.getQueryData(['skills']);
100
+ queryClient.setQueriesData({ queryKey: ['skills'] }, (oldData) => {
101
+ if (!oldData)
102
+ return oldData;
103
+ if (oldData.pages) {
104
+ const updatedPages = oldData.pages.map((page) => page.filter((skill) => skill.id !== skillId));
105
+ return { ...oldData, pages: updatedPages };
106
+ }
107
+ if (Array.isArray(oldData)) {
108
+ return oldData.filter((skill) => skill.id !== skillId);
109
+ }
110
+ return oldData;
111
+ });
112
+ return { previousSkills };
113
+ },
114
+ onError: (_err, variables, context) => {
115
+ if (context?.previousSkills) {
116
+ queryClient.setQueryData(['skills'], context.previousSkills);
117
+ }
118
+ },
119
+ onSettled: () => {
120
+ queryClient.invalidateQueries({ queryKey: ['skills'] });
121
+ },
122
+ });
123
+ };
@@ -0,0 +1 @@
1
+ export {};