forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseAffiliatePayouts.ts
More file actions
77 lines (66 loc) · 1.91 KB
/
Copy pathuseAffiliatePayouts.ts
File metadata and controls
77 lines (66 loc) · 1.91 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
'use client';
import { useState, useEffect, useCallback } from 'react';
import { useAuthFetch } from '@/hooks/useAuthToken';
export interface AffiliatePayout {
affiliate_id: number;
name: string;
email: string;
ref_code: string;
pending_amount: number;
total_earned: number;
total_paid: number;
payment_method: string;
stripe_account_id: string | null;
total_orders: number;
ad_orders: number;
organic_orders: number;
sales_commission: number;
}
export function useAffiliatePayouts() {
const { fetchWithAuth, token } = useAuthFetch();
const [affiliates, setAffiliates] = useState<AffiliatePayout[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const loadPayouts = useCallback(async () => {
if (!token) return;
try {
setLoading(true);
setError(null);
const response = await fetchWithAuth('/api/omi/affiliate-payouts?action=pending');
if (!response.ok) {
throw new Error('Failed to fetch affiliate payouts');
}
const data = await response.json();
setAffiliates(data.affiliates || []);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to load payouts');
} finally {
setLoading(false);
}
}, [token, fetchWithAuth]);
useEffect(() => {
loadPayouts();
}, [loadPayouts]);
const transfer = async (affiliateId: number) => {
const response = await fetchWithAuth('/api/omi/affiliate-payouts', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
action: 'transfer',
affiliate_id: affiliateId,
}),
});
if (!response.ok) {
const err = await response.json();
throw new Error(err.error || 'Transfer failed');
}
return response.json();
};
return {
affiliates,
loading,
error,
refresh: loadPayouts,
transfer,
};
}