mi-v1/frontend/src/Main.elm

186 lines
4.7 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 01:23:46 +00:00
import Mi exposing (..)
import Page exposing (..)
2020-01-15 12:08:40 +00:00
import Page.SwitchData as PSD
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
2020-01-15 12:08:40 +00:00
, switch_data_model : PSD.Model
2020-01-12 23:31:49 +00:00
}
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
2020-01-15 12:08:40 +00:00
PSD.init
2020-01-12 23:31:49 +00:00
, Cmd.none
)
2020-01-12 19:05:49 +00:00
2020-01-15 22:40:17 +00:00
type Page
= NotFound
| Index
| SwitchData PSD.Model
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
2020-01-15 12:08:40 +00:00
| GotSwitchDataMSG PSD.Msg
2020-01-12 23:31:49 +00:00
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 ->
2020-01-13 01:23:46 +00:00
( model
, Nav.pushUrl
model.key
(Url.toString url)
)
2020-01-12 23:31:49 +00:00
Browser.External href ->
( model, Nav.load href )
UrlChanged url ->
case url.path of
"/logout" ->
2020-01-14 23:41:11 +00:00
( { model | token = Nothing, token_data = Nothing }
, Nav.pushUrl
model.key
"/"
)
2020-01-12 23:31:49 +00:00
default ->
( { model | url = url }
, Cmd.none
)
TokenInput token ->
( { model | token = Just token }
2020-01-13 01:23:46 +00:00
, request
"GET"
token
"/.within/tokeninfo"
Http.emptyBody
(expectJson TokenValidate tokenDecoder)
2020-01-12 23:31:49 +00:00
)
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 "/"
)
2020-01-15 22:40:17 +00:00
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
)
2020-01-15 12:08:40 +00:00
2020-01-12 23:31:49 +00:00
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
]
]
2020-01-15 22:40:17 +00:00
"/switch" ->
PSD.view model.switch_data_model
2020-01-12 23:31:49 +00:00
other ->
template "Not found"
[ h1 [] [ text "Not found" ]
, p []
[ text "The requested URL "
, b [] [ text other ]
, text " was not found."
]
]