diff --git a/frontend/src/Main.elm b/frontend/src/Main.elm index efb81af..5f10542 100644 --- a/frontend/src/Main.elm +++ b/frontend/src/Main.elm @@ -9,6 +9,7 @@ import Http import Json.Decode as D import Mi exposing (..) import Page exposing (..) +import Page.SwitchData as PSD import SwitchData import Url import Url.Builder @@ -31,6 +32,7 @@ type alias Model = , url : Url.Url , token : Maybe String , token_data : Maybe TokenData + , switch_data_model : PSD.Model } @@ -41,6 +43,7 @@ init flags url key = url Nothing Nothing + PSD.init , Cmd.none ) @@ -51,6 +54,7 @@ type Msg | TokenInput String | TokenValidate (Result Http.Error TokenData) | Logout + | GotSwitchDataMSG PSD.Msg update : Msg -> Model -> ( Model, Cmd Msg ) @@ -109,6 +113,9 @@ update msg model = , Nav.load "/" ) + _ -> + ( model, Cmd.none ) + subscriptions : Model -> Sub Msg subscriptions _ = diff --git a/frontend/src/Page/SwitchData.elm b/frontend/src/Page/SwitchData.elm new file mode 100644 index 0000000..ea0613f --- /dev/null +++ b/frontend/src/Page/SwitchData.elm @@ -0,0 +1,98 @@ +module Page.SwitchData exposing + ( Model + , Msg + , init + , update + , view + ) + +import Browser exposing (Document) +import Html exposing (..) +import Html.Attributes exposing (..) +import Http +import Json.Decode exposing (list) +import Mi +import Page +import SwitchData + + +type alias Model = + { page : Int + , limit : Int + , data : Data + } + + +type Data + = Init + | HaveData (List SwitchData.Switch) + | Error String + + +init : Model +init = + Model + 0 + 40 + Init + + +type Msg + = NeedData + | Settings Int Int + | GotData (Result Http.Error (List SwitchData.Switch)) + + +update : String -> Msg -> Model -> ( Model, Cmd Msg ) +update token msg model = + case msg of + NeedData -> + ( model + , Mi.request + "GET" + token + (SwitchData.listURL model.limit model.page) + Http.emptyBody + (Mi.expectJson GotData (Json.Decode.list SwitchData.decoder)) + ) + + Settings page limit -> + ( { model | page = page, limit = limit } + , Mi.request + "GET" + token + (SwitchData.listURL model.limit model.page) + Http.emptyBody + (Mi.expectJson GotData (Json.Decode.list SwitchData.decoder)) + ) + + GotData result -> + case result of + Ok data -> + ( { model | data = HaveData data } + , Cmd.none + ) + + Err _ -> + ( { model | data = Error "got an error" } + , Cmd.none + ) + + +view : Model -> Browser.Document msg +view model = + case model.data of + Init -> + Page.template "Switch data" + [ h1 [] [ text "loading data..." ] ] + + HaveData _ -> + Page.template "Switch data" + [ h1 [] [ text "Switch data here" ] + ] + + Error msg -> + Page.template "Switch data error" + [ h1 [] [ text "oh no got an error" ] + , p [] [ text msg ] + ] diff --git a/frontend/src/SwitchData.elm b/frontend/src/SwitchData.elm index 3465ba3..a5447fd 100644 --- a/frontend/src/SwitchData.elm +++ b/frontend/src/SwitchData.elm @@ -1,7 +1,10 @@ module SwitchData exposing ( Switch - , dataURL , decoder + , frontURL + , idURL + , listURL + , switchURL ) import Iso8601 @@ -29,8 +32,29 @@ decoder = (field "duration" int) -dataURL : Int -> Int -> String -dataURL limit page = +switchURL : String +switchURL = + UB.absolute + [ "switches", "switch" ] + [] + + +idURL : String -> String +idURL id = + UB.absolute + [ "switches", "id", id ] + [] + + +frontURL : String +frontURL = + UB.absolute + [ "switches", "current" ] + [] + + +listURL : Int -> Int -> String +listURL limit page = UB.absolute [ "switches", "" ] [ UB.int "limit" limit