From dd22a5ac9807e0d663526735e00db91bbb7521a7 Mon Sep 17 00:00:00 2001 From: Omer Gery <68545675+OmerGery@users.noreply.github.com> Date: Mon, 15 Jan 2024 17:41:26 +0200 Subject: [PATCH] new logic of storing last BA --- .../src/context/newRideContext/index.tsx | 64 +++++++++++++------ .../Locomotion/src/context/payments/index.js | 12 +++- .../RideDrawer/RideOptions/index.tsx | 2 + .../Locomotion/src/pages/Payments/consts.ts | 1 - .../src/popups/ChoosePaymentMethod/index.tsx | 14 +++- 5 files changed, 69 insertions(+), 24 deletions(-) diff --git a/examples/client/Locomotion/src/context/newRideContext/index.tsx b/examples/client/Locomotion/src/context/newRideContext/index.tsx index 500832875..59929dd22 100644 --- a/examples/client/Locomotion/src/context/newRideContext/index.tsx +++ b/examples/client/Locomotion/src/context/newRideContext/index.tsx @@ -8,12 +8,15 @@ import { useNavigation } from '@react-navigation/native'; import _, { pick } from 'lodash'; import moment, { Moment } from 'moment-timezone'; import debounce from 'lodash/debounce'; +import { PAYMENT_MODES } from '../../pages/Payments/consts'; +import offlinePaymentMethod from '../../pages/Payments/offlinePaymentMethod'; import i18n from '../../I18n'; import { FutureRidesContext } from '../futureRides'; import { UserContext } from '../user'; +import PaymentContext from '../payments'; import { getPosition, DEFAULT_COORDS } from '../../services/geo'; import { - getPlaces, getGeocode, getPlaceDetails, getLocationTimezone, + getPlaces, getGeocode, getPlaceDetails, } from './google-api'; import StorageService from '../../services/storage'; import Mixpanel from '../../services/Mixpanel'; @@ -231,6 +234,7 @@ const HISTORY_RECORDS_NUM = 10; const RidePageContextProvider = ({ children }: { children: any }) => { + const { getClientDefaultMethod } = PaymentContext.useContainer(); const { locationGranted, user } = useContext(UserContext); const { isStationsEnabled, @@ -271,8 +275,11 @@ const RidePageContextProvider = ({ children }: { clearInterval(intervalRef.current); }; - const saveLastRide = async (rideId: string) => { - await StorageService.save({ lastRideId: rideId }); + const saveLastRide = async (rideId: string, rideBusinessAccountId: string) => { + await Promise.all([ + StorageService.save({ lastRideId: rideId }), + StorageService.save({ lastBusinessAccountId: rideBusinessAccountId || PAYMENT_MODES.PERSONAL }), + ]); }; const clearLastRide = async () => { @@ -325,13 +332,13 @@ const RidePageContextProvider = ({ children }: { cleanRequestStopPoints(); setRide(newRide); changeBsPage(BS_PAGES.ACTIVE_RIDE); - saveLastRide(newRide.id); + saveLastRide(newRide.id, newRide.businessAccountId); }, [RIDE_STATES.ACTIVE]: (activeRide: any) => { cleanRequestStopPoints(); setRide(activeRide); changeBsPage(BS_PAGES.ACTIVE_RIDE); - saveLastRide(activeRide.id); + saveLastRide(activeRide.id, activeRide.businessAccountId); }, [RIDE_STATES.CANCELED]: (canceledRide: any) => { if (canceledRide.canceledBy !== user?.id) { @@ -388,7 +395,32 @@ const RidePageContextProvider = ({ children }: { return timezoneResponse.time; }; - const getServiceEstimations = async (throwError = true) => { + const updateRidePayload = (newRide: RideInterface) => { + setRide({ + ...ride, + ...newRide, + }); + }; + + const getBusinessAccountIdWithFallback = async (paymentChosen: boolean) => { + const doNotUseFallback = paymentChosen || businessAccountId; + if (doNotUseFallback) { + return businessAccountId; + } + const [notFirstRide, fallbackId] = await Promise.all([ + StorageService.get('lastRideId'), + StorageService.get('lastBusinessAccountId'), + ]); + const defaultPaymentMethod = notFirstRide ? getClientDefaultMethod() : null; + const usePersonalPayment = !fallbackId || fallbackId === PAYMENT_MODES.PERSONAL || defaultPaymentMethod; + if (usePersonalPayment) { return null; } + updateRidePayload({ paymentMethodId: offlinePaymentMethod.id }); + setBusinessAccountId(fallbackId); + return fallbackId; + }; + + const getServiceEstimations = async (throwError = true, paymentChosen = true) => { + const relevantBusinessAccountId = await getBusinessAccountIdWithFallback(paymentChosen); changeBsPage(BS_PAGES.SERVICE_ESTIMATIONS); try { const formattedStopPoints = formatStopPointsForEstimations(requestStopPoints); @@ -400,7 +432,7 @@ const RidePageContextProvider = ({ children }: { scheduledTime = await getLocationTimezoneTime(formattedStopPoints[0].lat, formattedStopPoints[0].lng, unixScheduledTo); } const { estimations, services } = await rideApi - .createServiceEstimations(formattedStopPoints, scheduledTime, businessAccountId); + .createServiceEstimations(formattedStopPoints, scheduledTime, relevantBusinessAccountId); const tags = getEstimationTags(estimations); const formattedEstimations = formatEstimations(services, estimations, tags); @@ -439,24 +471,24 @@ const RidePageContextProvider = ({ children }: { } }; - const tryServiceEstimations = async () => { + const tryServiceEstimations = async (paymentChosen = true) => { const serviceEstimationsInterval = await getServiceEstimationsFetchingInterval(); - await getServiceEstimations(); + await getServiceEstimations(true, paymentChosen); clearInterval(intervalRef.current); intervalRef.current = setInterval(async () => { if (intervalRef.current) { - await getServiceEstimations(false); + await getServiceEstimations(false, true); } }, ((serviceEstimationsInterval || 60) * 1000)); }; const validateStopPointInTerritory = (stopPoints: any[]) => checkStopPointsInTerritory(stopPoints); - const validateRequestedStopPoints = (reqSps: any[]) => { + const validateRequestedStopPoints = (reqSps: any[], paymentChosen = true) => { const stopPoints = reqSps; const isSpsReady = stopPoints.every(r => r.lat && r.lng && r.description); if (stopPoints.length && isSpsReady) { - tryServiceEstimations(); + tryServiceEstimations(paymentChosen); } else if (![BS_PAGES.ADDRESS_SELECTOR, BS_PAGES.LOADING].includes(currentBsPage)) { // reset req stop point request if (!ride.id) { @@ -607,7 +639,7 @@ const RidePageContextProvider = ({ children }: { }, 4000); useEffect(() => { - validateRequestedStopPoints(requestStopPoints); + validateRequestedStopPoints(requestStopPoints, false); }, [requestStopPoints]); const getRideFromApi = async (rideId: string): Promise => formatRide(await rideApi.getRide(rideId)); @@ -1147,12 +1179,6 @@ const RidePageContextProvider = ({ children }: { } }; - const updateRidePayload = (newRide: RideInterface) => { - setRide({ - ...ride, - ...newRide, - }); - }; const patchRideRating = async (rideId: string, rating: number | null, feedback: RideFeedback | null): Promise => { if (!rating && !feedback) { diff --git a/examples/client/Locomotion/src/context/payments/index.js b/examples/client/Locomotion/src/context/payments/index.js index 0d58ac723..0f20b1099 100644 --- a/examples/client/Locomotion/src/context/payments/index.js +++ b/examples/client/Locomotion/src/context/payments/index.js @@ -1,5 +1,6 @@ import { useState, useEffect } from 'react'; import { createContainer } from 'unstated-next'; +import { StorageService } from '../../services'; import i18n from '../../I18n'; import { PAYMENT_STATES } from '../../lib/commonTypes'; import Mixpanel from '../../services/Mixpanel'; @@ -38,11 +39,20 @@ const usePayments = () => { } }; + const handleBusinessAccountStorage = async (businessAccounts) => { + const lastBusinessAccountId = await StorageService.get('lastBusinessAccountId'); + if (businessAccounts?.length > 0 && !lastBusinessAccountId) { + await StorageService.save({ lastBusinessAccountId: businessAccounts[0].id }); + } + }; + const loadCustomer = async () => { const customerData = await getCustomer(); setCustomer(customerData); setPaymentMethods(customerData.paymentMethods); - setBusinessPaymentMethods(customerData.businessAccounts); + const { businessAccounts } = customerData; + setBusinessPaymentMethods(businessAccounts); + await handleBusinessAccountStorage(businessAccounts); return customerData; }; diff --git a/examples/client/Locomotion/src/pages/ActiveRide/RideDrawer/RideOptions/index.tsx b/examples/client/Locomotion/src/pages/ActiveRide/RideDrawer/RideOptions/index.tsx index 75f30fd97..8a565b81a 100644 --- a/examples/client/Locomotion/src/pages/ActiveRide/RideDrawer/RideOptions/index.tsx +++ b/examples/client/Locomotion/src/pages/ActiveRide/RideDrawer/RideOptions/index.tsx @@ -32,6 +32,7 @@ const RideOptions = () => { serviceEstimations, chosenService, updateBusinessAccountId, + businessAccountId, } = useContext(RidePageContext); const { @@ -112,6 +113,7 @@ const RideOptions = () => { /> 0} onAddNewMethod={() => { clearPopup(); diff --git a/examples/client/Locomotion/src/pages/Payments/consts.ts b/examples/client/Locomotion/src/pages/Payments/consts.ts index f37104621..542a67f40 100644 --- a/examples/client/Locomotion/src/pages/Payments/consts.ts +++ b/examples/client/Locomotion/src/pages/Payments/consts.ts @@ -24,7 +24,6 @@ export const PAYMENT_TABS = [ Svg: businessPaymentIcon, }, ]; -export const INITIAL_ACTIVE_PAYMENT_TAB = PAYMENT_TABS[0].id; export const paymentMethodToIconMap = { [PAYMENT_METHODS.CASH]: cashIcon, [PAYMENT_METHODS.OFFLINE]: offlineIcon, diff --git a/examples/client/Locomotion/src/popups/ChoosePaymentMethod/index.tsx b/examples/client/Locomotion/src/popups/ChoosePaymentMethod/index.tsx index a7779fe86..c2b82c06e 100644 --- a/examples/client/Locomotion/src/popups/ChoosePaymentMethod/index.tsx +++ b/examples/client/Locomotion/src/popups/ChoosePaymentMethod/index.tsx @@ -6,7 +6,7 @@ import Modal from 'react-native-modal'; import { useNavigation } from '@react-navigation/native'; import EmptyState from '../../Components/EmptyState'; import Mixpanel from '../../services/Mixpanel'; -import { INITIAL_ACTIVE_PAYMENT_TAB, PAYMENT_MODES, PAYMENT_TABS } from '../../pages/Payments/consts'; +import { PAYMENT_MODES, PAYMENT_TABS } from '../../pages/Payments/consts'; import TabSwitch from '../../Components/TabSwitch'; import { getPaymentMethod } from '../../pages/Payments/cardDetailUtils'; import CloseButton from '../../Components/CloseButton'; @@ -39,6 +39,7 @@ interface PaymentMethodPopupProps { onAddNewMethod: () => void; showOffline: boolean; showBusinessPaymentMethods: boolean; + selectedBusinessAccountId: string | null; } const PaymentMethodPopup = ({ @@ -51,11 +52,14 @@ const PaymentMethodPopup = ({ onAddNewMethod, showOffline, showBusinessPaymentMethods, + selectedBusinessAccountId, }: PaymentMethodPopupProps) => { const usePayments = PaymentsContext.useContainer(); const { chosenService } = useContext(MewRidePageContext); const [selectedPaymentId, setSelectedPaymentId] = useState(selected); - const [activePaymentTab, setActivePaymentTab] = useState(INITIAL_ACTIVE_PAYMENT_TAB); + const [activePaymentTab, setActivePaymentTab] = useState( + selectedBusinessAccountId ? PAYMENT_MODES.BUSINESS : PAYMENT_MODES.PERSONAL, + ); const isBusinessMode = activePaymentTab === PAYMENT_MODES.BUSINESS; const personalPaymentMethods = [ ...usePayments.paymentMethods, @@ -83,7 +87,11 @@ const PaymentMethodPopup = ({ setSelectedPaymentId(selected); } else { const paymentMethod = await usePayments.getClientDefaultMethod(); - setSelectedPaymentId(paymentMethod?.id); + if (paymentMethod?.id) { + setSelectedPaymentId(paymentMethod?.id); + } else if (selectedBusinessAccountId) { + setSelectedPaymentId(selectedBusinessAccountId); + } } };