attempt to make a switchdata page

This commit is contained in:
Cadey Ratio 2020-01-15 12:08:40 +00:00
parent f1e1ed7ca5
commit 4f5a969c34
3 changed files with 132 additions and 3 deletions

View File

@ -9,6 +9,7 @@ import Http
import Json.Decode as D import Json.Decode as D
import Mi exposing (..) import Mi exposing (..)
import Page exposing (..) import Page exposing (..)
import Page.SwitchData as PSD
import SwitchData import SwitchData
import Url import Url
import Url.Builder import Url.Builder
@ -31,6 +32,7 @@ type alias Model =
, url : Url.Url , url : Url.Url
, token : Maybe String , token : Maybe String
, token_data : Maybe TokenData , token_data : Maybe TokenData
, switch_data_model : PSD.Model
} }
@ -41,6 +43,7 @@ init flags url key =
url url
Nothing Nothing
Nothing Nothing
PSD.init
, Cmd.none , Cmd.none
) )
@ -51,6 +54,7 @@ type Msg
| TokenInput String | TokenInput String
| TokenValidate (Result Http.Error TokenData) | TokenValidate (Result Http.Error TokenData)
| Logout | Logout
| GotSwitchDataMSG PSD.Msg
update : Msg -> Model -> ( Model, Cmd Msg ) update : Msg -> Model -> ( Model, Cmd Msg )
@ -109,6 +113,9 @@ update msg model =
, Nav.load "/" , Nav.load "/"
) )
_ ->
( model, Cmd.none )
subscriptions : Model -> Sub Msg subscriptions : Model -> Sub Msg
subscriptions _ = subscriptions _ =

View File

@ -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 ]
]

View File

@ -1,7 +1,10 @@
module SwitchData exposing module SwitchData exposing
( Switch ( Switch
, dataURL
, decoder , decoder
, frontURL
, idURL
, listURL
, switchURL
) )
import Iso8601 import Iso8601
@ -29,8 +32,29 @@ decoder =
(field "duration" int) (field "duration" int)
dataURL : Int -> Int -> String switchURL : String
dataURL limit page = 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 UB.absolute
[ "switches", "" ] [ "switches", "" ]
[ UB.int "limit" limit [ UB.int "limit" limit