@go-avro/avro-js 0.1.15 → 1.6.0

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 (38) hide show
  1. package/dist/auth/AuthManager.d.ts +28 -0
  2. package/dist/auth/AuthManager.js +119 -43
  3. package/dist/client/QueryClient.d.ts +56 -1
  4. package/dist/client/QueryClient.js +260 -79
  5. package/dist/client/cache.d.ts +24 -0
  6. package/dist/client/cache.js +51 -0
  7. package/dist/client/core/fetch.js +58 -49
  8. package/dist/client/core/policy.d.ts +17 -0
  9. package/dist/client/core/policy.js +35 -0
  10. package/dist/client/core/xhr.js +16 -14
  11. package/dist/client/hooks/bills.js +10 -14
  12. package/dist/client/hooks/catalog_items.js +18 -8
  13. package/dist/client/hooks/chats.js +2 -8
  14. package/dist/client/hooks/companies.js +11 -5
  15. package/dist/client/hooks/events.js +36 -30
  16. package/dist/client/hooks/groups.js +11 -26
  17. package/dist/client/hooks/jobs.js +3 -8
  18. package/dist/client/hooks/labels.js +11 -26
  19. package/dist/client/hooks/messages.js +3 -8
  20. package/dist/client/hooks/months.js +28 -22
  21. package/dist/client/hooks/payouts.js +3 -8
  22. package/dist/client/hooks/prepayments.js +28 -22
  23. package/dist/client/hooks/proposal.js +1 -0
  24. package/dist/client/hooks/routes.js +8 -6
  25. package/dist/client/hooks/sessions.js +11 -27
  26. package/dist/client/hooks/skills.js +6 -4
  27. package/dist/client/hooks/teams.js +6 -4
  28. package/dist/client/hooks/timecards.js +7 -7
  29. package/dist/client/hooks/waivers.js +6 -4
  30. package/dist/client/persist.js +21 -2
  31. package/dist/client/realtime.d.ts +12 -0
  32. package/dist/client/realtime.js +160 -46
  33. package/dist/types/api/Company.d.ts +2 -0
  34. package/dist/types/api/LineItem.d.ts +4 -0
  35. package/dist/types/api/LineItem.js +8 -1
  36. package/dist/version.d.ts +1 -1
  37. package/dist/version.js +1 -1
  38. package/package.json +7 -2
@@ -1,5 +1,6 @@
1
1
  import { useInfiniteQuery, useQuery, useMutation } from '@tanstack/react-query';
2
2
  import { AvroQueryClient } from '../QueryClient.js';
3
+ import { restoreNs, snapshotNs, seedDetails, removeOptimisticRow } from '../cache.js';
3
4
  import { keys, matchesList } from '../realtime.js';
4
5
  import { constructEvent, DelayEvent, Job, LineItemStatus, } from '../../types/api.js';
5
6
  const isJobsQuery = (q) => q.queryKey[0] === 'jobs';
@@ -7,15 +8,20 @@ const isEventsQuery = (q) => q.queryKey[0] === 'events';
7
8
  const isEventsQueryFor = (item) => (q) => q.queryKey[0] === 'events' && matchesList('event', q.queryKey, item) === 'match';
8
9
  AvroQueryClient.prototype.useGetEvents = function (body) {
9
10
  const queryClient = this.getQueryClient();
11
+ // Status buckets default to the backend's (unbilled only) and are
12
+ // always sent, so the key describes exactly what the server returns.
13
+ const unbilled = body.unbilled ?? true;
14
+ const billed = body.billed ?? false;
15
+ const paid = body.paid ?? false;
10
16
  const result = useInfiniteQuery({
11
17
  queryKey: keys.infinite('events', this.companyId, {
12
18
  amt: body.amt ?? 50,
13
19
  known_ids: body.known_ids ?? [],
14
20
  unknown_ids: body.unknown_ids ?? [],
15
21
  query: body.query ?? '',
16
- unbilled: body.unbilled ?? true,
17
- billed: body.billed ?? true,
18
- paid: body.paid ?? true,
22
+ unbilled,
23
+ billed,
24
+ paid,
19
25
  jobId: body.jobId ?? '',
20
26
  sort_by: body.sort_by ?? null,
21
27
  direction: body.direction ?? null,
@@ -28,19 +34,21 @@ AvroQueryClient.prototype.useGetEvents = function (body) {
28
34
  return allPages.flat().length; // next offset
29
35
  },
30
36
  queryFn: ({ pageParam = 0 }) => this.fetchEvents({
31
- ...body,
37
+ amt: body.amt,
38
+ known_ids: body.known_ids,
39
+ unknown_ids: body.unknown_ids,
40
+ query: body.query,
41
+ unbilled,
42
+ billed,
43
+ paid,
32
44
  job_id: body.jobId,
45
+ sort_by: body.sort_by,
46
+ direction: body.direction,
47
+ line_item_types: body.line_item_types,
33
48
  offset: pageParam,
34
- }).then((events) => events.map((event) => constructEvent(event))),
35
- enabled: body.enabled ?? true,
49
+ }).then((events) => seedDetails(queryClient, 'events', events.map((event) => constructEvent(event)))),
50
+ enabled: (body.enabled ?? true) && Boolean(this.companyId),
36
51
  });
37
- if (result.data) {
38
- result.data.pages.forEach((data_page) => {
39
- data_page.forEach((event) => {
40
- queryClient.setQueryData(['events', event.id], event);
41
- });
42
- });
43
- }
44
52
  return result;
45
53
  };
