Skip to content

Commit

Permalink
Merge pull request #44748 from Krishna2323/krishna2323/issue/44153
Browse files Browse the repository at this point in the history
fix: Distance - Start and Stop labels do not change when dragging one over another.
  • Loading branch information
MonilBhavsar authored Jul 12, 2024
2 parents 29393b1 + 826e52f commit cdbb01b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,8 @@ function initMoneyRequest(reportID: string, policy: OnyxEntry<OnyxTypes.Policy>,
// 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);
Expand Down
24 changes: 20 additions & 4 deletions src/pages/iou/request/step/IOURequestStepDistance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -76,7 +76,15 @@ function IOURequestStepDistance({
const {translate} = useLocalize();

const [optimisticWaypoints, setOptimisticWaypoints] = useState<WaypointCollection | null>(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;
Expand All @@ -93,7 +101,15 @@ function IOURequestStepDistance({
const isEmptyCoordinates = !transaction?.routes?.route0?.geometry?.coordinates?.length;
const shouldFetchRoute = (isEmptyCoordinates || 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 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],
Expand Down Expand Up @@ -345,7 +361,7 @@ 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}`] ?? {})) {
if (isWaypointEmpty(newWaypoints[`waypoint${index}`]) && !isWaypointEmpty(waypoints[`waypoint${index}`])) {
emptyWaypointIndex = index;
}
});
Expand Down

0 comments on commit cdbb01b

Please sign in to comment.