Skip to content

Commit 04a0b64

Browse files
authored
Merge pull request #191 from Dew2118/add-distance-to-thermometer
Add display for distance between start and end points in thermometer question
2 parents 25303d9 + 9a425a5 commit 04a0b64

1 file changed

Lines changed: 43 additions & 13 deletions

File tree

src/components/cards/thermometer.tsx

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { useStore } from "@nanostores/react";
2+
import { distance, point } from "@turf/turf";
23

34
import { LatitudeLongitude } from "@/components/LatLngPicker";
45
import { Label } from "@/components/ui/label";
56
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group";
7+
import { defaultUnit } from "@/lib/context";
68
import {
79
hiderMode,
810
isLoading,
@@ -30,6 +32,10 @@ export const ThermometerQuestionComponent = ({
3032
const $hiderMode = useStore(hiderMode);
3133
const $questions = useStore(questions);
3234
const $isLoading = useStore(isLoading);
35+
36+
const $defaultUnit = useStore(defaultUnit);
37+
const DISTANCE_UNIT = $defaultUnit ?? "miles";
38+
3339
const label = `Thermometer
3440
${
3541
$questions
@@ -38,6 +44,27 @@ export const ThermometerQuestionComponent = ({
3844
.indexOf(questionKey) + 1
3945
}`;
4046

47+
const hasCoords =
48+
data.latA !== null &&
49+
data.lngA !== null &&
50+
data.latB !== null &&
51+
data.lngB !== null;
52+
53+
const distanceValue = hasCoords
54+
? distance(
55+
point([data.lngA!, data.latA!]),
56+
point([data.lngB!, data.latB!]),
57+
{ units: DISTANCE_UNIT },
58+
)
59+
: null;
60+
61+
const unitLabel =
62+
DISTANCE_UNIT === "meters"
63+
? "Meters"
64+
: DISTANCE_UNIT === "kilometers"
65+
? "KM"
66+
: "Miles";
67+
4168
return (
4269
<QuestionCard
4370
questionKey={questionKey}
@@ -46,7 +73,7 @@ export const ThermometerQuestionComponent = ({
4673
className={className}
4774
collapsed={data.collapsed}
4875
setCollapsed={(collapsed) => {
49-
data.collapsed = collapsed; // Doesn't trigger a re-render so no need for questionModified
76+
data.collapsed = collapsed;
5077
}}
5178
locked={!data.drag}
5279
setLocked={(locked) => questionModified((data.drag = !locked))}
@@ -57,32 +84,35 @@ export const ThermometerQuestionComponent = ({
5784
label="Start"
5885
colorName={data.colorA}
5986
onChange={(lat, lng) => {
60-
if (lat !== null) {
61-
data.latA = lat;
62-
}
63-
if (lng !== null) {
64-
data.lngA = lng;
65-
}
87+
if (lat !== null) data.latA = lat;
88+
if (lng !== null) data.lngA = lng;
6689
questionModified();
6790
}}
6891
disabled={!data.drag || $isLoading}
6992
/>
93+
7094
<LatitudeLongitude
7195
latitude={data.latB}
7296
longitude={data.lngB}
7397
label="End"
7498
colorName={data.colorB}
7599
onChange={(lat, lng) => {
76-
if (lat !== null) {
77-
data.latB = lat;
78-
}
79-
if (lng !== null) {
80-
data.lngB = lng;
81-
}
100+
if (lat !== null) data.latB = lat;
101+
if (lng !== null) data.lngB = lng;
82102
questionModified();
83103
}}
84104
disabled={!data.drag || $isLoading}
85105
/>
106+
107+
{distanceValue !== null && (
108+
<div className="px-2 text-sm text-muted-foreground">
109+
Distance:{" "}
110+
<span className="font-medium text-foreground">
111+
{distanceValue.toFixed(3)} {unitLabel}
112+
</span>
113+
</div>
114+
)}
115+
86116
<div className="flex gap-2 items-center p-2">
87117
<Label
88118
className={cn(

0 commit comments

Comments
 (0)