From a91aa43bb62ad6cf5d0305aac9b4ec5795cf1a40 Mon Sep 17 00:00:00 2001 From: sina_sajjadi Date: Tue, 10 Nov 2026 11:01:37 +0330 Subject: [PATCH] feat: implement mobile-first shop interface with pages, components, and api integration --- .env | 2 + index.html | 139 +- package-lock.json | 18 + package.json | 4 +- src/components/account/HabibCoinsBanner.tsx | 11 +- .../bridge/HabibBridgeProviders.tsx | 1 + src/components/checkout/AddressCard.tsx | 2 + src/components/checkout/CartItemCard.tsx | 3 + src/components/checkout/PaymentMethodCard.tsx | 2 + src/components/common/PhoneNumberInput.tsx | 669 +++++++++ src/components/common/ShimmerSkeletons.tsx | 1257 +++++++++++------ src/components/layout/BottomNavBar.tsx | 2 + src/components/layout/MobileShell.tsx | 68 +- src/components/layout/TopHeader.tsx | 1 + src/components/modals/AddressFormSheet.tsx | 67 +- src/components/modals/LoginRequiredModal.tsx | 7 +- src/components/modals/OrderSummarySheet.tsx | 81 +- src/components/modals/PaymentMethodsSheet.tsx | 82 +- .../modals/ShipmentTrackingSheet.tsx | 31 +- src/components/orders/OrderFilterTabs.tsx | 38 +- src/context/ShopAppContext.tsx | 86 +- src/hooks/use-animated-presence.ts | 32 + src/hooks/useSmartBack.ts | 13 + src/index.css | 298 +++- src/lib/cookies.ts | 44 - src/lib/http.ts | 42 +- src/lib/phone.ts | 403 ++++++ src/lib/shopApi.ts | 54 +- src/pages/account/LanguageSettingPage.tsx | 21 +- src/pages/account/ProviderRequestPage.tsx | 134 +- src/pages/account/ShopAccountPage.tsx | 7 +- src/pages/catalog/ShopSinglePage.tsx | 15 +- src/pages/chat/ShopChatPage.tsx | 3 + src/pages/checkout/ShopBuy01Page.tsx | 15 +- src/pages/checkout/ShopCartPage.tsx | 19 +- src/pages/checkout/ShopPayResultPage.tsx | 2 + src/pages/checkout/ShopPaymentMethodsPage.tsx | 1 + src/pages/checkout/ShopPaymentPage.tsx | 67 +- src/pages/orders/ShopOrderDetailPage.tsx | 1 + src/pages/orders/ShopOrdersPage.tsx | 7 +- vite.config.ts | 2 +- 41 files changed, 2812 insertions(+), 939 deletions(-) create mode 100644 src/components/common/PhoneNumberInput.tsx create mode 100644 src/hooks/use-animated-presence.ts create mode 100644 src/lib/phone.ts diff --git a/.env b/.env index e803925..a70fe1c 100644 --- a/.env +++ b/.env @@ -1,3 +1,5 @@ # Set to true to fall back to mock data when backend responses are empty or offline VITE_USE_MOCK_DATA=false +VITE_BACKEND_ORIGIN=http://127.0.0.1:8000 + diff --git a/index.html b/index.html index 6f21d73..749873e 100644 --- a/index.html +++ b/index.html @@ -8,6 +8,83 @@ content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover, interactive-widget=overlays-content" /> Habib Shop + + -
+
+ +
diff --git a/package-lock.json b/package-lock.json index 30d4832..9fa4aa0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "0.0.0", "dependencies": { "@tanstack/react-query": "^5.105.1", + "google-libphonenumber": "^3.2.48", "react": "^19.3.1", "react-dom": "^19.3.1", "react-router-dom": "^7.18.5" @@ -17,6 +18,7 @@ "@babel/core": "^7.29.7", "@rolldown/plugin-babel": "^0.2.4", "@types/babel__core": "^7.20.5", + "@types/google-libphonenumber": "^7.4.31", "@types/node": "^24.19.2", "@types/react": "^19.3.0", "@types/react-dom": "^19.3.0", @@ -1062,6 +1064,13 @@ "@babel/types": "^7.28.2" } }, + "node_modules/@types/google-libphonenumber": { + "version": "7.4.31", + "resolved": "https://registry.npmjs.org/@types/google-libphonenumber/-/google-libphonenumber-7.4.31.tgz", + "integrity": "sha512-NqtRPbLfRbCsqYTVb2UHM67AhMUfTYarxy32u14QilQq7i2C37jtvxbEmb0U0oCMMZtFfpoAtbuhrXwF7/EyVg==", + "dev": true, + "license": "MIT" + }, "node_modules/@types/node": { "version": "24.19.2", "resolved": "https://registry.npmjs.org/@types/node/-/node-24.19.2.tgz", @@ -1655,6 +1664,15 @@ "node": ">=6.9.0" } }, + "node_modules/google-libphonenumber": { + "version": "3.2.48", + "resolved": "https://registry.npmjs.org/google-libphonenumber/-/google-libphonenumber-3.2.48.tgz", + "integrity": "sha512-x8Z8baHTiMchHIcQUsCU5HNwbCVA8UOoVkOMfGxOpCtkoUG6pAAp6jtjKILHgmhNzEb6t3t8i4UxAMIkNZr3DA==", + "license": "(MIT AND Apache-2.0)", + "engines": { + "node": ">=0.10" + } + }, "node_modules/js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", diff --git a/package.json b/package.json index 7a58cbe..4c4b8a6 100644 --- a/package.json +++ b/package.json @@ -4,13 +4,14 @@ "version": "0.0.0", "type": "module", "scripts": { - "dev": "vite", + "dev": "vite --host", "build": "tsc -b && vite build", "lint": "oxlint", "preview": "vite preview" }, "dependencies": { "@tanstack/react-query": "^5.105.1", + "google-libphonenumber": "^3.2.48", "react": "^19.3.1", "react-dom": "^19.3.1", "react-router-dom": "^7.18.5" @@ -19,6 +20,7 @@ "@babel/core": "^7.29.7", "@rolldown/plugin-babel": "^0.2.4", "@types/babel__core": "^7.20.5", + "@types/google-libphonenumber": "^7.4.31", "@types/node": "^24.19.2", "@types/react": "^19.3.0", "@types/react-dom": "^19.3.0", diff --git a/src/components/account/HabibCoinsBanner.tsx b/src/components/account/HabibCoinsBanner.tsx index 7521ef4..fbaabd8 100644 --- a/src/components/account/HabibCoinsBanner.tsx +++ b/src/components/account/HabibCoinsBanner.tsx @@ -19,10 +19,15 @@ export const HabibCoinsBanner: React.FC = ({ }) => { const location = useLocation(); const isBare = isBareMode(location.search); - const { habibCoinsBalance, setHabibCoinsBalance, showToast } = useShopApp(); + const { showToast } = useShopApp(); + const [hostCoins, setHostCoins] = React.useState(() => + typeof window !== 'undefined' && typeof window.HABIB_COINS === 'number' + ? window.HABIB_COINS + : 21 + ); const resolvedCoins = - coinsCount !== undefined ? coinsCount : isBare ? 21 : habibCoinsBalance; + coinsCount !== undefined ? coinsCount : isBare ? 21 : hostCoins; const handleBuyCoins = async () => { if (onBuyCoins) { @@ -32,7 +37,7 @@ export const HabibCoinsBanner: React.FC = ({ if (isInFlutterWebView()) { const updated = await buyHabibCoinPackages(); if (typeof updated === 'number') { - setHabibCoinsBalance(updated); + setHostCoins(updated); showToast(`Balance updated: ${updated} Habib Coins`); } return; diff --git a/src/components/bridge/HabibBridgeProviders.tsx b/src/components/bridge/HabibBridgeProviders.tsx index e4ddb44..47e0cc0 100644 --- a/src/components/bridge/HabibBridgeProviders.tsx +++ b/src/components/bridge/HabibBridgeProviders.tsx @@ -65,6 +65,7 @@ export const HabibBridgeProviders: React.FC<{ children: React.ReactNode }> = ({
= ({ alignItems: 'center', justifyContent: 'center', flexShrink: 0, + transition: 'background-color 180ms ease, border-color 180ms ease', }} > {item.selected && (
= ({ {/* Close / Remove Button */}
onRemove?.(item.id)} role={onRemove ? 'button' : undefined} aria-label="Remove item" @@ -210,6 +211,8 @@ export const MainCartItemCard: React.FC = ({ {/* Quantity Number */} = ({ alignItems: 'center', justifyContent: 'center', flexShrink: 0, + transition: 'background-color 180ms ease, border-color 180ms ease', }} > {isSelected && (
void; + countryHint?: string; + defaultDialCode?: string; + required?: boolean; + bare?: boolean; + height?: number; + borderRadius?: number; + backgroundColor?: string; + borderColor?: string; + fontSize?: number; + showError?: boolean; + errorMessage?: string | null; +} + +export const PhoneNumberInput: React.FC = ({ + id, + value, + onChange, + countryHint, + defaultDialCode = '+1', + required = true, + bare = false, + height = 44, + borderRadius = 12, + backgroundColor = '#ffffff', + borderColor = '#d7dbe2', + fontSize = 13, + showError = false, + errorMessage, +}) => { + const resolvedDefaultDial = useMemo( + () => + countryHint + ? getDialCodeForCountryName(countryHint, defaultDialCode) + : defaultDialCode, + [countryHint, defaultDialCode] + ); + + const initialParts = useMemo( + () => parsePhoneParts(value, resolvedDefaultDial, countryHint), + // eslint-disable-next-line react-hooks/exhaustive-deps + [] + ); + + const [dialCode, setDialCode] = useState(initialParts.dialCode); + const [selectedRegion, setSelectedRegion] = useState( + initialParts.regionCode + ); + const [nationalInput, setNationalInput] = useState( + initialParts.nationalInput + ); + const [touched, setTouched] = useState(false); + const [pickerOpen, setPickerOpen] = useState(false); + const [searchQuery, setSearchQuery] = useState(''); + const listRef = useRef(null); + const lastEmittedValueRef = useRef(value); + const userSelectedCountryRef = useRef(false); + const prevCountryHintRef = useRef(countryHint); + + const { shouldRender: shouldRenderSheet, isClosing: isSheetClosing } = + useAnimatedPresence(pickerOpen, 200); + + const closeSheet = () => { + setPickerOpen(false); + setSearchQuery(''); + }; + + useHardwareBackHandler(() => { + closeSheet(); + return true; + }, pickerOpen); + + // Sync only when external value changes from outside (e.g. profile loaded or address modal opened) + useEffect(() => { + if (value === lastEmittedValueRef.current) return; + lastEmittedValueRef.current = value; + + if (!value) { + setNationalInput(''); + if (!userSelectedCountryRef.current) { + setDialCode(resolvedDefaultDial); + setSelectedRegion(resolveRegionFromDialCode(resolvedDefaultDial)); + } + return; + } + + const parsed = parsePhoneParts( + value, + dialCode || resolvedDefaultDial, + countryHint + ); + setDialCode(parsed.dialCode); + setSelectedRegion(parsed.regionCode); + setNationalInput(parsed.nationalInput); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [value]); + + // Update dial code only if countryHint prop itself changes and user hasn't picked a code or typed a number + useEffect(() => { + if (countryHint === prevCountryHintRef.current) return; + prevCountryHintRef.current = countryHint; + if (!countryHint || userSelectedCountryRef.current || nationalInput.trim()) { + return; + } + const nextDial = getDialCodeForCountryName(countryHint, defaultDialCode); + setDialCode(nextDial); + setSelectedRegion(resolveRegionFromDialCode(nextDial)); + }, [countryHint, defaultDialCode, nationalInput]); + + // Ensure list starts scrolled at the top when opened or when search query changes + useEffect(() => { + if (pickerOpen && listRef.current) { + listRef.current.scrollTop = 0; + } + }, [pickerOpen, searchQuery]); + + const countries = useMemo(() => getSupportedCountryOptions('en'), []); + + const activeCountry = useMemo(() => { + const bySelectedRegion = countries.find( + (c) => c.region === selectedRegion && c.dialCode === dialCode + ); + if (bySelectedRegion) return bySelectedRegion; + const fallbackRegion = resolveRegionFromDialCode(dialCode); + return ( + countries.find((c) => c.region === fallbackRegion) || + countries.find((c) => c.dialCode === dialCode) || + null + ); + }, [countries, dialCode, selectedRegion]); + + const filteredCountries = useMemo(() => { + const q = searchQuery.trim().toLowerCase(); + if (!q) return countries; + const digits = q.replace(/\D/g, ''); + return countries + .filter( + (c) => + c.name.toLowerCase().includes(q) || + c.region.toLowerCase().includes(q) || + c.dialCode.includes(q) || + (digits.length > 0 && c.callingCode.includes(digits)) + ) + .sort((a, b) => { + if (digits) { + const aExact = a.callingCode === digits; + const bExact = b.callingCode === digits; + if (aExact && !bExact) return -1; + if (!aExact && bExact) return 1; + const aStarts = a.callingCode.startsWith(digits); + const bStarts = b.callingCode.startsWith(digits); + if (aStarts && !bStarts) return -1; + if (!aStarts && bStarts) return 1; + } + const aStartsName = a.name.toLowerCase().startsWith(q); + const bStartsName = b.name.toLowerCase().startsWith(q); + if (aStartsName && !bStartsName) return -1; + if (!aStartsName && bStartsName) return 1; + return a.name.localeCompare(b.name, 'en'); + }); + }, [countries, searchQuery]); + + const validation = useMemo( + () => validatePhoneNumber(dialCode, nationalInput, selectedRegion), + [dialCode, nationalInput, selectedRegion] + ); + + const hasError = + !bare && + (showError || (touched && nationalInput.trim().length > 0)) && + !validation.isValid; + + const placeholderText = bare + ? 'Phone number' + : `${getPlaceholderForDialCode(dialCode)}${required ? ' *' : ''}`; + + const emitChange = ( + nextDial: string, + nextNational: string, + nextRegion: string + ) => { + const res = validatePhoneNumber(nextDial, nextNational, nextRegion); + const combined = nextNational ? `${nextDial} ${nextNational}` : ''; + lastEmittedValueRef.current = combined; + onChange(combined, res.isValid, res.e164); + }; + + const handleInputChange = (e: React.ChangeEvent) => { + const raw = e.target.value; + if (bare) { + setNationalInput(raw); + lastEmittedValueRef.current = raw; + onChange(raw, true, raw); + return; + } + + const nextNational = raw.replace(/[^\d]/g, ''); + setNationalInput(nextNational); + emitChange(dialCode, nextNational, selectedRegion); + }; + + const handleSelectCountry = (nextDialCode: string, nextRegion: string) => { + userSelectedCountryRef.current = true; + setDialCode(nextDialCode); + setSelectedRegion(nextRegion); + closeSheet(); + emitChange(nextDialCode, nationalInput, nextRegion); + }; + + return ( +
+
+ {bare ? ( + + +23 + + ) : ( + + )} + +
+ + setTouched(true)} + placeholder={placeholderText} + style={{ + flex: 1, + minWidth: 0, + height: '100%', + border: 'none', + outline: 'none', + padding: 0, + fontFamily: 'var(--font-sans)', + fontWeight: 400, + fontSize, + lineHeight: '20px', + color: '#111111', + backgroundColor: 'transparent', + }} + /> +
+ + {hasError && ( + + {errorMessage || validation.error || 'Enter a valid phone number'} + + )} + + {shouldRenderSheet && + !bare && + typeof document !== 'undefined' && + createPortal( +
+
e.stopPropagation()} + style={{ + height: '78dvh', + maxHeight: 620, + backgroundColor: '#f3f1ef', + padding: + '22px var(--page-px, 16px) calc(20px + var(--safe-bottom, 0px))', + display: 'flex', + flexDirection: 'column', + gap: 14, + overflow: 'hidden', + boxSizing: 'border-box', + }} + > + {/* Sheet Header */} +
+ + Select Country Code + + +
+ + {/* Search Input matching Habib Shop SearchBar design */} +
+ Search + setSearchQuery(e.target.value)} + className="habib-search-input" + placeholder="Search country name or code (+1, +98...)" + aria-label="Search country name or code" + style={{ + width: '100%', + height: '100%', + borderRadius: 15, + padding: searchQuery + ? '15px 36px 17px 39px' + : '15px 12px 17px 39px', + fontFamily: 'var(--font-sans)', + fontWeight: 400, + fontSize: 12, + lineHeight: '18px', + color: '#111111', + backgroundColor: 'transparent', + boxSizing: 'border-box', + }} + /> + {searchQuery && ( + + )} +
+ + {/* Scrollable Country List */} +
+ {filteredCountries.length > 0 ? ( + filteredCountries.map((country) => { + const selected = + country.dialCode === dialCode && + country.region === activeCountry?.region; + return ( + + ); + }) + ) : ( + + )} +
+
+
, + document.body + )} +
+ ); +}; diff --git a/src/components/common/ShimmerSkeletons.tsx b/src/components/common/ShimmerSkeletons.tsx index 664750d..4ca4593 100644 --- a/src/components/common/ShimmerSkeletons.tsx +++ b/src/components/common/ShimmerSkeletons.tsx @@ -1,5 +1,9 @@ import React from 'react'; +const ShimmerGate: React.FC<{ children: React.ReactNode }> = ({ children }) => ( + <>{children} +); + interface ShimmerBoxProps { width?: number | string; height?: number | string; @@ -15,17 +19,19 @@ export const ShimmerBox: React.FC = ({ dark = false, style, }) => ( -