diff --git a/package.json b/package.json index c98824f6..c6ce4403 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fleetbase/storefront-app", - "version": "0.0.17", + "version": "0.0.18", "private": true, "scripts": { "android": "react-native run-android", diff --git a/src/contexts/AuthContext.native.tsx b/src/contexts/AuthContext.native.tsx index a5eb0b68..70a8e9ed 100644 --- a/src/contexts/AuthContext.native.tsx +++ b/src/contexts/AuthContext.native.tsx @@ -15,6 +15,8 @@ const authReducer = (state, action) => { switch (action.type) { case 'RESTORE_SESSION': return { ...state, customer: action.customer }; + case 'UPDATE': + return { ...state, customer: action.customer }; case 'LOGIN': return { ...state, phone: action.phone, isSendingCode: action.isSendingCode ?? false }; case 'CREATING_ACCOUNT': @@ -69,6 +71,7 @@ export const AuthProvider = ({ children }) => { } setStoredCustomer(customerInstance.serialize()); + dispatch({ type: 'UPDATE', customer: customerInstance }); EventRegister.emit('customer.updated', customerInstance); }, [storefront, setStoredCustomer, authToken] @@ -109,7 +112,7 @@ export const AuthProvider = ({ children }) => { // Update customer meta attributes const updateCustomerMeta = async (newMeta = {}) => { - const meta = { ...state.customer.getAttribute('meta'), ...newMeta }; + const meta = { ...state.customer.getAttribute('meta', {}), ...newMeta }; try { const customer = await state.customer.update({ meta }); setCustomer(customer); diff --git a/src/contexts/AuthContext.web.tsx b/src/contexts/AuthContext.web.tsx index b33b7738..6ac7395c 100644 --- a/src/contexts/AuthContext.web.tsx +++ b/src/contexts/AuthContext.web.tsx @@ -14,6 +14,8 @@ const authReducer = (state, action) => { switch (action.type) { case 'RESTORE_SESSION': return { ...state, customer: action.customer }; + case 'UPDATE': + return { ...state, customer: action.customer }; case 'LOGIN': return { ...state, phone: action.phone, isSendingCode: action.isSendingCode ?? false }; case 'CREATING_ACCOUNT': @@ -68,6 +70,7 @@ export const AuthProvider = ({ children }) => { } setStoredCustomer(customerInstance.serialize()); + dispatch({ type: 'UPDATE', customer: customerInstance }); EventRegister.emit('customer.updated', customerInstance); }, [storefront, setStoredCustomer, authToken] @@ -108,7 +111,7 @@ export const AuthProvider = ({ children }) => { // Update customer meta attributes const updateCustomerMeta = async (newMeta = {}) => { - const meta = { ...state.customer.getAttribute('meta'), ...newMeta }; + const meta = { ...state.customer.getAttribute('meta', {}), ...newMeta }; try { const customer = await state.customer.update({ meta }); setCustomer(customer); diff --git a/src/hooks/use-qpay-checkout.ts b/src/hooks/use-qpay-checkout.ts index de517932..9f3e4d7f 100644 --- a/src/hooks/use-qpay-checkout.ts +++ b/src/hooks/use-qpay-checkout.ts @@ -26,16 +26,12 @@ export default function useQPayCheckout({ onOrderComplete }) { const { currentLocation: deliveryLocation, updateDefaultLocation } = useCurrentLocation(); const { listen } = useSocketClusterClient(); const [cart, updateCart] = useCart(); - const defaultCompanyRegistrationNo = useMemo(() => { - return customer?.getAttribute('meta.ebarimt_registration_no', '') ?? ''; - }, [customer]); const [checkoutOptions, setCheckoutOptions] = useState({ leavingTip: false, tip: 0, leavingDeliveryTip: false, deliveryTip: 0, pickup: storefrontConfig('prioritizePickup') ? 1 : 0, - ebarimt_registration_no: defaultCompanyRegistrationNo, }); const [invoice, setInvoice] = useState(); const [checkoutId, setCheckoutId] = useState(); @@ -44,9 +40,17 @@ export default function useQPayCheckout({ onOrderComplete }) { const [isServiceQuoteUnavailable, setIsServiceQuoteUnavailable] = useState(false); const [isLoading, setIsLoading] = useState(true); const [isCapturingOrder, setIsCapturingOrder] = useState(false); - const [isPersonal, setIsPersonal] = useState(isBlank(defaultCompanyRegistrationNo)); const [error, setError] = useState(false); + // Order notes const [orderNotes, setOrderNotes] = useStorage(`${customer?.id ?? 'anon'}_order_notes`, ''); + // Ebarimt company registration no + const companyRegistrationNumber = useMemo(() => { + if (customer && typeof customer.getAttribute === 'function') { + return customer.getAttribute('meta.ebarimt_registration_no', ''); + } + return ''; + }, [customer]); + const [isPersonal, setIsPersonal] = useState(isBlank(companyRegistrationNumber)); const listenerRef = useRef(); const hasOrderCompleted = useRef(false); const cartContentsString = JSON.stringify(cart.contents() || []); @@ -141,7 +145,7 @@ export default function useQPayCheckout({ onOrderComplete }) { const debouncedUpdateRegistration = useMemo( () => debounce((registrationNumber) => { - setCheckoutOptions((prev) => ({ ...prev, ebarimt_registration_no: registrationNumber })); + updateCustomerMeta({ ebarimt_registration_no: registrationNumber }); }, 500), // 500ms delay [] ); @@ -372,7 +376,7 @@ export default function useQPayCheckout({ onOrderComplete }) { isPersonal, setIsPersonal, isCompany: !isPersonal, - companyRegistrationNumber: customer?.getAttribute('meta.ebarimt_registration_no', '') ?? '', + companyRegistrationNumber, setCompanyRegistrationNumber, }), [ @@ -397,6 +401,7 @@ export default function useQPayCheckout({ onOrderComplete }) { isServiceQuoteUnavailable, isPersonal, setIsPersonal, + companyRegistrationNumber, setCompanyRegistrationNumber, ] ); diff --git a/src/screens/CartScreen.tsx b/src/screens/CartScreen.tsx index b77b1c4f..86404722 100644 --- a/src/screens/CartScreen.tsx +++ b/src/screens/CartScreen.tsx @@ -1,5 +1,5 @@ import React, { useRef, useState, useEffect, useCallback } from 'react'; -import { useNavigation, useFocusEffect } from '@react-navigation/native'; +import { useNavigation } from '@react-navigation/native'; import { Animated, Pressable, StyleSheet, LayoutAnimation, UIManager, Platform } from 'react-native'; import { useSafeTabBarHeight as useBottomTabBarHeight } from '../hooks/use-safe-tab-bar-height'; import { useSafeAreaInsets } from 'react-native-safe-area-context'; @@ -149,13 +149,6 @@ const CartScreen = ({ route }) => { setDisplayedItems(cart ? cart.contents() : []); }, [cart]); - // Run once - useFocusEffect( - useCallback(() => { - refreshCustomer(); - }, []) - ); - const renderRightActions = (cartItem) => ( handleEdit(cartItem)}> diff --git a/test-vehicle-instantiation.js b/test-vehicle-instantiation.js deleted file mode 100644 index 3701be94..00000000 --- a/test-vehicle-instantiation.js +++ /dev/null @@ -1,39 +0,0 @@ -// Test to verify Vehicle instantiation issue - -// The problem: VehicleMarker is not rendering because Vehicle instantiation might be failing - -// In FoodTruckScreen.tsx line 401: -// vehicle={new Vehicle(foodTruck.vehicle, fleetbaseAdapter)} - -// foodTruck.vehicle data structure from console: -const vehicleData = { - id: 'vehicle_vM3dTUi', - internal_id: null, - photo_url: 'https://s3.ap-southeast-1.amazonaws.com/flb-assets/static/vehicle-placeholder.png', - avatar_url: 'https://flb-assets.s3-ap-southeast-1.amazonaws.com/static/vehicle-icons/light_commercial_van.svg', - name: '2018 Kia BONGO 2', - description: null, - driver: null, - make: 'Kia', - model: 'BONGO 2', - location: { - coordinates: [106.898928, 47.9167609], - type: 'Point' - } -}; - -// When Vehicle is instantiated with this data: -// const vehicle = new Vehicle(vehicleData, fleetbaseAdapter); - -// Then in VehicleMarker.tsx: -// vehicle.getAttribute('location.coordinates.1') // Should return 47.9167609 (latitude) -// vehicle.getAttribute('location.coordinates.0') // Should return 106.898928 (longitude) - -// POTENTIAL ISSUES: -// 1. Vehicle constructor might be throwing an error (silently caught?) -// 2. fleetbaseAdapter might be undefined -// 3. getAttribute might not work with nested array access like 'location.coordinates.1' -// 4. The component might be failing to render due to error boundary - -// SOLUTION: Add error boundary or try-catch around Vehicle instantiation -// OR: Extract coordinates before passing to VehicleMarker diff --git a/translations/en.json b/translations/en.json index 709c6e09..ce98dbff 100644 --- a/translations/en.json +++ b/translations/en.json @@ -285,7 +285,7 @@ "vatRegistration": "VAT Registration", "personal": "Personal", "company": "Company", - "companyRegistrationNumber": "Company Registration No." + "companyRegistrationNumber": "Company Registration" }, "SavedLocationScreen": { "addressSaved": "Address saved.", diff --git a/translations/mn.json b/translations/mn.json index 43d34fea..c6733a1f 100644 --- a/translations/mn.json +++ b/translations/mn.json @@ -283,7 +283,7 @@ "vatRegistration": "НӨАТ Registration", "personal": "Хувь хүн", "company": "Байгуулга", - "companyRegistrationNumber": "Компанийн регистерийн дугаар" + "companyRegistrationNumber": "Компани рд" }, "SavedLocationScreen": { "addressSaved": "Хаяг хадгалагдсан.",