Skip to content

Commit

Permalink
fix(admin-ui): Correctly handle defaults for configurable operations
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Sep 30, 2019
1 parent e486b61 commit 9bd6a79
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ConfigArgType } from 'shared/shared-types';
import { assertNever } from 'shared/shared-utils';

import { ConfigArg, ConfigArgDefinition } from '../generated-types';

export function getDefaultConfigArgValue(arg: ConfigArg | ConfigArgDefinition): any {
const type = arg.type as ConfigArgType;
switch (type) {
case 'boolean':
return false;
case 'int':
case 'float':
return '0';
case 'facetValueIds':
return [];
case 'string':
return '';
case 'datetime':
return new Date();
default:
assertNever(type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
Promotion,
UpdatePromotionInput,
} from '../../../common/generated-types';
import { getDefaultConfigArgValue } from '../../../common/utilities/get-default-config-arg-value';
import { _ } from '../../../core/providers/i18n/mark-for-extraction';
import { NotificationService } from '../../../core/providers/notification/notification.service';
import { DataService } from '../../../data/providers/data.service';
Expand Down Expand Up @@ -233,7 +234,7 @@ export class PromotionDetailComponent extends BaseDetailComponent<Promotion.Frag
const argsHash = operation.args.reduce(
(output, arg) => ({
...output,
[arg.name]: arg.value,
[arg.name]: getDefaultConfigArgValue(arg),
}),
{},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class="btn btn-primary"
*ngIf="isNew$ | async; else updateButton"
(click)="create()"
[disabled]="detailForm.pristine || detailForm.invalid"
[disabled]="detailForm.pristine || detailForm.invalid || !selectedChecker || !selectedCalculator"
>
{{ 'common.create' | translate }}
</button>
Expand All @@ -16,7 +16,7 @@
class="btn btn-primary"
(click)="save()"
*vdrIfPermissions="'UpdateSettings'"
[disabled]="detailForm.pristine || detailForm.invalid"
[disabled]="detailForm.pristine || detailForm.invalid || !selectedChecker || !selectedCalculator"
>
{{ 'common.update' | translate }}
</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { normalizeString } from 'shared/normalize-string';

import { BaseDetailComponent } from '../../../common/base-detail.component';
import {
ConfigArg,
ConfigurableOperation,
ConfigurableOperationDefinition,
ConfigurableOperationInput,
Expand All @@ -18,6 +17,7 @@ import {
TestShippingMethodResult,
UpdateShippingMethodInput,
} from '../../../common/generated-types';
import { getDefaultConfigArgValue } from '../../../common/utilities/get-default-config-arg-value';
import { _ } from '../../../core/providers/i18n/mark-for-extraction';
import { NotificationService } from '../../../core/providers/notification/notification.service';
import { DataService } from '../../../data/providers/data.service';
Expand Down Expand Up @@ -133,11 +133,21 @@ export class ShippingMethodDetailComponent extends BaseDetailComponent<ShippingM
selectChecker(checker: ConfigurableOperationDefinition) {
this.selectedCheckerDefinition = checker;
this.selectedChecker = this.configurableDefinitionToInstance(checker);
const formControl = this.detailForm.get('checker');
if (formControl) {
formControl.patchValue(this.selectedChecker);
}
this.detailForm.markAsDirty();
}

selectCalculator(calculator: ConfigurableOperationDefinition) {
this.selectedCalculatorDefinition = calculator;
this.selectedCalculator = this.configurableDefinitionToInstance(calculator);
const formControl = this.detailForm.get('calculator');
if (formControl) {
formControl.patchValue(this.selectedCalculator);
}
this.detailForm.markAsDirty();
}

private configurableDefinitionToInstance(def: ConfigurableOperationDefinition): ConfigurableOperation {
Expand All @@ -146,7 +156,7 @@ export class ShippingMethodDetailComponent extends BaseDetailComponent<ShippingM
args: def.args.map(arg => {
return {
...arg,
value: '',
value: getDefaultConfigArgValue(arg),
};
}),
} as ConfigurableOperation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
import { Subscription } from 'rxjs';
import { StringFieldOption } from 'shared/generated-types';
import { ConfigArgType } from 'shared/shared-types';
import { assertNever } from 'shared/shared-utils';

import {
ConfigArg,
Expand All @@ -31,6 +32,7 @@ import {
FacetWithValues,
GetActiveChannel,
} from '../../../common/generated-types';
import { getDefaultConfigArgValue } from '../../../common/utilities/get-default-config-arg-value';
import { interpolateDescription } from '../../../common/utilities/interpolate-description';

/**
Expand Down Expand Up @@ -178,8 +180,12 @@ export class ConfigurableInputComponent implements OnChanges, OnDestroy, Control
if (this.operation.args) {
for (const arg of this.operation.args) {
let value: any = arg.value;
if (arg.type === 'boolean') {
value = arg.value === 'true';
if (value === undefined) {
value = getDefaultConfigArgValue(arg);
} else {
if (arg.type === 'boolean') {
value = arg.value === 'true';
}
}
this.form.addControl(arg.name, new FormControl(value, Validators.required));
}
Expand Down

0 comments on commit 9bd6a79

Please sign in to comment.