-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathSpeedSectionMetadataForm.tsx
153 lines (147 loc) · 5.81 KB
/
SpeedSectionMetadataForm.tsx
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
148
149
150
151
152
153
import React, { FC, useContext, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { SpeedSectionEntity } from 'types';
import { AiOutlinePlusCircle } from 'react-icons/ai';
import { FaTimes } from 'react-icons/fa';
import { MdSpeed } from 'react-icons/md';
import { cloneDeep, isEmpty, map, mapKeys, omit } from 'lodash';
import EditorContext from '../../../context';
import { ExtendedEditorContextType } from '../../editorContextTypes';
import { RangeEditionState } from '../types';
import SpeedInput from './SpeedInput';
const getNewSpeedLimitTag = (
speedLimitsByTag: Record<string, number | undefined>,
defaultTag: string
) => {
let newSpeedLimitTag = defaultTag;
let i = 1;
if (speedLimitsByTag[newSpeedLimitTag]) {
while (speedLimitsByTag[`${newSpeedLimitTag} ${i}`]) i += 1;
newSpeedLimitTag += ` ${i}`;
}
return newSpeedLimitTag;
};
const SpeedSectionMetadataForm: FC = () => {
const { t } = useTranslation();
const {
state: { entity },
setState,
} = useContext(EditorContext) as ExtendedEditorContextType<RangeEditionState<SpeedSectionEntity>>;
const addSpeedSectionButtonIsDisabled = useMemo(() => {
const { speed_limit_by_tag } = entity.properties;
if (speed_limit_by_tag) {
const speed_limits = Object.values(speed_limit_by_tag);
const has_undefined_speed_limit = speed_limits.some(
(speed_limit) => speed_limit === undefined || speed_limit === 0
);
return has_undefined_speed_limit;
}
return false;
}, [entity]);
return (
<div>
<h4 className="pb-0">
<MdSpeed className="me-1" /> {t('Editor.tools.speed-edition.speed-limits')}
</h4>
{/* The following tag is here to mimick other tools' forms style: */}
<form className="rjsf" onSubmit={(e) => e.preventDefault()}>
<div className="form-group field field-string mb-2">
<label className="control-label" htmlFor="speed-section.main-limit">
{t('Editor.tools.speed-edition.main-speed-limit')}
</label>
<div className="d-flex flex-row align-items-center">
<SpeedInput
className="form-control flex-grow-1 flex-shrink-1"
id="speed-section.main-limit"
placeholder=""
msSpeed={entity.properties.speed_limit || undefined}
onChange={(newMsSpeed) => {
const newEntity = cloneDeep(entity);
newEntity.properties.speed_limit = newMsSpeed;
setState({ entity: newEntity });
}}
/>
<span className="text-muted ml-2">km/h</span>
</div>
</div>
{!isEmpty(entity.properties.speed_limit_by_tag) && (
<div className="control-label mb-1">
{t('Editor.tools.speed-edition.additional-speed-limit')}
</div>
)}
{map(entity.properties.speed_limit_by_tag || {}, (value, key) => (
<div key={key} className="form-group field field-string">
<div className="d-flex flex-row align-items-center">
<input
required
className="form-control flex-grow-2 flex-shrink-1 mr-2 px-2"
placeholder=""
type="text"
value={key}
onChange={(e) => {
const newEntity = cloneDeep(entity);
const newKey = e.target.value;
newEntity.properties.speed_limit_by_tag = mapKeys(
newEntity.properties.speed_limit_by_tag || {},
(_v, k) => (k === key ? newKey : k)
);
setState({ entity: newEntity });
}}
/>
<SpeedInput
className="form-control flex-shrink-0 px-2"
style={{ width: '5em' }}
placeholder=""
msSpeed={value}
onChange={(newMsSpeed) => {
const newEntity = cloneDeep(entity);
newEntity.properties.speed_limit_by_tag =
newEntity.properties.speed_limit_by_tag || {};
newEntity.properties.speed_limit_by_tag[key] = newMsSpeed;
setState({ entity: newEntity });
}}
/>
<span className="text-muted ml-2">km/h</span>
<small>
<button
type="button"
className="btn btn-primary btn-sm px-2 ml-2"
title={t('commons.delete')}
onClick={() => {
const newEntity = cloneDeep(entity);
newEntity.properties.speed_limit_by_tag = omit(
newEntity.properties.speed_limit_by_tag || {},
key
);
setState({ entity: newEntity });
}}
>
<FaTimes />
</button>
</small>
</div>
</div>
))}
<button
type="button"
className="btn btn-secondary w-100 text-wrap small mb-2"
onClick={async () => {
const newEntity = cloneDeep(entity);
newEntity.properties.speed_limit_by_tag = newEntity.properties.speed_limit_by_tag || {};
const newSpeedLimitTag = getNewSpeedLimitTag(
newEntity.properties.speed_limit_by_tag,
t('Editor.tools.speed-edition.new-tag')
);
newEntity.properties.speed_limit_by_tag[newSpeedLimitTag] = undefined;
setState({ entity: newEntity });
}}
disabled={addSpeedSectionButtonIsDisabled}
>
<AiOutlinePlusCircle className="mr-2" />
{t('Editor.tools.speed-edition.add-new-speed-limit')}
</button>
</form>
</div>
);
};
export default SpeedSectionMetadataForm;