diff --git a/frontend/src/Layout.elm b/frontend/src/Layout.elm index 3444059..a2f3f60 100644 --- a/frontend/src/Layout.elm +++ b/frontend/src/Layout.elm @@ -43,7 +43,7 @@ viewHeader currentRoute = , width (fill |> maximum 750) ] [ viewLink currentRoute ( "Mi", routes.top ) - , viewLink currentRoute ( "Switch Data", routes.switches ) + , viewLink currentRoute ( "Switch Data", routes.switch ) ] diff --git a/frontend/src/Mi/SwitchData.elm b/frontend/src/Mi/SwitchData.elm index 66d99de..315081d 100644 --- a/frontend/src/Mi/SwitchData.elm +++ b/frontend/src/Mi/SwitchData.elm @@ -11,7 +11,7 @@ import Html exposing (..) import Html.Attributes exposing (..) import Iso8601 import Json.Decode exposing (Decoder, field, int, map5, nullable, string) -import Time exposing (Posix) +import Time exposing (..) import Url.Builder as UB diff --git a/frontend/src/Pages/Switch.elm b/frontend/src/Pages/Switch.elm new file mode 100644 index 0000000..fe6936e --- /dev/null +++ b/frontend/src/Pages/Switch.elm @@ -0,0 +1,252 @@ +module Pages.Switch exposing (Model, Msg, page) + +import Element exposing (..) +import Element.Font as Font +import Element.Input as Input +import Generated.Params as Params +import Global +import Http +import Iso8601 +import Json.Decode +import Mi +import Mi.SwitchData +import Spa.Page +import Utils.Spa exposing (Page, PageContext) + + +page : Page Params.Switch Model Msg model msg appMsg +page = + Spa.Page.component + { title = always "Switch Data" + , init = init + , update = update + , subscriptions = always subscriptions + , view = always view + } + + + +-- INIT + + +type alias Model = + { currentFront : Maybe Mi.SwitchData.Switch + , frontData : Maybe (List Mi.SwitchData.Switch) + , error : Maybe String + , amount : Int + , page : Int + } + + +init : PageContext -> Params.Switch -> ( Model, Cmd Msg, Cmd Global.Msg ) +init { global } _ = + let + model = + { currentFront = Nothing + , frontData = Nothing + , error = Nothing + , amount = 40 + , page = 0 + } + in + ( model + , Cmd.batch + [ Mi.request + "GET" + (Maybe.withDefault + "" + global.token + ) + Mi.SwitchData.frontURL + Http.emptyBody + (Mi.expectJson ValidateCurrentFront Mi.SwitchData.decoder) + , Mi.request + "GET" + (Maybe.withDefault + "" + global.token + ) + (Mi.SwitchData.listURL model.amount model.page) + Http.emptyBody + (Mi.expectJson ValidateFrontData (Json.Decode.list Mi.SwitchData.decoder)) + ] + , Cmd.none + ) + + + +-- UPDATE + + +type Msg + = ValidateCurrentFront (Result Http.Error Mi.SwitchData.Switch) + | ValidateFrontData (Result Http.Error (List Mi.SwitchData.Switch)) + | NextPage + | PrevPage + + +update : PageContext -> Msg -> Model -> ( Model, Cmd Msg, Cmd Global.Msg ) +update { global } msg model = + case msg of + PrevPage -> + ( { model | page = model.page - 1, frontData = Nothing } + , Mi.request + "GET" + (Maybe.withDefault + "" + global.token + ) + (Mi.SwitchData.listURL model.amount (model.page - 1)) + Http.emptyBody + (Mi.expectJson ValidateFrontData (Json.Decode.list Mi.SwitchData.decoder)) + , Cmd.none + ) + + NextPage -> + ( { model | page = model.page + 1, frontData = Nothing } + , Mi.request + "GET" + (Maybe.withDefault + "" + global.token + ) + (Mi.SwitchData.listURL model.amount (model.page + 1)) + Http.emptyBody + (Mi.expectJson ValidateFrontData (Json.Decode.list Mi.SwitchData.decoder)) + , Cmd.none + ) + + ValidateCurrentFront result -> + case result of + Ok data -> + ( { model | currentFront = Just data } + , Cmd.none + , Cmd.none + ) + + Err _ -> + ( { model | error = Just "can't fetch current front" } + , Cmd.none + , Cmd.none + ) + + ValidateFrontData result -> + case result of + Ok data -> + ( { model | frontData = Just data } + , Cmd.none + , Cmd.none + ) + + Err _ -> + ( { model | error = Just "can't fetch historical front data" } + , Cmd.none + , Cmd.none + ) + + + +-- SUBSCRIPTIONS + + +subscriptions : Model -> Sub Msg +subscriptions model = + Sub.none + + + +-- VIEW + + +view : Model -> Element Msg +view model = + let + currentFront = + case model.currentFront of + Just data -> + let + startTime = + Iso8601.fromTime data.started_at + in + column + [] + [ el [ Font.size 30 ] (text data.who) + , text ("started at " ++ startTime) + ] + + Nothing -> + text "Loading..." + + prevButton = + if model.page /= 0 then + Input.button [] { onPress = Just PrevPage, label = text "Prev" } + + else + none + + nextButton = + Input.button [] { onPress = Just NextPage, label = text "Next" } + + tableButtons = + row [] [ prevButton, nextButton ] + + switchDataTable = + case model.frontData of + Nothing -> + case model.currentFront of + Nothing -> + none + + Just _ -> + text "Loading..." + + Just data -> + column [] + [ table [] + { data = data + , columns = + [ { header = text "ID" + , width = fill + , view = + \switch -> + link + [ Font.underline + , Font.color (rgb255 204 75 75) + , mouseOver [ alpha 0.5 ] + ] + { label = text (String.slice 0 10 switch.id) + , url = "/switch/" ++ switch.id + } + } + , { header = text "Who" + , width = fill + , view = + \switch -> text switch.who + } + , { header = text "Start" + , width = fill + , view = + \switch -> el [ padding 4 ] (text (Iso8601.fromTime switch.started_at)) + } + , { header = text "End" + , width = fill + , view = + \switch -> + case switch.ended_at of + Nothing -> + none + + Just time -> + el [ padding 4 ] (text (Iso8601.fromTime time)) + } + ] + } + , tableButtons + ] + in + column + [] + [ currentFront + , el [ padding 15 ] none + , switchDataTable + ] diff --git a/frontend/src/Pages/Switches.elm b/frontend/src/Pages/Switches.elm deleted file mode 100644 index f47096e..0000000 --- a/frontend/src/Pages/Switches.elm +++ /dev/null @@ -1,70 +0,0 @@ -module Pages.Switches exposing (Model, Msg, page) - -import Element exposing (..) -import Generated.Params as Params -import Global -import Mi -import Mi.SwitchData -import Spa.Page -import Utils.Spa exposing (Page) - - -page : Page Params.Switches Model Msg model msg appMsg -page = - Spa.Page.component - { title = always "Switches" - , init = always init - , update = always update - , subscriptions = always subscriptions - , view = always view - } - - - --- INIT - - -type alias Model = - {} - - -init : Params.Switches -> ( Model, Cmd Msg, Cmd Global.Msg ) -init _ = - ( {} - , Cmd.none - , Cmd.none - ) - - - --- UPDATE - - -type Msg - = Msg - - -update : Msg -> Model -> ( Model, Cmd Msg, Cmd Global.Msg ) -update msg model = - ( model - , Cmd.none - , Cmd.none - ) - - - --- SUBSCRIPTIONS - - -subscriptions : Model -> Sub Msg -subscriptions model = - Sub.none - - - --- VIEW - - -view : Model -> Element Msg -view model = - text "Switches"