merge main into websitefinal
This commit is contained in:
@@ -64,10 +64,15 @@ export function AuthProvider({ children }) {
|
||||
body: JSON.stringify({ username, password }),
|
||||
});
|
||||
|
||||
const data = await res.json();
|
||||
let data;
|
||||
try {
|
||||
data = await res.json();
|
||||
} catch {
|
||||
throw new Error("Unable to log in, please try again later.");
|
||||
}
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(data.message || "Login failed");
|
||||
throw new Error(data.message || "Unable to log in, please try again later.");
|
||||
}
|
||||
|
||||
const jwt = data.token;
|
||||
@@ -85,16 +90,22 @@ export function AuthProvider({ children }) {
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ username, password, email, firstName, lastName, phone }),
|
||||
});
|
||||
const data = await res.json();
|
||||
|
||||
let data;
|
||||
try {
|
||||
data = await res.json();
|
||||
} catch {
|
||||
throw new Error("Unable to register, please try again later.");
|
||||
}
|
||||
|
||||
if (!res.ok) {
|
||||
if (data.errors && typeof data.errors === "object") {
|
||||
const fieldErrors = Object.entries(data.errors)
|
||||
.map(([field, msg]) => `${field}: ${msg}`)
|
||||
.join(", ");
|
||||
throw new Error(fieldErrors || data.message || "Registration failed");
|
||||
throw new Error(fieldErrors || data.message || "Unable to register, please try again later.");
|
||||
}
|
||||
throw new Error(data.message || "Registration failed");
|
||||
throw new Error(data.message || "Unable to register, please try again later.");
|
||||
}
|
||||
|
||||
const jwt = data.token;
|
||||
|
||||
@@ -9,6 +9,8 @@ import {
|
||||
apiRemoveCartItem,
|
||||
apiClearCart,
|
||||
apiApplyCoupon,
|
||||
apiRemoveCoupon,
|
||||
apiApplyPoints,
|
||||
apiCheckout,
|
||||
apiCancelCheckout,
|
||||
} from "@/lib/cartApi";
|
||||
@@ -124,6 +126,28 @@ export function CartProvider({ children }) {
|
||||
[token, selectedStoreId]
|
||||
);
|
||||
|
||||
const applyPoints = useCallback(
|
||||
async (useLoyaltyPoints) => {
|
||||
if (!token || !selectedStoreId) throw new Error("Select a store first");
|
||||
const updated = await apiApplyPoints(token, selectedStoreId, useLoyaltyPoints);
|
||||
setCart(updated);
|
||||
|
||||
return updated;
|
||||
},
|
||||
[token, selectedStoreId]
|
||||
);
|
||||
|
||||
const removeCoupon = useCallback(
|
||||
async () => {
|
||||
if (!token || !selectedStoreId) throw new Error("Select a store first");
|
||||
const updated = await apiRemoveCoupon(token, selectedStoreId);
|
||||
setCart(updated);
|
||||
|
||||
return updated;
|
||||
},
|
||||
[token, selectedStoreId]
|
||||
);
|
||||
|
||||
const checkout = useCallback(
|
||||
async () => {
|
||||
if (!token || !selectedStoreId) throw new Error("Select a store first");
|
||||
@@ -159,6 +183,8 @@ export function CartProvider({ children }) {
|
||||
removeItem,
|
||||
clearCart,
|
||||
applyCoupon,
|
||||
applyPoints,
|
||||
removeCoupon,
|
||||
checkout,
|
||||
cancelCheckout,
|
||||
refreshCart,
|
||||
|
||||
Reference in New Issue
Block a user