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){
|
||||
|
||||
@@ -87,7 +87,7 @@ class CartCustomerDetailsContainer extends Component {
|
||||
|
||||
handleProjectChange(project){
|
||||
const newDetails = this.state.details;
|
||||
newDetails.idProject = project ? project.idProject : null;
|
||||
newDetails.idProject = project ? project.id : null;
|
||||
return this.setState({details: newDetails});
|
||||
}
|
||||
|
||||
|
||||
@@ -37,10 +37,10 @@ class CartReviewOrderContainer extends Component {
|
||||
|
||||
getProjectName(idProject){
|
||||
const selectedProject = this.props.orderProjects.find(orderProject => {
|
||||
return orderProject.idProject === idProject
|
||||
return orderProject.id === idProject
|
||||
});
|
||||
|
||||
return selectedProject && selectedProject.projectName ? selectedProject.projectName : '-';
|
||||
return selectedProject && selectedProject.name ? selectedProject.name : '-';
|
||||
}
|
||||
|
||||
render() {
|
||||
|
||||
@@ -23,7 +23,7 @@ class AddOrderProject extends Component {
|
||||
}
|
||||
|
||||
onProjectAdd(){
|
||||
this.props.dispatch(addProject(this.state));
|
||||
this.props.dispatch(addProject(this.state.projectName));
|
||||
}
|
||||
|
||||
render() {
|
||||
|
||||
55
frontend/src/helpers/CartHelper.js
Normal file
55
frontend/src/helpers/CartHelper.js
Normal file
@@ -0,0 +1,55 @@
|
||||
|
||||
export const fromWCCartItems = wcCartItem => {
|
||||
const paymentInfo = wcCartItem['payment_info'] || {};
|
||||
const totalPrices = wcCartItem['total_prices'] || {};
|
||||
|
||||
return {
|
||||
idPackage: wcCartItem['package_id'],
|
||||
key: wcCartItem.key,
|
||||
packageName: wcCartItem['package_name'],
|
||||
quantity: wcCartItem.quantity,
|
||||
|
||||
idCommercialLead: wcCartItem['commercial_lead_id'],
|
||||
commercialLead: wcCartItem['commercial_lead'],
|
||||
country: wcCartItem.country ? wcCartItem.country : [],
|
||||
|
||||
areAdditionalAvailable: wcCartItem['are_additional_available'],
|
||||
additionalPackages: wcCartItem['additional_packages'] ? wcCartItem['additional_packages'].map(additionalPackageItem => ({
|
||||
idAdditionalPackage: additionalPackageItem['package_id'],
|
||||
packageName: additionalPackageItem['package_name'],
|
||||
prices: {
|
||||
fixedExtra: additionalPackageItem['prices'] ? additionalPackageItem['prices']['fixed_extra'] : 0,
|
||||
recurrentExtra: additionalPackageItem['prices'] ? additionalPackageItem['prices']['recurrent_extra'] : 0,
|
||||
servicesExtra: additionalPackageItem['prices'] ? additionalPackageItem['prices']['services_extra'] : 0,
|
||||
}
|
||||
})) : [],
|
||||
|
||||
areOptionsAvailable: wcCartItem['are_options_available'],
|
||||
options: wcCartItem.options ? wcCartItem.options.map(optionPackageItem => ({
|
||||
idOptionPackage: optionPackageItem['package_id'],
|
||||
packageName: optionPackageItem['package_name'],
|
||||
groupName: optionPackageItem['group_name'],
|
||||
prices: {
|
||||
fixedExtra: optionPackageItem['prices'] ? optionPackageItem['prices']['fixed_extra'] : 0,
|
||||
recurrentExtra: optionPackageItem['prices'] ? optionPackageItem['prices']['recurrent_extra'] : 0,
|
||||
servicesExtra: optionPackageItem['prices'] ? optionPackageItem['prices']['services_extra'] : 0,
|
||||
}
|
||||
})) : [],
|
||||
|
||||
bids: wcCartItem.bids,
|
||||
|
||||
idPayType: paymentInfo.id,
|
||||
payType: paymentInfo.type,
|
||||
periodUnit: paymentInfo['period_unit'] || 'month',
|
||||
idPrice: paymentInfo.id,
|
||||
fixedPrice: paymentInfo['fixed_extra'] !== undefined ? paymentInfo['fixed_extra'] : 0,
|
||||
recurrentPrice: paymentInfo['recurrent_extra'] !== undefined ? paymentInfo['recurrent_extra'] : 0,
|
||||
servicesPrice: paymentInfo['services_extra'] !== undefined ? paymentInfo['services_extra'] : 0,
|
||||
|
||||
totalPrices: {
|
||||
fixedPrice: totalPrices['fixed_extra'] !== undefined ? totalPrices['fixed_extra'] : 0,
|
||||
recurrentPrice: totalPrices['recurrent_extra'] !== undefined ? totalPrices['recurrent_extra'] : 0,
|
||||
servicesPrice: totalPrices['services_extra'] !== undefined ? totalPrices['services_extra'] : 0,
|
||||
}
|
||||
};
|
||||
};
|
||||
@@ -60,6 +60,24 @@ class HtmlClient {
|
||||
}
|
||||
|
||||
onError(error) {
|
||||
const response = error.response;
|
||||
if (response.data) {
|
||||
switch (response.data.code) {
|
||||
case 'rest_missing_callback_param':
|
||||
store.dispatch(updateMessages([{code:'error', message: response.data.message }]));
|
||||
return;
|
||||
case 'rest_invalid_param':
|
||||
const messages = Object.keys(response.data.data.params).map(paramName => {
|
||||
const errorMessage = response.data.data.params[paramName];
|
||||
if (errorMessage === 'Invalid parameter.') {
|
||||
return { code: 'error', message: `Invalid ${paramName} parameter!` };
|
||||
}
|
||||
return { code: 'error', message: errorMessage };
|
||||
});
|
||||
store.dispatch(updateMessages(messages));
|
||||
return;
|
||||
}
|
||||
}
|
||||
store.dispatch(updateMessages([{code:'error', message: 'HTML_ERROR'}], notificationMessages));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,11 @@ export const fromWCOrder = (WCOrder) => {
|
||||
dateCreated: formatDate(WCOrder['date_created']),
|
||||
dateCompleted: formatDate(WCOrder['date_completed']),
|
||||
estimatedDeliveryDate: undefined,
|
||||
vatCode: WCOrder['vat'],
|
||||
reference: WCOrder['reference'],
|
||||
tender: WCOrder['tender'],
|
||||
companyName: WCOrder['company_name'],
|
||||
projectName: WCOrder['project_name'],
|
||||
assignedTo: 'assigned to',
|
||||
fixedPrice: WCOrder.total,
|
||||
recurringPrice: WCOrder['recurring_price'],
|
||||
|
||||
Reference in New Issue
Block a user