tripleo-ui/src/js/actions/WorkflowExecutionsActions.js
Jiri Tomasek 03b835bcdb MistralApiService, SwiftApiService error handling
* Introduces axios for API requests
* Introduces error handling refactoring which is based on handling
  errors directly in *ApiService and creating standardized Error types
  which can be handled in action thunks. handleErrors action is
  generally used to do so, but any thunk can use custom error handling
  if needed. This approach allows thunks to chain API call promises to
  different API services and catch them all in single catch statement at
  the end of the chain
* Refactors Action thunks which use Mistral and Swift API to use chain
  of promises which are handled in single catch statement
* updates MistralApiService, SwiftApiService to support new error
  handling

Closes-Bug: 1663243
Change-Id: I4e60ae8d1b29ceaf497a99313d0e2fbc6d4d1068
2017-08-03 16:49:11 +02:00

99 lines
2.8 KiB
JavaScript

/**
* Copyright 2017 Red Hat Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
import { normalize, arrayOf } from 'normalizr';
import { handleErrors } from './ErrorActions';
import MistralApiService from '../services/MistralApiService';
import WorkflowExecutionsConstants
from '../constants/WorkflowExecutionsConstants';
import {
workflowExecutionSchema
} from '../normalizrSchemas/workflowExecutions';
export default {
fetchWorkflowExecutions() {
return (dispatch, getState) => {
dispatch(this.fetchWorkflowExecutionsPending());
MistralApiService.getWorkflowExecutions()
.then(response => {
const executions = normalize(
response,
arrayOf(workflowExecutionSchema)
).entities.executions || {};
dispatch(this.fetchWorkflowExecutionsSuccess(executions));
})
.catch(error => {
dispatch(
handleErrors(error, 'Workflow Executions could not be loaded')
);
dispatch(this.fetchWorkflowExecutionsFailed());
});
};
},
fetchWorkflowExecutionsPending() {
return {
type: WorkflowExecutionsConstants.FETCH_WORKFLOW_EXECUTIONS_PENDING
};
},
fetchWorkflowExecutionsSuccess(executions) {
return {
type: WorkflowExecutionsConstants.FETCH_WORKFLOW_EXECUTIONS_SUCCESS,
payload: executions
};
},
fetchWorkflowExecutionsFailed() {
return {
type: WorkflowExecutionsConstants.FETCH_WORKFLOW_EXECUTIONS_FAILED
};
},
addWorkflowExecution(execution) {
return {
type: WorkflowExecutionsConstants.ADD_WORKFLOW_EXECUTION,
payload: execution
};
},
updateWorkflowExecution(id, patch) {
return (dispatch, getState) => {
dispatch(this.updateWorkflowExecutionPending(id, patch));
MistralApiService.updateWorkflowExecution(id, patch)
.then(response => {
dispatch(this.addWorkflowExecution(response));
})
.catch(error => {
dispatch(
handleErrors(error, 'Workflow Execution could not be updated')
);
});
};
},
updateWorkflowExecutionPending(id, patch) {
return {
type: WorkflowExecutionsConstants.UPDATE_WORKFLOW_EXECUTION_PENDING,
payload: {
id,
patch
}
};
}
};