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));
|
||||
|
||||
@@ -111,13 +111,6 @@ export const addToCart = (addParams) => {
|
||||
addParams.selectedAdditionals,
|
||||
addParams.selectedAgreement);
|
||||
|
||||
const params = {
|
||||
'package_id': addParams.selectedPackage.id,
|
||||
'price_id': addParams.selectedAgreement.idPrice,
|
||||
'addons': result.additionalPackages,
|
||||
'options': result.optionPackages
|
||||
};
|
||||
|
||||
if(result.unavailablePackages.length){
|
||||
const unavailable = result.unavailablePackages.map((unavailable) =>{return unavailable.optionName || unavailable.packageName;});
|
||||
const message = coMarketMessages.UNAVAILABLE_PACKAGES + ' ' + unavailable.join();
|
||||
@@ -128,9 +121,14 @@ export const addToCart = (addParams) => {
|
||||
return dispatch => {
|
||||
dispatch(requestAddToCart());
|
||||
return client.fetch({
|
||||
url: `${API_SERVER}/wp-json/wiaas/cart/add`,
|
||||
url: `${API_SERVER}/wp-json/wiaas/cart/items`,
|
||||
method: 'post',
|
||||
data: params
|
||||
data: {
|
||||
'package_id': addParams.selectedPackage.id,
|
||||
'price_id': addParams.selectedAgreement.idPrice,
|
||||
'addons_ids': result.additionalPackages,
|
||||
'options_ids': result.optionPackages
|
||||
},
|
||||
})
|
||||
.then(response => {
|
||||
if(response.data && response.data.messages){
|
||||
|
||||
@@ -23,8 +23,8 @@ const recieveOrderProjects = (json) => ({
|
||||
|
||||
const generateOptions = (orderProjects) => {
|
||||
orderProjects.forEach((orderProject) => {
|
||||
orderProject.value = orderProject.idProject;
|
||||
orderProject.label = orderProject.projectName;
|
||||
orderProject.value = orderProject.id;
|
||||
orderProject.label = orderProject.name;
|
||||
});
|
||||
|
||||
return orderProjects;
|
||||
@@ -34,40 +34,18 @@ export const getOrderProjects = () => {
|
||||
return dispatch => {
|
||||
dispatch(requestOrderProjects());
|
||||
|
||||
dispatch(recieveOrderProjects(generateOptions([
|
||||
{
|
||||
"idProject": 1,
|
||||
"projectName": "Innovation Center",
|
||||
"isAvailable": 1
|
||||
},
|
||||
{
|
||||
"idProject": 2,
|
||||
"projectName": "Demo01",
|
||||
"isAvailable": 1
|
||||
},
|
||||
{
|
||||
"idProject": 3,
|
||||
"projectName": "Kontorsrådet",
|
||||
"isAvailable": 1
|
||||
},
|
||||
{
|
||||
"idProject": 4,
|
||||
"projectName": "PerProj01",
|
||||
"isAvailable": 1
|
||||
}
|
||||
])));
|
||||
// return htmlClient.fetch({
|
||||
// url: `${API_SERVER}/orderProjects/api/getOrderProjects`
|
||||
// })
|
||||
// .then(response => {
|
||||
// if (response.data) {
|
||||
// const orderProjects = generateOptions(response.data);
|
||||
// dispatch(recieveOrderProjects(orderProjects));
|
||||
// }
|
||||
// })
|
||||
// .catch(error => {
|
||||
// htmlClient.onError(error, dispatch);
|
||||
// });
|
||||
return htmlClient.fetch({
|
||||
url: `${API_SERVER}/wp-json/wiaas/order-projects`
|
||||
})
|
||||
.then(response => {
|
||||
if (response.data) {
|
||||
const orderProjects = generateOptions(response.data);
|
||||
dispatch(recieveOrderProjects(orderProjects));
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
htmlClient.onError(error, dispatch);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,13 +54,15 @@ const requestAddProject = () => ({
|
||||
isLoading: true
|
||||
});
|
||||
|
||||
export const addProject = (projectData) => {
|
||||
export const addProject = (projectName) => {
|
||||
return dispatch => {
|
||||
dispatch(requestAddProject());
|
||||
return htmlClient.fetch({
|
||||
url: `${API_SERVER}/orderProjects/api/addOrderProject`,
|
||||
url: `${API_SERVER}/wp-json/wiaas/order-projects`,
|
||||
method: 'post',
|
||||
data: {projectData: JSON.stringify(projectData)}
|
||||
data: {
|
||||
name: projectName
|
||||
}
|
||||
})
|
||||
.then(response => {
|
||||
if(response.data && response.data.messages){
|
||||
|
||||
Reference in New Issue
Block a user