mi/sina/src/Main.elm

184 lines
5.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
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
import Mi.PackageTracking.OrangeConnex
2020-11-16 02:52:38 +00:00
import Mi.Switch
import Mi.WebMention
2020-11-16 20:25:48 +00:00
import Model exposing (Model, Msg(..), get, init)
2020-11-16 03:09:53 +00:00
import Page.Index
import Page.Login
import Page.OrangeConnex
import Page.Packages
2020-11-16 17:35:33 +00:00
import Page.SwitchInfo
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-16 20:25:48 +00:00
let
if_okay : Result Http.Error a -> (a -> ( Model, Cmd Msg )) -> ( Model, Cmd Msg )
if_okay result doer =
case result of
Ok data ->
doer data
Err why ->
( { model | error = Just <| Mi.errorToString why }, Cmd.none )
in
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
2020-11-16 20:25:48 +00:00
[ get model Mi.tokenIntrospectURL <|
Mi.expectJson ValidateToken Mi.tokenDecoder
, get model Mi.Switch.frontURL <|
Mi.expectJson ValidateFront Mi.Switch.decoder
, get model (Mi.Switch.listURL 40 model.switchPage) <|
Mi.expectJson ValidateSwitches <|
Json.Decode.list Mi.Switch.decoder
2020-11-16 15:10:57 +00:00
]
)
FetchSwitch id ->
2020-11-16 02:52:38 +00:00
( model
2020-11-16 20:25:48 +00:00
, get model (Mi.Switch.idURL id) <|
Mi.expectJson ValidateSwitchByID Mi.Switch.decoder
2020-11-16 15:10:57 +00:00
)
2020-11-16 16:26:03 +00:00
NextSwitchesPage ->
( { model | switchPage = model.switchPage + 1 }
2020-11-16 20:25:48 +00:00
, get model (Mi.Switch.listURL 40 <| model.switchPage + 1) <|
Mi.expectJson ValidateSwitches <|
Json.Decode.list Mi.Switch.decoder
2020-11-16 16:26:03 +00:00
)
PrevSwitchesPage ->
( { model | switchPage = model.switchPage - 1 }
2020-11-16 20:25:48 +00:00
, get model (Mi.Switch.listURL 40 <| model.switchPage - 1) <|
Mi.expectJson ValidateSwitches <|
Json.Decode.list Mi.Switch.decoder
2020-11-16 02:52:38 +00:00
)
FetchOCPackages ->
( model
, get model Mi.PackageTracking.OrangeConnex.packageListURL <|
Mi.expectJson ValidateOCPackages <|
Json.Decode.list Mi.PackageTracking.OrangeConnex.decodePackage
)
FetchOCTraces trackingID ->
( { model | ocTrackingID = Just trackingID }
, get model (Mi.PackageTracking.OrangeConnex.packageStatusURL trackingID) <|
Mi.expectJson ValidateOCTraces <|
Json.Decode.list Mi.PackageTracking.OrangeConnex.decodeTrace
)
2020-11-16 15:10:57 +00:00
ValidateSwitchByID result ->
2020-11-16 20:25:48 +00:00
if_okay result <|
\data ->
2020-11-16 15:10:57 +00:00
( { model | switchByID = Just data }, Cmd.none )
ValidateSwitches result ->
2020-11-16 20:25:48 +00:00
if_okay result <|
\data ->
2020-11-16 15:10:57 +00:00
( { model | switches = data }, Cmd.none )
ValidateFront result ->
2020-11-16 20:25:48 +00:00
if_okay result <|
\data ->
2020-11-16 15:10:57 +00:00
( { model | front = Just data }, Cmd.none )
2020-11-16 02:52:38 +00:00
ValidateToken result ->
2020-11-16 20:25:48 +00:00
if_okay result <|
\data ->
2020-11-16 02:52:38 +00:00
( { model | tokenData = Just data }
, Nav.pushUrl model.navKey "/"
)
2020-11-15 21:08:41 +00:00
ValidateOCPackages result ->
if_okay result <|
\data ->
( { model | ocPackages = Just data }, Cmd.none )
ValidateOCTraces result ->
if_okay result <|
\data ->
( { model | ocTraces = Just data }, Cmd.none )
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 17:35:33 +00:00
SwitchID _ ->
Page.SwitchInfo.view model
Packages ->
Page.Packages.view
OCPackages ->
Page.OrangeConnex.viewList model
OCPackage packageID ->
Page.OrangeConnex.viewPackage { model | ocTrackingID = Just packageID }
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
}