diff --git a/src/Database/Sync.elm b/src/Database/Sync.elm index 7807a11..5a2b699 100644 --- a/src/Database/Sync.elm +++ b/src/Database/Sync.elm @@ -1,9 +1,10 @@ -module Database.Sync exposing (Sync, decoder, encode, isValid, sorter) +module Database.Sync exposing (Sync, decoder, encode, isValid, sorter, toUrl) import Json.Decode as Decode exposing (Decoder) import Json.Decode.Pipeline as Pipeline exposing (required) import Json.Encode as Encode import Sort exposing (Sorter) +import Url type alias Sync = @@ -16,7 +17,7 @@ type alias Sync = isValid : Sync -> Bool isValid { host, database, username, password } = - (host /= "") + (Url.fromString host /= Nothing) && (database /= "") && (username /= "") && (password /= "") @@ -47,3 +48,21 @@ encode sync = , ( "username", Encode.string sync.username ) , ( "password", Encode.string sync.password ) ] + + +toUrl : Sync -> Maybe String +toUrl { host, database, username, password } = + Url.fromString host + |> Maybe.map + (\url -> + { url + | host = + if username /= "" || password /= "" then + username ++ ":" ++ password ++ "@" ++ url.host + + else + url.host + , path = "/" ++ database + } + ) + |> Maybe.map Url.toString diff --git a/src/Main.elm b/src/Main.elm index dc9f88f..4b17116 100644 --- a/src/Main.elm +++ b/src/Main.elm @@ -82,7 +82,12 @@ init flags url key = , draftSync = Nothing } in - ( model, routingEffects ) + ( model + , Batch + [ routingEffects + , Batch (List.map StartSyncing settings.syncs) + ] + ) flagsDecoder : @@ -150,6 +155,7 @@ type Effect | ReplaceUrl Route | Put Value | FocusOnEditor + | StartSyncing Sync type UpdatedRevision @@ -541,7 +547,10 @@ update msg model = Settings.insertSync draftSync model.settings in ( { model | draftSync = Nothing, settings = newSettings } - , Put (Settings.encode newSettings) + , Batch + [ Put (Settings.encode newSettings) + , StartSyncing draftSync + ] ) else @@ -609,10 +618,21 @@ perform model effect = (\_ -> FocusedOnEditor) (Dom.focus "editor") + StartSyncing sync -> + case Sync.toUrl sync of + Just url -> + startSyncing url + + Nothing -> + Cmd.none + port put : Value -> Cmd msg +port startSyncing : String -> Cmd msg + + port putSuccessfully : (Value -> msg) -> Sub msg diff --git a/src/index.js b/src/index.js index b452252..7a9ca66 100644 --- a/src/index.js +++ b/src/index.js @@ -55,11 +55,15 @@ var db = new PouchDB("notes"); } }); - // set up ports + // saving app.ports.put.subscribe(item => { - console.log(item) db.put(item) .then(success => app.ports.putSuccessfully.send(success)) .catch(err => console.error(err)); }); + + // syncing + app.ports.startSyncing.subscribe(url => { + console.log(url) + }) })(); diff --git a/tests/MainTest.elm b/tests/MainTest.elm index 4867475..23f2743 100644 --- a/tests/MainTest.elm +++ b/tests/MainTest.elm @@ -65,6 +65,9 @@ testPerform effect = FocusOnEditor -> SCmd.none + StartSyncing _ -> + SCmd.none + programTest : Test programTest =