import { API_SERVER } from '../../config'; import { REQUEST_CUSTOMER_ACCEPTANCE, RECEIVE_CUSTOMER_ACCEPTANCE, UPLOAD_CUSTOMER_ACCEPTANCE, SEND_CUSTOMER_ACCEPTANCE, orderMessages } from '../../constants/ordersConstants'; import { updateMessages } from '../notification/notificationActions'; import HtmlClient from '../../helpers/HtmlClient'; import { fromWiaasCustomerAcceptance } from '../../helpers/ProcessHelper'; const htmlClient = new HtmlClient(); const requestCustomerAcceptance = () => ({ type: REQUEST_CUSTOMER_ACCEPTANCE }); const recieveCustomerAcceptance = (json) => ({ type: RECEIVE_CUSTOMER_ACCEPTANCE, customerAcceptance: json }); export const fetchCustomerAcceptance = (idOrder) => { return dispatch => { dispatch(requestCustomerAcceptance()); return htmlClient.fetch({ url: `${API_SERVER}/wp-json/wiaas/delivery/${idOrder}/customer-acceptance`, method: 'get' }) .then(response => { if (response.data) { dispatch( recieveCustomerAcceptance( fromWiaasCustomerAcceptance(response.data) ) ); } }) .catch(error => { htmlClient.onError(error, dispatch); }); } } const uploadAcceptanceAction = () => ({ type: UPLOAD_CUSTOMER_ACCEPTANCE }); export const uploadAcceptance = (idOrder, file) => { return dispatch => { dispatch(uploadAcceptanceAction()); return htmlClient.uploadFile(file, { url: `${API_SERVER}/wp-json/wiaas/delivery/${idOrder}/customer-acceptance/upload` }).then(response => { if (typeof response.data !== 'undefined') { dispatch(updateMessages(response.data.messages, orderMessages)); dispatch(fetchCustomerAcceptance(idOrder)); } }).catch(error => { htmlClient.onError(error, dispatch); }); } } export const badFile = () =>{ return dispatch => { dispatch(updateMessages([{code: 'warning', message: 'INVALID_FILE_ACCEPTANCE'}], orderMessages)); } } const sendCustomerAcceptance = () => ({ type: SEND_CUSTOMER_ACCEPTANCE }); export const acceptDeclineInstallation = (idOrder, actionType, declineReason) => { return dispatch => { dispatch(sendCustomerAcceptance()); return htmlClient.fetch({ url: `${API_SERVER}/wp-json/wiaas/delivery/${idOrder}/customer-acceptance`, method: 'post', data: { 'action_type': actionType, 'decline_reason': declineReason } }) .then(response => { if (response.data) { dispatch(updateMessages(response.data.messages, orderMessages)); dispatch(fetchCustomerAcceptance(idOrder)); } }) .catch(error => { htmlClient.onError(error, dispatch); }); } }