Skip to content

Commit

Permalink
fix(admin-ui): Save custom fields in the Customer detail view
Browse files Browse the repository at this point in the history
Fixes #387
  • Loading branch information
michaelbromley committed Jul 2, 2020
1 parent 67b80ad commit 3c45b16
Showing 1 changed file with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ export class CustomerDetailComponent extends BaseDetailComponent<CustomerWithOrd
this.init();
this.availableCountries$ = this.dataService.settings
.getAvailableCountries()
.mapSingle(result => result.countries.items)
.mapSingle((result) => result.countries.items)
.pipe(shareReplay(1));

const customerWithUpdates$ = this.entity$.pipe(merge(this.orderListUpdates$));
this.orders$ = customerWithUpdates$.pipe(map(customer => customer.orders.items));
this.ordersCount$ = this.entity$.pipe(map(customer => customer.orders.totalItems));
this.orders$ = customerWithUpdates$.pipe(map((customer) => customer.orders.items));
this.ordersCount$ = this.entity$.pipe(map((customer) => customer.orders.totalItems));
this.history$ = this.fetchHistory.pipe(
startWith(null),
switchMap(() => {
Expand All @@ -109,7 +109,7 @@ export class CustomerDetailComponent extends BaseDetailComponent<CustomerWithOrd
createdAt: SortOrder.DESC,
},
})
.mapStream(data => data.customer?.history.items);
.mapStream((data) => data.customer?.history.items);
}),
);
}
Expand Down Expand Up @@ -172,15 +172,17 @@ export class CustomerDetailComponent extends BaseDetailComponent<CustomerWithOrd
return;
}
const formValue = customerForm.value;
const customFields = customerForm.get('customFields')?.value;
const customer: CreateCustomerInput = {
title: formValue.title,
emailAddress: formValue.emailAddress,
firstName: formValue.firstName,
lastName: formValue.lastName,
phoneNumber: formValue.phoneNumber,
customFields,
};
this.dataService.customer.createCustomer(customer, formValue.password).subscribe(
data => {
(data) => {
this.notificationService.success(_('common.notify-create-success'), {
entity: 'Customer',
});
Expand All @@ -197,7 +199,7 @@ export class CustomerDetailComponent extends BaseDetailComponent<CustomerWithOrd
this.changeDetector.markForCheck();
this.router.navigate(['../', data.createCustomer.id], { relativeTo: this.route });
},
err => {
(err) => {
this.notificationService.error(_('common.notify-create-error'), {
entity: 'Customer',
});
Expand All @@ -214,13 +216,15 @@ export class CustomerDetailComponent extends BaseDetailComponent<CustomerWithOrd
const customerForm = this.detailForm.get('customer');
if (customerForm && customerForm.dirty) {
const formValue = customerForm.value;
const customFields = customerForm.get('customFields')?.value;
const customer: UpdateCustomerInput = {
id,
title: formValue.title,
emailAddress: formValue.emailAddress,
firstName: formValue.firstName,
lastName: formValue.lastName,
phoneNumber: formValue.phoneNumber,
customFields,
};
saveOperations.push(this.dataService.customer.updateCustomer(customer));
}
Expand Down Expand Up @@ -261,7 +265,7 @@ export class CustomerDetailComponent extends BaseDetailComponent<CustomerWithOrd
}),
)
.subscribe(
data => {
(data) => {
this.notificationService.success(_('common.notify-update-success'), {
entity: 'Customer',
});
Expand All @@ -270,7 +274,7 @@ export class CustomerDetailComponent extends BaseDetailComponent<CustomerWithOrd
this.changeDetector.markForCheck();
this.fetchHistory.next();
},
err => {
(err) => {
this.notificationService.error(_('common.notify-update-error'), {
entity: 'Customer',
});
Expand All @@ -284,11 +288,11 @@ export class CustomerDetailComponent extends BaseDetailComponent<CustomerWithOrd
size: 'md',
})
.pipe(
switchMap(groupIds => (groupIds ? from(groupIds) : EMPTY)),
concatMap(groupId => this.dataService.customer.addCustomersToGroup(groupId, [this.id])),
switchMap((groupIds) => (groupIds ? from(groupIds) : EMPTY)),
concatMap((groupId) => this.dataService.customer.addCustomersToGroup(groupId, [this.id])),
)
.subscribe({
next: res => {
next: (res) => {
this.notificationService.success(_(`customer.add-customers-to-group-success`), {
customerCount: 1,
groupName: res.addCustomersToGroup.name,
Expand All @@ -311,14 +315,14 @@ export class CustomerDetailComponent extends BaseDetailComponent<CustomerWithOrd
],
})
.pipe(
switchMap(response =>
switchMap((response) =>
response
? this.dataService.customer.removeCustomersFromGroup(group.id, [this.id])
: EMPTY,
),
switchMap(() => this.dataService.customer.getCustomer(this.id, { take: 0 }).single$),
)
.subscribe(result => {
.subscribe((result) => {
this.notificationService.success(_(`customer.remove-customers-from-group-success`), {
customerCount: 1,
groupName: group.name,
Expand Down Expand Up @@ -346,7 +350,7 @@ export class CustomerDetailComponent extends BaseDetailComponent<CustomerWithOrd
},
})
.pipe(
switchMap(result => {
switchMap((result) => {
if (result) {
return this.dataService.customer.updateCustomerNote({
noteId: entry.id,
Expand All @@ -357,7 +361,7 @@ export class CustomerDetailComponent extends BaseDetailComponent<CustomerWithOrd
}
}),
)
.subscribe(result => {
.subscribe((result) => {
this.fetchHistory.next();
this.notificationService.success(_('common.notify-update-success'), {
entity: 'Note',
Expand All @@ -375,7 +379,7 @@ export class CustomerDetailComponent extends BaseDetailComponent<CustomerWithOrd
{ type: 'danger', label: _('common.delete'), returnValue: true },
],
})
.pipe(switchMap(res => (res ? this.dataService.customer.deleteCustomerNote(entry.id) : EMPTY)))
.pipe(switchMap((res) => (res ? this.dataService.customer.deleteCustomerNote(entry.id) : EMPTY)))
.subscribe(() => {
this.fetchHistory.next();
this.notificationService.success(_('common.notify-delete-success'), {
Expand Down Expand Up @@ -417,7 +421,7 @@ export class CustomerDetailComponent extends BaseDetailComponent<CustomerWithOrd

for (const fieldDef of this.customFields) {
const key = fieldDef.name;
const value = (entity as any).customFields[key];
const value = (entity as any).customFields?.[key];
const control = customFieldsGroup.get(key);
if (control) {
control.patchValue(value);
Expand All @@ -437,9 +441,9 @@ export class CustomerDetailComponent extends BaseDetailComponent<CustomerWithOrd
skip: (this.currentOrdersPage - 1) * this.ordersPerPage,
})
.single$.pipe(
map(data => data.customer),
map((data) => data.customer),
filter(notNullOrUndefined),
)
.subscribe(result => this.orderListUpdates$.next(result));
.subscribe((result) => this.orderListUpdates$.next(result));
}
}

0 comments on commit 3c45b16

Please sign in to comment.