-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
75 lines (69 loc) · 2.13 KB
/
api.js
File metadata and controls
75 lines (69 loc) · 2.13 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
// This file contains utility functions for making API calls to the backend.
// It exports functions for fetching items, user data, purchase history, and submitting reviews.
const API_BASE_URL = '/api'; // Using relative path for proxy
export const fetchItems = async () => {
try {
const response = await fetch(`${API_BASE_URL}/items`);
if (!response.ok) {
throw new Error('Failed to fetch items');
}
return await response.json();
} catch (error) {
console.error(error);
return [];
}
};
export const fetchUserData = async (userId) => {
try {
const response = await fetch(`${API_BASE_URL}/users/${userId}`);
if (!response.ok) {
throw new Error('Failed to fetch user data');
}
return await response.json();
} catch (error) {
console.error(error);
return null;
}
};
export const fetchPurchaseHistory = async (userId) => {
try {
const response = await fetch(`${API_BASE_URL}/users/${userId}/purchases`);
if (!response.ok) {
throw new Error('Failed to fetch purchase history');
}
return await response.json();
} catch (error) {
console.error(error);
return [];
}
};
export const fetchSalesHistory = async (userId) => {
try {
const response = await fetch(`${API_BASE_URL}/users/${userId}/sales`);
if (!response.ok) {
throw new Error('Failed to fetch sales history');
}
return await response.json();
} catch (error) {
console.error(error);
return [];
}
};
export const submitReview = async (itemId, reviewData) => {
try {
const response = await fetch(`${API_BASE_URL}/items/${itemId}/reviews`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(reviewData),
});
if (!response.ok) {
throw new Error('Failed to submit review');
}
return await response.json();
} catch (error) {
console.error(error);
return null;
}
};