From 85c040023dead7209b3aef6d9c5c585ae7f576c0 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Tue, 12 Nov 2024 18:52:44 +0000 Subject: [PATCH] fe: extract Point into a module --- frontend/src/Main.elm | 36 ++---------------------------------- frontend/src/Point.elm | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 34 deletions(-) create mode 100644 frontend/src/Point.elm diff --git a/frontend/src/Main.elm b/frontend/src/Main.elm index bb7774e..c5a7df6 100644 --- a/frontend/src/Main.elm +++ b/frontend/src/Main.elm @@ -9,6 +9,7 @@ import Html.Events.Extra.Pointer as Pointer import Maybe exposing (Maybe) import Json.Decode as D import Http +import Point exposing(Point, Pos ,decoder) import Svg exposing (Svg, svg, rect, circle, g, polyline) import Svg.Attributes as S exposing ( viewBox @@ -153,42 +154,9 @@ fetchTrack start duration = Http.get String.fromInt start ++ "&duration=" ++ String.fromInt duration) - , expect = Http.expectJson Loaded trackDecoder + , expect = Http.expectJson Loaded (D.list Point.decoder) } -type alias Pos = - { lat : Float - , lon : Float - , ele : Maybe Float - } - - -type alias Point = - { time : Float - , pos : Pos - , cadence : Maybe Int - , power : Maybe Int - , heartRate : Maybe Int - } - -posDecoder : D.Decoder Pos -posDecoder = D.map3 Pos - (D.field "lat" D.float) - (D.field "lon" D.float) - (D.field "ele" (D.maybe D.float)) - - -pointDecoder : D.Decoder Point -pointDecoder = D.map5 Point - (D.field "time" D.float) - (D.field "pos" posDecoder) - (D.field "cadence" (D.maybe D.int)) - (D.field "power" (D.maybe D.int)) - (D.field "heartRate" (D.maybe D.int)) - -trackDecoder : D.Decoder (List Point) -trackDecoder = D.list pointDecoder - -- UPDATE diff --git a/frontend/src/Point.elm b/frontend/src/Point.elm new file mode 100644 index 0000000..690b331 --- /dev/null +++ b/frontend/src/Point.elm @@ -0,0 +1,33 @@ +module Point exposing(Pos, Point, decoder) + +import Json.Decode as D + +type alias Pos = + { lat : Float + , lon : Float + , ele : Maybe Float + } + + +type alias Point = + { time : Float + , pos : Pos + , cadence : Maybe Int + , power : Maybe Int + , heartRate : Maybe Int + } + +posDecoder : D.Decoder Pos +posDecoder = D.map3 Pos + (D.field "lat" D.float) + (D.field "lon" D.float) + (D.field "ele" (D.maybe D.float)) + + +decoder : D.Decoder Point +decoder = D.map5 Point + (D.field "time" D.float) + (D.field "pos" posDecoder) + (D.field "cadence" (D.maybe D.int)) + (D.field "power" (D.maybe D.int)) + (D.field "heartRate" (D.maybe D.int))