Add configuration

This commit is contained in:
Correl Roush 2018-02-01 18:28:31 -05:00
parent e9a22cb1e8
commit 9db12cc731
5 changed files with 246 additions and 26 deletions

View file

@ -1,7 +1,6 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<body> <body>
<h1>Pass</h1>
<div id="app"></div> <div id="app"></div>
<script type="text/javascript" src="elm.js"></script> <script type="text/javascript" src="elm.js"></script>
<script type="text/javascript" src="portal.js"></script> <script type="text/javascript" src="portal.js"></script>

View file

@ -1,2 +1,20 @@
const container = document.querySelector('#app'); const container = document.querySelector('#app');
const app = Elm.App.embed(container); 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);
});

View file

@ -1,32 +1,37 @@
module App exposing (..) module App exposing (..)
import Config
import Gitlab import Gitlab
import Html import Html
import Html.Events as Events
import Paginated import Paginated
import Ports
import RemoteData import RemoteData
import Task 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 = type alias ObjectsResponse =
RemoteData.WebData (List Gitlab.Object) RemoteData.WebData (List Gitlab.Object)
type alias Model = type alias State =
{ objects : ObjectsResponse } { client : Gitlab.Client
, repo : Gitlab.Repo
, objects : ObjectsResponse
}
type Model
= Loading
| Configuring Ports.Config
| Configured State
type Msg type Msg
= GetObjects = Loaded (Maybe Ports.Config)
| UpdateConfig Config.Msg
| SaveConfig
| Configure
| GotObjects ObjectsResponse | GotObjects ObjectsResponse
@ -36,22 +41,85 @@ main =
{ init = init { init = init
, update = update , update = update
, view = view , view = view
, subscriptions = \_ -> Sub.none , subscriptions = subscriptions
} }
init : ( Model, Cmd Msg ) init : ( Model, Cmd Msg )
init = init =
( { objects = RemoteData.NotAsked } ( Loading
, getObjects , 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 -> ( Model, Cmd Msg )
update msg model = update msg model =
case msg of case ( msg, model ) of
GotObjects objects -> ( Loaded loaded, Loading ) ->
( { model | objects = objects } ( 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 , Cmd.none
) )
@ -61,10 +129,23 @@ update msg model =
view : Model -> Html.Html Msg view : Model -> Html.Html Msg
view model = view model =
Html.body [] case model of
[ Html.h1 [] [ Html.text "Files" ] Loading ->
, viewFiles model.objects 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 viewFiles : ObjectsResponse -> Html.Html Msg
@ -99,8 +180,13 @@ viewFile file =
Html.text file.path Html.text file.path
getObjects : Cmd Msg getConfig : Cmd Msg
getObjects = getConfig =
Cmd.none
getObjects : Gitlab.Client -> Gitlab.Repo -> Cmd Msg
getObjects client repo =
Gitlab.getObjects repo client Gitlab.getObjects repo client
|> Paginated.toTask |> Paginated.toTask
|> RemoteData.fromTask |> RemoteData.fromTask

99
src/Config.elm Normal file
View file

@ -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
]

18
src/Ports.elm Normal file
View file

@ -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