Skip to content

Commit

Permalink
added task functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
MV88 committed Oct 24, 2016
1 parent eeb7fd5 commit 222f785
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
48 changes: 48 additions & 0 deletions web/client/actions/tasks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright 2016, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
const TASK_STARTED = 'TASK_STARTED';
const TASK_SUCCESS = 'TASK_SUCCESS';
const TASK_ERROR = 'TASK_ERROR';

function taskSuccess(result, name, actionPayload) {
return {
type: TASK_SUCCESS,
result,
name,
actionPayload
};
}

function taskStarted(name) {
return {
type: TASK_STARTED,
name
};
}

function taskError(error, name) {
return {
type: TASK_ERROR,
error,
name
};
}

function startTask(task, taskPayload, name, actionPayload) {
return (dispatch) => {
dispatch(taskStarted(name));
task(taskPayload, (result) => {
dispatch(taskSuccess(result, name, actionPayload));
}, (error) => {
dispatch(taskError(error, name, actionPayload));
});
};
}


module.exports = {TASK_STARTED, TASK_SUCCESS, TASK_ERROR, startTask};
48 changes: 48 additions & 0 deletions web/client/reducers/tasks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Copyright 2016, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

var { TASK_STARTED, TASK_SUCCESS, TASK_ERROR } = require('../actions/tasks');
var assign = require('object-assign');

function tasks(state = {}, action) {
switch (action.type) {
case TASK_STARTED: {
return assign({}, state, {
[action.name]: {
running: true
}
});
}
case TASK_SUCCESS: {
return assign({}, state, {
[action.name]: {
actionPayload: action.actionPayload,
name: action.name,
result: action.result,
running: false,
success: true
}
});
}
case TASK_ERROR: {
return assign({}, state, {
[action.name]: {
actionPayload: action.actionPayload,
error: action.error,
name: action.name,
running: false,
success: false
}
});
}
default:
return state;
}
}

module.exports = tasks;

0 comments on commit 222f785

Please sign in to comment.