forked from ZyntariHQ/Invoisio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvoices.ts
More file actions
225 lines (207 loc) · 5.93 KB
/
Copy pathinvoices.ts
File metadata and controls
225 lines (207 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import axios from "axios";
import { API_URL } from "@env";
import { useAuthStore } from "../hooks/use-auth-store";
import { useInfiniteQuery, useQueryClient } from "@tanstack/react-query";
import { useEffect } from "react";
import { setCachedInvoices, getCachedInvoices } from "./cache";
import { offlineQueue } from "./offline-queue";
import { useConnectivity } from "../hooks/use-connectivity";
export type InvoiceStatus =
| "pending"
| "paid"
| "partially_paid"
| "overdue"
| "cancelled"
| "draft";
export interface PaymentRecord {
id: string;
amount: number;
txHash?: string;
createdAt?: string;
}
export interface Invoice {
id: string;
invoiceNumber?: string;
clientName?: string;
clientEmail?: string;
description?: string;
amount: number;
amountPaid?: number;
amountDue?: number;
asset?: string;
asset_code?: string;
asset_issuer?: string;
memo?: string;
memo_type?: string;
status: InvoiceStatus;
destination?: string;
destination_address?: string;
tx_hash?: string;
payments?: PaymentRecord[];
createdAt: string;
updatedAt?: string;
dueDate?: string;
}
export interface InvoicesPage {
items: Invoice[];
page: number;
pageSize: number;
total?: number;
hasMore: boolean;
}
async function fetchInvoicesPage(
page: number,
pageSize: number,
token: string | null,
search?: string,
status?: string,
): Promise<InvoicesPage> {
const headers =
token != null
? {
Authorization: `Bearer ${token}`,
}
: undefined;
const params = new URLSearchParams({
page: String(page),
limit: String(pageSize),
});
if (search && search.trim().length > 0) {
params.set("search", search.trim());
}
if (status && status !== "all") {
params.set("status", status);
}
const url = `${API_URL}/invoices?${params.toString()}`;
try {
const response = await axios.get(url, headers ? { headers } : undefined);
const data: unknown = response.data;
if (Array.isArray(data)) {
const items: Invoice[] = data as Invoice[];
return {
items,
page,
pageSize,
total: items.length,
hasMore: items.length === pageSize,
};
}
const obj = (data ?? {}) as {
items?: unknown;
total?: unknown;
hasMore?: unknown;
};
const items: Invoice[] = Array.isArray(obj.items)
? (obj.items as Invoice[])
: [];
const total: number | undefined =
typeof obj.total === "number" ? obj.total : undefined;
const hasMore =
typeof obj.hasMore === "boolean"
? obj.hasMore
: total != null
? page * pageSize < total
: items.length === pageSize;
return total != null
? { items, page, pageSize, hasMore, total }
: { items, page, pageSize, hasMore };
} catch (error) {
// Handle offline requests: queue and return cached data if available
if (axios.isAxiosError(error) && !error.response) {
// Network error - try to get from cache
const cached = await getCachedInvoices();
// Check if we have cached data for this page
if (cached?.pages && cached.pages.length > 0 && cached.pages[page - 1]) {
// Type assertion to ensure it matches InvoicesPage
return cached.pages[page - 1] as InvoicesPage;
}
// Queue the request for retry when online
await offlineQueue.enqueue(url, "GET", undefined, headers);
// Return empty page with no more data
return {
items: [],
page,
pageSize,
hasMore: false,
total: 0,
};
}
throw error;
}
}
export function useInvoicesList(
pageSize = 20,
search?: string,
status?: string,
) {
const { accessToken } = useAuthStore();
const queryClient = useQueryClient();
const { isOffline } = useConnectivity();
// Derive effective filter values
const effectiveSearch =
search && search.trim().length > 0 ? search.trim() : undefined;
const effectiveStatus = status && status !== "all" ? status : undefined;
// seed cached data (if any) into the query cache so UI can show offline data
// without blocking the hook caller. Read AsyncStorage in effect and set
// the query data so components render immediately from cache when offline.
useEffect(() => {
let cancelled = false;
void (async () => {
try {
const cached = await getCachedInvoices();
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- cancelled may be mutated by cleanup
if (!cancelled && cached?.pages) {
queryClient.setQueryData(
[
"invoices",
pageSize,
accessToken,
effectiveSearch,
effectiveStatus,
],
{
pages: cached.pages,
pageParams: cached.pages.map((_, i) => i + 1),
},
);
}
} catch (err) {
console.error("seed cache error:", err);
}
})();
return () => {
cancelled = true;
};
}, [accessToken, pageSize, queryClient, effectiveSearch, effectiveStatus]);
const query = useInfiniteQuery({
queryKey: [
"invoices",
pageSize,
accessToken,
effectiveSearch,
effectiveStatus,
isOffline, // Refetch when coming online
],
queryFn: async ({ pageParam }: { pageParam: number }) =>
fetchInvoicesPage(
pageParam,
pageSize,
accessToken,
effectiveSearch,
effectiveStatus,
),
initialPageParam: 1,
getNextPageParam: (lastPage) =>
lastPage.hasMore ? lastPage.page + 1 : undefined,
staleTime: isOffline ? 1000 * 60 * 5 : 1000 * 30, // Keep data fresh longer when offline
});
// Persist fetched pages to AsyncStorage for offline use
useEffect(() => {
if (query.data) {
setCachedInvoices(query.data.pages).catch((err: unknown) => {
console.error("cache persist error:", err);
});
}
}, [query.data]);
return query;
}