Scylla/src/Main.elm

82 lines
2.4 KiB
Elm
Raw Normal View History

import Browser exposing (application)
import Browser.Navigation as Nav
2018-12-08 13:49:30 -08:00
import Scylla.Sync exposing (..)
2018-12-08 15:06:14 -08:00
import Scylla.Login exposing (..)
2018-12-08 13:49:30 -08:00
import Scylla.Model exposing (..)
import Scylla.Http exposing (..)
2018-12-08 15:06:14 -08:00
import Scylla.Views exposing (viewFull)
import Url exposing (Url)
2018-12-08 13:49:30 -08:00
import Html exposing (div, text)
2018-12-08 15:06:14 -08:00
import Http
type alias Flags =
{ token : Maybe String
}
init : Flags -> Url -> Nav.Key -> (Model, Cmd Msg)
init flags url key =
let
model =
{ key = key
, token = flags.token
2018-12-08 15:06:14 -08:00
, loginUsername = ""
, loginPassword = ""
2018-12-08 13:49:30 -08:00
, apiUrl = "https://matrix.org"
2018-12-08 17:15:35 -08:00
, sync =
{ nextBatch = ""
, rooms = Nothing
, presence = Nothing
, accountData = Nothing
}
, errors = []
}
cmd = case flags.token of
Just _ -> Cmd.none
Nothing -> Cmd.none
in
(model, cmd)
view : Model -> Browser.Document Msg
view m =
{ title = "Scylla"
2018-12-08 17:15:35 -08:00
, body = viewFull m
}
update : Msg -> Model -> (Model, Cmd Msg)
2018-12-08 15:06:14 -08:00
update msg model = case msg of
ChangeApiUrl u -> ({ model | apiUrl = u }, Cmd.none)
ChangeLoginUsername u -> ({ model | loginUsername = u }, Cmd.none)
ChangeLoginPassword p -> ({ model | loginPassword = p }, Cmd.none)
AttemptLogin -> (model, Scylla.Http.login model.apiUrl model.loginUsername model.loginPassword) -- TODO
ReceiveLoginResponse r -> updateLoginResponse model r
ReceiveSyncResponse r -> updateSyncResponse model r
_ -> (model, Cmd.none)
updateLoginResponse : Model -> Result Http.Error LoginResponse -> (Model, Cmd Msg)
updateLoginResponse model r = case r of
Ok lr -> ( { model | token = Just lr.accessToken } , firstSync model.apiUrl lr.accessToken )
Err e -> (model, Cmd.none)
updateSyncResponse : Model -> Result Http.Error SyncResponse -> (Model, Cmd Msg)
2018-12-08 17:15:35 -08:00
updateSyncResponse model r = let sync = model.sync in case r of
Ok sr -> ({ model | sync = mergeSyncResponse model.sync sr }, Cmd.none)
_ -> (model, Cmd.none)
subscriptions : Model -> Sub Msg
subscriptions m = Sub.none
onUrlRequest : Browser.UrlRequest -> Msg
onUrlRequest = TryUrl
onUrlChange : Url -> Msg
onUrlChange = ChangeUrl
main = application
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
, onUrlRequest = onUrlRequest
, onUrlChange = onUrlChange
}