Last active
May 9, 2020 16:25
-
-
Save simenbrekken/de69d3ce27ea5934c8b2 to your computer and use it in GitHub Desktop.
Category Actions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var createAsyncActions = require('./createAsyncActions') | |
var CategoryAPI = require('../api/CategoryAPI') | |
var CategoryActions = createAsyncActions({ | |
create: function(category) { | |
CategoryAPI | |
.create(category) | |
.then(CategoryActions.createCompleted) | |
.catch(function(error) { | |
CategoryActions.createFailed(error, category) | |
}) | |
}, | |
update: function(category) { | |
CategoryAPI | |
.update(category) | |
.then(CategoryActions.updateCompleted) | |
.catch(function(error) { | |
CategoryActions.updateFailed(error, category) | |
}) | |
}, | |
delete: function(category) { | |
CategoryAPI | |
.delete(category) | |
.then(function() { | |
CategoryActions.deleteCompleted(category) | |
}) | |
.catch(function(error) { | |
CategoryActions.deleteFailed(error, category) | |
}) | |
} | |
}) | |
module.exports = CategoryActions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var createAction = require('reflux').createAction | |
var lodash = require('lodash') | |
function createAsyncActions(definitions) { | |
return lodash.reduce(definitions, function(actions, callback, name) { | |
actions[name] = createAction() | |
actions[name].listen(callback) | |
actions[name + 'Completed'] = createAction() | |
actions[name + 'Failed'] = createAction() | |
return actions | |
}, {}) | |
} | |
module.exports = createAsyncActions |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
CategoryAPI
basically produces HTTP REST Promises that reject onstatus >= 300
.