forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseDistributors.ts
More file actions
114 lines (98 loc) · 2.98 KB
/
Copy pathuseDistributors.ts
File metadata and controls
114 lines (98 loc) · 2.98 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
import { useState, useEffect } from 'react'
import { Distributor, ShopifyLocation } from '@/types/distributor'
import { useAuthFetch } from '@/hooks/useAuthToken'
export interface CreateDistributorData {
email: string
name: string
isActive: boolean
isAdmin: boolean
countries: string[]
locationId: string
locationName: string
}
export interface UpdateDistributorData extends Partial<CreateDistributorData> {
id: string
}
export const useDistributors = () => {
const [distributors, setDistributors] = useState<Distributor[]>([])
const [locations, setLocations] = useState<ShopifyLocation[]>([])
const [loading, setLoading] = useState(true)
const [error, setError] = useState<string | null>(null)
const { fetchWithAuth, token } = useAuthFetch()
const fetchDistributors = async () => {
if (!token) return
try {
setLoading(true)
setError(null)
const [distRes, locRes] = await Promise.all([
fetchWithAuth('/api/distributors'),
fetchWithAuth('/api/distributors/locations'),
])
if (!distRes.ok) throw new Error('Failed to fetch distributors')
const distData = await distRes.json()
setDistributors(distData.distributors || [])
// Locations may fail if Shopify isn't configured - don't block on it
if (locRes.ok) {
const locData = await locRes.json()
setLocations(locData.locations || [])
}
} catch (err) {
setError(err instanceof Error ? err.message : 'An error occurred')
} finally {
setLoading(false)
}
}
const createDistributor = async (data: CreateDistributorData) => {
try {
setError(null)
const response = await fetchWithAuth('/api/distributors', {
method: 'POST',
body: JSON.stringify(data),
})
const result = await response.json()
if (!response.ok) {
throw new Error(result.error || 'Failed to create distributor')
}
await fetchDistributors()
return result.distributor
} catch (err) {
const msg = err instanceof Error ? err.message : 'An error occurred'
setError(msg)
throw new Error(msg)
}
}
const updateDistributor = async (data: UpdateDistributorData) => {
try {
setError(null)
const response = await fetchWithAuth('/api/distributors', {
method: 'PUT',
body: JSON.stringify(data),
})
const result = await response.json()
if (!response.ok) {
throw new Error(result.error || 'Failed to update distributor')
}
await fetchDistributors()
return result.distributor
} catch (err) {
const msg = err instanceof Error ? err.message : 'An error occurred'
setError(msg)
throw new Error(msg)
}
}
useEffect(() => {
if (token) {
fetchDistributors()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [token])
return {
distributors,
locations,
loading,
error,
fetchDistributors,
createDistributor,
updateDistributor,
}
}