-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrewardAPI.js
More file actions
114 lines (106 loc) Β· 2.97 KB
/
Copy pathrewardAPI.js
File metadata and controls
114 lines (106 loc) Β· 2.97 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
// Rewards API Configuration
const API_BASE_URL = import.meta.env.VITE_API_URL || 'http://localhost:5000/api';
const rewardAPI = {
// Get all rewards for a child
getChildRewards: async (childId) => {
try {
const response = await fetch(`${API_BASE_URL}/rewards/child/${childId}`);
const data = await response.json();
return data;
} catch (error) {
console.error('Error fetching child rewards:', error);
throw error;
}
},
// Create a new reward
createReward: async (rewardData) => {
try {
const response = await fetch(`${API_BASE_URL}/rewards`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(rewardData),
});
const data = await response.json();
return data;
} catch (error) {
console.error('Error creating reward:', error);
throw error;
}
},
// Redeem a reward
redeemReward: async (rewardId, childId) => {
try {
const response = await fetch(`${API_BASE_URL}/rewards/${rewardId}/redeem`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ childId }),
});
const data = await response.json();
return data;
} catch (error) {
console.error('Error redeeming reward:', error);
throw error;
}
},
// Get reward by ID
getRewardById: async (rewardId) => {
try {
const response = await fetch(`${API_BASE_URL}/rewards/detail/${rewardId}`);
const data = await response.json();
return data;
} catch (error) {
console.error('Error fetching reward:', error);
throw error;
}
},
// Update reward
updateReward: async (rewardId, updateData) => {
try {
const response = await fetch(`${API_BASE_URL}/rewards/${rewardId}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(updateData),
});
const data = await response.json();
return data;
} catch (error) {
console.error('Error updating reward:', error);
throw error;
}
},
// Delete reward
deleteReward: async (rewardId) => {
try {
const response = await fetch(`${API_BASE_URL}/rewards/${rewardId}`, {
method: 'DELETE',
});
const data = await response.json();
return data;
} catch (error) {
console.error('Error deleting reward:', error);
throw error;
}
},
// Get rewards by category
getRewardsByCategory: async (category, childId = null) => {
try {
let url = `${API_BASE_URL}/rewards/category/${category}`;
if (childId) {
url += `?childId=${childId}`;
}
const response = await fetch(url);
const data = await response.json();
return data;
} catch (error) {
console.error('Error fetching rewards by category:', error);
throw error;
}
},
};
export default rewardAPI;