186 lines
4.7 KiB
Elm
186 lines
4.7 KiB
Elm
module Main exposing (main)
|
|
|
|
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
|
|
import Mi exposing (..)
|
|
import Page exposing (..)
|
|
import Page.SwitchData as PSD
|
|
import SwitchData
|
|
import Url
|
|
import Url.Builder
|
|
|
|
|
|
main : Program () Model Msg
|
|
main =
|
|
Browser.application
|
|
{ init = init
|
|
, view = view
|
|
, update = update
|
|
, subscriptions = subscriptions
|
|
, onUrlChange = UrlChanged
|
|
, onUrlRequest = LinkClicked
|
|
}
|
|
|
|
|
|
type alias Model =
|
|
{ key : Nav.Key
|
|
, url : Url.Url
|
|
, token : Maybe String
|
|
, token_data : Maybe TokenData
|
|
, switch_data_model : PSD.Model
|
|
}
|
|
|
|
|
|
init : () -> Url.Url -> Nav.Key -> ( Model, Cmd Msg )
|
|
init flags url key =
|
|
( Model
|
|
key
|
|
url
|
|
Nothing
|
|
Nothing
|
|
PSD.init
|
|
, Cmd.none
|
|
)
|
|
|
|
|
|
type Page
|
|
= NotFound
|
|
| Index
|
|
| SwitchData PSD.Model
|
|
|
|
|
|
type Msg
|
|
= LinkClicked Browser.UrlRequest
|
|
| UrlChanged Url.Url
|
|
| TokenInput String
|
|
| TokenValidate (Result Http.Error TokenData)
|
|
| Logout
|
|
| GotSwitchDataMSG PSD.Msg
|
|
|
|
|
|
update : Msg -> Model -> ( Model, Cmd Msg )
|
|
update msg model =
|
|
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 | token = Nothing, token_data = Nothing }
|
|
, Nav.pushUrl
|
|
model.key
|
|
"/"
|
|
)
|
|
|
|
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 "/"
|
|
)
|
|
|
|
GotSwitchDataMSG psd_msg ->
|
|
let
|
|
( psd_model, cmd ) =
|
|
PSD.update
|
|
(Maybe.withDefault "" model.token)
|
|
psd_msg
|
|
model.switch_data_model
|
|
in
|
|
( { model | switch_data_model = psd_model }
|
|
, Cmd.map GotSwitchDataMSG cmd
|
|
)
|
|
|
|
|
|
subscriptions : Model -> Sub Msg
|
|
subscriptions _ =
|
|
Sub.none
|
|
|
|
|
|
view : Model -> Browser.Document Msg
|
|
view model =
|
|
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" ->
|
|
PSD.view model.switch_data_model
|
|
|
|
other ->
|
|
template "Not found"
|
|
[ h1 [] [ text "Not found" ]
|
|
, p []
|
|
[ text "The requested URL "
|
|
, b [] [ text other ]
|
|
, text " was not found."
|
|
]
|
|
]
|