46
54
  AvroQueryClient.prototype.useGetEvent = function (eventId) {
@@ -117,9 +125,12 @@ AvroQueryClient.prototype.useCreateEvent = function () {
117
125
  }
118
126
  return oldData;
119
127
  });
120
- return { previousJob };
128
+ return { previousJob, tempId: optimisticEvent.id };
121
129
  },
122
- onSuccess: () => {
130
+ onSuccess: (_data, _vars, context) => {
131
+ // The echoed change (or the fallback refetch) brings the real row.
132
+ if (context?.tempId)
133
+ removeOptimisticRow(queryClient, 'events', context.tempId);
123
134
  this.syncAfterMutation('event', 'job');
124
135
  },
125
136
  onError: (err, variables, context) => {
@@ -204,9 +215,11 @@ AvroQueryClient.prototype.useCreateDelayEvent = function () {
204
215
  }
205
216
  return oldData;
206
217
  });
207
- return { previousJob };
218
+ return { previousJob, tempId: optimisticEvent.id };
208
219
  },
209
- onSuccess: () => {
220
+ onSuccess: (_data, _vars, context) => {
221
+ if (context?.tempId)
222
+ removeOptimisticRow(queryClient, 'events', context.tempId);
210
223
  this.syncAfterMutation('event', 'job');
211
224
  },
212
225
  onError: (_err, _variables, context) => {
@@ -358,8 +371,7 @@ AvroQueryClient.prototype.useUpdateEvents = function () {
358
371
  await queryClient.cancelQueries({ queryKey: ['events'] });
359
372
  await queryClient.cancelQueries({ queryKey: ['events'] });
360
373
  const billable = events.filter((event) => event.line_item_type !== 'DELAY');
361
- const previousEvents = queryClient.getQueryData(['events']);
362
- const previousEventObjs = billable.map((event) => queryClient.getQueryData(['events', event.id]));
374
+ const previousEvents = snapshotNs(queryClient, 'events');
363
375
  const eventIds = billable.map((event) => event.id);
364
376
  // Optimistically update individual event cache
365
377
  eventIds.forEach((eventId, idx) => {
@@ -404,20 +416,14 @@ AvroQueryClient.prototype.useUpdateEvents = function () {
404
416
  }
405
417
  return oldData;
406
418
  });
407
- return { previousEvents, previousEventObjs };
419
+ return { previousEvents };
408
420
  },
409
421
  onSuccess: () => {
410
422
  this.syncAfterMutation('event');
411
423
  },
412
424
  onError: (err, variables, context) => {
413
- if (context?.previousEvents) {
414
- queryClient.setQueryData(['events'], context.previousEvents);
415
- }
416
- if (context?.previousEventObjs) {
417
- context.previousEventObjs.forEach((eventObj) => {
418
- queryClient.setQueryData(['events', eventObj.id], eventObj);
419
- });
420
- }
425
+ // The namespace snapshot covers lists and details.
426
+ restoreNs(queryClient, context?.previousEvents);
421
427
  },
422
428
  });
423
429
  };
