mi/sina/src/Main.elm

147 lines
4.3 KiB
Elm
Raw Normal View History

2020-11-15 21:53:45 +00:00
module Main exposing (main)
2020-11-15 20:27:09 +00:00
2020-11-16 02:52:38 +00:00
import Browser exposing (Document, UrlRequest(..))
import Browser.Navigation as Nav
import Html exposing (Html, a, br, button, div, h1, img, input, p, pre, span, text)
import Html.Attributes exposing (href, placeholder, src, value)
import Html.Events exposing (onClick, onInput)
2020-11-15 21:08:41 +00:00
import Http
2020-11-15 21:53:45 +00:00
import Layout
2020-11-15 21:08:41 +00:00
import Mi
2020-11-16 02:52:38 +00:00
import Mi.Switch
import Mi.WebMention
import Route exposing (Route(..), routeParser)
import Url exposing (Url)
import Url.Parser as UrlParser exposing ((</>))
{-| All of the data that the app can hold.
-}
type alias Model =
{ navKey : Nav.Key
, route : Maybe Route
, token : Maybe String
, tokenData : Maybe Mi.TokenData
, error : Maybe String
}
init : () -> Url -> Nav.Key -> ( Model, Cmd Msg )
init _ url key =
( { navKey = key
, route = UrlParser.parse routeParser url
, token = Nothing
, tokenData = Nothing
, error = Nothing
}
, Cmd.none
2020-11-15 21:08:41 +00:00
)
2020-11-15 20:27:09 +00:00
type Msg
2020-11-16 02:52:38 +00:00
= ChangeUrl Url
| ClickLink UrlRequest
| UpdateToken String
| SubmitToken
| ValidateToken (Result Http.Error Mi.TokenData)
| ClearError
2020-11-15 20:27:09 +00:00
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
2020-11-15 21:08:41 +00:00
case msg of
2020-11-16 02:52:38 +00:00
UpdateToken newToken ->
( { model | token = Just newToken }, Cmd.none )
ChangeUrl url ->
( { model | route = UrlParser.parse routeParser url }, Cmd.none )
SubmitToken ->
( model
, Mi.request
"GET"
(Maybe.withDefault "" model.token)
Mi.tokenIntrospectURL
Http.emptyBody
(Mi.expectJson ValidateToken Mi.tokenDecoder)
)
ValidateToken result ->
2020-11-15 21:08:41 +00:00
case result of
2020-11-16 02:52:38 +00:00
Ok data ->
( { model | tokenData = Just data }
, Nav.pushUrl model.navKey "/"
)
2020-11-15 21:08:41 +00:00
Err why ->
2020-11-16 02:52:38 +00:00
( { model | error = Just <| Mi.errorToString why }, Cmd.none )
2020-11-15 20:27:09 +00:00
2020-11-16 02:52:38 +00:00
ClickLink urlRequest ->
case urlRequest of
Internal url ->
( model, Nav.pushUrl model.navKey <| Url.toString url )
2020-11-15 20:27:09 +00:00
2020-11-16 02:52:38 +00:00
External url ->
( model, Nav.load url )
ClearError ->
( { model | error = Nothing, token = Nothing }, Cmd.none )
2020-11-15 21:08:41 +00:00
2020-11-16 02:52:38 +00:00
view : Model -> Document Msg
view model =
case model.error of
Nothing ->
case Maybe.withDefault Index model.route of
Index ->
case model.tokenData of
Nothing ->
Layout.basic "Login Required" []
Just data ->
Layout.template "Mi"
[ p
[]
[ span
[]
[ text "Subscriber: "
, text data.sub
, br [] []
, text "Token ID: "
, text data.jti
, br [] []
, text "Issuer: "
, text data.iss
]
]
]
Login ->
Layout.basic "Login"
[ p [] [ text "Enter the secret code. Unauthorized access is prohibited." ]
, input [ placeholder "API Token", value (Maybe.withDefault "" model.token), onInput UpdateToken ] []
, button [ onClick SubmitToken ] [ text "Login" ]
]
_ ->
Debug.todo "implement routing"
Just why ->
Layout.basic
"Error"
[ p [] [ text why ]
, a [ onClick ClearError, href "/" ] [ text "Clear error" ]
2020-11-15 21:08:41 +00:00
]
2020-11-15 20:27:09 +00:00
main : Program () Model Msg
main =
2020-11-16 02:52:38 +00:00
Browser.application
2020-11-15 20:27:09 +00:00
{ view = view
2020-11-15 21:08:41 +00:00
, init = init
2020-11-15 20:27:09 +00:00
, update = update
, subscriptions = always Sub.none
2020-11-16 02:52:38 +00:00
, onUrlRequest = ClickLink
, onUrlChange = ChangeUrl
2020-11-15 20:27:09 +00:00
}