chore: build
This commit is contained in:
@@ -2292,6 +2292,29 @@ export const getMyPointsRewards = async (): Promise<{
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const swapMyRewardProduct = async (
|
||||||
|
productId: number,
|
||||||
|
): Promise<{ success: boolean; product_names?: string[]; error?: string }> => {
|
||||||
|
const token = getAuthToken();
|
||||||
|
if (!token) return { success: false, error: "Non authentifié" };
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${API_URL}/points/swap`, {
|
||||||
|
method: "PUT",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ product_id: productId }),
|
||||||
|
});
|
||||||
|
const data = await safeJson(response);
|
||||||
|
if (!response.ok)
|
||||||
|
return { success: false, error: data.error || "Erreur" };
|
||||||
|
return { success: true, product_names: data.product_names };
|
||||||
|
} catch {
|
||||||
|
return { success: false, error: "Erreur de connexion" };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
export const submitLivreurRating = async (
|
export const submitLivreurRating = async (
|
||||||
orderId: number,
|
orderId: number,
|
||||||
rating: number,
|
rating: number,
|
||||||
|
|||||||
@@ -2,11 +2,11 @@ import { useCart } from "../../context/useCart";
|
|||||||
import Navbar from "../../components/Navbar";
|
import Navbar from "../../components/Navbar";
|
||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import { isUserAuthenticated, getProductById, getMediaUrl } from "../../api/api";
|
import { isUserAuthenticated, getProductById, getMediaUrl, getMyPointsRewards, swapMyRewardProduct } from "../../api/api";
|
||||||
import type { Product } from "../../api/api";
|
import type { Product, RewardItemConfig } from "../../api/api";
|
||||||
import { Trash2, ShoppingCart, AlertTriangle, Leaf, X } from "lucide-react";
|
import { Trash2, ShoppingCart, AlertTriangle, Leaf, X } from "lucide-react";
|
||||||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
||||||
import { faGift } from "@fortawesome/free-solid-svg-icons";
|
import { faGift, faRepeat } from "@fortawesome/free-solid-svg-icons";
|
||||||
import "./Cart.css";
|
import "./Cart.css";
|
||||||
|
|
||||||
interface CartItemWithMedia {
|
interface CartItemWithMedia {
|
||||||
@@ -30,6 +30,9 @@ function Cart() {
|
|||||||
const [currentVideoUrl, setCurrentVideoUrl] = useState<string>("");
|
const [currentVideoUrl, setCurrentVideoUrl] = useState<string>("");
|
||||||
const [enrichedItems, setEnrichedItems] = useState<CartItemWithMedia[]>([]);
|
const [enrichedItems, setEnrichedItems] = useState<CartItemWithMedia[]>([]);
|
||||||
const [loadingMedia, setLoadingMedia] = useState(false);
|
const [loadingMedia, setLoadingMedia] = useState(false);
|
||||||
|
const [swapTarget, setSwapTarget] = useState<CartItemWithMedia | null>(null);
|
||||||
|
const [swapChoices, setSwapChoices] = useState<RewardItemConfig[]>([]);
|
||||||
|
const [swapLoading, setSwapLoading] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isUserAuthenticated()) navigate("/login/client", { replace: true });
|
if (!isUserAuthenticated()) navigate("/login/client", { replace: true });
|
||||||
@@ -105,6 +108,28 @@ function Cart() {
|
|||||||
await removeFromCart(itemId);
|
await removeFromCart(itemId);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleOpenSwap = async (item: CartItemWithMedia) => {
|
||||||
|
setSwapTarget(item);
|
||||||
|
setSwapChoices([]);
|
||||||
|
const res = await getMyPointsRewards();
|
||||||
|
const pool = res.pools.find((p) =>
|
||||||
|
p.eligible_reward_items.some((it) => it.product_id === item.product_id),
|
||||||
|
);
|
||||||
|
setSwapChoices(
|
||||||
|
(pool?.eligible_reward_items || []).filter((it) => it.product_id !== item.product_id),
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSwapChoice = async (productId: number) => {
|
||||||
|
setSwapLoading(true);
|
||||||
|
const res = await swapMyRewardProduct(productId);
|
||||||
|
setSwapLoading(false);
|
||||||
|
if (res.success) {
|
||||||
|
setSwapTarget(null);
|
||||||
|
await refreshCart();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const handleCheckout = () => {
|
const handleCheckout = () => {
|
||||||
if (!isUserAuthenticated()) { navigate("/login/client", { replace: true }); return; }
|
if (!isUserAuthenticated()) { navigate("/login/client", { replace: true }); return; }
|
||||||
navigate("/user/checkout");
|
navigate("/user/checkout");
|
||||||
@@ -200,6 +225,19 @@ function Cart() {
|
|||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{/* Changer le produit de la récompense */}
|
||||||
|
{item.is_reward && (
|
||||||
|
<button
|
||||||
|
className="cart-row-remove"
|
||||||
|
onClick={() => handleOpenSwap(item)}
|
||||||
|
disabled={loading}
|
||||||
|
aria-label={`Changer le produit de la récompense`}
|
||||||
|
title="Changer de produit"
|
||||||
|
>
|
||||||
|
<FontAwesomeIcon icon={faRepeat} />
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
|
||||||
{/* Bouton supprimer */}
|
{/* Bouton supprimer */}
|
||||||
<button
|
<button
|
||||||
className="cart-row-remove"
|
className="cart-row-remove"
|
||||||
@@ -214,6 +252,34 @@ function Cart() {
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{/* Modale de changement de produit récompense */}
|
||||||
|
{swapTarget && (
|
||||||
|
<div style={{ position: "fixed", inset: 0, background: "rgba(0,0,0,0.6)", display: "flex", alignItems: "center", justifyContent: "center", zIndex: 1000 }}>
|
||||||
|
<div style={{ background: "#1a1a1a", borderRadius: 12, padding: 20, maxWidth: 360, width: "90%" }}>
|
||||||
|
<div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", marginBottom: 12 }}>
|
||||||
|
<p style={{ color: "#fff", fontWeight: 700 }}>Choisir un autre produit</p>
|
||||||
|
<button onClick={() => setSwapTarget(null)} style={{ background: "none", border: "none", color: "#fff", cursor: "pointer" }}>
|
||||||
|
<X size={18} />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
{swapChoices.length === 0 ? (
|
||||||
|
<p style={{ color: "#999", fontSize: "0.85rem" }}>Aucun autre produit disponible pour cette récompense.</p>
|
||||||
|
) : (
|
||||||
|
swapChoices.map((choice) => (
|
||||||
|
<button
|
||||||
|
key={choice.product_id}
|
||||||
|
onClick={() => handleSwapChoice(choice.product_id)}
|
||||||
|
disabled={swapLoading}
|
||||||
|
style={{ display: "block", width: "100%", textAlign: "left", padding: "8px 10px", marginBottom: 6, background: "#2a2a2a", border: "none", borderRadius: 8, color: "#fff", cursor: "pointer" }}
|
||||||
|
>
|
||||||
|
{choice.product_name} {choice.price === 0 ? "(offert)" : `(${choice.price.toFixed(2)} €)`}
|
||||||
|
</button>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
{/* Footer fixe */}
|
{/* Footer fixe */}
|
||||||
<div className="cart-footer">
|
<div className="cart-footer">
|
||||||
<div className="cart-footer-total">
|
<div className="cart-footer-total">
|
||||||
|
|||||||
@@ -1071,6 +1071,19 @@ export const claimMyReward = async (
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const swapMyRewardProduct = async (
|
||||||
|
productId: number,
|
||||||
|
): Promise<{ success: boolean; product_names?: string[]; error?: string }> => {
|
||||||
|
try {
|
||||||
|
const { data } = await apiClient.put(`${V1}/points/swap`, {
|
||||||
|
product_id: productId,
|
||||||
|
});
|
||||||
|
return { success: true, product_names: data.product_names };
|
||||||
|
} catch (e: any) {
|
||||||
|
return { success: false, error: e?.response?.data?.error || "Erreur" };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
export const submitLivreurRating = async (
|
export const submitLivreurRating = async (
|
||||||
orderId: number,
|
orderId: number,
|
||||||
rating: number,
|
rating: number,
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ export interface CartItem {
|
|||||||
quantity: number;
|
quantity: number;
|
||||||
category: string;
|
category: string;
|
||||||
image?: string;
|
image?: string;
|
||||||
|
is_reward?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ToastData {
|
interface ToastData {
|
||||||
@@ -89,6 +90,7 @@ export function CartProvider({ children }: { children: ReactNode }) {
|
|||||||
quantity: item.quantity,
|
quantity: item.quantity,
|
||||||
category: (item.category || "autre").toLowerCase().trim(),
|
category: (item.category || "autre").toLowerCase().trim(),
|
||||||
image: item.image,
|
image: item.image,
|
||||||
|
is_reward: item.is_reward,
|
||||||
}));
|
}));
|
||||||
setCartItems(items);
|
setCartItems(items);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -12,7 +12,12 @@ import { useNavigation, useFocusEffect } from "@react-navigation/native";
|
|||||||
import type { NativeStackNavigationProp } from "@react-navigation/native-stack";
|
import type { NativeStackNavigationProp } from "@react-navigation/native-stack";
|
||||||
import { Ionicons } from "@expo/vector-icons";
|
import { Ionicons } from "@expo/vector-icons";
|
||||||
import { useCart } from "../../context/CartContext";
|
import { useCart } from "../../context/CartContext";
|
||||||
import { getProductById } from "../../api/api";
|
import {
|
||||||
|
getProductById,
|
||||||
|
getMyPointsRewards,
|
||||||
|
swapMyRewardProduct,
|
||||||
|
} from "../../api/api";
|
||||||
|
import type { RewardItemConfig } from "../../api/api";
|
||||||
import type { ClientStackParamList } from "../../navigation/types";
|
import type { ClientStackParamList } from "../../navigation/types";
|
||||||
import LoadingSpinner from "../../components/ui/LoadingSpinner";
|
import LoadingSpinner from "../../components/ui/LoadingSpinner";
|
||||||
import Button from "../../components/ui/Button";
|
import Button from "../../components/ui/Button";
|
||||||
@@ -37,6 +42,7 @@ interface EnrichedItem {
|
|||||||
price: number;
|
price: number;
|
||||||
quantity: number;
|
quantity: number;
|
||||||
category: string;
|
category: string;
|
||||||
|
is_reward?: boolean;
|
||||||
imageUri?: string;
|
imageUri?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,6 +64,9 @@ export default function CartScreen() {
|
|||||||
const [loadingMedia, setLoadingMedia] = useState(false);
|
const [loadingMedia, setLoadingMedia] = useState(false);
|
||||||
const [removeId, setRemoveId] = useState<number | null>(null);
|
const [removeId, setRemoveId] = useState<number | null>(null);
|
||||||
const [showClearModal, setShowClearModal] = useState(false);
|
const [showClearModal, setShowClearModal] = useState(false);
|
||||||
|
const [swapTarget, setSwapTarget] = useState<EnrichedItem | null>(null);
|
||||||
|
const [swapChoices, setSwapChoices] = useState<RewardItemConfig[]>([]);
|
||||||
|
const [swapLoading, setSwapLoading] = useState(false);
|
||||||
|
|
||||||
useFocusEffect(
|
useFocusEffect(
|
||||||
useCallback(() => {
|
useCallback(() => {
|
||||||
@@ -106,6 +115,32 @@ export default function CartScreen() {
|
|||||||
|
|
||||||
const handleClear = () => setShowClearModal(true);
|
const handleClear = () => setShowClearModal(true);
|
||||||
|
|
||||||
|
const handleOpenSwap = async (item: EnrichedItem) => {
|
||||||
|
setSwapTarget(item);
|
||||||
|
setSwapChoices([]);
|
||||||
|
const res = await getMyPointsRewards();
|
||||||
|
const pool = res.pools.find((p) =>
|
||||||
|
p.eligible_reward_items.some(
|
||||||
|
(it) => it.product_id === item.product_id,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
setSwapChoices(
|
||||||
|
(pool?.eligible_reward_items || []).filter(
|
||||||
|
(it) => it.product_id !== item.product_id,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSwapChoice = async (productId: number) => {
|
||||||
|
setSwapLoading(true);
|
||||||
|
const res = await swapMyRewardProduct(productId);
|
||||||
|
setSwapLoading(false);
|
||||||
|
if (res.success) {
|
||||||
|
setSwapTarget(null);
|
||||||
|
await refreshCart();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const confirmClear = () => {
|
const confirmClear = () => {
|
||||||
clearCart();
|
clearCart();
|
||||||
setShowClearModal(false);
|
setShowClearModal(false);
|
||||||
@@ -210,6 +245,43 @@ export default function CartScreen() {
|
|||||||
removeBtn: {
|
removeBtn: {
|
||||||
padding: spacing.m,
|
padding: spacing.m,
|
||||||
},
|
},
|
||||||
|
rewardBadge: {
|
||||||
|
flexDirection: "row",
|
||||||
|
alignItems: "center",
|
||||||
|
alignSelf: "flex-start",
|
||||||
|
gap: 4,
|
||||||
|
marginTop: 2,
|
||||||
|
paddingHorizontal: spacing.s,
|
||||||
|
paddingVertical: 2,
|
||||||
|
borderRadius: borderRadius.sm,
|
||||||
|
backgroundColor: "rgba(245,158,11,0.12)",
|
||||||
|
},
|
||||||
|
rewardBadgeText: {
|
||||||
|
fontSize: fontSize.xs,
|
||||||
|
fontWeight: fontWeight.semibold,
|
||||||
|
color: "#f59e0b",
|
||||||
|
},
|
||||||
|
itemActions: {
|
||||||
|
alignItems: "center",
|
||||||
|
},
|
||||||
|
swapChoice: {
|
||||||
|
flexDirection: "row",
|
||||||
|
justifyContent: "space-between",
|
||||||
|
alignItems: "center",
|
||||||
|
padding: spacing.m,
|
||||||
|
borderRadius: borderRadius.sm,
|
||||||
|
backgroundColor: colors.bgInput,
|
||||||
|
marginBottom: spacing.s,
|
||||||
|
},
|
||||||
|
swapChoiceText: {
|
||||||
|
color: colors.textPrimary,
|
||||||
|
fontSize: fontSize.sm,
|
||||||
|
},
|
||||||
|
swapEmptyText: {
|
||||||
|
color: colors.textMuted,
|
||||||
|
fontSize: fontSize.sm,
|
||||||
|
textAlign: "center",
|
||||||
|
},
|
||||||
modalBody: {
|
modalBody: {
|
||||||
gap: spacing.l,
|
gap: spacing.l,
|
||||||
},
|
},
|
||||||
@@ -351,17 +423,49 @@ export default function CartScreen() {
|
|||||||
<Text style={styles.itemPrice}>
|
<Text style={styles.itemPrice}>
|
||||||
{item.price.toFixed(2)} €
|
{item.price.toFixed(2)} €
|
||||||
</Text>
|
</Text>
|
||||||
|
{(item as EnrichedItem).is_reward && (
|
||||||
|
<View style={styles.rewardBadge}>
|
||||||
|
<Ionicons
|
||||||
|
name="gift-outline"
|
||||||
|
size={10}
|
||||||
|
color="#f59e0b"
|
||||||
|
/>
|
||||||
|
<Text
|
||||||
|
style={styles.rewardBadgeText}
|
||||||
|
>
|
||||||
|
Récompense
|
||||||
|
</Text>
|
||||||
|
</View>
|
||||||
|
)}
|
||||||
|
</View>
|
||||||
|
<View style={styles.itemActions}>
|
||||||
|
{(item as EnrichedItem).is_reward && (
|
||||||
|
<TouchableOpacity
|
||||||
|
onPress={() =>
|
||||||
|
handleOpenSwap(
|
||||||
|
item as EnrichedItem,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
style={styles.removeBtn}
|
||||||
|
>
|
||||||
|
<Ionicons
|
||||||
|
name="swap-horizontal-outline"
|
||||||
|
size={20}
|
||||||
|
color={colors.accent}
|
||||||
|
/>
|
||||||
|
</TouchableOpacity>
|
||||||
|
)}
|
||||||
|
<TouchableOpacity
|
||||||
|
onPress={() => handleRemove(item.id)}
|
||||||
|
style={styles.removeBtn}
|
||||||
|
>
|
||||||
|
<Ionicons
|
||||||
|
name="trash-outline"
|
||||||
|
size={20}
|
||||||
|
color={colors.danger}
|
||||||
|
/>
|
||||||
|
</TouchableOpacity>
|
||||||
</View>
|
</View>
|
||||||
<TouchableOpacity
|
|
||||||
onPress={() => handleRemove(item.id)}
|
|
||||||
style={styles.removeBtn}
|
|
||||||
>
|
|
||||||
<Ionicons
|
|
||||||
name="trash-outline"
|
|
||||||
size={20}
|
|
||||||
color={colors.danger}
|
|
||||||
/>
|
|
||||||
</TouchableOpacity>
|
|
||||||
</View>
|
</View>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
@@ -463,6 +567,43 @@ export default function CartScreen() {
|
|||||||
</View>
|
</View>
|
||||||
</View>
|
</View>
|
||||||
</Modal>
|
</Modal>
|
||||||
|
|
||||||
|
<Modal
|
||||||
|
visible={swapTarget !== null}
|
||||||
|
onClose={() => setSwapTarget(null)}
|
||||||
|
title="Choisir un autre produit"
|
||||||
|
icon="swap-horizontal-outline"
|
||||||
|
iconColor={colors.accent}
|
||||||
|
>
|
||||||
|
<View style={styles.modalBody}>
|
||||||
|
{swapChoices.length === 0 ? (
|
||||||
|
<Text style={styles.swapEmptyText}>
|
||||||
|
Aucun autre produit disponible pour cette
|
||||||
|
récompense.
|
||||||
|
</Text>
|
||||||
|
) : (
|
||||||
|
swapChoices.map((choice) => (
|
||||||
|
<TouchableOpacity
|
||||||
|
key={choice.product_id}
|
||||||
|
style={styles.swapChoice}
|
||||||
|
disabled={swapLoading}
|
||||||
|
onPress={() =>
|
||||||
|
handleSwapChoice(choice.product_id)
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<Text style={styles.swapChoiceText}>
|
||||||
|
{choice.product_name}
|
||||||
|
</Text>
|
||||||
|
<Text style={styles.swapChoiceText}>
|
||||||
|
{choice.price === 0
|
||||||
|
? "Offert"
|
||||||
|
: `${choice.price.toFixed(2)} €`}
|
||||||
|
</Text>
|
||||||
|
</TouchableOpacity>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
</View>
|
||||||
|
</Modal>
|
||||||
</View>
|
</View>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user