mi-v1/frontend/src/Main.elm

236 lines
6.3 KiB
Elm
Raw Normal View History

2020-01-13 00:02:11 +00:00
module Main exposing (main)
2020-01-12 19:05:49 +00:00
import Browser
import Browser.Navigation as Nav
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (onInput)
import Http
import Json.Decode as D
2020-01-13 00:02:11 +00:00
import Skeleton exposing (..)
2020-01-12 23:31:49 +00:00
import SwitchData
2020-01-12 19:05:49 +00:00
import Url
2020-01-12 23:31:49 +00:00
import Url.Builder
2020-01-12 19:05:49 +00:00
main : Program () Model Msg
main =
2020-01-12 23:31:49 +00:00
Browser.application
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
, onUrlChange = UrlChanged
, onUrlRequest = LinkClicked
}
2020-01-12 19:05:49 +00:00
type alias Model =
2020-01-12 23:31:49 +00:00
{ key : Nav.Key
, url : Url.Url
, token : Maybe String
, token_data : Maybe TokenData
, switch_cursor : ( Int, Int )
, switch_data : Maybe (List SwitchData.Switch)
}
2020-01-12 19:05:49 +00:00
init : () -> Url.Url -> Nav.Key -> ( Model, Cmd Msg )
init flags url key =
2020-01-12 23:31:49 +00:00
( Model
key
url
Nothing
Nothing
( 40, 0 )
Nothing
, Cmd.none
)
2020-01-12 19:05:49 +00:00
type Msg
2020-01-12 23:31:49 +00:00
= LinkClicked Browser.UrlRequest
| UrlChanged Url.Url
| TokenInput String
| TokenValidate (Result Http.Error TokenData)
| Logout
| GetSwitchData ( Int, Int )
| GotSwitchData (Result Http.Error (List SwitchData.Switch))
2020-01-12 19:05:49 +00:00
2020-01-12 23:31:49 +00:00
type alias TokenData =
{ sub : String
2020-01-12 19:05:49 +00:00
, jti : String
}
2020-01-12 23:31:49 +00:00
2020-01-12 19:05:49 +00:00
tokenDecoder : D.Decoder TokenData
tokenDecoder =
2020-01-12 23:31:49 +00:00
D.map2 TokenData
(D.field "sub" D.string)
(D.field "jti" D.string)
2020-01-12 19:05:49 +00:00
request method token path body expect =
2020-01-12 23:31:49 +00:00
Http.request
{ method = method
, body = body
, headers =
[ Http.header "Authorization" token
]
, url = path
, expect = expect
, timeout = Nothing
, tracker = Nothing
}
2020-01-12 19:05:49 +00:00
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
2020-01-12 23:31:49 +00:00
case msg of
LinkClicked urlRequest ->
case urlRequest of
Browser.Internal url ->
( model, Nav.pushUrl model.key (Url.toString url) )
Browser.External href ->
( model, Nav.load href )
UrlChanged url ->
case url.path of
"/logout" ->
( model, Nav.load "/" )
default ->
( { model | url = url }
, Cmd.none
)
TokenInput token ->
( { model | token = Just token }
, request "GET" token "/.within/tokeninfo" Http.emptyBody (expectJson TokenValidate tokenDecoder)
)
TokenValidate result ->
case result of
Ok data ->
( { model | token_data = Just data }
, Cmd.none
)
Err _ ->
( { model | token = Nothing }
, Cmd.none
)
Logout ->
( { model | token = Nothing, token_data = Nothing }
, Nav.load "/"
)
GetSwitchData ( limit, page ) ->
case model.token of
Just token ->
( model
, request "GET"
token
(SwitchData.dataURL limit page)
Http.emptyBody
(expectJson GotSwitchData (D.list SwitchData.decoder))
)
Nothing ->
( model, Cmd.none )
_ ->
( model, Cmd.none )
2020-01-12 19:05:49 +00:00
subscriptions : Model -> Sub Msg
subscriptions _ =
2020-01-12 23:31:49 +00:00
Sub.none
2020-01-12 19:05:49 +00:00
view : Model -> Browser.Document Msg
view model =
2020-01-12 23:31:49 +00:00
case model.token_data of
Nothing ->
{ title = "Login"
, body =
[ node "main"
[ style "align" "center" ]
[ h1 [] [ text "Login" ]
, viewInput "password" "API Token" "" TokenInput
]
]
}
Just token_data ->
case model.url.path of
"/" ->
template "Mi"
[ h1 [] [ text "Mi" ]
, h2 [] [ text "TODO" ]
, ul []
[ li [] [ text "Switch CRUD" ]
, li [] [ text "POSSE manual announcement" ]
]
, h2 [] [ text "Token data" ]
, p []
[ text "Token sub: "
, text token_data.sub
, Html.br [] []
, text "ID: "
, text token_data.jti
]
]
"/switch" ->
case model.switch_data of
Nothing ->
template "Switch counter"
[ h1 [] [ text "Switch counter" ]
, p [] [ text "loading..." ]
]
Just switches ->
template "Switch counter"
[ h1 [] [ text "TODO: table" ]
]
other ->
template "Not found"
[ h1 [] [ text "Not found" ]
, p []
[ text "The requested URL "
, b [] [ text other ]
, text " was not found."
]
]
2020-01-12 19:05:49 +00:00
expectJson : (Result Http.Error a -> msg) -> D.Decoder a -> Http.Expect msg
expectJson toMsg decoder =
2020-01-12 23:31:49 +00:00
Http.expectStringResponse toMsg <|
\response ->
case response of
Http.BadUrl_ url ->
Err (Http.BadUrl url)
2020-01-12 19:05:49 +00:00
2020-01-12 23:31:49 +00:00
Http.Timeout_ ->
Err Http.Timeout
2020-01-12 19:05:49 +00:00
2020-01-12 23:31:49 +00:00
Http.NetworkError_ ->
Err Http.NetworkError
2020-01-12 19:05:49 +00:00
2020-01-12 23:31:49 +00:00
Http.BadStatus_ metadata body ->
Err (Http.BadStatus metadata.statusCode)
2020-01-12 19:05:49 +00:00
2020-01-12 23:31:49 +00:00
Http.GoodStatus_ metadata body ->
case D.decodeString decoder body of
Ok value ->
Ok value
2020-01-12 19:05:49 +00:00
2020-01-12 23:31:49 +00:00
Err err ->
Err (Http.BadBody (D.errorToString err))