mi/sina/src/Main.elm

94 lines
2.6 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
2020-11-16 03:09:53 +00:00
import Model exposing (Model, Msg(..), init)
import Page.Index
import Page.Login
2020-11-16 02:52:38 +00:00
import Route exposing (Route(..), routeParser)
import Url exposing (Url)
import Url.Parser as UrlParser exposing ((</>))
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 ->
2020-11-16 03:09:53 +00:00
( { model | error = 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 ->
2020-11-16 03:09:53 +00:00
Page.Index.view model
2020-11-16 02:52:38 +00:00
Login ->
2020-11-16 03:09:53 +00:00
Page.Login.view model
2020-11-16 02:52:38 +00:00
_ ->
Debug.todo "implement routing"
Just why ->
Layout.basic
"Error"
2020-11-16 03:09:53 +00:00
[ p [] [ text why, text ". Please clear the error to proceed." ]
2020-11-16 02:52:38 +00:00
, 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
}