|
1 | | -import axios from 'axios'; |
2 | | - |
3 | | -const API_URL = 'https://splitwiser-production.up.railway.app'; |
4 | | - |
5 | | -// This creates a new axios instance. |
6 | | -// It's better to have a single instance that can be configured with interceptors. |
7 | | -// I will create a single apiClient in a separate file later if needed. |
8 | | -const apiClient = axios.create({ |
9 | | - baseURL: API_URL, |
10 | | - headers: { |
11 | | - 'Content-Type': 'application/json', |
12 | | - }, |
13 | | -}); |
14 | | - |
15 | | -export const getGroups = (token) => { |
16 | | - return apiClient.get('/groups', { |
17 | | - headers: { |
18 | | - Authorization: `Bearer ${token}`, |
19 | | - }, |
20 | | - }); |
21 | | -}; |
| 1 | +import { apiClient } from "./client"; |
22 | 2 |
|
23 | | -export const getOptimizedSettlements = (token, groupId) => { |
24 | | - return apiClient.post(`/groups/${groupId}/settlements/optimize`, {}, { |
25 | | - headers: { |
26 | | - Authorization: `Bearer ${token}`, |
27 | | - }, |
28 | | - }); |
29 | | -}; |
| 3 | +export const getGroups = () => apiClient.get("/groups"); |
30 | 4 |
|
31 | | -export const createExpense = (token, groupId, expenseData) => { |
32 | | - return apiClient.post(`/groups/${groupId}/expenses`, expenseData, { |
33 | | - headers: { |
34 | | - Authorization: `Bearer ${token}`, |
35 | | - }, |
36 | | - }); |
37 | | -}; |
| 5 | +export const getOptimizedSettlements = (groupId) => |
| 6 | + apiClient.post(`/groups/${groupId}/settlements/optimize`, {}); |
38 | 7 |
|
39 | | -export const getGroupDetails = (token, groupId) => { |
40 | | - return Promise.all([ |
41 | | - getGroupMembers(token, groupId), |
42 | | - getGroupExpenses(token, groupId), |
43 | | - ]); |
44 | | -}; |
| 8 | +export const createExpense = (groupId, expenseData) => |
| 9 | + apiClient.post(`/groups/${groupId}/expenses`, expenseData); |
45 | 10 |
|
46 | | -export const getGroupMembers = (token, groupId) => { |
47 | | - return apiClient.get(`/groups/${groupId}/members`, { |
48 | | - headers: { |
49 | | - Authorization: `Bearer ${token}`, |
50 | | - }, |
51 | | - }); |
| 11 | +export const getGroupDetails = (groupId) => { |
| 12 | + return Promise.all([getGroupMembers(groupId), getGroupExpenses(groupId)]); |
52 | 13 | }; |
53 | 14 |
|
54 | | -export const getGroupExpenses = (token, groupId) => { |
55 | | - return apiClient.get(`/groups/${groupId}/expenses`, { |
56 | | - headers: { |
57 | | - Authorization: `Bearer ${token}`, |
58 | | - }, |
59 | | - }); |
60 | | -}; |
| 15 | +export const getGroupMembers = (groupId) => |
| 16 | + apiClient.get(`/groups/${groupId}/members`); |
61 | 17 |
|
62 | | -export const createGroup = (token, name) => { |
63 | | - return apiClient.post('/groups', { name }, { |
64 | | - headers: { |
65 | | - Authorization: `Bearer ${token}`, |
66 | | - }, |
67 | | - }); |
68 | | -}; |
| 18 | +export const getGroupExpenses = (groupId) => |
| 19 | + apiClient.get(`/groups/${groupId}/expenses`); |
69 | 20 |
|
70 | | -export const joinGroup = (token, joinCode) => { |
71 | | - return apiClient.post('/groups/join', { joinCode }, { |
72 | | - headers: { |
73 | | - Authorization: `Bearer ${token}`, |
74 | | - }, |
75 | | - }); |
76 | | -}; |
| 21 | +export const createGroup = (name) => apiClient.post("/groups", { name }); |
77 | 22 |
|
78 | | -export const getUserBalanceSummary = (token) => { |
79 | | - return apiClient.get('/users/me/balance-summary', { |
80 | | - headers: { |
81 | | - Authorization: `Bearer ${token}`, |
82 | | - }, |
83 | | - }); |
84 | | -}; |
| 23 | +export const joinGroup = (joinCode) => |
| 24 | + apiClient.post("/groups/join", { joinCode }); |
85 | 25 |
|
86 | | -export const getFriendsBalance = (token) => { |
87 | | - return apiClient.get('/users/me/friends-balance', { |
88 | | - headers: { |
89 | | - Authorization: `Bearer ${token}`, |
90 | | - }, |
91 | | - }); |
92 | | -}; |
| 26 | +export const getUserBalanceSummary = () => |
| 27 | + apiClient.get("/users/me/balance-summary"); |
| 28 | + |
| 29 | +export const getFriendsBalance = () => |
| 30 | + apiClient.get("/users/me/friends-balance"); |
93 | 31 |
|
94 | 32 | // New APIs for Group Settings |
95 | | -export const getGroupById = (token, groupId) => { |
96 | | - return apiClient.get(`/groups/${groupId}`, { |
97 | | - headers: { |
98 | | - Authorization: `Bearer ${token}`, |
99 | | - }, |
100 | | - }); |
101 | | -}; |
| 33 | +export const getGroupById = (groupId) => apiClient.get(`/groups/${groupId}`); |
102 | 34 |
|
103 | | -export const updateGroup = (token, groupId, updates) => { |
104 | | - return apiClient.patch(`/groups/${groupId}`, updates, { |
105 | | - headers: { |
106 | | - Authorization: `Bearer ${token}`, |
107 | | - }, |
108 | | - }); |
109 | | -}; |
| 35 | +export const updateGroup = (groupId, updates) => |
| 36 | + apiClient.patch(`/groups/${groupId}`, updates); |
110 | 37 |
|
111 | | -export const deleteGroup = (token, groupId) => { |
112 | | - return apiClient.delete(`/groups/${groupId}`, { |
113 | | - headers: { |
114 | | - Authorization: `Bearer ${token}`, |
115 | | - }, |
116 | | - }); |
117 | | -}; |
| 38 | +export const deleteGroup = (groupId) => apiClient.delete(`/groups/${groupId}`); |
118 | 39 |
|
119 | | -export const leaveGroup = (token, groupId) => { |
120 | | - return apiClient.post(`/groups/${groupId}/leave`, {}, { |
121 | | - headers: { |
122 | | - Authorization: `Bearer ${token}`, |
123 | | - }, |
124 | | - }); |
125 | | -}; |
| 40 | +export const leaveGroup = (groupId) => |
| 41 | + apiClient.post(`/groups/${groupId}/leave`, {}); |
126 | 42 |
|
127 | | -export const updateMemberRole = (token, groupId, memberId, role) => { |
128 | | - return apiClient.patch(`/groups/${groupId}/members/${memberId}`, { role }, { |
129 | | - headers: { |
130 | | - Authorization: `Bearer ${token}`, |
131 | | - }, |
132 | | - }); |
133 | | -}; |
| 43 | +export const updateMemberRole = (groupId, memberId, role) => |
| 44 | + apiClient.patch(`/groups/${groupId}/members/${memberId}`, { role }); |
134 | 45 |
|
135 | | -export const removeMember = (token, groupId, memberId) => { |
136 | | - return apiClient.delete(`/groups/${groupId}/members/${memberId}`, { |
137 | | - headers: { |
138 | | - Authorization: `Bearer ${token}`, |
139 | | - }, |
140 | | - }); |
141 | | -}; |
| 46 | +export const removeMember = (groupId, memberId) => |
| 47 | + apiClient.delete(`/groups/${groupId}/members/${memberId}`); |
0 commit comments