From 00d5b265b0c8538e0ece52d8b6eaf87654563919 Mon Sep 17 00:00:00 2001 From: Krishna Gupta Date: Wed, 3 Jul 2024 02:53:52 +0530 Subject: [PATCH 1/5] fix: Distance - Start and Stop labels do not change when dragging one over another. Signed-off-by: Krishna Gupta --- src/libs/actions/IOU.ts | 4 ++-- .../request/step/IOURequestStepDistance.tsx | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 48c70021cacc..831291cbd6e5 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -339,8 +339,8 @@ function initMoneyRequest(reportID: string, policy: OnyxEntry, // Add initial empty waypoints when starting a distance expense if (iouRequestType === CONST.IOU.REQUEST_TYPE.DISTANCE) { comment.waypoints = { - waypoint0: {}, - waypoint1: {}, + waypoint0: {keyForList: 'start_waypoint'}, + waypoint1: {keyForList: 'stop_waypoint'}, }; if (!isFromGlobalCreate) { const customUnitRateID = DistanceRequestUtils.getCustomUnitRateID(reportID); diff --git a/src/pages/iou/request/step/IOURequestStepDistance.tsx b/src/pages/iou/request/step/IOURequestStepDistance.tsx index 11d5687a55e9..acbec6ece047 100644 --- a/src/pages/iou/request/step/IOURequestStepDistance.tsx +++ b/src/pages/iou/request/step/IOURequestStepDistance.tsx @@ -76,7 +76,15 @@ function IOURequestStepDistance({ const {translate} = useLocalize(); const [optimisticWaypoints, setOptimisticWaypoints] = useState(null); - const waypoints = useMemo(() => optimisticWaypoints ?? transaction?.comment?.waypoints ?? {waypoint0: {}, waypoint1: {}}, [optimisticWaypoints, transaction]); + const waypoints = useMemo( + () => + optimisticWaypoints ?? + transaction?.comment?.waypoints ?? { + waypoint0: {keyForList: 'start_waypoint'}, + waypoint1: {keyForList: 'stop_waypoint'}, + }, + [optimisticWaypoints, transaction], + ); const waypointsList = Object.keys(waypoints); const previousWaypoints = usePrevious(waypoints); const numberOfWaypoints = Object.keys(waypoints).length; @@ -92,7 +100,14 @@ function IOURequestStepDistance({ const isRouteAbsentWithoutErrors = !hasRoute && !hasRouteError; const shouldFetchRoute = (isRouteAbsentWithoutErrors || haveValidatedWaypointsChanged) && !isLoadingRoute && Object.keys(validatedWaypoints).length > 1; const [shouldShowAtLeastTwoDifferentWaypointsError, setShouldShowAtLeastTwoDifferentWaypointsError] = useState(false); - const nonEmptyWaypointsCount = useMemo(() => Object.keys(waypoints).filter((key) => !isEmpty(waypoints[key])).length, [waypoints]); + const nonEmptyWaypointsCount = useMemo( + () => + Object.keys(waypoints).filter((key) => { + const {keyForList, ...waypointWithoutKey} = waypoints[key]; + return !isEmpty(waypointWithoutKey); + }).length, + [waypoints], + ); const duplicateWaypointsError = useMemo( () => nonEmptyWaypointsCount >= 2 && Object.keys(validatedWaypoints).length !== nonEmptyWaypointsCount, [nonEmptyWaypointsCount, validatedWaypoints], From 717e2ec21e34e4ed6fddb1349df0e0588e9d0bb2 Mon Sep 17 00:00:00 2001 From: Krishna Gupta Date: Wed, 3 Jul 2024 03:26:42 +0530 Subject: [PATCH 2/5] minor fix. Signed-off-by: Krishna Gupta --- src/pages/iou/request/step/IOURequestStepDistance.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/pages/iou/request/step/IOURequestStepDistance.tsx b/src/pages/iou/request/step/IOURequestStepDistance.tsx index acbec6ece047..ac90b126fdc0 100644 --- a/src/pages/iou/request/step/IOURequestStepDistance.tsx +++ b/src/pages/iou/request/step/IOURequestStepDistance.tsx @@ -361,7 +361,9 @@ function IOURequestStepDistance({ data.forEach((waypoint, index) => { newWaypoints[`waypoint${index}`] = waypoints[waypoint] ?? {}; // Find waypoint that BECOMES empty after dragging - if (isEmpty(newWaypoints[`waypoint${index}`]) && !isEmpty(waypoints[`waypoint${index}`] ?? {})) { + const {keyForList, ...newWaypointsWithoutKey} = newWaypoints[`waypoint${index}`]; + const {keyForList: waypointKey, ...waypointWithoutKey} = waypoints[`waypoint${index}`]; + if (isEmpty(newWaypointsWithoutKey) && !isEmpty(waypointWithoutKey ?? {})) { emptyWaypointIndex = index; } }); From 82fc0971a7837042d9418bb3b28c08c1c095d914 Mon Sep 17 00:00:00 2001 From: krishna2323 Date: Tue, 9 Jul 2024 17:25:44 +0530 Subject: [PATCH 3/5] minor update. Signed-off-by: krishna2323 --- src/pages/iou/request/step/IOURequestStepDistance.tsx | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/pages/iou/request/step/IOURequestStepDistance.tsx b/src/pages/iou/request/step/IOURequestStepDistance.tsx index ba0abaf65e54..2a84ea5fc8c6 100644 --- a/src/pages/iou/request/step/IOURequestStepDistance.tsx +++ b/src/pages/iou/request/step/IOURequestStepDistance.tsx @@ -361,10 +361,12 @@ function IOURequestStepDistance({ data.forEach((waypoint, index) => { newWaypoints[`waypoint${index}`] = waypoints[waypoint] ?? {}; // Find waypoint that BECOMES empty after dragging - const {keyForList, ...newWaypointsWithoutKey} = newWaypoints[`waypoint${index}`]; - const {keyForList: waypointKey, ...waypointWithoutKey} = waypoints[`waypoint${index}`]; - if (isEmpty(newWaypointsWithoutKey) && !isEmpty(waypointWithoutKey ?? {})) { - emptyWaypointIndex = index; + if (newWaypoints[`waypoint${index}`] && waypoints[`waypoint${index}`]) { + const {keyForList, ...newWaypointsWithoutKey} = newWaypoints[`waypoint${index}`]; + const {keyForList: waypointKey, ...waypointWithoutKey} = waypoints[`waypoint${index}`]; + if (isEmpty(newWaypointsWithoutKey) && !isEmpty(waypointWithoutKey ?? {})) { + emptyWaypointIndex = index; + } } }); From e166e19aff08a5350d6cb68e9e440a2333a212a8 Mon Sep 17 00:00:00 2001 From: krishna2323 Date: Fri, 12 Jul 2024 06:59:26 +0530 Subject: [PATCH 4/5] added isWaypointEmpty to check if a waypoint is empty or not. Signed-off-by: krishna2323 --- .../request/step/IOURequestStepDistance.tsx | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/pages/iou/request/step/IOURequestStepDistance.tsx b/src/pages/iou/request/step/IOURequestStepDistance.tsx index 2a84ea5fc8c6..b5c48ffa3ded 100644 --- a/src/pages/iou/request/step/IOURequestStepDistance.tsx +++ b/src/pages/iou/request/step/IOURequestStepDistance.tsx @@ -32,7 +32,7 @@ import ROUTES from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; import type * as OnyxTypes from '@src/types/onyx'; import type {Errors} from '@src/types/onyx/OnyxCommon'; -import type {WaypointCollection} from '@src/types/onyx/Transaction'; +import type {Waypoint, WaypointCollection} from '@src/types/onyx/Transaction'; import StepScreenWrapper from './StepScreenWrapper'; import withFullTransactionOrNotFound from './withFullTransactionOrNotFound'; import type {WithWritableReportOrNotFoundProps} from './withWritableReportOrNotFound'; @@ -100,14 +100,15 @@ function IOURequestStepDistance({ const isRouteAbsentWithoutErrors = !hasRoute && !hasRouteError; const shouldFetchRoute = (isRouteAbsentWithoutErrors || haveValidatedWaypointsChanged) && !isLoadingRoute && Object.keys(validatedWaypoints).length > 1; const [shouldShowAtLeastTwoDifferentWaypointsError, setShouldShowAtLeastTwoDifferentWaypointsError] = useState(false); - const nonEmptyWaypointsCount = useMemo( - () => - Object.keys(waypoints).filter((key) => { - const {keyForList, ...waypointWithoutKey} = waypoints[key]; - return !isEmpty(waypointWithoutKey); - }).length, - [waypoints], - ); + const isWaypointEmpty = (waypoint?: Waypoint) => { + if (!waypoint) { + return true; + } + const {keyForList, ...waypointWithoutKey} = waypoint; + return isEmpty(waypointWithoutKey); + }; + const nonEmptyWaypointsCount = useMemo(() => Object.keys(waypoints).filter((key) => !isWaypointEmpty(waypoints[key])).length, [waypoints]); + const duplicateWaypointsError = useMemo( () => nonEmptyWaypointsCount >= 2 && Object.keys(validatedWaypoints).length !== nonEmptyWaypointsCount, [nonEmptyWaypointsCount, validatedWaypoints], @@ -361,12 +362,8 @@ function IOURequestStepDistance({ data.forEach((waypoint, index) => { newWaypoints[`waypoint${index}`] = waypoints[waypoint] ?? {}; // Find waypoint that BECOMES empty after dragging - if (newWaypoints[`waypoint${index}`] && waypoints[`waypoint${index}`]) { - const {keyForList, ...newWaypointsWithoutKey} = newWaypoints[`waypoint${index}`]; - const {keyForList: waypointKey, ...waypointWithoutKey} = waypoints[`waypoint${index}`]; - if (isEmpty(newWaypointsWithoutKey) && !isEmpty(waypointWithoutKey ?? {})) { - emptyWaypointIndex = index; - } + if (!isWaypointEmpty(newWaypoints[`waypoint${index}`]) && !isWaypointEmpty(waypoints[`waypoint${index}`])) { + emptyWaypointIndex = index; } }); From 826e52f067cf0d411b6407be304a3b8af945ce44 Mon Sep 17 00:00:00 2001 From: krishna2323 Date: Fri, 12 Jul 2024 07:07:04 +0530 Subject: [PATCH 5/5] minor fix. Signed-off-by: krishna2323 --- src/pages/iou/request/step/IOURequestStepDistance.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/iou/request/step/IOURequestStepDistance.tsx b/src/pages/iou/request/step/IOURequestStepDistance.tsx index b5c48ffa3ded..f4dc08409824 100644 --- a/src/pages/iou/request/step/IOURequestStepDistance.tsx +++ b/src/pages/iou/request/step/IOURequestStepDistance.tsx @@ -362,7 +362,7 @@ function IOURequestStepDistance({ data.forEach((waypoint, index) => { newWaypoints[`waypoint${index}`] = waypoints[waypoint] ?? {}; // Find waypoint that BECOMES empty after dragging - if (!isWaypointEmpty(newWaypoints[`waypoint${index}`]) && !isWaypointEmpty(waypoints[`waypoint${index}`])) { + if (isWaypointEmpty(newWaypoints[`waypoint${index}`]) && !isWaypointEmpty(waypoints[`waypoint${index}`])) { emptyWaypointIndex = index; } });