From 9db12cc73157ce8967d9cf36f1f91549250f8c74 Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Thu, 1 Feb 2018 18:28:31 -0500 Subject: [PATCH] Add configuration --- extension/index.html | 1 - extension/portal.js | 18 ++++++ src/App.elm | 136 +++++++++++++++++++++++++++++++++++-------- src/Config.elm | 99 +++++++++++++++++++++++++++++++ src/Ports.elm | 18 ++++++ 5 files changed, 246 insertions(+), 26 deletions(-) create mode 100644 src/Config.elm create mode 100644 src/Ports.elm diff --git a/extension/index.html b/extension/index.html index 457c35e..2b63557 100644 --- a/extension/index.html +++ b/extension/index.html @@ -1,7 +1,6 @@ -

Pass

diff --git a/extension/portal.js b/extension/portal.js index 248b77e..a3748a2 100644 --- a/extension/portal.js +++ b/extension/portal.js @@ -1,2 +1,20 @@ const container = document.querySelector('#app'); const app = Elm.App.embed(container); + +app.ports.loadConfig.subscribe(() => { + chrome.storage.sync.get( + [ + 'server', + 'token', + 'owner', + 'repo' + ], + (config) => { + app.ports.config.send(Object.keys(config).length < 4 + ? null : config); + }); +}); + +app.ports.saveConfig.subscribe((config) => { + chrome.storage.sync.set(config); +}); diff --git a/src/App.elm b/src/App.elm index 97d4243..03e38ed 100644 --- a/src/App.elm +++ b/src/App.elm @@ -1,32 +1,37 @@ module App exposing (..) +import Config import Gitlab import Html +import Html.Events as Events import Paginated +import Ports import RemoteData import Task -repo : Gitlab.Repo -repo = - Gitlab.Repo "correlr" "password-store" - - -client : Gitlab.Client -client = - Gitlab.Client "http://git.phoenixinquis.net" (Just "AuGrRWBdXfSJbm36itUG") - - type alias ObjectsResponse = RemoteData.WebData (List Gitlab.Object) -type alias Model = - { objects : ObjectsResponse } +type alias State = + { client : Gitlab.Client + , repo : Gitlab.Repo + , objects : ObjectsResponse + } + + +type Model + = Loading + | Configuring Ports.Config + | Configured State type Msg - = GetObjects + = Loaded (Maybe Ports.Config) + | UpdateConfig Config.Msg + | SaveConfig + | Configure | GotObjects ObjectsResponse @@ -36,22 +41,85 @@ main = { init = init , update = update , view = view - , subscriptions = \_ -> Sub.none + , subscriptions = subscriptions } init : ( Model, Cmd Msg ) init = - ( { objects = RemoteData.NotAsked } - , getObjects + ( Loading + , Ports.loadConfig () ) +subscriptions : Model -> Sub Msg +subscriptions _ = + Ports.config Loaded + + +loadConfig : Ports.Config -> Maybe State +loadConfig cfg = + Maybe.map2 + (\client repo -> + { client = client + , repo = repo + , objects = RemoteData.NotAsked + } + ) + (Config.toClient cfg) + (Config.toRepo cfg) + + update : Msg -> Model -> ( Model, Cmd Msg ) update msg model = - case msg of - GotObjects objects -> - ( { model | objects = objects } + case ( msg, model ) of + ( Loaded loaded, Loading ) -> + ( Debug.log "loaded" loaded + |> Maybe.andThen loadConfig + |> Maybe.map Configured + |> Maybe.withDefault (Configuring Config.init) + |> Debug.log "configured" + , Cmd.none + ) + + ( UpdateConfig update, Configuring cfg ) -> + ( Configuring <| + Config.update update cfg + , Cmd.none + ) + + ( SaveConfig, Configuring cfg ) -> + let + validated = + Maybe.map2 + (\client repo -> + { client = client + , repo = repo + , objects = RemoteData.NotAsked + } + ) + (Config.toClient cfg) + (Config.toRepo cfg) + in + case validated of + Just state -> + ( Configured state + , Cmd.batch + [ Ports.saveConfig cfg + , getObjects state.client state.repo + ] + ) + + Nothing -> + ( Configuring cfg, Cmd.none ) + + ( Configure, Configured state ) -> + ( Configuring (Config.fromGitlab state.client state.repo) + , Cmd.none + ) + + ( GotObjects objects, Configured state ) -> + ( Configured { state | objects = objects } , Cmd.none ) @@ -61,10 +129,23 @@ update msg model = view : Model -> Html.Html Msg view model = - Html.body [] - [ Html.h1 [] [ Html.text "Files" ] - , viewFiles model.objects - ] + case model of + Loading -> + Html.div [] [ Html.text "Loading..." ] + + Configuring cfg -> + Html.div [] + [ Html.map UpdateConfig (Config.view cfg) + , Html.hr [] [] + , Html.button [ Events.onClick SaveConfig ] [ Html.text "Save" ] + ] + + Configured state -> + Html.div [] + [ Html.button [ Events.onClick Configure ] [ Html.text "Configure" ] + , Html.hr [] [] + , viewFiles state.objects + ] viewFiles : ObjectsResponse -> Html.Html Msg @@ -99,8 +180,13 @@ viewFile file = Html.text file.path -getObjects : Cmd Msg -getObjects = +getConfig : Cmd Msg +getConfig = + Cmd.none + + +getObjects : Gitlab.Client -> Gitlab.Repo -> Cmd Msg +getObjects client repo = Gitlab.getObjects repo client |> Paginated.toTask |> RemoteData.fromTask diff --git a/src/Config.elm b/src/Config.elm new file mode 100644 index 0000000..b7e2382 --- /dev/null +++ b/src/Config.elm @@ -0,0 +1,99 @@ +module Config exposing (..) + +import Gitlab +import Html +import Html.Attributes as Attr +import Html.Events as Events +import Ports + + +type Msg + = UpdateServer String + | UpdateToken String + | UpdateOwner String + | UpdateRepo String + + +type alias Model = + Ports.Config + + +init : Model +init = + { server = "" + , token = "" + , owner = "" + , repo = "" + } + + +maybeIf : (a -> Bool) -> a -> Maybe a +maybeIf f x = + if f x then + Just x + else + Nothing + + +fromGitlab : Gitlab.Client -> Gitlab.Repo -> Model +fromGitlab client repo = + { server = client.url + , token = Maybe.withDefault "" client.token + , owner = repo.owner + , repo = repo.name + } + + +toClient : Model -> Maybe Gitlab.Client +toClient config = + Maybe.map + (flip Gitlab.Client <| + maybeIf (not << String.isEmpty) config.token + ) + (maybeIf (not << String.isEmpty) config.server) + + +toRepo : Model -> Maybe Gitlab.Repo +toRepo config = + Maybe.map2 Gitlab.Repo + (maybeIf (not << String.isEmpty) config.owner) + (maybeIf (not << String.isEmpty) config.repo) + + +update : Msg -> Model -> Model +update msg config = + case msg of + UpdateServer server -> + { config | server = server } + + UpdateToken token -> + { config | token = token } + + UpdateOwner owner -> + { config | owner = owner } + + UpdateRepo repo -> + { config | repo = repo } + + +view : Model -> Html.Html Msg +view config = + let + input name value update = + Html.label [] + [ Html.text name + , Html.input + [ Attr.name name + , Attr.value value + , Events.onInput update + ] + [] + ] + in + Html.div [] + [ Html.h1 [] [ Html.text "Settings" ] + , input "Server" config.server UpdateServer + , input "Token" config.token UpdateToken + , input "Owner" config.owner UpdateOwner + , input "Repo" config.repo UpdateRepo + ] diff --git a/src/Ports.elm b/src/Ports.elm new file mode 100644 index 0000000..5f6fc80 --- /dev/null +++ b/src/Ports.elm @@ -0,0 +1,18 @@ +port module Ports exposing (..) + + +type alias Config = + { server : String + , token : String + , owner : String + , repo : String + } + + +port loadConfig : () -> Cmd msg + + +port saveConfig : Config -> Cmd msg + + +port config : (Maybe Config -> msg) -> Sub msg