From 5fa3a57e8426a65887bead947cc0e0e806f8f085 Mon Sep 17 00:00:00 2001 From: Sidharth Prabhu Date: Tue, 4 Aug 2026 14:21:14 +0530 Subject: [PATCH] Unrated orders error fixed --- ordering_site/src/pages/CheckoutScreen.tsx | 5 +++-- ordering_site/src/pages/HomeScreen.tsx | 9 +++++---- ordering_site/src/pages/ItemDetailScreen.tsx | 5 +++-- ordering_site/src/pages/MyOrdersScreen.tsx | 5 +++-- ordering_site/src/pages/StallDetailScreen.tsx | 6 +++--- ordering_site/src/pages/WalletScreen.tsx | 7 ++++--- ordering_site/src/utils/api.ts | 6 ++++++ ordering_site/src/utils/razorpay.ts | 4 +++- 8 files changed, 30 insertions(+), 17 deletions(-) create mode 100644 ordering_site/src/utils/api.ts diff --git a/ordering_site/src/pages/CheckoutScreen.tsx b/ordering_site/src/pages/CheckoutScreen.tsx index a79da0a3..adecc3b7 100644 --- a/ordering_site/src/pages/CheckoutScreen.tsx +++ b/ordering_site/src/pages/CheckoutScreen.tsx @@ -16,6 +16,7 @@ import { useCart } from '../contexts/CartContext'; import { useAuth } from '../contexts/AuthContext'; import { useFood } from '../contexts/FoodContext'; import { payAndVerify, authHeaders } from '../utils/razorpay'; +import { getApiBaseUrl } from '../utils/api'; import './CheckoutScreen.css'; type PaymentMethod = 'RITZ_TOKEN' | 'RAZORPAY'; @@ -50,7 +51,7 @@ const CheckoutScreen: React.FC = () => { const fetchBalance = async () => { if (!user) return; try { - const response = await fetch(`http://${window.location.hostname}:8080/api/wallet/balance/${user.id}`, { + const response = await fetch(`${getApiBaseUrl()}/api/wallet/balance/${user.id}`, { headers: authHeaders() }); const data = await response.json(); @@ -101,7 +102,7 @@ const CheckoutScreen: React.FC = () => { user: { id: user.id } }; - const response = await fetch(`http://${window.location.hostname}:8080/api/orders`, { + const response = await fetch(`${getApiBaseUrl()}/api/orders`, { method: 'POST', headers: authHeaders(), body: JSON.stringify(orderData), diff --git a/ordering_site/src/pages/HomeScreen.tsx b/ordering_site/src/pages/HomeScreen.tsx index b11cab20..f9776955 100644 --- a/ordering_site/src/pages/HomeScreen.tsx +++ b/ordering_site/src/pages/HomeScreen.tsx @@ -10,6 +10,7 @@ import { useFood } from '../contexts/FoodContext'; import { useAuth } from '../contexts/AuthContext'; import { motion, AnimatePresence } from 'framer-motion'; import walletIcon from '../assets/front.webp'; +import { getApiBaseUrl } from '../utils/api'; import './HomeScreen.css'; const HomeScreen: React.FC = () => { @@ -36,7 +37,7 @@ const HomeScreen: React.FC = () => { try { const token = localStorage.getItem('token'); - const response = await fetch(`http://${window.location.hostname}:8080/api/feedback/latest-unrated/${user?.id}`, { + const response = await fetch(`${getApiBaseUrl()}/api/feedback/latest-unrated/${user?.id}`, { headers: { ...(token ? { 'Authorization': `Bearer ${token}` } : {}) } @@ -59,7 +60,7 @@ const HomeScreen: React.FC = () => { try { const token = localStorage.getItem('token'); - await fetch(`http://${window.location.hostname}:8080/api/feedback/skip/${unratedOrder.id}`, { + await fetch(`${getApiBaseUrl()}/api/feedback/skip/${unratedOrder.id}`, { method: 'POST', headers: { ...(token ? { 'Authorization': `Bearer ${token}` } : {}) @@ -80,7 +81,7 @@ const HomeScreen: React.FC = () => { const handleFeedbackSubmit = async (feedbackData: any) => { try { const token = localStorage.getItem('token'); - const response = await fetch(`http://${window.location.hostname}:8080/api/feedback/submit`, { + const response = await fetch(`${getApiBaseUrl()}/api/feedback/submit`, { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -146,7 +147,7 @@ const HomeScreen: React.FC = () => { const fetchSearchResults = async () => { setIsSearching(true); try { - const response = await fetch(`http://${window.location.hostname}:8080/api/products?search=${encodeURIComponent(debouncedSearch)}&size=100`); + const response = await fetch(`${getApiBaseUrl()}/api/products?search=${encodeURIComponent(debouncedSearch)}&size=100`); if (response.ok) { const data = await response.json(); const products = data.content || data || []; diff --git a/ordering_site/src/pages/ItemDetailScreen.tsx b/ordering_site/src/pages/ItemDetailScreen.tsx index 1b8967ba..ffb6cf03 100644 --- a/ordering_site/src/pages/ItemDetailScreen.tsx +++ b/ordering_site/src/pages/ItemDetailScreen.tsx @@ -8,6 +8,7 @@ import { useFood } from '../contexts/FoodContext'; import CartTab from '../components/CartTab'; import { VegNonVegIcon } from '../components/ItemCard'; import './ItemDetailScreen.css'; +import { getApiBaseUrl } from '../utils/api'; const ItemDetailScreen: React.FC = () => { const { itemId } = useParams<{ itemId: string }>(); @@ -32,7 +33,7 @@ const ItemDetailScreen: React.FC = () => { const fetchItem = async () => { setIsFetching(true); try { - const response = await fetch(`http://${window.location.hostname}:8080/api/products/${itemId}`); + const response = await fetch(`${getApiBaseUrl()}/api/products/${itemId}`); if (response.ok) { const data = await response.json(); @@ -47,7 +48,7 @@ const ItemDetailScreen: React.FC = () => { let finalRatingCount = 0; try { const token = localStorage.getItem('token'); - const statsRes = await fetch(`http://${window.location.hostname}:8080/api/feedback/item-stats?productName=${encodeURIComponent(data.name)}`, { + const statsRes = await fetch(`${getApiBaseUrl()}/api/feedback/item-stats?productName=${encodeURIComponent(data.name)}`, { headers: token ? { 'Authorization': `Bearer ${token}` } : {} }); if (statsRes.ok) { diff --git a/ordering_site/src/pages/MyOrdersScreen.tsx b/ordering_site/src/pages/MyOrdersScreen.tsx index e1a16712..c7cfd730 100644 --- a/ordering_site/src/pages/MyOrdersScreen.tsx +++ b/ordering_site/src/pages/MyOrdersScreen.tsx @@ -10,6 +10,7 @@ import { useFood } from '../contexts/FoodContext'; import { useCart } from '../contexts/CartContext'; import type { FoodItem } from '../types'; import './MyOrdersScreen.css'; +import { getApiBaseUrl } from '../utils/api'; interface Order { id: number; @@ -82,7 +83,7 @@ const MyOrdersScreen: React.FC = () => { const fetchOrders = async () => { try { const token = localStorage.getItem('token'); - const response = await fetch(`http://${window.location.hostname}:8080/api/orders/user/${user?.id}`, { + const response = await fetch(`${getApiBaseUrl()}/api/orders/user/${user?.id}`, { headers: { ...(token ? { 'Authorization': `Bearer ${token}` } : {}) } @@ -143,7 +144,7 @@ const MyOrdersScreen: React.FC = () => { setIsCancelling(true); try { const token = localStorage.getItem('token'); - const response = await fetch(`http://${window.location.hostname}:8080/api/orders/${orderId}/cancel`, { + const response = await fetch(`${getApiBaseUrl()}/api/orders/${orderId}/cancel`, { method: 'POST', headers: { ...(token ? { 'Authorization': `Bearer ${token}` } : {}) diff --git a/ordering_site/src/pages/StallDetailScreen.tsx b/ordering_site/src/pages/StallDetailScreen.tsx index 14169ca9..90e67c64 100644 --- a/ordering_site/src/pages/StallDetailScreen.tsx +++ b/ordering_site/src/pages/StallDetailScreen.tsx @@ -6,6 +6,7 @@ import CartTab from '../components/CartTab'; import BottomNav from '../components/BottomNav'; import type { Stall } from '../types'; import './StallDetailScreen.css'; +import { getApiBaseUrl } from '../utils/api'; const StallDetailScreen: React.FC = () => { const { id } = useParams<{ id: string }>(); @@ -18,7 +19,7 @@ const StallDetailScreen: React.FC = () => { const fetchStallDetails = async () => { setLoading(true); try { - const response = await fetch(`http://${window.location.hostname}:8080/api/stalls/${id}`); + const response = await fetch(`${getApiBaseUrl()}/api/stalls/${id}`); if (!response.ok) throw new Error('Stall not found'); const data = await response.json(); @@ -61,8 +62,7 @@ const StallDetailScreen: React.FC = () => { fetchStallDetails(); // SSE Real-time stock updates - const host = window.location.hostname; - const eventSource = new EventSource(`http://${host}:8080/api/stock/stream`); + const eventSource = new EventSource(`${getApiBaseUrl()}/api/stock/stream`); eventSource.addEventListener('stockUpdate', (event: any) => { try { diff --git a/ordering_site/src/pages/WalletScreen.tsx b/ordering_site/src/pages/WalletScreen.tsx index dc65f21d..0c163a76 100644 --- a/ordering_site/src/pages/WalletScreen.tsx +++ b/ordering_site/src/pages/WalletScreen.tsx @@ -7,6 +7,7 @@ import Header from '../components/Header'; import BottomNav from '../components/BottomNav'; import tokenImage from '../assets/display_ritz.webp'; import './WalletScreen.css'; +import { getApiBaseUrl } from '../utils/api'; interface Transaction { id: number; @@ -40,7 +41,7 @@ const WalletScreen: React.FC = () => { try { setIsLoading(true); // Fetch balance - const balanceRes = await fetch(`http://${window.location.hostname}:8080/api/wallet/balance/${user.id}`, { + const balanceRes = await fetch(`${getApiBaseUrl()}/api/wallet/balance/${user.id}`, { headers: authHeaders() }); if (balanceRes.ok) { @@ -49,7 +50,7 @@ const WalletScreen: React.FC = () => { } // Fetch transactions - const txRes = await fetch(`http://${window.location.hostname}:8080/api/wallet/transactions/${user.id}`, { + const txRes = await fetch(`${getApiBaseUrl()}/api/wallet/transactions/${user.id}`, { headers: authHeaders() }); if (txRes.ok) { @@ -71,7 +72,7 @@ const WalletScreen: React.FC = () => { setIsRedeeming(true); setRedeemStatus({ type: null, message: '' }); - const response = await fetch(`http://${window.location.hostname}:8080/api/coupons/redeem`, { + const response = await fetch(`${getApiBaseUrl()}/api/coupons/redeem`, { method: 'POST', headers: authHeaders(), body: JSON.stringify({ diff --git a/ordering_site/src/utils/api.ts b/ordering_site/src/utils/api.ts new file mode 100644 index 00000000..c09e6234 --- /dev/null +++ b/ordering_site/src/utils/api.ts @@ -0,0 +1,6 @@ +export const getApiBaseUrl = (): string => { + if (window.location.protocol === 'https:') { + return 'https://cmsapi.rit-services.in'; + } + return `http://${window.location.hostname}:8080`; +}; diff --git a/ordering_site/src/utils/razorpay.ts b/ordering_site/src/utils/razorpay.ts index 1b35349c..ae90062c 100644 --- a/ordering_site/src/utils/razorpay.ts +++ b/ordering_site/src/utils/razorpay.ts @@ -55,7 +55,9 @@ declare global { } } -const API_HOST = () => `http://${window.location.hostname}:8080`; +import { getApiBaseUrl } from './api'; + +const API_HOST = () => getApiBaseUrl(); export function authHeaders(): HeadersInit { const token = localStorage.getItem('token');