From b65e2b41d83dbd2b0695eb5167cf3bc81fec8c9f Mon Sep 17 00:00:00 2001 From: Xor290 Date: Sun, 18 Oct 2026 12:18:58 +0200 Subject: [PATCH] chore: build --- frontend-prep/src/api/api.ts | 23 ++++ frontend-prep/src/pages/User/Cart.tsx | 72 +++++++++- mobile/src/api/api.ts | 13 ++ mobile/src/context/CartContext.tsx | 2 + mobile/src/screens/client/CartScreen.tsx | 163 +++++++++++++++++++++-- 5 files changed, 259 insertions(+), 14 deletions(-) diff --git a/frontend-prep/src/api/api.ts b/frontend-prep/src/api/api.ts index 018df021..4d62422e 100644 --- a/frontend-prep/src/api/api.ts +++ b/frontend-prep/src/api/api.ts @@ -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 ( orderId: number, rating: number, diff --git a/frontend-prep/src/pages/User/Cart.tsx b/frontend-prep/src/pages/User/Cart.tsx index 3252cf98..054a2939 100644 --- a/frontend-prep/src/pages/User/Cart.tsx +++ b/frontend-prep/src/pages/User/Cart.tsx @@ -2,11 +2,11 @@ import { useCart } from "../../context/useCart"; import Navbar from "../../components/Navbar"; import { useEffect, useState } from "react"; import { useNavigate } from "react-router-dom"; -import { isUserAuthenticated, getProductById, getMediaUrl } from "../../api/api"; -import type { Product } from "../../api/api"; +import { isUserAuthenticated, getProductById, getMediaUrl, getMyPointsRewards, swapMyRewardProduct } from "../../api/api"; +import type { Product, RewardItemConfig } from "../../api/api"; import { Trash2, ShoppingCart, AlertTriangle, Leaf, X } from "lucide-react"; 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"; interface CartItemWithMedia { @@ -30,6 +30,9 @@ function Cart() { const [currentVideoUrl, setCurrentVideoUrl] = useState(""); const [enrichedItems, setEnrichedItems] = useState([]); const [loadingMedia, setLoadingMedia] = useState(false); + const [swapTarget, setSwapTarget] = useState(null); + const [swapChoices, setSwapChoices] = useState([]); + const [swapLoading, setSwapLoading] = useState(false); useEffect(() => { if (!isUserAuthenticated()) navigate("/login/client", { replace: true }); @@ -105,6 +108,28 @@ function Cart() { 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 = () => { if (!isUserAuthenticated()) { navigate("/login/client", { replace: true }); return; } navigate("/user/checkout"); @@ -200,6 +225,19 @@ function Cart() {

+ {/* Changer le produit de la récompense */} + {item.is_reward && ( + + )} + {/* Bouton supprimer */} + + {swapChoices.length === 0 ? ( +

Aucun autre produit disponible pour cette récompense.

+ ) : ( + swapChoices.map((choice) => ( + + )) + )} + + + )} + {/* Footer fixe */}
diff --git a/mobile/src/api/api.ts b/mobile/src/api/api.ts index b37f964d..908fa126 100644 --- a/mobile/src/api/api.ts +++ b/mobile/src/api/api.ts @@ -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 ( orderId: number, rating: number, diff --git a/mobile/src/context/CartContext.tsx b/mobile/src/context/CartContext.tsx index 6591b697..1f08063c 100644 --- a/mobile/src/context/CartContext.tsx +++ b/mobile/src/context/CartContext.tsx @@ -23,6 +23,7 @@ export interface CartItem { quantity: number; category: string; image?: string; + is_reward?: boolean; } interface ToastData { @@ -89,6 +90,7 @@ export function CartProvider({ children }: { children: ReactNode }) { quantity: item.quantity, category: (item.category || "autre").toLowerCase().trim(), image: item.image, + is_reward: item.is_reward, })); setCartItems(items); } else { diff --git a/mobile/src/screens/client/CartScreen.tsx b/mobile/src/screens/client/CartScreen.tsx index e8bd8552..9c5b4323 100644 --- a/mobile/src/screens/client/CartScreen.tsx +++ b/mobile/src/screens/client/CartScreen.tsx @@ -12,7 +12,12 @@ import { useNavigation, useFocusEffect } from "@react-navigation/native"; import type { NativeStackNavigationProp } from "@react-navigation/native-stack"; import { Ionicons } from "@expo/vector-icons"; 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 LoadingSpinner from "../../components/ui/LoadingSpinner"; import Button from "../../components/ui/Button"; @@ -37,6 +42,7 @@ interface EnrichedItem { price: number; quantity: number; category: string; + is_reward?: boolean; imageUri?: string; } @@ -58,6 +64,9 @@ export default function CartScreen() { const [loadingMedia, setLoadingMedia] = useState(false); const [removeId, setRemoveId] = useState(null); const [showClearModal, setShowClearModal] = useState(false); + const [swapTarget, setSwapTarget] = useState(null); + const [swapChoices, setSwapChoices] = useState([]); + const [swapLoading, setSwapLoading] = useState(false); useFocusEffect( useCallback(() => { @@ -106,6 +115,32 @@ export default function CartScreen() { 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 = () => { clearCart(); setShowClearModal(false); @@ -210,6 +245,43 @@ export default function CartScreen() { removeBtn: { 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: { gap: spacing.l, }, @@ -351,17 +423,49 @@ export default function CartScreen() { {item.price.toFixed(2)} € + {(item as EnrichedItem).is_reward && ( + + + + Récompense + + + )} + + + {(item as EnrichedItem).is_reward && ( + + handleOpenSwap( + item as EnrichedItem, + ) + } + style={styles.removeBtn} + > + + + )} + handleRemove(item.id)} + style={styles.removeBtn} + > + + - handleRemove(item.id)} - style={styles.removeBtn} - > - - )} /> @@ -463,6 +567,43 @@ export default function CartScreen() { + + setSwapTarget(null)} + title="Choisir un autre produit" + icon="swap-horizontal-outline" + iconColor={colors.accent} + > + + {swapChoices.length === 0 ? ( + + Aucun autre produit disponible pour cette + récompense. + + ) : ( + swapChoices.map((choice) => ( + + handleSwapChoice(choice.product_id) + } + > + + {choice.product_name} + + + {choice.price === 0 + ? "Offert" + : `${choice.price.toFixed(2)} €`} + + + )) + )} + + ); }