140 lines
3.5 KiB
TypeScript
140 lines
3.5 KiB
TypeScript
|
|
import {RecurrenceType} from "@/types/ScheduleType";
|
||
|
|
import {apiRequest} from "@/utils/api/apiRequest";
|
||
|
|
import {UserType} from "@/types/UserType";
|
||
|
|
|
||
|
|
export const listUsers = async () => {
|
||
|
|
const token = localStorage.getItem('token');
|
||
|
|
|
||
|
|
if (!token) {
|
||
|
|
throw new Error('No token found in localStorage.');
|
||
|
|
}
|
||
|
|
|
||
|
|
return apiRequest.get(`/api/users`, {
|
||
|
|
headers: {
|
||
|
|
Authorization: `Bearer ${token}`,
|
||
|
|
}})
|
||
|
|
.then((data) => {
|
||
|
|
if (data?.payload?.users) {
|
||
|
|
return data.payload.users;
|
||
|
|
}
|
||
|
|
throw new Error('Failed to fetch users');
|
||
|
|
})
|
||
|
|
.catch((err) => {
|
||
|
|
throw err;
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
export const showUser = async (userId: number) => {
|
||
|
|
const token = localStorage.getItem('token');
|
||
|
|
|
||
|
|
if (!token) {
|
||
|
|
throw new Error('No token found in localStorage.');
|
||
|
|
}
|
||
|
|
|
||
|
|
return apiRequest.get(`/api/users/${userId}`, {
|
||
|
|
headers: {
|
||
|
|
Authorization: `Bearer ${token}`,
|
||
|
|
}
|
||
|
|
})
|
||
|
|
.then((data) => {
|
||
|
|
if (data?.payload?.user) {
|
||
|
|
return data.payload.user;
|
||
|
|
}
|
||
|
|
throw new Error('Failed to fetch users');
|
||
|
|
})
|
||
|
|
.catch((err) => {
|
||
|
|
throw err;
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
export const createUser = async (name: string) => {
|
||
|
|
const token = localStorage.getItem('token');
|
||
|
|
|
||
|
|
if (!token) {
|
||
|
|
throw new Error('No token found in localStorage.');
|
||
|
|
}
|
||
|
|
|
||
|
|
return await apiRequest.post('/api/users', {name}, {
|
||
|
|
headers: {
|
||
|
|
Authorization: `Bearer ${token}`,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
);
|
||
|
|
};
|
||
|
|
|
||
|
|
export const updateUser = async (user: UserType, name: string) => {
|
||
|
|
const token = localStorage.getItem('token');
|
||
|
|
|
||
|
|
if (!token) {
|
||
|
|
throw new Error('No token found in localStorage.');
|
||
|
|
}
|
||
|
|
|
||
|
|
return await apiRequest.put(`/api/users/${user.id}`, { name }, {
|
||
|
|
headers: {
|
||
|
|
Authorization: `Bearer ${token}`,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
);
|
||
|
|
};
|
||
|
|
|
||
|
|
export const deleteUser = async (user: UserType) => {
|
||
|
|
const token = localStorage.getItem('token');
|
||
|
|
|
||
|
|
if (!token) {
|
||
|
|
throw new Error('No token found in localStorage.');
|
||
|
|
}
|
||
|
|
|
||
|
|
return await apiRequest.delete(`/api/users/${user.id}`, {
|
||
|
|
headers: {
|
||
|
|
Authorization: `Bearer ${token}`,
|
||
|
|
},
|
||
|
|
}
|
||
|
|
);
|
||
|
|
};
|
||
|
|
|
||
|
|
|
||
|
|
export const getUserDishForUserAndDish = async (userId: number, dishId: number) => {
|
||
|
|
const endpoint = `/api/users/${userId}/dishes/${dishId}`;
|
||
|
|
const token = localStorage.getItem('token');
|
||
|
|
|
||
|
|
if (!token) {
|
||
|
|
throw new Error('No token found in localStorage.');
|
||
|
|
}
|
||
|
|
|
||
|
|
return apiRequest.get(endpoint, {
|
||
|
|
headers: {
|
||
|
|
Authorization: `Bearer ${token}`,
|
||
|
|
}})
|
||
|
|
.then((data) => {
|
||
|
|
if (data?.payload?.user_dish) {
|
||
|
|
return data.payload.user_dish;
|
||
|
|
}
|
||
|
|
throw new Error('Failed to fetch user dish');
|
||
|
|
})
|
||
|
|
.catch((err) => {
|
||
|
|
throw err;
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
export const syncUserDishRecurrences = async (
|
||
|
|
dish_id: number,
|
||
|
|
user_id: number,
|
||
|
|
recurrenceData: RecurrenceType[]
|
||
|
|
) => {
|
||
|
|
const url = `/api/users/${user_id}/dishes/${dish_id}/recurrences`;
|
||
|
|
const token = localStorage.getItem('token');
|
||
|
|
|
||
|
|
if (!token) {
|
||
|
|
throw new Error('No token found in localStorage.');
|
||
|
|
}
|
||
|
|
|
||
|
|
return apiRequest.post(url, { recurrences: recurrenceData }, {
|
||
|
|
headers: {
|
||
|
|
Authorization: `Bearer ${token}`,
|
||
|
|
},
|
||
|
|
}).catch((err) => {
|
||
|
|
throw err;
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|