This commit is contained in:
zadit
2024-12-28 07:39:48 +07:00
parent 47bb8b40c8
commit 9a89e3e996
5 changed files with 205 additions and 68 deletions

View File

@@ -146,30 +146,44 @@ export const loginUser = async (username, password) => {
return { success: false, token: null };
}
};
export const updateUser = async (formData) => {
const token = getLocalStorage("auth");
if (token) {
try {
const response = await fetch(API_BASE_URL + "/user/update-user", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(formData),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const token = getLocalStorage("auth"); // Retrieve token from local storage
if (!token) {
// Handle missing token scenario
throw new Error("User not authenticated. No token found.");
}
try {
const response = await fetch(API_BASE_URL + "/user/update-user", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify(formData),
});
// Check if response status is not ok (e.g., 400 or 500 errors)
if (!response.ok) {
const data = await response.json();
return data;
} catch (error) {
console.error("Error updating user:", error);
// If the response body has an error, throw it to propagate to the frontend
throw new Error(data.error || `Error: ${response.statusText}`);
}
// If the response is OK, return the data
const data = await response.json();
return data;
} catch (error) {
// Log and rethrow error to be handled in the calling function
console.error("Error updating user:", error);
throw error; // Re-throw the error so the calling function can handle it
}
};
//for super
export const getAnalytics = async (formData) => {
const token = getLocalStorage("auth");