0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-13 21:42:45 +00:00

Finally testing, things should be working a bit better now

This commit is contained in:
Scott Tolksdorf
2017-01-01 13:31:33 -08:00
parent 95c09ba7ad
commit 5ba3f98696
8 changed files with 70 additions and 84 deletions

View File

@@ -1,3 +1,4 @@
const _ = require('lodash');
const dispatch = require('pico-flux').dispatch;
const request = require('superagent');
@@ -6,6 +7,42 @@ const Store = require('./brew.store.js');
let pendingTimer;
const PENDING_TIMEOUT = 3000;
const APIActions = {
save : () => {
clearTimeout(pendingTimer);
const brew = Store.getBrew();
dispatch('SET_STATUS', 'saving');
request
.put('/api/brew/' + brew.editId)
.send(brew)
.end((err, res) => {
if(err) return dispatch('SET_STATUS', 'error', err);
dispatch('SET_BREW', res.body);
dispatch('SET_STATUS', 'ready');
});
},
create : () => {
dispatch('SET_STATUS', 'saving');
request.post('/api/brew')
.send(Store.getBrew())
.end((err, res)=>{
if(err) return dispatch('SET_STATUS', 'error', err);
localStorage.setItem('homebrewery-new', null);
const brew = res.body;
window.location = '/edit/' + brew.editId;
});
},
delete : (editId) => {
dispatch('SET_STATUS', 'deleting');
request.delete('/api/brew/' + editId)
.send()
.end((err, res)=>{
if(err) return dispatch('SET_STATUS', 'error', err);
window.location = '/';
});
}
}
const Actions = {
init : (initState) => {
Store.init(initState);
@@ -21,33 +58,10 @@ const Actions = {
},
pendingSave : () => {
clearTimeout(pendingTimer);
pendingTimer = setTimeout(Actions.save, PENDING_TIMEOUT);
pendingTimer = setTimeout(APIActions.save, PENDING_TIMEOUT);
dispatch('SET_STATUS', 'pending');
},
save : () => {
clearTimeout(pendingTimer);
const brew = Store.getBrew();
dispatch('SET_STATUS', 'saving');
request
.put('/api/update/' + brew.editId)
.send(brew)
.end((err, res) => {
if(err) return dispatch('SET_STATUS', 'error', err);
dispatch('SET_BREW', res.body);
dispatch('SET_STATUS', 'ready');
});
},
saveNew : () => {
dispatch('SET_STATUS', 'saving');
request.post('/api')
.send(Store.getBrew())
.end((err, res)=>{
if(err) return dispatch('SET_STATUS', 'error', err);
localStorage.setItem('homebrewery-new', null);
const brew = res.body;
window.location = '/edit/' + brew.editId;
});
},
localPrint : ()=>{
localStorage.setItem('print', Store.getBrewText());
window.open('/print?dialog=true&local=print','_blank');
@@ -57,4 +71,4 @@ const Actions = {
}
};
module.exports = Actions;
module.exports = _.merge(Actions, APIActions);