Skip to content

Commit

Permalink
save captures when offline, #8
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonLab committed Jan 8, 2020
1 parent 7ab35e3 commit cc6acd6
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 26 deletions.
30 changes: 16 additions & 14 deletions elm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5321,12 +5321,12 @@ var $elm$core$Task$perform = F2(
var $elm$browser$Browser$application = _Browser_application;
var $author$project$Main$Model = F5(
function (key, url, capture, message, online) {
return {y: capture, X: key, E: message, H: online, M: url};
return {y: capture, X: key, E: message, F: online, M: url};
});
var $elm$core$Platform$Cmd$batch = _Platform_batch;
var $elm$core$Platform$Cmd$none = $elm$core$Platform$Cmd$batch(_List_Nil);
var $author$project$Main$init = F3(
function (flags, url, key) {
function (_v0, url, key) {
return _Utils_Tuple2(
A5($author$project$Main$Model, key, url, '', '', true),
$elm$core$Platform$Cmd$none);
Expand Down Expand Up @@ -6167,15 +6167,17 @@ var $elm$http$Http$post = function (r) {
return $elm$http$Http$request(
{O: r.O, az: r.az, U: _List_Nil, aE: 'POST', aP: $elm$core$Maybe$Nothing, ao: $elm$core$Maybe$Nothing, M: r.M});
};
var $author$project$Main$saveCapture = function (capture) {
return $elm$http$Http$post(
{
O: $elm$http$Http$jsonBody(
$author$project$Main$captureEncode(capture)),
az: A2($elm$http$Http$expectJson, $author$project$Main$SaveCaptureResult, $author$project$Main$captureDecoder),
M: 'https://dwylapp.herokuapp.com/api/captures/create'
});
};
var $author$project$Main$pouchDB = _Platform_outgoingPort('pouchDB', $elm$json$Json$Encode$string);
var $author$project$Main$saveCapture = F2(
function (appOnline, capture) {
return appOnline ? $elm$http$Http$post(
{
O: $elm$http$Http$jsonBody(
$author$project$Main$captureEncode(capture)),
az: A2($elm$http$Http$expectJson, $author$project$Main$SaveCaptureResult, $author$project$Main$captureDecoder),
M: 'https://dwylapp.herokuapp.com/api/captures/create'
}) : $author$project$Main$pouchDB(capture);
});
var $elm$url$Url$addPort = F2(
function (maybePort, starter) {
if (maybePort.$ === 1) {
Expand Down Expand Up @@ -6256,7 +6258,7 @@ var $author$project$Main$update = F2(
case 3:
return _Utils_Tuple2(
model,
$author$project$Main$saveCapture(model.y));
A2($author$project$Main$saveCapture, model.F, model.y));
case 4:
if (!msg.a.$) {
var response = msg.a.a;
Expand All @@ -6278,7 +6280,7 @@ var $author$project$Main$update = F2(
return _Utils_Tuple2(
_Utils_update(
model,
{H: status}),
{F: status}),
$elm$core$Platform$Cmd$none);
}
});
Expand Down Expand Up @@ -6420,7 +6422,7 @@ var $author$project$Main$view = function (model) {
_List_fromArray(
[
$elm$html$Html$text(model.E),
$author$project$Main$onlineView(model.H),
$author$project$Main$onlineView(model.F),
A2(
$elm$html$Html$h1,
_List_fromArray(
Expand Down
10 changes: 9 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
<body class="bg-near-white helvetica">
<noscript>
<span>This website is using Elm, so it needs js enabled</span>
</noscript>
</noscript>
<script src="//cdn.jsdelivr.net/npm/pouchdb@7.1.1/dist/pouchdb.min.js"></script>
<script src="./pouchdb.js"></script>
<script>
var app = Elm.Main.init();

Expand All @@ -37,6 +39,12 @@
app.ports.online.send(false);
}, false);

app.ports.pouchDB.subscribe(function(capture) {
console.log("capture offline", capture)
saveCapture(capture)
});

</script>

</body>
</html>
19 changes: 19 additions & 0 deletions pouchdb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var db = new PouchDB('dwyl');

function saveCapture(capture) {
var capture = {
_id: new Date().toISOString(),
text: capture
};
db.put(capture, function callback(err, result) {
if (!err) {
console.log('Successfully saved capture!');
}
});
}

function showCaptures() {
db.allDocs({include_docs: true, descending: true}, function(err, doc) {
console.log(doc.rows)
});
}
26 changes: 15 additions & 11 deletions src/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type alias Model =


init : () -> Url.Url -> Nav.Key -> ( Model, Cmd Msg )
init flags url key =
init _ url key =
( Model key url "" "" True, Cmd.none )


Expand Down Expand Up @@ -78,7 +78,7 @@ update msg model =
( { model | capture = text }, Cmd.none )

CreateCapture ->
( model, saveCapture model.capture )
( model, saveCapture model.online model.capture )

SaveCaptureResult (Ok response) ->
( { model | capture = "", message = "Capture saved" }, Cmd.none )
Expand All @@ -94,6 +94,7 @@ update msg model =
-- SUBSCRIPTIONS

port online : (Bool -> msg) -> Sub msg
port pouchDB : String -> Cmd msg

subscriptions : Model -> Sub Msg
subscriptions _ =
Expand Down Expand Up @@ -129,7 +130,7 @@ view model =
]
]
}

onlineView : Bool -> Html Msg
onlineView onlineStatus =
div [classList [("dn", onlineStatus)]] [
Expand All @@ -140,14 +141,17 @@ onlineView onlineStatus =
-- Capture


saveCapture : String -> Cmd Msg
saveCapture capture =
Http.post
{ url = "https://dwylapp.herokuapp.com/api/captures/create"
, body = Http.jsonBody (captureEncode capture)
, expect = Http.expectJson SaveCaptureResult captureDecoder
}

saveCapture : Bool -> String -> Cmd Msg
saveCapture appOnline capture =
if appOnline then
Http.post
{ url = "https://dwylapp.herokuapp.com/api/captures/create"
, body = Http.jsonBody (captureEncode capture)
, expect = Http.expectJson SaveCaptureResult captureDecoder
}
else
-- if not online save the item in PouchDB via ports
pouchDB capture

captureEncode : String -> JE.Value
captureEncode capture =
Expand Down

0 comments on commit cc6acd6

Please sign in to comment.