mi-v1/frontend/src/Page/SwitchData.elm

105 lines
2.3 KiB
Elm

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, Cmd Msg )
init =
( Model
0
40
Init
, NeedData
)
type Msg
= NeedData
| Settings Int Int
| GotData (Result Http.Error (List SwitchData.Switch))
update : String -> Msg -> Model -> ( Model, Cmd Msg )
update token msg model =
if token == "" then
( model, Cmd.none )
else
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 ]
]