user profile edit actions

This commit is contained in:
GotPPay
2018-09-11 09:26:00 +02:00
committed by Bilal Catic
parent b974eab225
commit a79ef1044c
4 changed files with 214 additions and 56 deletions

View File

@@ -8,9 +8,11 @@ defined( 'ABSPATH' ) || exit;
class Wiaas_User { class Wiaas_User {
public static function init() { public static function init() {
include_once dirname( __FILE__ ) . '/user/class-wiaas-user-profile.php';
add_action('init', array(__CLASS__, 'load_user_organization')); add_action('init', array(__CLASS__, 'load_user_organization'));
add_action('plugins_loaded', array(__CLASS__, 'remove_default_user_groups'), 30); add_action('plugins_loaded', array(__CLASS__, 'remove_default_user_groups'), 30);
add_action('woocommerce_rest_insert_customer', array(__CLASS__, 'test_method'), 10, 3); add_action('woocommerce_rest_insert_customer', array(__CLASS__, 'add_meta_data_to_customer'), 10, 3);
add_filter('woocommerce_rest_prepare_customer', array(__CLASS__, 'transform_rest_customer'), 10, 3); add_filter('woocommerce_rest_prepare_customer', array(__CLASS__, 'transform_rest_customer'), 10, 3);
add_filter('jwt_auth_token_before_dispatch', array(__CLASS__, 'transform_jwt_token_response'), 10, 2); add_filter('jwt_auth_token_before_dispatch', array(__CLASS__, 'transform_jwt_token_response'), 10, 2);
@@ -23,7 +25,7 @@ class Wiaas_User {
* @param WP_REST_Request $request Request object. * @param WP_REST_Request $request Request object.
* @param boolean $creating True when creating customer, false when updating customer. * @param boolean $creating True when creating customer, false when updating customer.
*/ */
public static function test_method($customer, $request, $creating){ public static function add_meta_data_to_customer($customer, $request, $creating){
if (isset($request['phone'])){ if (isset($request['phone'])){
update_user_meta( $customer->ID, 'phone', $request['phone'] ); update_user_meta( $customer->ID, 'phone', $request['phone'] );
} }
@@ -35,6 +37,118 @@ class Wiaas_User {
if (isset($request['companyName'])){ if (isset($request['companyName'])){
update_user_meta( $customer->ID, 'company_name', $request['companyName']); update_user_meta( $customer->ID, 'company_name', $request['companyName']);
} }
if (isset($request['deliveryAddress'])){
$received_address = json_decode($request['deliveryAddress']);
$delivery_addresses = get_user_meta($customer->ID, 'delivery_addresses', true) ?: [];
if ($received_address->id){
$updated = array(
'id' => $received_address->id,
'countryName' => Wiaas_User_Profile::get_country_name_by_id($received_address->idCountrySelected),
'deliveryMail' => $received_address->deliveryMail,
'idCountrySelected' => $received_address->idCountrySelected,
'city' => $received_address->city,
'detailedAddress' => $received_address->detailedAddress,
'zipCode' => $received_address->zipCode
);
foreach($delivery_addresses as $key => $address){
if ($address['id'] === $received_address->id){
$delivery_addresses[$key] = $updated;
break;
}
}
}else{
$new_delivery_address = array(
'id' => time(),
'countryName' => Wiaas_User_Profile::get_country_name_by_id($received_address->idCountrySelected),
'deliveryMail' => $received_address->deliveryMail,
'idCountrySelected' => $received_address->idCountrySelected,
'city' => $received_address->city,
'detailedAddress' => $received_address->detailedAddress,
'zipCode' => $received_address->zipCode
);
array_push($delivery_addresses, $new_delivery_address);
}
update_user_meta( $customer->ID, 'delivery_addresses', $delivery_addresses);
//delete_user_meta ($customer->ID, 'delivery_addresses');
}
if (isset($request['billingAddress'])){
$received_address = json_decode($request['billingAddress']);
$billing_addresses = get_user_meta($customer->ID, 'billing_addresses', true) ?: [];
if ($received_address->id){
$updated = array(
'id' => $received_address->id,
'countryName' => Wiaas_User_Profile::get_country_name_by_id($received_address->idCountrySelected),
'deliveryMail' => $received_address->deliveryMail,
'idCountrySelected' => $received_address->idCountrySelected,
'city' => $received_address->city,
'detailedAddress' => $received_address->detailedAddress,
'zipCode' => $received_address->zipCode,
'firstName' => $received_address->firstName,
'lastName' => $received_address->lastName,
'invoiceMail' => $received_address->invoiceMail
);
foreach($billing_addresses as $key => $address){
if ($address['id'] === $received_address->id){
$billing_addresses[$key] = $updated;
break;
}
}
}else{
$new_billing_address = array(
'id' => time(),
'countryName' => Wiaas_User_Profile::get_country_name_by_id($received_address->idCountrySelected),
'deliveryMail' => $received_address->deliveryMail,
'idCountrySelected' => $received_address->idCountrySelected,
'city' => $received_address->city,
'detailedAddress' => $received_address->detailedAddress,
'zipCode' => $received_address->zipCode,
'firstName' => $received_address->firstName,
'lastName' => $received_address->lastName,
'invoiceMail' => $received_address->invoiceMail
);
array_push($billing_addresses, $new_billing_address);
}
update_user_meta( $customer->ID, 'billing_addresses', $billing_addresses);
//delete_user_meta ($customer->ID, 'delivery_addresses');
}
if (isset($request['removeDeliveryAddress'])){
$addressID = $request['removeDeliveryAddress'];
$delivery_addresses = get_user_meta($customer->ID, 'delivery_addresses', true) ?: [];
$counter = 0;
foreach($delivery_addresses as $key => $address){
if ($address['id'] == $addressID){
array_splice($delivery_addresses, $counter, 1);
break;
}
$counter++;
}
update_user_meta( $customer->ID, 'delivery_addresses', $delivery_addresses);
}
if (isset($request['removeBillingAddress'])){
$addressID = $request['removeBillingAddress'];
$billing_addresses = get_user_meta($customer->ID, 'billing_addresses', true) ?: [];
$counter = 0;
foreach($billing_addresses as $key => $address){
if ($address['id'] == $addressID){
array_splice($billing_addresses, $counter, 1);
break;
}
$counter++;
}
update_user_meta( $customer->ID, 'billing_addresses', $billing_addresses);
}
} }
public static function load_user_organization() { public static function load_user_organization() {
@@ -71,8 +185,8 @@ class Wiaas_User {
'phone' => get_user_meta($user_id, 'phone', true), 'phone' => get_user_meta($user_id, 'phone', true),
'companyName' => get_user_meta($user_id, 'company_name', true), 'companyName' => get_user_meta($user_id, 'company_name', true),
'vatCode' => get_user_meta($user_id, 'vat_code', true), 'vatCode' => get_user_meta($user_id, 'vat_code', true),
'billingAddresses' => [], 'billingAddresses' => get_user_meta($user_id, 'billing_addresses', true) ?: [],
'profileAddresses' => [], 'deliveryAddresses' => get_user_meta($user_id, 'delivery_addresses', true) ?: [],
'userType' => $data['role'] 'userType' => $data['role']
); );

View File

@@ -9,9 +9,9 @@ import {
REQUEST_SAVE_BILLING_ADDRESS, REQUEST_SAVE_BILLING_ADDRESS,
profileTexts profileTexts
} from '../../constants/profileSettingsConstants'; } from '../../constants/profileSettingsConstants';
import {fetchProfileInfo} from './profileSettingsActions';
import {updateMessages} from '../notification/notificationActions'; import {updateMessages} from '../notification/notificationActions';
import {setDialogOpenFlag} from '../dialog/dialogActions'; import {setDialogOpenFlag} from '../dialog/dialogActions';
import {recieveProfileInfo} from './profileSettingsActions';
const client = new HtmlClient(); const client = new HtmlClient();
@@ -23,18 +23,26 @@ export const saveProfileAddress = (idUser, profileAddress) => {
return dispatch => { return dispatch => {
dispatch(requestSaveAddress()); dispatch(requestSaveAddress());
return client.fetch({ return client.fetch({
url: `${API_SERVER}/profileSettings/api/saveProfileAddress`, url: `${API_SERVER}/wp-json/wc/v2/customers/${idUser}`,
method: 'post', method: 'put',
data: {profileAddress: JSON.stringify(profileAddress)} data: {deliveryAddress: JSON.stringify(profileAddress)}
}) })
.then(response => { .then(response => {
if(response.data && response.data.messages){ let messages = [];
dispatch(updateMessages(response.data.messages, profileTexts.messages)); if(response.data){
if(response.data.messages[0].code === 'success'){ dispatch(recieveProfileInfo(response.data));
dispatch(fetchProfileInfo(idUser)); dispatch(setDialogOpenFlag(false));
dispatch(setDialogOpenFlag(false)); messages.push({
} code:'success',
message:'PROFILE_ADDRESS_UPDATED'
});
}else{
messages.push({
code:'error',
message:'INTERNAL_SERVER_ERROR'
});
} }
dispatch(updateMessages(messages, profileTexts.messages));
}) })
.catch(error => { .catch(error => {
client.onError(error, dispatch); client.onError(error, dispatch);
@@ -50,15 +58,25 @@ export const removeProfileAddress = (idUser, idProfileAddress) => {
return dispatch => { return dispatch => {
dispatch(requestRemoveAddress()); dispatch(requestRemoveAddress());
return client.fetch({ return client.fetch({
url: `${API_SERVER}/profileSettings/api/removeProfileAddress`, url: `${API_SERVER}/wp-json/wc/v2/customers/${idUser}`,
method: 'post', method: 'put',
data: {idProfileAddress} data: {removeDeliveryAddress:idProfileAddress}
}) })
.then(response => { .then(response => {
if(response.data && response.data.messages){ let messages = [];
dispatch(updateMessages(response.data.messages, profileTexts.messages)); if(response.data){
dispatch(fetchProfileInfo(idUser)); dispatch(recieveProfileInfo(response.data));
messages.push({
code:'success',
message:'ADDRESS_REMOVED'
});
}else{
messages.push({
code:'error',
message:'INTERNAL_SERVER_ERROR'
});
} }
dispatch(updateMessages(messages, profileTexts.messages));
}) })
.catch(error => { .catch(error => {
client.onError(error, dispatch); client.onError(error, dispatch);
@@ -70,22 +88,30 @@ const requestSaveBillingAddress = () => ({
type: REQUEST_SAVE_BILLING_ADDRESS type: REQUEST_SAVE_BILLING_ADDRESS
}); });
export const saveBillingAddress = (idUser, idCompany, billingAddress) => { export const saveBillingAddress = (idUser, billingAddress) => {
return dispatch => { return dispatch => {
dispatch(requestSaveBillingAddress()); dispatch(requestSaveBillingAddress());
return client.fetch({ return client.fetch({
url: `${API_SERVER}/profileSettings/api/saveBillingAddress`, url: `${API_SERVER}/wp-json/wc/v2/customers/${idUser}`,
method: 'post', method: 'put',
data: {idCompany, billingAddress: JSON.stringify(billingAddress)} data: {billingAddress: JSON.stringify(billingAddress)}
}) })
.then(response => { .then(response => {
if(response.data && response.data.messages){ let messages = [];
dispatch(updateMessages(response.data.messages, profileTexts.messages)); if(response.data){
if(response.data.messages[0].code === 'success'){ dispatch(recieveProfileInfo(response.data));
dispatch(fetchProfileInfo(idUser)); dispatch(setDialogOpenFlag(false));
dispatch(setDialogOpenFlag(false)); messages.push({
} code:'success',
message:'BILLING_ADDRESS_UPDATED'
});
}else{
messages.push({
code:'error',
message:'INTERNAL_SERVER_ERROR'
});
} }
dispatch(updateMessages(messages, profileTexts.messages));
}) })
.catch(error => { .catch(error => {
client.onError(error, dispatch); client.onError(error, dispatch);
@@ -101,15 +127,25 @@ export const removeBillingAddress = (idUser, idBillingAddress) => {
return dispatch => { return dispatch => {
dispatch(requestRemoveBillingAddress()); dispatch(requestRemoveBillingAddress());
return client.fetch({ return client.fetch({
url: `${API_SERVER}/profileSettings/api/removeBillingAddress`, url: `${API_SERVER}/wp-json/wc/v2/customers/${idUser}`,
method: 'post', method: 'put',
data: {idBillingAddress} data: {removeBillingAddress:idBillingAddress}
}) })
.then(response => { .then(response => {
if(response.data && response.data.messages){ let messages = [];
dispatch(updateMessages(response.data.messages, profileTexts.messages)); if(response.data){
dispatch(fetchProfileInfo(idUser)); dispatch(recieveProfileInfo(response.data));
messages.push({
code:'success',
message:'BILLING_ADDRESS_REMOVED'
});
}else{
messages.push({
code:'error',
message:'INTERNAL_SERVER_ERROR'
});
} }
dispatch(updateMessages(messages, profileTexts.messages));
}) })
.catch(error => { .catch(error => {
client.onError(error, dispatch); client.onError(error, dispatch);

View File

@@ -38,19 +38,6 @@ export const fetchProfileInfo = (idUser) => {
dispatch(recieveProfileInfo(response.data)); dispatch(recieveProfileInfo(response.data));
} }
}); });
// return client.fetch({
// url: `${API_SERVER}/wp-json/wiaas/cart/customer-info`,
// method: 'get',
// data: {idUser}
// })
// .then(response => {
// if(response.data){
// dispatch(recieveProfileInfo(response.data));
// }
// })
// .catch(error => {
// client.onError(error, dispatch);
// });
} }
}; };
@@ -72,10 +59,20 @@ export const saveProfileInfo = (idUser, profile) => {
} }
}) })
.then(response => { .then(response => {
if(response.data && response.data.messages){ let messages = [];
dispatch(updateMessages(response.data.messages, profileTexts.messages)); if(response.data){
dispatch(fetchProfileInfo(idUser)); dispatch(recieveProfileInfo(response.data));
messages.push({
code:'success',
message:'PROFILE_UPDATED'
});
}else{
messages.push({
code:'error',
message:'INTERNAL_SERVER_ERROR'
});
} }
dispatch(updateMessages(messages, profileTexts.messages));
}) })
.catch(error => { .catch(error => {
client.onError(error, dispatch); client.onError(error, dispatch);
@@ -99,10 +96,20 @@ export const saveCompanyInfo = (idUser, companyInfo) => {
} }
}) })
.then(response => { .then(response => {
if(response.data && response.data.messages){ let messages = [];
dispatch(updateMessages(response.data.messages, profileTexts.messages)); if(response.data){
dispatch(fetchProfileInfo(idUser)); dispatch(recieveProfileInfo(response.data));
messages.push({
code:'success',
message:'COMPANY_UPDATED'
});
}else{
messages.push({
code:'error',
message:'INTERNAL_SERVER_ERROR'
});
} }
dispatch(updateMessages(messages, profileTexts.messages));
}) })
.catch(error => { .catch(error => {
client.onError(error, dispatch); client.onError(error, dispatch);

View File

@@ -77,6 +77,7 @@ export const profileTexts = {
ADD_FIRST_NAME: 'The first name field can not be empty', ADD_FIRST_NAME: 'The first name field can not be empty',
ADD_LAST_NAME: 'The last name field can not be empty', ADD_LAST_NAME: 'The last name field can not be empty',
ADD_INVOICE_MAIL: 'The invoice mail field can not be empty', ADD_INVOICE_MAIL: 'The invoice mail field can not be empty',
INVALID_INVOICE_MAIL: 'Invalid invoice mail address' INVALID_INVOICE_MAIL: 'Invalid invoice mail address',
INTERNAL_SERVER_ERROR: 'Error occured. Please try again'
} }
}; };