@@ -433,7 +439,7 @@ AvroQueryClient.prototype.useDeleteEvent = function () {
433
439
  onMutate: async ({ eventId }) => {
434
440
  await queryClient.cancelQueries({ queryKey: ['events'] });
435
441
  await queryClient.cancelQueries({ queryKey: ['events', eventId] });
436
- const previousEvents = queryClient.getQueryData(['events']);
442
+ const previousEvents = snapshotNs(queryClient, 'events');
437
443
  const previousEvent = queryClient.getQueryData(['events', eventId]);
438
444
  queryClient.setQueryData(['events', eventId], undefined);
439
445
  queryClient.setQueriesData({ queryKey: ['events'] }, (oldData) => {
@@ -456,7 +462,7 @@ AvroQueryClient.prototype.useDeleteEvent = function () {
456
462
  onError: (_err, variables, context) => {
457
463
  const { eventId } = variables;
458
464
  if (context?.previousEvents) {
459
- queryClient.setQueryData(['events'], context.previousEvents);
465
+ restoreNs(queryClient, context.previousEvents);
460
466
  }
461
467
  if (context?.previousEvent) {
462
468
  queryClient.setQueryData(['events', eventId], context.previousEvent);
@@ -1,6 +1,7 @@
1
1
  import { useMutation, useQuery } from '@tanstack/react-query';
2
2
  import { AvroQueryClient } from '../QueryClient.js';
3
3
  import { keys } from '../realtime.js';
4
+ import { restoreNs, snapshotNs } from '../cache.js';
4
5
  AvroQueryClient.prototype.useGetGroups = function (body, total, onProgress) {
5
6
  return useQuery({
6
7
  queryKey: keys.list('groups', this.companyId, {
@@ -8,27 +9,15 @@ AvroQueryClient.prototype.useGetGroups = function (body, total, onProgress) {
8
9
  query: body.query ?? '',
9
10
  total: total ?? 'all',
10
11
  }),
12
+ // Embedded in the company payload (no list endpoint): one request
13
+ // returns them all, so `total`/paging is moot.
11
14
  queryFn: async () => {
12
- if (typeof total !== 'number') {
13
- return this.fetchGroups({ ...body, offset: 0 });
14
- }
15
15
  onProgress?.(0);
16
- const pageCount = body.amt ? Math.ceil(total / body.amt) + 1 : 1;
17
- let completed = 0;
18
- const promises = Array.from({ length: pageCount }, (_, i) => this.fetchGroups({
19
- ...body,
20
- offset: i * (body.amt ?? 1),
21
- }));
22
- const trackedPromises = promises.map((promise) => promise.then((result) => {
23
- completed++;
24
- const fraction = completed / pageCount;
25
- onProgress?.(fraction);
26
- return result;
27
- }));
28
- const pages = await Promise.all(trackedPromises);
29
- const groups = pages.flat();
30
- return groups;
16
+ const items = await this.fetchGroups({ query: body.query });
17
+ onProgress?.(1);
18
+ return items;
31
19
  },
20
+ enabled: Boolean(this.companyId),
32
21
  });
33
22
  };
34
23
  AvroQueryClient.prototype.useCreateGroup = function () {
@@ -59,7 +48,7 @@ AvroQueryClient.prototype.useUpdateGroup = function () {
59
48
  onMutate: async ({ groupId, groupData }) => {
60
49
  await queryClient.cancelQueries({ queryKey: ['groups'] });
61
50
  await queryClient.cancelQueries({ queryKey: ['groups', groupId] });
62
- const previousGroups = queryClient.getQueryData(['groups']);
51
+ const previousGroups = snapshotNs(queryClient, 'groups');
63
52
  const previousGroup = queryClient.getQueryData(['groups', groupId]);
64
53
  queryClient.setQueryData(['groups', groupId], (oldData) => {
65
54
  if (!oldData)
@@ -85,9 +74,7 @@ AvroQueryClient.prototype.useUpdateGroup = function () {
85
74
  },
86
75
  onError: (_err, variables, context) => {
87
76
  const { groupId } = variables;
88
- if (context?.previousGroups) {
89
- queryClient.setQueryData(['groups'], context.previousGroups);
90
- }
77
+ restoreNs(queryClient, context?.previousGroups);
91
78
  if (context?.previousGroup) {
92
79
  queryClient.setQueryData(['groups', groupId], context.previousGroup);
93
80
  }
@@ -105,7 +92,7 @@ AvroQueryClient.prototype.useDeleteGroup = function () {
105
92
  onMutate: async ({ groupId }) => {
106
93
  await queryClient.cancelQueries({ queryKey: ['groups'] });
107
94
  await queryClient.cancelQueries({ queryKey: ['groups', groupId] });
108
- const previousGroups = queryClient.getQueryData(['groups']);
95
+ const previousGroups = snapshotNs(queryClient, 'groups');
109
96
  const previousGroup = queryClient.getQueryData(['groups', groupId]);
110
97
  queryClient.setQueryData(['groups', groupId], undefined);
111
98
  queryClient.setQueriesData({ queryKey: ['groups'] }, (oldData) => {
@@ -127,9 +114,7 @@ AvroQueryClient.prototype.useDeleteGroup = function () {
127
114
  },
128
115
  onError: (_err, variables, context) => {
129
116
  const { groupId } = variables;
130
- if (context?.previousGroups) {
131
- queryClient.setQueryData(['groups'], context.previousGroups);
132
- }
117
+ restoreNs(queryClient, context?.previousGroups);
133
118
  if (context?.previousGroup) {
134
119
  queryClient.setQueryData(['groups', groupId], context.previousGroup);
135
120
  }
@@ -1,5 +1,6 @@
1
1
  import { useMutation, useQuery, useInfiniteQuery } from '@tanstack/react-query';
2
2
  import { AvroQueryClient } from '../QueryClient.js';
3
+ import { seedDetails } from '../cache.js';
3
4
  import { keys, constructJob } from '../realtime.js';
4
5
  import { Job } from '../../types/api.js';
5
6
  AvroQueryClient.prototype.getJobsFromCache = function (queryClient) {
@@ -83,16 +84,10 @@ AvroQueryClient.prototype.useGetInfiniteJobs = function (body, onProgress) {
83
84
  ...body,
84
85
  route_id: body.routeId,
85
86
  offset: pageParam,
86
- }).then((jobs) => jobs.map((job) => new Job(job)));
87
+ }).then((jobs) => seedDetails(queryClient, 'jobs', jobs.map((job) => new Job(job))));
87
88
  },
89
+ enabled: Boolean(this.companyId),
88
90
  });
89
- if (result.data) {
90
- result.data.pages.forEach((data_page) => {
91
- data_page.forEach((job) => {
92
- queryClient.setQueryData(['jobs', job.id], job);
93
- });
94
- });
95
- }
96
91
  return result;
97
92
  };
98
93
  AvroQueryClient.prototype.useGetJob = function (jobId) {
@@ -1,6 +1,7 @@
1
1
  import { useMutation, useQuery } from '@tanstack/react-query';
2
2
  import { AvroQueryClient } from '../QueryClient.js';
3
3
  import { keys } from '../realtime.js';
4
+ import { restoreNs, snapshotNs } from '../cache.js';
4
5
  AvroQueryClient.prototype.useGetLabels = function (body, total, onProgress) {
5
6
  return useQuery({
6
7
  queryKey: keys.list('labels', this.companyId, {
@@ -8,27 +9,15 @@ AvroQueryClient.prototype.useGetLabels = function (body, total, onProgress) {
8
9
  query: body.query ?? '',
9
10
  total: total ?? 'all',
10
11
  }),
12
+ // Embedded in the company payload (no list endpoint): one request
13
+ // returns them all, so `total`/paging is moot.
11
14
  queryFn: async () => {
12
- if (typeof total !== 'number') {
13
- return this.fetchLabels({ ...body, offset: 0 });
14
- }
15
15
  onProgress?.(0);
16
- const pageCount = body.amt ? Math.ceil(total / body.amt) + 1 : 1;
17
- let completed = 0;
18
- const promises = Array.from({ length: pageCount }, (_, i) => this.fetchLabels({
19
- ...body,
20
- offset: i * (body.amt ?? 1),
21
- }));
22
- const trackedPromises = promises.map((promise) => promise.then((result) => {
23
- completed++;
24
- const fraction = completed / pageCount;
25
- onProgress?.(fraction);
26
- return result;
27
- }));
28
- const pages = await Promise.all(trackedPromises);
29
- const labels = pages.flat();
30
- return labels;
16
+ const items = await this.fetchLabels({ query: body.query });
17
+ onProgress?.(1);
18
+ return items;
31
19
  },
20
+ enabled: Boolean(this.companyId),
32
21
  });
33
22
  };
34
23
  AvroQueryClient.prototype.useCreateLabel = function () {
@@ -59,7 +48,7 @@ AvroQueryClient.prototype.useUpdateLabel = function () {
59
48
  onMutate: async ({ labelId, labelData }) => {
60
49
  await queryClient.cancelQueries({ queryKey: ['labels'] });
61
50
  await queryClient.cancelQueries({ queryKey: ['labels', labelId] });
62
- const previousLabels = queryClient.getQueryData(['labels']);
51
+ const previousLabels = snapshotNs(queryClient, 'labels');
63
52
  const previousLabel = queryClient.getQueryData(['labels', labelId]);
64
53
  queryClient.setQueryData(['labels', labelId], (oldData) => {
65
54
  if (!oldData)
@@ -85,9 +74,7 @@ AvroQueryClient.prototype.useUpdateLabel = function () {
85
74
  },
86
75
  onError: (_err, variables, context) => {
87
76
  const { labelId } = variables;
88
- if (context?.previousLabels) {
89
- queryClient.setQueryData(['labels'], context.previousLabels);
90
- }
77
+ restoreNs(queryClient, context?.previousLabels);
91
78
  if (context?.previousLabel) {
92
79
  queryClient.setQueryData(['labels', labelId], context.previousLabel);
93
80
  }
@@ -103,7 +90,7 @@ AvroQueryClient.prototype.useDeleteLabel = function () {
103
90
  onMutate: async ({ labelId }) => {
104
91
  await queryClient.cancelQueries({ queryKey: ['labels'] });
105
92
  await queryClient.cancelQueries({ queryKey: ['labels', labelId] });
106
- const previousLabels = queryClient.getQueryData(['labels']);
93
+ const previousLabels = snapshotNs(queryClient, 'labels');
107
94
  const previousLabel = queryClient.getQueryData(['labels', labelId]);
108
95
  queryClient.setQueryData(['labels', labelId], undefined);
109
96
  queryClient.setQueriesData({ queryKey: ['labels'] }, (oldData) => {
@@ -125,9 +112,7 @@ AvroQueryClient.prototype.useDeleteLabel = function () {
125
112
  },
126
113
  onError: (_err, variables, context) => {
127
114
  const { labelId } = variables;
128
- if (context?.previousLabels) {
129
- queryClient.setQueryData(['labels'], context.previousLabels);
130
- }
115
+ restoreNs(queryClient, context?.previousLabels);
131
116
  if (context?.previousLabel) {
132
117
  queryClient.setQueryData(['labels', labelId], context.previousLabel);
133
118
  }
@@ -1,5 +1,6 @@
1
1
  import { useInfiniteQuery, useMutation } from '@tanstack/react-query';
2
2
  import { AvroQueryClient } from '../QueryClient.js';
3
+ import { seedDetails } from '../cache.js';
3
4
  import { keys } from '../realtime.js';
4
5
  AvroQueryClient.prototype.useGetMessages = function (chatId, body) {
5
6
  const queryClient = this.getQueryClient();
@@ -19,15 +20,9 @@ AvroQueryClient.prototype.useGetMessages = function (chatId, body) {
19
20
  queryFn: ({ pageParam = 0 }) => this.fetchMessages(chatId, {
20
21
  ...body,
21
22
  offset: pageParam,
22
- }),
23
+ }).then((messages) => seedDetails(queryClient, 'message', messages)),
24
+ enabled: Boolean(chatId),
23
25
  });
24
- if (result.data) {
25
- result.data.pages.forEach((data_page) => {
26
- data_page.forEach((message) => {
27
- queryClient.setQueryData(['message', message.id], message);
28
- });
29
- });
30
- }
31
26
  return result;
32
27
  };
33
28
  AvroQueryClient.prototype.useCreateMessage = function (chatId) {
@@ -1,18 +1,24 @@
1
1
  import { useInfiniteQuery, useMutation } from '@tanstack/react-query';
2
2
  import { AvroQueryClient } from '../QueryClient.js';
3
+ import { restoreNs, seedDetails, snapshotNs } from '../cache.js';
3
4
  import { keys } from '../realtime.js';
4
5
  import { LineItemStatus } from '../../types/api.js';
5
6
  AvroQueryClient.prototype.useGetMonths = function (body) {
6
7
  const queryClient = this.getQueryClient();
8
+ // Status buckets default to the backend's (unbilled only) and are
9
+ // always sent, so the key describes exactly what the server returns.
10
+ const unbilled = body.unbilled ?? true;
11
+ const billed = body.billed ?? false;
12
+ const paid = body.paid ?? false;
7
13
  const result = useInfiniteQuery({
8
14
  queryKey: keys.infinite('months', this.companyId, {
9
15
  amt: body.amt ?? 50,
10
16
  known_ids: body.known_ids ?? [],
11
17
  unknown_ids: body.unknown_ids ?? [],
12
18
  query: body.query ?? '',
13
- unbilled: body.unbilled ?? true,
14
- billed: body.billed ?? true,
15
- paid: body.paid ?? true,
19
+ unbilled,
20
+ billed,
21
+ paid,
16
22
  jobId: body.jobId ?? '',
17
23
  sort_by: body.sort_by ?? null,
18
24
  direction: body.direction ?? null,
@@ -23,16 +29,21 @@ AvroQueryClient.prototype.useGetMonths = function (body) {
23
29
  return undefined;
24
30
  return allPages.flat().length; // next offset
25
31
  },
26
- queryFn: ({ pageParam = 0 }) => this.fetchMonths({ ...body, offset: pageParam }),
27
- enabled: body.enabled ?? true,
32
+ queryFn: ({ pageParam = 0 }) => this.fetchMonths({
33
+ amt: body.amt,
34
+ known_ids: body.known_ids,
35
+ unknown_ids: body.unknown_ids,
36
+ query: body.query,
37
+ unbilled,
38
+ billed,
39
+ paid,
40
+ job_id: body.jobId,
41
+ sort_by: body.sort_by,
42
+ direction: body.direction,
43
+ offset: pageParam,
44
+ }).then((items) => seedDetails(queryClient, 'months', items)),
45
+ enabled: (body.enabled ?? true) && Boolean(this.companyId),
28
46
  });
29
- if (result.data) {
30
- result.data.pages.forEach((data_page) => {
31
- data_page.forEach((month) => {
32
- queryClient.setQueryData(['month', month.id], month);
33
- });
34
- });
35
- }
36
47
  return result;
37
48
  };
38
49
  AvroQueryClient.prototype.useUpdateMonth = function () {
@@ -71,11 +82,10 @@ AvroQueryClient.prototype.useUpdateMonths = function () {
71
82
  onMutate: async ({ months, action }) => {
72
83
  await queryClient.cancelQueries({ queryKey: ['months'] });
73
84
  await queryClient.cancelQueries({ queryKey: ['months'] });
74
- const previousMonths = queryClient.getQueryData(['months']);
75
- const previousMonthObjs = months.map((month) => queryClient.getQueryData(['month', month.id]));
85
+ const previousMonths = snapshotNs(queryClient, 'months');
76
86
  const monthIds = months.map((month) => month.id);
77
87
  monthIds.forEach((monthId, idx) => {
78
- queryClient.setQueryData(['month', monthId], (oldData) => {
88
+ queryClient.setQueryData(['months', monthId], (oldData) => {
79
89
  return oldData
80
90
  ? {
81
91
  ...oldData,
@@ -112,18 +122,14 @@ AvroQueryClient.prototype.useUpdateMonths = function () {
112
122
  }
113
123
  return oldData;
114
124
  });
115
- return { previousMonths, previousMonthObjs };
125
+ return { previousMonths };
116
126
  },
117
127
  onSuccess: () => {
118
128
  queryClient.invalidateQueries({ queryKey: ['months'] });
119
129
  },
120
130
  onError: (err, variables, context) => {
121
- if (context) {
122
- queryClient.setQueryData(['months'], context.previousMonths);
123
- context.previousMonthObjs.forEach((monthObj) => {
124
- queryClient.setQueryData(['month', monthObj.id], monthObj);
125
- });
126
- }
131
+ // The namespace snapshot covers lists and details.
132
+ restoreNs(queryClient, context?.previousMonths);
127
133
  },
128
134
  });
129
135
  };
@@ -1,5 +1,6 @@
1
1
  import { useInfiniteQuery } from '@tanstack/react-query';
2
2
  import { AvroQueryClient } from '../QueryClient.js';
3
+ import { seedDetails } from '../cache.js';
3
4
  import { keys } from '../realtime.js';
4
5
  AvroQueryClient.prototype.useGetPayouts = function (body) {
5
6
  const queryClient = this.getQueryClient();
@@ -22,14 +23,8 @@ AvroQueryClient.prototype.useGetPayouts = function (body) {
22
23
  return undefined;
23
24
  return allPages.flat().length; // next offset
24
25
  },
25
- queryFn: ({ pageParam = 0 }) => this.fetchPayouts({ ...body, offset: pageParam }),
26
+ queryFn: ({ pageParam = 0 }) => this.fetchPayouts({ ...body, offset: pageParam }).then((payouts) => seedDetails(queryClient, 'payouts', payouts)),
27
+ enabled: Boolean(this.companyId),
26
28
  });
27
- if (result.data) {
28
- result.data.pages.forEach((data_page) => {
29
- data_page.forEach((payout) => {
30
- queryClient.setQueryData(['payouts', payout.id], payout);
31
- });
32
- });
33
- }
34
29
  return result;
35
30
  };
@@ -1,18 +1,24 @@
1
1
  import { useInfiniteQuery, useMutation } from '@tanstack/react-query';
2
2
  import { AvroQueryClient } from '../QueryClient.js';
3
+ import { restoreNs, seedDetails, snapshotNs } from '../cache.js';
3
4
  import { keys } from '../realtime.js';
4
5
  import { LineItemStatus } from '../../types/api.js';
5
6
  AvroQueryClient.prototype.useGetPrepayments = function (body) {
6
7
  const queryClient = this.getQueryClient();
8
+ // Status buckets default to the backend's (unbilled only) and are
9
+ // always sent, so the key describes exactly what the server returns.
10
+ const unbilled = body.unbilled ?? true;
11
+ const billed = body.billed ?? false;
12
+ const paid = body.paid ?? false;
7
13
  const result = useInfiniteQuery({
8
14
  queryKey: keys.infinite('prepayments', this.companyId, {
9
15
  amt: body.amt ?? 50,
10
16
  known_ids: body.known_ids ?? [],
11
17
  unknown_ids: body.unknown_ids ?? [],
12
18
  query: body.query ?? '',
13
- unbilled: body.unbilled ?? true,
14
- billed: body.billed ?? true,
15
- paid: body.paid ?? true,
19
+ unbilled,
20
+ billed,
21
+ paid,
16
22
  taskId: body.taskId ?? '',
17
23
  sort_by: body.sort_by ?? null,
18
24
  direction: body.direction ?? null,
@@ -23,16 +29,21 @@ AvroQueryClient.prototype.useGetPrepayments = function (body) {
23
29
  return undefined;
24
30
  return allPages.flat().length; // next offset
25
31
  },
26
- queryFn: ({ pageParam = 0 }) => this.fetchPrepayments({ ...body, offset: pageParam }),
27
- enabled: body.enabled ?? true,
32
+ queryFn: ({ pageParam = 0 }) => this.fetchPrepayments({
33
+ amt: body.amt,
34
+ known_ids: body.known_ids,
35
+ unknown_ids: body.unknown_ids,
36
+ query: body.query,
37
+ unbilled,
38
+ billed,
39
+ paid,
40
+ task_id: body.taskId,
41
+ sort_by: body.sort_by,
42
+ direction: body.direction,
43
+ offset: pageParam,
44
+ }).then((items) => seedDetails(queryClient, 'prepayments', items)),
45
+ enabled: (body.enabled ?? true) && Boolean(this.companyId),
28
46
  });
29
- if (result.data) {
30
- result.data.pages.forEach((data_page) => {
31
- data_page.forEach((prepayment) => {
32
- queryClient.setQueryData(['prepayment', prepayment.id], prepayment);
33
- });
34
- });
35
- }
36
47
  return result;
37
48
  };
38
49
  AvroQueryClient.prototype.useUpdatePrepayments = function () {
@@ -56,11 +67,10 @@ AvroQueryClient.prototype.useUpdatePrepayments = function () {
56
67
  onMutate: async ({ prepayments, action }) => {
57
68
  await queryClient.cancelQueries({ queryKey: ['prepayments'] });
58
69
  await queryClient.cancelQueries({ queryKey: ['prepayments'] });
59
- const previousPrepayments = queryClient.getQueryData(['prepayments']);
60
- const previousPrepaymentObjs = prepayments.map((prepayment) => queryClient.getQueryData(['prepayment', prepayment.id]));
70
+ const previousPrepayments = snapshotNs(queryClient, 'prepayments');
61
71
  const prepaymentIds = prepayments.map((prepayment) => prepayment.id);
62
72
  prepaymentIds.forEach((prepaymentId, idx) => {
63
- queryClient.setQueryData(['prepayment', prepaymentId], (oldData) => {
73
+ queryClient.setQueryData(['prepayments', prepaymentId], (oldData) => {
64
74
  return oldData
65
75
  ? {
66
76
  ...oldData,
@@ -97,18 +107,14 @@ AvroQueryClient.prototype.useUpdatePrepayments = function () {
97
107
  }
98
108
  return oldData;
99
109
  });
100
- return { previousPrepayments, previousPrepaymentObjs };
110
+ return { previousPrepayments };
101
111
  },
102
112
  onSuccess: () => {
103
113
  queryClient.invalidateQueries({ queryKey: ['prepayments'] });
104
114
  },
105
115
  onError: (err, variables, context) => {
106
- if (context) {
107
- queryClient.setQueryData(['prepayments'], context.previousPrepayments);
108
- context.previousPrepaymentObjs.forEach((prepaymentObj) => {
109
- queryClient.setQueryData(['prepayment', prepaymentObj.id], prepaymentObj);
110
- });
111
- }
116
+ // The namespace snapshot covers lists and details.
117
+ restoreNs(queryClient, context?.previousPrepayments);
112
118
  },
113
119
  });
114
120
  };
@@ -40,5 +40,6 @@ AvroQueryClient.prototype.useGetProposal = function (proposal_id) {
40
40
  });
41
41
  return { ...proposal, tasks: proposal.tasks.map((task) => new Task(task)) };
42
42
  },
43
+ enabled: Boolean(proposal_id),
43
44
  });
44
45
  };
@@ -1,5 +1,6 @@
1
1
  import { useMutation, useQuery } from '@tanstack/react-query';
2
2
  import { AvroQueryClient } from '../QueryClient.js';
3
+ import { restoreNs, snapshotNs } from '../cache.js';
3
4
  import { keys } from '../realtime.js';
4
5
  import { Route } from '../../types/api.js';
5
6
  const dedupeJobTasks = (payload) => payload.jobs
@@ -42,6 +43,7 @@ AvroQueryClient.prototype.useGetRoutes = function (body, total, onProgress) {
42
43
  const routes = pages.flat().map((route) => new Route(route));
43
44
  return routes;
44
45
  },
46
+ enabled: Boolean(this.companyId),
45
47
  });
46
48
  };
47
49
  AvroQueryClient.prototype.useGetRoute = function (routeId) {
@@ -67,7 +69,7 @@ AvroQueryClient.prototype.useCreateRoute = function () {
67
69
  },
68
70
  onMutate: async ({ routeData: { optimize: _optimize, ...routeData } }) => {
69
71
  await queryClient.cancelQueries({ queryKey: ['routes'] });
70
- const previousRoutes = queryClient.getQueryData(['routes']);
72
+ const previousRoutes = snapshotNs(queryClient, 'routes');
71
73
  queryClient.setQueriesData({ queryKey: ['routes'] }, (oldData) => {
72
74
  if (!oldData)
73
75
  return oldData;
@@ -97,7 +99,7 @@ AvroQueryClient.prototype.useCreateRoute = function () {
97
99
  },
98
100
  onError: (_err, _variables, context) => {
99
101
  if (context?.previousRoutes) {
100
- queryClient.setQueryData(['routes'], context.previousRoutes);
102
+ restoreNs(queryClient, context.previousRoutes);
101
103
  }
102
104
  },
103
105
  onSettled: () => {
@@ -152,7 +154,7 @@ AvroQueryClient.prototype.useUpdateRoute = function () {
152
154
  await queryClient.cancelQueries({ queryKey: ['routes', routeId] });
153
155
  await queryClient.cancelQueries({ queryKey: ['routes'] });
154
156
  const previousRoute = queryClient.getQueryData(['routes', routeId]);
155
- const previousRoutes = queryClient.getQueryData(['routes']);
157
+ const previousRoutes = snapshotNs(queryClient, 'routes');
156
158
  queryClient.setQueryData(['routes', routeId], (oldData) => oldData ? new Route({ ...oldData, ...updates }) : undefined);
157
159
  queryClient.setQueriesData({ queryKey: ['routes'] }, (oldData) => {
158
160
  if (!oldData)
@@ -179,7 +181,7 @@ AvroQueryClient.prototype.useUpdateRoute = function () {
179
181
  queryClient.setQueryData(['routes', routeId], context.previousRoute);
180
182
  }
181
183
  if (context?.previousRoutes) {
182
- queryClient.setQueryData(['routes'], context.previousRoutes);
184
+ restoreNs(queryClient, context.previousRoutes);
183
185
  }
184
186
  },
185
187
  });
@@ -193,7 +195,7 @@ AvroQueryClient.prototype.useDeleteRoute = function () {
193
195
  onMutate: async ({ routeId }) => {
194
196
  await queryClient.cancelQueries({ queryKey: ['routes'] });
195
197
  await queryClient.cancelQueries({ queryKey: ['routes', routeId] });
196
- const previousRoutes = queryClient.getQueryData(['routes']);
198
+ const previousRoutes = snapshotNs(queryClient, 'routes');
197
199
  const previousRoute = queryClient.getQueryData(['routes', routeId]);
198
200
  queryClient.setQueryData(['routes', routeId], undefined);
199
201
  queryClient.setQueriesData({ queryKey: ['routes'] }, (oldData) => {
@@ -216,7 +218,7 @@ AvroQueryClient.prototype.useDeleteRoute = function () {
216
218
  onError: (_err, variables, context) => {
217
219
  const { routeId } = variables;
218
220
  if (context?.previousRoutes) {
219
- queryClient.setQueryData(['routes'], context.previousRoutes);
221
+ restoreNs(queryClient, context.previousRoutes);
220
222
  }
221
223
  if (context?.previousRoute) {
222
224
  queryClient.setQueryData(['routes', routeId], context.previousRoute);