Skip to content

Commit bf2815b

Browse files
Merge pull request #787 from jovandeginste/rs-from-climb
feat(route-segment): Create route segment from climb/descent
2 parents cc6d320 + 1b8f5be commit bf2815b

8 files changed

Lines changed: 140 additions & 62 deletions

File tree

frontend/src/views/workouts/create_route_segment.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class CreateRouteSegmentMap extends HTMLElement {
1919
}
2020

2121
document.addEventListener("DOMContentLoaded", () => {
22-
this.updateInfo();
22+
this.updateAll();
2323
});
2424
}
2525

@@ -91,7 +91,7 @@ class CreateRouteSegmentMap extends HTMLElement {
9191

9292
document.getElementById("start-show").textContent = start;
9393
this.startMarker.setLatLng(
94-
new L.LatLng(this.points[start].lat, this.points[start].lng),
94+
new L.LatLng(this.points[start - 1].lat, this.points[start - 1].lng),
9595
);
9696

9797
if (start > end) {
@@ -136,7 +136,7 @@ class CreateRouteSegmentMap extends HTMLElement {
136136
const start = Number(document.getElementById("start").value);
137137
const end = Number(document.getElementById("end").value);
138138

139-
const d = this.points[end - 1].distance - this.points[start].distance;
139+
const d = this.points[end - 1].distance - this.points[start - 1].distance;
140140
document.getElementById("distance-show").textContent = d.toFixed(2) + " m";
141141
}
142142

pkg/database/workouts_map.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ func (m *MapPoint) ToOrbPoint() *orb.Point {
119119
return &orb.Point{m.Lng, m.Lat}
120120
}
121121

122+
func (m *MapPoint) IsZero() bool {
123+
return m.Lat == 0 && m.Lng == 0
124+
}
125+
122126
func (d *MapDataDetails) Save(db *gorm.DB) error {
123127
return db.Save(d).Error
124128
}

translations/en.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ en:
106106
category: Cat.
107107
translation:
108108
Climbs: Climbs
109+
Create_route_segment_from_climb: Create route segment from this segment
109110
Latest_measurements: Latest measurements
110111
Update_measurement: Update measurement
111112
Steps: Steps

translations/nl.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ nl:
103103
update: Instellingen bijwerken
104104
translation:
105105
Climbs: Beklimmingen
106+
Create_route_segment_from_climb: Maak route segment van dit segment
106107
Latest_measurements: Laatste metingen
107108
Update_measurement: Meting aanpassen
108109
Steps: Stappen

views/workouts/create_route_segment.templ

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,24 @@ templ CreateRouteSegment(w *database.Workout) {
2424
<div class="inner-form">
2525
{{
2626
type mapPoint struct {
27-
Lat float64 `json:"lat"`
28-
Lng float64 `json:"lng"`
29-
TotalDistance float64 `json:"distance"`
27+
Lat *float64 `json:"lat"`
28+
Lng *float64 `json:"lng"`
29+
TotalDistance *float64 `json:"distance"`
3030
}
3131

3232
points := []mapPoint{}
3333
for _, p := range w.Details().Points {
34+
if p.IsZero() {
35+
if len(points) > 0 {
36+
points = append(points, points[len(points)-1])
37+
}
38+
continue
39+
}
40+
3441
points = append(points, mapPoint{
35-
Lat: p.Lat,
36-
Lng: p.Lng,
37-
TotalDistance: p.TotalDistance,
42+
Lat: &p.Lat,
43+
Lng: &p.Lng,
44+
TotalDistance: &p.TotalDistance,
3845
})
3946
}
4047
}}
@@ -74,7 +81,7 @@ templ CreateRouteSegment(w *database.Workout) {
7481
id="start"
7582
name="start"
7683
min="1"
77-
max={ helpers.A2S(len(w.Details().Points)) }
84+
max={ helpers.A2S(len(points)) }
7885
value="1"
7986
oninput="getElementById('segment-map').updateStart()"
8087
/>
@@ -90,8 +97,8 @@ templ CreateRouteSegment(w *database.Workout) {
9097
id="end"
9198
name="end"
9299
min="1"
93-
max={ helpers.A2S(len(w.Details().Points)) }
94-
value={ helpers.A2S(len(w.Details().Points)) }
100+
max={ helpers.A2S(len(points)) }
101+
value={ helpers.A2S(len(points)) }
95102
oninput="getElementById('segment-map').updateEnd()"
96103
/>
97104
<span id="end-show"></span>
@@ -115,6 +122,17 @@ templ CreateRouteSegment(w *database.Workout) {
115122
</tr>
116123
</tfoot>
117124
</table>
125+
<script>
126+
const urlParams = new URLSearchParams(window.location.search);
127+
const start = urlParams.get("start");
128+
if (start) {
129+
document.getElementById("start").value = start;
130+
}
131+
const end = urlParams.get("end");
132+
if (end) {
133+
document.getElementById("end").value = end;
134+
}
135+
</script>
118136
</form>
119137
</div>
120138
</div>

views/workouts/create_route_segment_templ.go

Lines changed: 29 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

views/workouts/show.templ

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/jovandeginste/workout-tracker/v2/views/helpers"
77
"github.com/jovandeginste/workout-tracker/v2/views/partials"
88
"math"
9+
"strconv"
910
)
1011

1112
templ Show(w *database.Workout) {
@@ -117,7 +118,8 @@ templ renderNotes(w *database.Workout) {
117118
}
118119

119120
templ renderClimbs(w *database.Workout) {
120-
{{ pu := helpers.CurrentUser(ctx).PreferredUnits() }}
121+
{{ currentUser := helpers.CurrentUser(ctx) }}
122+
{{ pu := currentUser.PreferredUnits() }}
121123
<div class="print:w-full overflow-y-auto max-h-[300px] sm:max-h-[400px] md:max-h-[600px] print:max-h-[600px]">
122124
<table>
123125
<thead>
@@ -151,6 +153,7 @@ templ renderClimbs(w *database.Workout) {
151153
@helpers.IconFor("slope_cat")
152154
{ i18n.T(ctx, "climb.category") }
153155
</th>
156+
<th></th>
154157
</tr>
155158
</thead>
156159
<tbody class="font-mono">
@@ -184,6 +187,14 @@ templ renderClimbs(w *database.Workout) {
184187
{ helpers.SpeedUnitForWorkout(ctx, w) }
185188
</td>
186189
<td class="hidden sm:table-cell">{ climb.Category }</td>
190+
<td class="text-right">
191+
<a
192+
href={ templ.SafeURL(helpers.RouteFor(ctx, "workout-route-segment", w.ID) + "?start=" + strconv.Itoa(climb.StartIdx+1) + "&end=" + strconv.Itoa(climb.EndIdx+1)) }
193+
title={ i18n.T(ctx, "translation.Create_route_segment_from_climb") }
194+
>
195+
@helpers.IconFor("route-segment-add")
196+
</a>
197+
</td>
187198
</tr>
188199
}
189200
</tbody>

0 commit comments

Comments
 (0)