Handle order project and refactor api
This commit is contained in:
@@ -29,6 +29,8 @@ import {
|
||||
cartMessages
|
||||
} from '../../constants/cartConstants';
|
||||
import {updateMessages} from '../notification/notificationActions';
|
||||
import { fromWCCartItems } from '../../helpers/CartHelper';
|
||||
|
||||
const client = new HtmlClient();
|
||||
|
||||
export const requestShopCartCount = () => ({type: REQUEST_SHOP_CART_COUNT});
|
||||
@@ -72,10 +74,13 @@ export const fetchCartItems = (isForSteps = false) => {
|
||||
return dispatch => {
|
||||
dispatch(requestShopCartItems());
|
||||
return client.fetch({url: `${API_SERVER}/wp-json/wiaas/cart/items`}).then(response => {
|
||||
if (typeof response.data !== 'undefined' && 'cartItems' in response.data) {
|
||||
dispatch(receiveShopCartItems(response.data.cartItems));
|
||||
dispatch(fetchCartDocuments(response.data.cartItems.map((cartItem) => cartItem.idPackage), isForSteps));
|
||||
dispatch(updateOrderTotalPrice(response.data.cartItems));
|
||||
if (typeof response.data !== 'undefined' && 'items' in response.data) {
|
||||
|
||||
const cartItems = response.data.items.map(wcCartItem => fromWCCartItems(wcCartItem));
|
||||
|
||||
dispatch(receiveShopCartItems(cartItems));
|
||||
dispatch(fetchCartDocuments(cartItems.map((cartItem) => cartItem.idPackage), isForSteps));
|
||||
dispatch(updateOrderTotalPrice(cartItems));
|
||||
}
|
||||
}).catch(error => {
|
||||
client.onError(error, dispatch);
|
||||
@@ -90,19 +95,14 @@ const updateCartItems = (newItem)=>(
|
||||
);
|
||||
|
||||
export const updateQuantity = (cartItem, quantity, updateCartAllItems) => {
|
||||
const params = {
|
||||
idPackage: cartItem.idPackage || 0,
|
||||
package_item_key: cartItem.key,
|
||||
idCustomerInstance: cartItem.idCustomerInstance || 0,
|
||||
idPrice: cartItem.idPrice || 0,
|
||||
quantity
|
||||
};
|
||||
|
||||
return dispatch => {
|
||||
return client.fetch({
|
||||
url: `${API_SERVER}/wp-json/wiaas/cart/update-quantity`,
|
||||
url: `${API_SERVER}/wp-json/wiaas/cart/items/${cartItem.key}`,
|
||||
method: 'post',
|
||||
data: params
|
||||
data: {
|
||||
quantity
|
||||
}
|
||||
}).then(response => {
|
||||
if (typeof response.data !== 'undefined' && 'messages' in response.data) {
|
||||
const filteredMessages = response.data.messages.filter(message => {
|
||||
@@ -125,11 +125,8 @@ export const updateQuantity = (cartItem, quantity, updateCartAllItems) => {
|
||||
export const removeCartItem = (cartItemKey) => {
|
||||
return dispatch => {
|
||||
return client.fetch({
|
||||
url: `${API_SERVER}/wp-json/wiaas/cart/remove`,
|
||||
method: 'post',
|
||||
data: {
|
||||
package_item_key: cartItemKey
|
||||
}
|
||||
url: `${API_SERVER}/wp-json/wiaas/cart/items/${cartItemKey}`,
|
||||
method: 'delete',
|
||||
}).then(response => {
|
||||
if (typeof response.data !== 'undefined' && 'messages' in response.data) {
|
||||
dispatch(updateMessages(response.data.messages, cartMessages));
|
||||
@@ -269,21 +266,30 @@ export const fetchCartDocuments = (packages, isForSteps = false) => {
|
||||
return dispatch => {
|
||||
dispatch(requestCartDocuments());
|
||||
|
||||
return client.fetch({
|
||||
url: `${API_SERVER}/wp-json/wiaas/cart/documents`,
|
||||
method: 'get',
|
||||
data: {packages}
|
||||
}).then(response => {
|
||||
if (response.data) {
|
||||
dispatch(receiveCartDocuments(response.data));
|
||||
if(isForSteps) {
|
||||
const whitoutUploadDoc = response.data.templates.length === 0;
|
||||
dispatch(loadSteps(whitoutUploadDoc));
|
||||
}
|
||||
}
|
||||
}).catch(error => {
|
||||
client.onError(error, dispatch);
|
||||
});
|
||||
dispatch(receiveCartDocuments({
|
||||
areFilesUploaded: true,
|
||||
templates: [],
|
||||
uploaded: []
|
||||
}));
|
||||
if(isForSteps) {
|
||||
dispatch(loadSteps(true));
|
||||
}
|
||||
|
||||
// return client.fetch({
|
||||
// url: `${API_SERVER}/wp-json/wiaas/cart/documents`,
|
||||
// method: 'get',
|
||||
// data: {packages}
|
||||
// }).then(response => {
|
||||
// if (response.data) {
|
||||
// dispatch(receiveCartDocuments(response.data));
|
||||
// if(isForSteps) {
|
||||
// const whitoutUploadDoc = response.data.templates.length === 0;
|
||||
// dispatch(loadSteps(whitoutUploadDoc));
|
||||
// }
|
||||
// }
|
||||
// }).catch(error => {
|
||||
// client.onError(error, dispatch);
|
||||
// });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -319,9 +325,17 @@ export const placeOrder = (orderInfo) => {
|
||||
const {orderDetails, cartItems} = orderInfo;
|
||||
return dispatch => {
|
||||
return client.fetch({
|
||||
url: `${API_SERVER}/wp-json/wiaas/cart/place-order`,
|
||||
url: `${API_SERVER}/wp-json/wiaas/cart/checkout`,
|
||||
method: 'post',
|
||||
data: orderDetails
|
||||
data: {
|
||||
vat: orderDetails.vatCode,
|
||||
company: orderDetails.companyName,
|
||||
reference: orderDetails.details.reference,
|
||||
tender: orderDetails.details.tender,
|
||||
project_id: orderDetails.details.idProject,
|
||||
delivery_address_id: orderDetails.delivery.id,
|
||||
billing_address_id: orderDetails.billing.id,
|
||||
}
|
||||
}).then(response => {
|
||||
if (typeof response.data !== 'undefined' && 'messages' in response.data) {
|
||||
dispatch(updateMessages(response.data.messages, cartMessages));
|
||||
|
||||
Reference in New Issue
Block a user