forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseAffiliates.ts
More file actions
131 lines (115 loc) · 3.65 KB
/
Copy pathuseAffiliates.ts
File metadata and controls
131 lines (115 loc) · 3.65 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
'use client';
import { useCallback, useEffect, useState } from 'react';
import { useAuthFetch } from '@/hooks/useAuthToken';
export interface Affiliate {
id: number;
name: string;
first_name?: string;
last_name?: string;
email: string;
ref_code: string;
coupon?: string;
status?: string;
phone?: string;
country?: string;
city?: string;
website?: string;
payment_method?: string | null;
created_at?: string;
updated_at?: string;
group_id?: number;
}
export interface AffiliateDetail extends Affiliate {
facebook?: string;
twitter?: string;
instagram?: string;
address_1?: string;
state?: string;
zip_code?: string;
payment_details?: Record<string, string>;
comments?: string;
personal_message?: string;
registration_ip?: string;
ref_codes?: Array<{ ref_code: string }>;
coupons?: Array<{ coupon: string }>;
}
export interface AffiliateStats {
total_orders: number;
pending_amount: number;
total_earned: number;
total_paid: number;
}
export interface AffiliateFilters {
status?: string;
search?: string;
}
const PAGE_SIZE = 50;
export function useAffiliates(filters: AffiliateFilters) {
const { fetchWithAuth, token } = useAuthFetch();
const [affiliates, setAffiliates] = useState<Affiliate[]>([]);
const [loading, setLoading] = useState(true);
const [loadingMore, setLoadingMore] = useState(false);
const [error, setError] = useState<string | null>(null);
const [hasMore, setHasMore] = useState(false);
const [offset, setOffset] = useState(0);
const fetchPage = useCallback(
async (nextOffset: number, replace: boolean) => {
if (!token) return;
const params = new URLSearchParams({
action: 'list',
limit: String(PAGE_SIZE),
offset: String(nextOffset),
});
if (filters.status) params.set('status', filters.status);
if (filters.search) params.set('search', filters.search);
const setLoader = replace ? setLoading : setLoadingMore;
setLoader(true);
setError(null);
try {
const res = await fetchWithAuth(`/api/omi/affiliates?${params.toString()}`);
if (!res.ok) {
const err = await res.json().catch(() => ({}));
throw new Error(err.error || 'Failed to load affiliates');
}
const data = await res.json();
const list: Affiliate[] = data.affiliates || [];
setAffiliates((prev) => (replace ? list : [...prev, ...list]));
setHasMore(!!data.has_more);
setOffset(nextOffset + list.length);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to load affiliates');
} finally {
setLoader(false);
}
},
[token, fetchWithAuth, filters.status, filters.search]
);
useEffect(() => {
setOffset(0);
fetchPage(0, true);
}, [fetchPage]);
const loadMore = useCallback(() => {
if (!hasMore || loadingMore || loading) return;
fetchPage(offset, false);
}, [hasMore, loadingMore, loading, offset, fetchPage]);
const refresh = useCallback(() => {
setOffset(0);
fetchPage(0, true);
}, [fetchPage]);
return { affiliates, loading, loadingMore, error, hasMore, loadMore, refresh };
}
export function useAffiliateDetail() {
const { fetchWithAuth } = useAuthFetch();
const load = useCallback(
async (id: number): Promise<{ affiliate: AffiliateDetail; stats: AffiliateStats }> => {
const res = await fetchWithAuth(`/api/omi/affiliates?action=detail&id=${id}`);
if (!res.ok) {
const err = await res.json().catch(() => ({}));
throw new Error(err.error || 'Failed to load affiliate');
}
return res.json();
},
[fetchWithAuth]
);
return { load };
}