From 91c1df2099469cebd2f32986571f2fa867038509 Mon Sep 17 00:00:00 2001 From: Pulkit Barwal Date: Tue, 6 May 2025 16:22:12 +0530 Subject: [PATCH] NTP-55956 (#1161) --- src/reducer/nearbyCasesSlice.ts | 5 ++ src/screens/addresses/actions.ts | 84 ++++++++++++++----- .../addresses/common/AddressItemHeader.tsx | 16 ++-- .../caseDetails/AllocatedAddressDetails.tsx | 38 ++++----- 4 files changed, 93 insertions(+), 50 deletions(-) diff --git a/src/reducer/nearbyCasesSlice.ts b/src/reducer/nearbyCasesSlice.ts index c812b0c2..cf86dac7 100644 --- a/src/reducer/nearbyCasesSlice.ts +++ b/src/reducer/nearbyCasesSlice.ts @@ -8,6 +8,7 @@ const initialState = { locationNearbyCasesListUpdated: {} as IGeolocationCoordinate, isPullToRefreshNearbyCasesVisible: false as boolean, caseReferenceIdToDistanceMap: new Map() as Map, + addressToDistanceMap: new Map() as Map, sortTabSelected: TABS_KEYS.HIGHEST_OD as string, }; @@ -30,6 +31,9 @@ export const nearbyCasesListSlice = createSlice({ setSortTabSelected: (state, action) => { state.sortTabSelected = action.payload; }, + setAddressToDistanceMap: (state, action) => { + state.addressToDistanceMap = action.payload; + }, resetNearbyCasesData: () => initialState, }, }); @@ -40,6 +44,7 @@ export const { setIsPullToRefreshNearbyCasesVisible, setCaseReferenceIdToDistanceMap, setSortTabSelected, + setAddressToDistanceMap, resetNearbyCasesData, } = nearbyCasesListSlice.actions; diff --git a/src/screens/addresses/actions.ts b/src/screens/addresses/actions.ts index e71cb3d4..67e30575 100644 --- a/src/screens/addresses/actions.ts +++ b/src/screens/addresses/actions.ts @@ -9,30 +9,76 @@ import { } from '@reducers/topAddressesSlice'; import { AppDispatch } from '@store'; import { PAGE_END } from './constants'; +import { ICaseItemLatLongData } from '@screens/allCases/interface'; +import store from '@store'; +import { getGeolocationDistance } from '@screens/allCases/allCasesActions'; +import { getDistanceFromLatLonInKm } from '@components/utlis/commonFunctions'; +import { setAddressToDistanceMap } from '@reducers/nearbyCasesSlice'; +import { ILocationData } from '@screens/addresses/interfaces'; -export const getTopAddresses = (caseId: string, start: number) => (dispatch: AppDispatch) => { +export const getTopAddresses = (caseId: string, start: number) => async (dispatch: AppDispatch) => { dispatch(setTopAddressesLoading({ caseId, isLoading: true })); const url = getApiUrl(ApiKeys.GET_TOP_ADDRESSES); - axiosInstance - .get(url, { + + try { + const response = await axiosInstance.get(url, { params: { caseReferenceId: caseId, startingRank: start }, - }) - .then((res) => { - if (res?.data) { - const { unifiedLocations = [], totalLocationEntities = 0 } = res?.data || {}; - dispatch( - setTopAddresses({ - caseId, - addresses: unifiedLocations, - totalLocationEntities, - }) - ); - } - }) - .catch((error) => {}) - .finally(() => { - dispatch(setTopAddressesLoading({ caseId, isLoading: false })); }); + + if (response?.data) { + const { unifiedLocations = [], totalLocationEntities = 0 } = response.data || {}; + dispatch( + setTopAddresses({ + caseId, + addresses: unifiedLocations, + totalLocationEntities, + }) + ); + + const deviceGeolocationCoordinate = + store?.getState()?.foregroundService?.deviceGeolocationCoordinate || {}; + const agentId = store?.getState()?.user?.user?.referenceId!; + const source = { + id: agentId, + latitude: deviceGeolocationCoordinate?.latitude, + longitude: deviceGeolocationCoordinate?.longitude, + }; + + const destinations: ICaseItemLatLongData[] = []; + unifiedLocations?.forEach((location: ILocationData) => { + destinations.push({ + id: location?.referenceId, + latitude: location?.latitude, + longitude: location?.longitude, + }); + }); + + let addressDistanceMap: Map = new Map(); + if (destinations.length > 0) { + addressDistanceMap = await getGeolocationDistance({ + source, + destinations, + }); + + if (!addressDistanceMap || addressDistanceMap?.size === 0) { + destinations?.forEach((destination) => { + const distanceInKm = getDistanceFromLatLonInKm( + destination, + deviceGeolocationCoordinate + ); + if (distanceInKm) { + addressDistanceMap?.set(destination?.id, distanceInKm); + } + }); + } + store.dispatch(setAddressToDistanceMap(addressDistanceMap)); + } + } + } catch (error) { + console.error('Error fetching top addresses:', error); + } finally { + dispatch(setTopAddressesLoading({ caseId, isLoading: false })); + } }; export const getOtherAddresses = (caseId: string) => (dispatch: AppDispatch) => { diff --git a/src/screens/addresses/common/AddressItemHeader.tsx b/src/screens/addresses/common/AddressItemHeader.tsx index 4732b037..47b968e5 100644 --- a/src/screens/addresses/common/AddressItemHeader.tsx +++ b/src/screens/addresses/common/AddressItemHeader.tsx @@ -33,17 +33,11 @@ const AddressItemHeader = (props: ITopAddressItemHeader) => { const { pinCode, city, latitude, longitude, rank, visited, locationSubType } = locationDetails || {}; - const deviceGeolocationCoordinate = useAppSelector( - (state) => state.foregroundService?.deviceGeolocationCoordinate + const addressToDistanceMap = useAppSelector( + (state) => state.nearbyCasesSlice?.addressToDistanceMap ); + const distanceOfAddress = addressToDistanceMap?.get(locationDetails?.referenceId); const [isModalVisible, setIsModalVisible] = useState(false); - const relativeDistanceBwLatLong = useMemo(() => { - const distance = getDistanceFromLatLonInKm(deviceGeolocationCoordinate, { - latitude, - longitude, - }); - return `${relativeDistanceFormatter(distance)} km`; - }, [deviceGeolocationCoordinate]); const addressString = useMemo(() => { return [pinCode, city].filter(Boolean).join(', '); @@ -91,14 +85,14 @@ const AddressItemHeader = (props: ITopAddressItemHeader) => { latitude={latitude} longitude={longitude} handleOpenMapForAddresses={handleOpenMapForAddresses} - relativeDistanceBwLatLong={relativeDistanceBwLatLong} + relativeDistanceBwLatLong={`${Number(distanceOfAddress?.toFixed(1))} km`} style={GenericStyles.pr16} isSimilarAddressPage={isSimilarAddressPage} /> ) : (