#832 Fix Wrong Delivery Address in Order Checkout

This commit is contained in:
Sebastian
2020-07-13 13:29:37 +02:00
parent cef1fa1e6f
commit 57be60e452
6 changed files with 457 additions and 122 deletions

View File

@@ -822,6 +822,9 @@ export class CustomerMapping {
fromOrganisationToOrganisationDTO(
organisation: Organisation
): OrganisationDTO {
if (!organisation) {
return {};
}
return {
name: organisation.name,
department: organisation.department,

View File

@@ -1,7 +1,5 @@
import { TestBed } from '@angular/core/testing';
import { CustomerService } from './customer.service';
describe('CustomerService', () => {
beforeEach(() => TestBed.configureTestingModule({}));
});

View File

@@ -155,7 +155,6 @@ export class CustomerService {
): Observable<User> {
return customer$.pipe(
map((response: ResponseArgsOfCustomerDTO) => {
console.log({ response });
if (response.error) {
throw new Error(response.message);
}
@@ -244,7 +243,6 @@ export class CustomerService {
updateCustomer.invoice_address = defaultAddress;
}
console.log({ updateCustomer });
return of(updateCustomer);
})
);

View File

@@ -40,7 +40,10 @@ export class CustomerState {
constructor(private store: Store, private customerService: CustomerService) {}
@Action(actions.SearchUser)
searchUser(ctx: StateContext<CustomerStateModel>, { payload }: actions.SearchUser): Observable<User[]> {
searchUser(
ctx: StateContext<CustomerStateModel>,
{ payload }: actions.SearchUser
): Observable<User[]> {
return this.customerService.searchCustomer(payload).pipe(
map((response: CustomerSearchResponse) => response.customers),
tap((_customers: User[]) => {
@@ -55,21 +58,30 @@ export class CustomerState {
customers = { ...customers, [t.id]: t };
});
ctx.patchState({ customers });
this.syncApiState(customers, state.lastCreatedCustomerId, state.cachedCustomerSearch);
this.syncApiState(
customers,
state.lastCreatedCustomerId,
state.cachedCustomerSearch
);
this.store.dispatch(new AddCustomerIds(customerIds));
})
);
}
@Action(actions.AddCustomers)
addCustomers(ctx: StateContext<CustomerStateModel>, { payload }: actions.AddCustomers) {
addCustomers(
ctx: StateContext<CustomerStateModel>,
{ payload }: actions.AddCustomers
) {
const state = ctx.getState();
if (!state) {
return;
}
const currentCustomers = state.customers;
const newCustomers = payload.filter((t) => t !== undefined);
const currentCustomerIds = this.store.selectSnapshot(ProcessSelectors.getCustomerIds);
const currentCustomerIds = this.store.selectSnapshot(
ProcessSelectors.getCustomerIds
);
const customerIds = [];
let customers = currentCustomers;
if (!!currentCustomerIds) {
@@ -84,12 +96,19 @@ export class CustomerState {
}
});
ctx.patchState({ customers });
this.syncApiState(customers, state.lastCreatedCustomerId, state.cachedCustomerSearch);
this.syncApiState(
customers,
state.lastCreatedCustomerId,
state.cachedCustomerSearch
);
this.store.dispatch(new AddCustomerIds(customerIds));
}
@Action(actions.SetActiveUser)
setActiveUser(ctx: StateContext<CustomerStateModel>, { payload, cartHasItemForDownload, cartEntriesIds }: actions.SetActiveUser) {
setActiveUser(
ctx: StateContext<CustomerStateModel>,
{ payload, cartHasItemForDownload, cartEntriesIds }: actions.SetActiveUser
) {
const state = ctx.getState();
if (!state) {
return;
@@ -101,14 +120,21 @@ export class CustomerState {
ctx.patchState({ customers });
this.store.dispatch(new SetActiveCustomer(_customer.id));
this.store.dispatch(new UpdateProcessName(_customer.last_name));
this.syncApiState(customers, state.lastCreatedCustomerId, state.cachedCustomerSearch);
this.syncApiState(
customers,
state.lastCreatedCustomerId,
state.cachedCustomerSearch
);
}
})
);
}
@Action(actions.AddUser)
addUser(ctx: StateContext<CustomerStateModel>, { payload, setActiveUser }: actions.AddUser) {
addUser(
ctx: StateContext<CustomerStateModel>,
{ payload, setActiveUser }: actions.AddUser
) {
const state = ctx.getState();
let customerApi$: Observable<User>;
let isOnline = false;
@@ -126,10 +152,21 @@ export class CustomerState {
}
return customerApi$.pipe(
tap((addedCustomer: User) => {
this.store.dispatch(new SetOnlineCustomerCreationStatus({ error: false, invalidProperties: null }));
this.store.dispatch(
new SetOnlineCustomerCreationStatus({
error: false,
invalidProperties: null,
})
);
if (state.customers[addedCustomer.id] === undefined) {
const customers = { ...state.customers, [addedCustomer.id]: addedCustomer };
ctx.patchState({ customers, lastCreatedCustomerId: addedCustomer.id });
const customers = {
...state.customers,
[addedCustomer.id]: addedCustomer,
};
ctx.patchState({
customers,
lastCreatedCustomerId: addedCustomer.id,
});
}
if (setActiveUser) {
@@ -153,7 +190,8 @@ export class CustomerState {
this.store.dispatch(
new SetOnlineCustomerCreationStatus({
error: true,
invalidProperties: error && error.error ? error.error.invalidProperties : undefined,
invalidProperties:
error && error.error ? error.error.invalidProperties : undefined,
})
);
console.error(error);
@@ -163,24 +201,40 @@ export class CustomerState {
}
@Action(actions.SetUserDetails)
editUser(ctx: StateContext<CustomerStateModel>, { payload, addBreadcrumb }: actions.SetUserDetails) {
editUser(
ctx: StateContext<CustomerStateModel>,
{ payload, addBreadcrumb }: actions.SetUserDetails
) {
const state = ctx.getState();
const id = +payload.id;
if (!state) {
return;
}
if (payload.features && payload.features.findIndex((v) => v.key === 'onlineshop') >= 0) {
const customers = { ...state.customers, [id]: { ...payload, id: id, error: null } };
if (
payload.features &&
payload.features.findIndex((v) => v.key === 'onlineshop') >= 0
) {
const customers = {
...state.customers,
[id]: { ...payload, id: id, error: null },
};
ctx.patchState({ customers });
this.syncApiState(customers, state.lastCreatedCustomerId, state.cachedCustomerSearch);
this.syncApiState(
customers,
state.lastCreatedCustomerId,
state.cachedCustomerSearch
);
return;
}
return this.customerService.updateCustomer(payload).pipe(
catchError((error) => {
console.error(error);
const customers = { ...state.customers, [id]: { ...state.customers[id], id: id, error: error } };
const customers = {
...state.customers,
[id]: { ...state.customers[id], id: id, error: error },
};
ctx.patchState({ customers });
return of(undefined);
}),
@@ -188,7 +242,10 @@ export class CustomerState {
if (!addedCustomer) {
return;
}
const customers = { ...state.customers, [id]: { ...payload, id: id, error: null } };
const customers = {
...state.customers,
[id]: { ...payload, id: id, error: null },
};
ctx.patchState({ customers });
if (addBreadcrumb) {
this.store.dispatch(
@@ -201,13 +258,20 @@ export class CustomerState {
)
);
}
this.syncApiState(customers, state.lastCreatedCustomerId, state.cachedCustomerSearch);
this.syncApiState(
customers,
state.lastCreatedCustomerId,
state.cachedCustomerSearch
);
})
);
}
@Action(actions.SetUserDetailsById)
getUserDetails(ctx: StateContext<CustomerStateModel>, { payload, addBreadcrumb }: actions.SetUserDetailsById) {
getUserDetails(
ctx: StateContext<CustomerStateModel>,
{ payload, addBreadcrumb }: actions.SetUserDetailsById
) {
const state = ctx.getState();
if (!state) {
return;
@@ -215,7 +279,12 @@ export class CustomerState {
return this.customerService.getCustomerById(payload).pipe(
tap((user: User) => {
if (user) {
ctx.setState(this.updateCustomer({ ...user, id: +payload }, state.lastCreatedCustomerId));
ctx.setState(
this.updateCustomer(
{ ...user, id: +payload },
state.lastCreatedCustomerId
)
);
}
})
);
@@ -229,7 +298,10 @@ export class CustomerState {
}
@Action(actions.DeleteCustomerForProcess)
deleteCustomerForProcess(ctx: StateContext<CustomerStateModel>, { payload }: actions.DeleteCustomerForProcess) {
deleteCustomerForProcess(
ctx: StateContext<CustomerStateModel>,
{ payload }: actions.DeleteCustomerForProcess
) {
const state = ctx.getState();
if (!state) {
return;
@@ -238,19 +310,29 @@ export class CustomerState {
delete customers[payload];
ctx.patchState({ customers });
this.store.dispatch(new RemoveCustomerId(payload));
this.syncApiState(customers, state.lastCreatedCustomerId, state.cachedCustomerSearch);
this.syncApiState(
customers,
state.lastCreatedCustomerId,
state.cachedCustomerSearch
);
return this.customerService.deleteCustomer(payload);
}
@Action(actions.SetDefaultShippingAddress)
setDefaultShippingAddress(ctx: StateContext<CustomerStateModel>, { customerId, shippingAddress }: actions.SetDefaultShippingAddress) {
setDefaultShippingAddress(
ctx: StateContext<CustomerStateModel>,
{ customerId, shippingAddress }: actions.SetDefaultShippingAddress
) {
const state = ctx.getState();
if (!state) {
return;
}
const currentCustomers = state.customers;
const shipping$ = this.customerService.setShippingAddressAsDefault(shippingAddress, customerId);
const shipping$ = this.customerService.setShippingAddressAsDefault(
shippingAddress,
customerId
);
if (!shipping$) {
return;
}
@@ -259,23 +341,38 @@ export class CustomerState {
const customer = currentCustomers[customerId];
const updatedCustomer = {
...customer,
delivery_addres: customer.poossible_delivery_addresses.find((t) => t.id === shippingAddress.id),
delivery_addres: customer.poossible_delivery_addresses.find(
(t) => t.id === shippingAddress.id
),
};
const customers = {
...currentCustomers,
[updatedCustomer.id]: updatedCustomer,
};
const customers = { ...currentCustomers, [updatedCustomer.id]: updatedCustomer };
ctx.patchState({ customers });
this.syncApiState(customers, state.lastCreatedCustomerId, state.cachedCustomerSearch);
this.syncApiState(
customers,
state.lastCreatedCustomerId,
state.cachedCustomerSearch
);
})
);
}
@Action(actions.SetDefaultInvoiceAddress)
setDefaultInvoiceAddress(ctx: StateContext<CustomerStateModel>, { customerId, invoiceAddress }: actions.SetDefaultInvoiceAddress) {
setDefaultInvoiceAddress(
ctx: StateContext<CustomerStateModel>,
{ customerId, invoiceAddress }: actions.SetDefaultInvoiceAddress
) {
const state = ctx.getState();
if (!state) {
return;
}
const currentCustomers = state.customers;
const invoice$ = this.customerService.setInvoiceAddressAsDefault(invoiceAddress, customerId);
const invoice$ = this.customerService.setInvoiceAddressAsDefault(
invoiceAddress,
customerId
);
if (!invoice$) {
return;
}
@@ -284,17 +381,29 @@ export class CustomerState {
const customer = currentCustomers[customerId];
const updatedCustomer = {
...customer,
invoice_address: customer.poossible_invoice_addresses.find((t) => t.id === invoiceAddress.id),
invoice_address: customer.poossible_invoice_addresses.find(
(t) => t.id === invoiceAddress.id
),
};
const customers = {
...currentCustomers,
[updatedCustomer.id]: updatedCustomer,
};
const customers = { ...currentCustomers, [updatedCustomer.id]: updatedCustomer };
ctx.patchState({ customers });
this.syncApiState(customers, state.lastCreatedCustomerId, state.cachedCustomerSearch);
this.syncApiState(
customers,
state.lastCreatedCustomerId,
state.cachedCustomerSearch
);
})
);
}
@Action(actions.AddNewShippingAddress)
addNewShippingAddress(ctx: StateContext<CustomerStateModel>, { customerId, shippingAddress }: actions.AddNewShippingAddress) {
addNewShippingAddress(
ctx: StateContext<CustomerStateModel>,
{ customerId, shippingAddress }: actions.AddNewShippingAddress
) {
const state = ctx.getState();
if (!state) {
return;
@@ -310,27 +419,53 @@ export class CustomerState {
updatedCustomer = <User>{
...customer,
delivery_addres: address,
poossible_delivery_addresses: [...customer.poossible_delivery_addresses.filter((t) => t.id !== address.id), address],
poossible_invoice_addresses: [...customer.poossible_invoice_addresses.filter((t) => t.id !== address.id), address],
poossible_delivery_addresses: [
...(customer.poossible_delivery_addresses.filter(
(t) => t.id !== address.id
) || []),
address,
],
poossible_invoice_addresses: [
...(customer.poossible_invoice_addresses.filter(
(t) => t.id !== address.id
) || []),
address,
],
};
} else {
updatedCustomer = <User>{
...customer,
delivery_addres: address,
poossible_delivery_addresses: [...customer.poossible_delivery_addresses, address],
poossible_invoice_addresses: [...customer.poossible_invoice_addresses, address],
poossible_delivery_addresses: [
...(customer.poossible_delivery_addresses || []),
address,
],
poossible_invoice_addresses: [
...(customer.poossible_invoice_addresses || []),
address,
],
};
}
this.store.dispatch(new actions.SetUserDetailsById(customerId));
const customers = { ...currentCustomers, [updatedCustomer.id]: updatedCustomer };
const customers = {
...currentCustomers,
[updatedCustomer.id]: updatedCustomer,
};
ctx.patchState({ customers });
this.syncApiState(customers, state.lastCreatedCustomerId, state.cachedCustomerSearch);
this.syncApiState(
customers,
state.lastCreatedCustomerId,
state.cachedCustomerSearch
);
})
);
}
@Action(actions.AddNewInvoiceAddress)
addNewInvoiceAddress(ctx: StateContext<CustomerStateModel>, { customerId, invoiceAddress }: actions.AddNewInvoiceAddress) {
addNewInvoiceAddress(
ctx: StateContext<CustomerStateModel>,
{ customerId, invoiceAddress }: actions.AddNewInvoiceAddress
) {
const state = ctx.getState();
if (!state) {
return;
@@ -346,27 +481,53 @@ export class CustomerState {
updatedCustomer = <User>{
...customer,
invoice_address: address,
poossible_delivery_addresses: [...customer.poossible_delivery_addresses.filter((t) => t && t.id !== address.id), address],
poossible_invoice_addresses: [...customer.poossible_invoice_addresses.filter((t) => t && t.id !== address.id), address],
poossible_delivery_addresses: [
...(customer.poossible_delivery_addresses.filter(
(t) => t && t.id !== address.id
) || []),
address,
],
poossible_invoice_addresses: [
...(customer.poossible_invoice_addresses.filter(
(t) => t && t.id !== address.id
) || []),
address,
],
};
} else {
updatedCustomer = <User>{
...customer,
invoice_address: address,
poossible_delivery_addresses: [...customer.poossible_delivery_addresses, address],
poossible_invoice_addresses: [...customer.poossible_invoice_addresses, address],
poossible_delivery_addresses: [
...(customer.poossible_delivery_addresses || []),
address,
],
poossible_invoice_addresses: [
...(customer.poossible_invoice_addresses || []),
address,
],
};
}
this.store.dispatch(new actions.SetUserDetailsById(customerId));
const customers = { ...currentCustomers, [updatedCustomer.id]: updatedCustomer };
const customers = {
...currentCustomers,
[updatedCustomer.id]: updatedCustomer,
};
ctx.patchState({ customers });
this.syncApiState(customers, state.lastCreatedCustomerId, state.cachedCustomerSearch);
this.syncApiState(
customers,
state.lastCreatedCustomerId,
state.cachedCustomerSearch
);
})
);
}
@Action(actions.SetLastCreatedCustomerId)
setLastCreatedCustomerId(ctx: StateContext<CustomerStateModel>, { customerId }: actions.SetLastCreatedCustomerId) {
setLastCreatedCustomerId(
ctx: StateContext<CustomerStateModel>,
{ customerId }: actions.SetLastCreatedCustomerId
) {
const state = ctx.getState();
if (!state) {
return;
@@ -376,30 +537,45 @@ export class CustomerState {
}
@Action(actions.SetCachedCustomers)
setCachedCustomers(ctx: StateContext<CustomerStateModel>, { cachedCustomerSearch }: actions.SetCachedCustomers) {
setCachedCustomers(
ctx: StateContext<CustomerStateModel>,
{ cachedCustomerSearch }: actions.SetCachedCustomers
) {
const state = ctx.getState();
const currentProcessId = this.store.selectSnapshot(AppState.getCurrentProcessId);
const currentProcessId = this.store.selectSnapshot(
AppState.getCurrentProcessId
);
if (currentProcessId) {
const cachedCustomers = { ...state.cachedCustomerSearch };
cachedCustomers[currentProcessId] = cachedCustomerSearch;
ctx.patchState({ cachedCustomerSearch: cachedCustomers });
this.syncApiState(state.customers, state.lastCreatedCustomerId, cachedCustomers);
this.syncApiState(
state.customers,
state.lastCreatedCustomerId,
cachedCustomers
);
}
}
@Action(actions.ClearCachedCustomers)
clearCachedCustomers(ctx: StateContext<CustomerStateModel>) {
const state = ctx.getState();
const currentProcessId = this.store.selectSnapshot(AppState.getCurrentProcessId);
const currentProcessId = this.store.selectSnapshot(
AppState.getCurrentProcessId
);
if (currentProcessId) {
const cachedCustomers = { ...state.cachedCustomerSearch };
cachedCustomers[currentProcessId] = undefined;
ctx.patchState({ cachedCustomerSearch: cachedCustomers });
this.syncApiState(state.customers, state.lastCreatedCustomerId, cachedCustomers);
this.syncApiState(
state.customers,
state.lastCreatedCustomerId,
cachedCustomers
);
}
}
@@ -414,7 +590,11 @@ export class CustomerState {
@Action(actions.ReloadCustomersData)
reloadProcesses(
ctx: StateContext<CustomerStateModel>,
{ customers, lastCreatedCustomerId, cachedCustomerSearch }: actions.ReloadCustomersData
{
customers,
lastCreatedCustomerId,
cachedCustomerSearch,
}: actions.ReloadCustomersData
) {
ctx.patchState({ customers, lastCreatedCustomerId, cachedCustomerSearch });
}

View File

@@ -1,14 +1,36 @@
import { Store, Select } from '@ngxs/store';
import { Component, OnInit, OnDestroy, Input, ViewChild, ChangeDetectorRef } from '@angular/core';
import { FormGroup, FormBuilder, Validators, AbstractControl } from '@angular/forms';
import {
Component,
OnInit,
OnDestroy,
Input,
ViewChild,
ChangeDetectorRef,
} from '@angular/core';
import {
FormGroup,
FormBuilder,
Validators,
AbstractControl,
} from '@angular/forms';
import { Router, ActivatedRoute } from '@angular/router';
import { Subject, Observable, of } from 'rxjs';
import { SharedSelectors } from '../../../../core/store/selectors/shared.selectors';
import { EditCustomerData } from '../../../../core/models/edit-customer.model';
import { takeUntil, distinctUntilChanged, take, map, filter, switchMap } from 'rxjs/operators';
import {
takeUntil,
distinctUntilChanged,
take,
map,
filter,
switchMap,
} from 'rxjs/operators';
import { User, Address } from '../../../../core/models/user.model';
import { ChangeCurrentRoute } from 'apps/sales/src/app/core/store/actions/process.actions';
import { AddNewShippingAddress, AddNewInvoiceAddress } from 'apps/sales/src/app/core/store/actions/customer.actions';
import {
AddNewShippingAddress,
AddNewInvoiceAddress,
} from 'apps/sales/src/app/core/store/actions/customer.actions';
import { GENDERS, TIITLES } from '../../dropdown-values';
import { CountrySelector } from 'apps/sales/src/app/core/store/selectors/countries.selector';
import { Country } from 'apps/sales/src/app/core/models/country.model';
@@ -35,9 +57,14 @@ export class CustomerAddressFormComponent implements OnInit, OnDestroy {
@Input() title: string;
@Input() addressId: string;
@Input() isBillingForm = true;
@Select(SharedSelectors.getCustomerEditData) customerEditData$: Observable<EditCustomerData>;
@Select(CountrySelector.getCountriesIterable) countries$: Observable<Country[]>;
@ViewChild('suggestions', { static: false }) suggestions: AddressSugestionsComponent;
@Select(SharedSelectors.getCustomerEditData) customerEditData$: Observable<
EditCustomerData
>;
@Select(CountrySelector.getCountriesIterable) countries$: Observable<
Country[]
>;
@ViewChild('suggestions', { static: false })
suggestions: AddressSugestionsComponent;
@ViewChild('postBtn', { static: false }) postBtn: ButtonComponent;
form: FormGroup;
submitted = false;
@@ -74,7 +101,7 @@ export class CustomerAddressFormComponent implements OnInit, OnDestroy {
private cdrf: ChangeDetectorRef,
private errorService: ErrorService,
private route: ActivatedRoute
) { }
) {}
ngOnInit(): void {
if (this.customerInput) {
@@ -82,9 +109,13 @@ export class CustomerAddressFormComponent implements OnInit, OnDestroy {
this.customer = this.customerInput;
if (this.isBillingForm) {
this.selecetedAddress = this.customerInput.poossible_invoice_addresses.find((address: Address) => address.id === +this.addressId);
this.selecetedAddress = this.customerInput.poossible_invoice_addresses.find(
(address: Address) => address.id === +this.addressId
);
} else {
this.selecetedAddress = this.customerInput.poossible_delivery_addresses.find((address: Address) => address.id === +this.addressId);
this.selecetedAddress = this.customerInput.poossible_delivery_addresses.find(
(address: Address) => address.id === +this.addressId
);
}
this.buildForm(this.fb, this.selecetedAddress);
this.onFormChange();
@@ -113,11 +144,13 @@ export class CustomerAddressFormComponent implements OnInit, OnDestroy {
this.countryList = data;
});
this.route.queryParams.pipe(takeUntil(this.destroy$)).subscribe((queryParams) => {
if (queryParams && queryParams['notEditable']) {
this.notEditable = true;
}
});
this.route.queryParams
.pipe(takeUntil(this.destroy$))
.subscribe((queryParams) => {
if (queryParams && queryParams['notEditable']) {
this.notEditable = true;
}
});
this.setCompanyValidators();
}
@@ -126,7 +159,9 @@ export class CustomerAddressFormComponent implements OnInit, OnDestroy {
// store address form data on changes
this.form.valueChanges.pipe(takeUntil(this.destroy$)).subscribe((v) => {
const formStateString = JSON.stringify(v);
this.store.dispatch(new SaveFormState(ADDRESS_FORM_STATE_KEY, formStateString));
this.store.dispatch(
new SaveFormState(ADDRESS_FORM_STATE_KEY, formStateString)
);
});
}
@@ -155,8 +190,15 @@ export class CustomerAddressFormComponent implements OnInit, OnDestroy {
if (key !== 'FirstName' && key !== 'LastName') {
showErrorPopUp = true;
}
if (keysChecked === Object.keys(invalidProperties).length && showErrorPopUp) {
this.errorService.addErrors(409, null, JSON.stringify(invalidProperties));
if (
keysChecked === Object.keys(invalidProperties).length &&
showErrorPopUp
) {
this.errorService.addErrors(
409,
null,
JSON.stringify(invalidProperties)
);
}
});
scrollToFirstInvalidElement();
@@ -216,7 +258,12 @@ export class CustomerAddressFormComponent implements OnInit, OnDestroy {
}
if (this.isBillingForm) {
this.store
.dispatch(new AddNewInvoiceAddress(updatedCustomer.id, updatedCustomer.invoice_address))
.dispatch(
new AddNewInvoiceAddress(
updatedCustomer.id,
updatedCustomer.invoice_address
)
)
.pipe(
takeUntil(this.destroy$),
switchMap(() => {
@@ -228,16 +275,25 @@ export class CustomerAddressFormComponent implements OnInit, OnDestroy {
this.addAddressErrorHandler(invalidProperties);
this.postBtn.stopLoading();
} else {
const queryParams = this.notEditable ? { notEditable: true } : undefined;
const queryParams = this.notEditable
? { notEditable: true }
: undefined;
const currentRoute = `customer/edit/${this.customer.id}/billing`;
this.store.dispatch(new ChangeCurrentRoute(currentRoute, false, queryParams));
this.store.dispatch(
new ChangeCurrentRoute(currentRoute, false, queryParams)
);
this.postBtn.stopLoading();
this.router.navigate([currentRoute], { queryParams: queryParams });
}
});
} else {
this.store
.dispatch(new AddNewShippingAddress(updatedCustomer.id, updatedCustomer.delivery_addres))
.dispatch(
new AddNewShippingAddress(
updatedCustomer.id,
updatedCustomer.delivery_addres
)
)
.pipe(
takeUntil(this.destroy$),
switchMap(() => {
@@ -249,9 +305,13 @@ export class CustomerAddressFormComponent implements OnInit, OnDestroy {
this.addAddressErrorHandler(invalidProperties);
this.postBtn.stopLoading();
} else {
const queryParams = this.notEditable ? { notEditable: true } : undefined;
const queryParams = this.notEditable
? { notEditable: true }
: undefined;
const currentRoute = `customer/edit/${this.customer.id}/delivery`;
this.store.dispatch(new ChangeCurrentRoute(currentRoute, false, queryParams));
this.store.dispatch(
new ChangeCurrentRoute(currentRoute, false, queryParams)
);
this.postBtn.stopLoading();
this.router.navigate([currentRoute], { queryParams: queryParams });
}
@@ -324,7 +384,9 @@ export class CustomerAddressFormComponent implements OnInit, OnDestroy {
}
countryKey(country: string) {
return this.store.selectSnapshot(CountrySelector.getCountriesIterable).find((t) => t.value === country).key;
return this.store
.selectSnapshot(CountrySelector.getCountriesIterable)
.find((t) => t.value === country).key;
}
private updateUserData(): User {
@@ -332,25 +394,29 @@ export class CustomerAddressFormComponent implements OnInit, OnDestroy {
const updatedCustomer: User = { ...this.customer };
if (this.isBillingForm) {
updatedCustomer.poossible_invoice_addresses = updatedCustomer.poossible_invoice_addresses.map((address: Address) => {
if (address.id === +this.addressId) {
return updateAddress;
}
updatedCustomer.poossible_invoice_addresses = updatedCustomer.poossible_invoice_addresses.map(
(address: Address) => {
if (address.id === +this.addressId) {
return updateAddress;
}
return address;
});
return address;
}
);
if (updatedCustomer.invoice_address.id === updateAddress.id) {
updatedCustomer.invoice_address = updateAddress;
}
} else {
updatedCustomer.poossible_delivery_addresses = updatedCustomer.poossible_delivery_addresses.map((address: Address) => {
if (address.id === +this.addressId) {
return updateAddress;
}
updatedCustomer.poossible_delivery_addresses = updatedCustomer.poossible_delivery_addresses.map(
(address: Address) => {
if (address.id === +this.addressId) {
return updateAddress;
}
return address;
});
return address;
}
);
if (updatedCustomer.delivery_addres.id === updateAddress.id) {
updatedCustomer.delivery_addres = updateAddress;
@@ -365,10 +431,16 @@ export class CustomerAddressFormComponent implements OnInit, OnDestroy {
const updatedCustomer: User = { ...this.customer };
if (this.isBillingForm) {
updatedCustomer.poossible_invoice_addresses = [...updatedCustomer.poossible_invoice_addresses, newAddress];
updatedCustomer.poossible_invoice_addresses = [
...(updatedCustomer.poossible_invoice_addresses || []),
newAddress,
];
updatedCustomer.invoice_address = newAddress;
} else {
updatedCustomer.poossible_delivery_addresses = [...updatedCustomer.poossible_delivery_addresses, newAddress];
updatedCustomer.poossible_delivery_addresses = [
...(updatedCustomer.poossible_delivery_addresses || []),
newAddress,
];
updatedCustomer.delivery_addres = newAddress;
}
@@ -417,9 +489,13 @@ export class CustomerAddressFormComponent implements OnInit, OnDestroy {
this.form.get('city').value,
this.countryKey(this.form.get('country').value),
this.form.get('company').value ? this.form.get('company').value : null,
this.form.get('department').value ? this.form.get('department').value : null,
this.form.get('department').value
? this.form.get('department').value
: null,
this.form.get('note').value ? this.form.get('note').value : null,
this.form.get('tax_number').value ? this.form.get('tax_number').value : null,
this.form.get('tax_number').value
? this.form.get('tax_number').value
: null,
this.form.get('title').value ? this.form.get('title').value : null,
this.form.get('gender').value ? this.form.get('gender').value : null
);
@@ -427,7 +503,11 @@ export class CustomerAddressFormComponent implements OnInit, OnDestroy {
return data;
}
private updateDataForSelectedOption(field: string, value: string, defaultValue: string) {
private updateDataForSelectedOption(
field: string,
value: string,
defaultValue: string
) {
if (value !== defaultValue) {
this.form.get(field).patchValue(value);
} else {
@@ -448,33 +528,95 @@ export class CustomerAddressFormComponent implements OnInit, OnDestroy {
.subscribe((value) => {
const savedAddress = value ? JSON.parse(value) : undefined;
this.form = fb.group({
id: [address ? address.id : this.getProperty(savedAddress, 'id') || ''],
firstName: [address ? address.first_name : this.getProperty(savedAddress, 'firstName') || '', Validators.required],
lastName: [address ? address.last_name : this.getProperty(savedAddress, 'lastName') || '', Validators.required],
id: [
address ? address.id : this.getProperty(savedAddress, 'id') || '',
],
firstName: [
address
? address.first_name
: this.getProperty(savedAddress, 'firstName') || '',
Validators.required,
],
lastName: [
address
? address.last_name
: this.getProperty(savedAddress, 'lastName') || '',
Validators.required,
],
address: [
address ? (address.street ? address.street : '') : this.getProperty(savedAddress, 'address') || '',
address
? address.street
? address.street
: ''
: this.getProperty(savedAddress, 'address') || '',
Validators.required,
],
streetNo: [
address ? (address.streetNo ? address.streetNo : '') : this.getProperty(savedAddress, 'streetNo') || '',
address
? address.streetNo
? address.streetNo
: ''
: this.getProperty(savedAddress, 'streetNo') || '',
Validators.required,
],
zipCode: [
address
? address.zip
? address.zip
: ''
: this.getProperty(savedAddress, 'zipCode') || '',
Validators.required,
],
city: [
address
? address.city
? address.city
: ''
: this.getProperty(savedAddress, 'city') || '',
Validators.required,
],
country: [
this.getProperty(savedAddress, 'country') ||
this.getCountryFromAddress(address),
Validators.required,
],
zipCode: [address ? (address.zip ? address.zip : '') : this.getProperty(savedAddress, 'zipCode') || '', Validators.required],
city: [address ? (address.city ? address.city : '') : this.getProperty(savedAddress, 'city') || '', Validators.required],
country: [this.getProperty(savedAddress, 'country') || this.getCountryFromAddress(address), Validators.required],
gender: [
address ? address.gender : this.getProperty(savedAddress, 'gender') || '',
address
? address.gender
: this.getProperty(savedAddress, 'gender') || '',
[Validators.required, CustomValidators.validateGender],
],
company: [address ? (address.company_name ? address.company_name : '') : this.getProperty(savedAddress, 'company') || ''],
company: [
address
? address.company_name
? address.company_name
: ''
: this.getProperty(savedAddress, 'company') || '',
],
department: [
address ? (address.company_department ? address.company_department : '') : this.getProperty(savedAddress, 'department') || '',
address
? address.company_department
? address.company_department
: ''
: this.getProperty(savedAddress, 'department') || '',
],
tax_number: [
address ? (address.company_tax_number ? address.company_tax_number : '') : this.getProperty(savedAddress, 'tax_number') || '',
address
? address.company_tax_number
? address.company_tax_number
: ''
: this.getProperty(savedAddress, 'tax_number') || '',
],
title: [
address
? address.title
: this.getProperty(savedAddress, 'title') || '',
],
note: [
address
? address.note
: this.getProperty(savedAddress, 'note') || '',
],
title: [address ? address.title : this.getProperty(savedAddress, 'title') || ''],
note: [address ? address.note : this.getProperty(savedAddress, 'note') || ''],
});
});
}
@@ -484,7 +626,9 @@ export class CustomerAddressFormComponent implements OnInit, OnDestroy {
return '';
}
const countries = this.store.selectSnapshot(CountrySelector.getCountriesIterable);
const countries = this.store.selectSnapshot(
CountrySelector.getCountriesIterable
);
const country = countries.find((t) => t.key === address.country);
if (isNullOrUndefined(country)) {
return address.country;
@@ -502,11 +646,21 @@ export class CustomerAddressFormComponent implements OnInit, OnDestroy {
const addressesLength = address.length;
address.forEach((add) => {
processedItems++;
const street = add.street ? add.street : (this.form.get('address').value as string);
const streetNo = add.streetNumber ? add.streetNumber : (this.form.get('streetNo').value as string);
const zipCode = add.zipCode ? add.zipCode : (this.form.get('zipCode').value as string);
const city = add.city ? add.city : (this.form.get('city').value as string);
const countryValue = add.country ? add.country : (this.form.get('country').value as string);
const street = add.street
? add.street
: (this.form.get('address').value as string);
const streetNo = add.streetNumber
? add.streetNumber
: (this.form.get('streetNo').value as string);
const zipCode = add.zipCode
? add.zipCode
: (this.form.get('zipCode').value as string);
const city = add.city
? add.city
: (this.form.get('city').value as string);
const countryValue = add.country
? add.country
: (this.form.get('country').value as string);
if (city && street && streetNo && zipCode && countryValue) {
this.addressSuggestions.push({
@@ -520,7 +674,10 @@ export class CustomerAddressFormComponent implements OnInit, OnDestroy {
zip: zipCode,
});
}
if (processedItems === addressesLength && this.addressSuggestions.length === addressesLength) {
if (
processedItems === addressesLength &&
this.addressSuggestions.length === addressesLength
) {
this.cdrf.detectChanges();
this.openSuggestions();
}

View File

@@ -55,7 +55,6 @@ export class CustomerSearchDataSource extends DataSource<User | undefined> {
this.dataStream
.pipe(takeUntil(this.destroy$), debounceTime(1000))
.subscribe((i) => {
console.log({ i });
this.store.dispatch(new AddCustomers([...i]));
});