mi/sina/src/Main.elm

167 lines
5.0 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
2020-11-16 15:10:57 +00:00
import Html exposing (a, p, text)
import Html.Attributes exposing (href)
import Html.Events exposing (onClick)
2020-11-15 21:08:41 +00:00
import Http
2020-11-16 15:10:57 +00:00
import Json.Decode
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 16:26:03 +00:00
import Page.Switches
2020-11-16 02:52:38 +00:00
import Route exposing (Route(..), routeParser)
2020-11-16 15:10:57 +00:00
import Url
import Url.Parser as UrlParser
2020-11-16 02:52:38 +00:00
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 ->
2020-11-16 15:10:57 +00:00
( model
, Cmd.batch
[ Mi.request
"GET"
(Maybe.withDefault "" model.token)
Mi.tokenIntrospectURL
Http.emptyBody
(Mi.expectJson ValidateToken Mi.tokenDecoder)
, Mi.request
"GET"
(Maybe.withDefault "" model.token)
Mi.Switch.frontURL
Http.emptyBody
(Mi.expectJson ValidateFront Mi.Switch.decoder)
, Mi.request
"GET"
2020-11-16 15:13:04 +00:00
(Maybe.withDefault "" model.token)
2020-11-16 16:26:03 +00:00
(Mi.Switch.listURL 40 model.switchPage)
2020-11-16 15:10:57 +00:00
Http.emptyBody
(Mi.expectJson ValidateSwitches (Json.Decode.list Mi.Switch.decoder))
]
)
FetchSwitch id ->
2020-11-16 02:52:38 +00:00
( model
, Mi.request
"GET"
(Maybe.withDefault "" model.token)
2020-11-16 15:10:57 +00:00
(Mi.Switch.idURL id)
Http.emptyBody
(Mi.expectJson ValidateSwitchByID Mi.Switch.decoder)
)
2020-11-16 16:26:03 +00:00
NextSwitchesPage ->
( { model | switchPage = model.switchPage + 1 }
, Mi.request
"GET"
(Maybe.withDefault "" model.token)
(Mi.Switch.listURL 40 <| model.switchPage + 1)
Http.emptyBody
(Mi.expectJson ValidateSwitches (Json.Decode.list Mi.Switch.decoder))
)
PrevSwitchesPage ->
( { model | switchPage = model.switchPage - 1 }
2020-11-16 15:10:57 +00:00
, Mi.request
"GET"
2020-11-16 15:13:04 +00:00
(Maybe.withDefault "" model.token)
2020-11-16 16:26:03 +00:00
(Mi.Switch.listURL 40 <| model.switchPage - 1)
2020-11-16 02:52:38 +00:00
Http.emptyBody
2020-11-16 15:10:57 +00:00
(Mi.expectJson ValidateSwitches (Json.Decode.list Mi.Switch.decoder))
2020-11-16 02:52:38 +00:00
)
2020-11-16 15:10:57 +00:00
ValidateSwitchByID result ->
case result of
Ok data ->
( { model | switchByID = Just data }, Cmd.none )
Err why ->
( { model | error = Just <| Mi.errorToString why }, Cmd.none )
ValidateSwitches result ->
case result of
Ok data ->
( { model | switches = data }, Cmd.none )
Err why ->
( { model | error = Just <| Mi.errorToString why }, Cmd.none )
ValidateFront result ->
case result of
Ok data ->
( { model | front = Just data }, Cmd.none )
Err why ->
( { model | error = Just <| Mi.errorToString why }, Cmd.none )
2020-11-16 02:52:38 +00:00
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
2020-11-16 16:26:03 +00:00
SwitchLog ->
Page.Switches.view model
2020-11-16 15:10:57 +00:00
2020-11-16 02:52:38 +00:00
_ ->
2020-11-16 15:10:57 +00:00
Layout.template "Oh noes" [ p [] [ text "todo: implement this 404 page" ] ]
2020-11-16 02:52:38 +00:00
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
}