-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathWayPointInput.tsx
More file actions
147 lines (135 loc) · 4.5 KB
/
WayPointInput.tsx
File metadata and controls
147 lines (135 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { useCallback, useContext, useEffect, useState } from 'react';
import { Pencil } from '@osrd-project/ui-icons';
import { isNil } from 'lodash';
import { useTranslation } from 'react-i18next';
import { FaMapMarkedAlt, FaTimesCircle } from 'react-icons/fa';
import EntitySumUp from 'applications/editor/components/EntitySumUp';
import EditorContext from 'applications/editor/context';
import { getEntity } from 'applications/editor/data/api';
import { NEW_ENTITY_ID } from 'applications/editor/data/utils';
import { EndPointKeys } from 'applications/editor/tools/routeEdition/types';
import type {
RouteEditionState,
WayPoint,
WayPointEntity,
} from 'applications/editor/tools/routeEdition/types';
import type { ExtendedEditorContextType } from 'applications/editor/types';
import { useInfraID } from 'common/osrdContext';
import { useAppDispatch } from 'store';
import useKeyboardShortcuts from 'utils/hooks/useKeyboardShortcuts';
import type { EndPoint } from '../../switchEdition/types';
type WayPointInputProps = {
endPoint: EndPoint;
wayPoint?: WayPoint | null;
onChange: (entity: WayPointEntity | null) => void;
};
const WayPointInput = ({ endPoint, wayPoint, onChange }: WayPointInputProps) => {
const dispatch = useAppDispatch();
const { state, setState } = useContext(
EditorContext
) as ExtendedEditorContextType<RouteEditionState>;
const { t } = useTranslation();
const infraID = useInfraID();
const [entityState, setEntityState] = useState<
{ type: 'data'; entity: WayPointEntity } | { type: 'loading' } | { type: 'empty' }
>({ type: 'empty' });
// When escape is pressed => idle
useKeyboardShortcuts([
{
code: 'Escape',
handler: () =>
setState((prev) => ({
...prev,
extremityState: { type: 'idle' },
})),
},
]);
const isPicking =
state.extremityState.type === 'selection' && state.extremityState.extremity === endPoint;
const isDisabled =
state.extremityState.type === 'selection' &&
!isPicking &&
state.extremityState.extremity !== endPoint;
const isWayPointSelected = !isNil(wayPoint);
const onBtnClick = useCallback(() => {
// Cancel current selection:
if (isPicking) {
setState({
...state,
extremityState: {
type: 'idle',
},
});
}
// Start selecting:
else {
setState({
...state,
isComplete: false,
extremityState: {
type: 'selection',
extremity: endPoint,
hoveredPoint: null,
onSelect: onChange,
},
});
}
}, [endPoint, isPicking, setState, state]);
const getButtonIcon = () => {
if (!isPicking && isWayPointSelected) return <Pencil />;
return isPicking ? <FaTimesCircle /> : <FaMapMarkedAlt />;
};
useEffect(() => {
if (
entityState.type === 'empty' ||
(entityState.type === 'data' && wayPoint?.id !== entityState.entity.properties.id)
) {
if (wayPoint && wayPoint.id !== NEW_ENTITY_ID) {
setEntityState({ type: 'loading' });
getEntity<WayPointEntity>(infraID!, wayPoint.id, wayPoint.type, dispatch)
.then((entity) => {
setEntityState({ type: 'data', entity });
// we call the onchange here to populate the state `extremitiesEntities`
onChange(entity);
})
.catch((e) => {
console.error(e);
setEntityState({ type: 'empty' });
onChange(null);
});
} else {
setEntityState({ type: 'empty' });
onChange(null);
}
}
}, [wayPoint]);
return (
<div className="mb-4">
<div className="d-flex flex-row align-items-center mb-2">
<div className="flex-grow-1 flex-shrink-1 mr-2">
{entityState.type === 'data' && entityState.entity ? (
<EntitySumUp entity={entityState.entity} />
) : (
<span className="text-info font-weight-bold">
{t('Editor.tools.routes-edition.no-waypoint-picked-yet')}
</span>
)}
</div>
<button
type="button"
title={t(
`Editor.tools.routes-edition.actions.pick-${EndPointKeys[endPoint].toLowerCase()}${
isPicking ? '-cancel' : ''
}${isWayPointSelected && !isPicking ? '-update' : ''}`
)}
className="btn btn-primary px-3"
onClick={onBtnClick}
disabled={isDisabled}
>
{getButtonIcon()}
</button>
</div>
</div>
);
};
export default WayPointInput;