Skip to content

Commit

Permalink
set up a port to start syncing
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHicks committed May 18, 2020
1 parent c047785 commit 35278a8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
23 changes: 21 additions & 2 deletions 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 =
Expand All @@ -16,7 +17,7 @@ type alias Sync =

isValid : Sync -> Bool
isValid { host, database, username, password } =
(host /= "")
(Url.fromString host /= Nothing)
&& (database /= "")
&& (username /= "")
&& (password /= "")
Expand Down Expand Up @@ -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
24 changes: 22 additions & 2 deletions src/Main.elm
Expand Up @@ -82,7 +82,12 @@ init flags url key =
, draftSync = Nothing
}
in
( model, routingEffects )
( model
, Batch
[ routingEffects
, Batch (List.map StartSyncing settings.syncs)
]
)


flagsDecoder :
Expand Down Expand Up @@ -150,6 +155,7 @@ type Effect
| ReplaceUrl Route
| Put Value
| FocusOnEditor
| StartSyncing Sync


type UpdatedRevision
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand Down
8 changes: 6 additions & 2 deletions src/index.js
Expand Up @@ -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)
})
})();
3 changes: 3 additions & 0 deletions tests/MainTest.elm
Expand Up @@ -65,6 +65,9 @@ testPerform effect =
FocusOnEditor ->
SCmd.none

StartSyncing _ ->
SCmd.none


programTest : Test
programTest =
Expand Down

0 comments on commit 35278a8

Please sign in to comment.