simplify the code

This commit is contained in:
Cadey Ratio 2020-11-16 15:25:48 -05:00
parent c6b7c338d0
commit 6d9aa3b064
3 changed files with 48 additions and 60 deletions

View File

@ -38,8 +38,9 @@ template title body =
, a [ href "/posse" ] [ text "POSSE" ] , a [ href "/posse" ] [ text "POSSE" ]
, text " - " , text " - "
, a [ href "/switches" ] [ text "Switches" ] , a [ href "/switches" ] [ text "Switches" ]
, text " - "
, a [ href "/webmentions" ] [ text "WebMentions" ] --, text " - "
--, a [ href "/webmentions" ] [ text "WebMentions" ]
] ]
, h1 [] [ text title ] , h1 [] [ text title ]
] ]

View File

@ -11,7 +11,7 @@ import Layout
import Mi import Mi
import Mi.Switch import Mi.Switch
import Mi.WebMention import Mi.WebMention
import Model exposing (Model, Msg(..), init) import Model exposing (Model, Msg(..), get, init)
import Page.Index import Page.Index
import Page.Login import Page.Login
import Page.SwitchInfo import Page.SwitchInfo
@ -23,6 +23,16 @@ import Url.Parser as UrlParser
update : Msg -> Model -> ( Model, Cmd Msg ) update : Msg -> Model -> ( Model, Cmd Msg )
update msg model = update msg model =
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
case msg of case msg of
UpdateToken newToken -> UpdateToken newToken ->
( { model | token = Just newToken }, Cmd.none ) ( { model | token = Just newToken }, Cmd.none )
@ -33,91 +43,58 @@ update msg model =
SubmitToken -> SubmitToken ->
( model ( model
, Cmd.batch , Cmd.batch
[ Mi.request [ get model Mi.tokenIntrospectURL <|
"GET" Mi.expectJson ValidateToken Mi.tokenDecoder
(Maybe.withDefault "" model.token) , get model Mi.Switch.frontURL <|
Mi.tokenIntrospectURL Mi.expectJson ValidateFront Mi.Switch.decoder
Http.emptyBody , get model (Mi.Switch.listURL 40 model.switchPage) <|
(Mi.expectJson ValidateToken Mi.tokenDecoder) Mi.expectJson ValidateSwitches <|
, Mi.request Json.Decode.list Mi.Switch.decoder
"GET"
(Maybe.withDefault "" model.token)
Mi.Switch.frontURL
Http.emptyBody
(Mi.expectJson ValidateFront Mi.Switch.decoder)
, Mi.request
"GET"
(Maybe.withDefault "" model.token)
(Mi.Switch.listURL 40 model.switchPage)
Http.emptyBody
(Mi.expectJson ValidateSwitches (Json.Decode.list Mi.Switch.decoder))
] ]
) )
FetchSwitch id -> FetchSwitch id ->
( model ( model
, Mi.request , get model (Mi.Switch.idURL id) <|
"GET" Mi.expectJson ValidateSwitchByID Mi.Switch.decoder
(Maybe.withDefault "" model.token)
(Mi.Switch.idURL id)
Http.emptyBody
(Mi.expectJson ValidateSwitchByID Mi.Switch.decoder)
) )
NextSwitchesPage -> NextSwitchesPage ->
( { model | switchPage = model.switchPage + 1 } ( { model | switchPage = model.switchPage + 1 }
, Mi.request , get model (Mi.Switch.listURL 40 <| model.switchPage + 1) <|
"GET" Mi.expectJson ValidateSwitches <|
(Maybe.withDefault "" model.token) Json.Decode.list Mi.Switch.decoder
(Mi.Switch.listURL 40 <| model.switchPage + 1)
Http.emptyBody
(Mi.expectJson ValidateSwitches (Json.Decode.list Mi.Switch.decoder))
) )
PrevSwitchesPage -> PrevSwitchesPage ->
( { model | switchPage = model.switchPage - 1 } ( { model | switchPage = model.switchPage - 1 }
, Mi.request , get model (Mi.Switch.listURL 40 <| model.switchPage - 1) <|
"GET" Mi.expectJson ValidateSwitches <|
(Maybe.withDefault "" model.token) Json.Decode.list Mi.Switch.decoder
(Mi.Switch.listURL 40 <| model.switchPage - 1)
Http.emptyBody
(Mi.expectJson ValidateSwitches (Json.Decode.list Mi.Switch.decoder))
) )
ValidateSwitchByID result -> ValidateSwitchByID result ->
case result of if_okay result <|
Ok data -> \data ->
( { model | switchByID = Just data }, Cmd.none ) ( { model | switchByID = Just data }, Cmd.none )
Err why ->
( { model | error = Just <| Mi.errorToString why }, Cmd.none )
ValidateSwitches result -> ValidateSwitches result ->
case result of if_okay result <|
Ok data -> \data ->
( { model | switches = data }, Cmd.none ) ( { model | switches = data }, Cmd.none )
Err why ->
( { model | error = Just <| Mi.errorToString why }, Cmd.none )
ValidateFront result -> ValidateFront result ->
case result of if_okay result <|
Ok data -> \data ->
( { model | front = Just data }, Cmd.none ) ( { model | front = Just data }, Cmd.none )
Err why ->
( { model | error = Just <| Mi.errorToString why }, Cmd.none )
ValidateToken result -> ValidateToken result ->
case result of if_okay result <|
Ok data -> \data ->
( { model | tokenData = Just data } ( { model | tokenData = Just data }
, Nav.pushUrl model.navKey "/" , Nav.pushUrl model.navKey "/"
) )
Err why ->
( { model | error = Just <| Mi.errorToString why }, Cmd.none )
ClickLink urlRequest -> ClickLink urlRequest ->
case urlRequest of case urlRequest of
Internal url -> Internal url ->

View File

@ -1,4 +1,4 @@
module Model exposing (Model, Msg(..), init) module Model exposing (Model, Msg(..), get, init)
import Browser exposing (UrlRequest(..)) import Browser exposing (UrlRequest(..))
import Browser.Navigation as Nav import Browser.Navigation as Nav
@ -27,6 +27,16 @@ type alias Model =
} }
get : Model -> String -> Http.Expect Msg -> Cmd Msg
get model url action =
Mi.request
"GET"
(Maybe.withDefault "" model.token)
url
Http.emptyBody
action
type Msg type Msg
= ChangeUrl Url = ChangeUrl Url
| ClickLink UrlRequest | ClickLink UrlRequest