Skip to content

Commit

Permalink
chore: improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
richardtreier committed Jan 16, 2024
1 parent fa465c4 commit dd36ff4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import {
Component,
HostBinding,
Input,
OnChanges,
SimpleChanges,
} from '@angular/core';
import {Component, HostBinding, Input, OnChanges} from '@angular/core';
import {FormControl} from '@angular/forms';
import {SimpleChangesTyped} from '../../../core/utils/angular-utils';
import {DataAddressType} from './data-address-type';
import {dataAddressTypeSelectItems} from './data-address-type-select-items';
import {DataAddressTypeSelectMode} from './data-address-type-select-mode';
Expand All @@ -29,7 +24,7 @@ export class DataAddressTypeSelectComponent implements OnChanges {
mode: DataAddressTypeSelectMode = 'Datasource';

items = dataAddressTypeSelectItems(this.mode);
ngOnChanges(changes: SimpleChanges) {
ngOnChanges(changes: SimpleChangesTyped<DataAddressTypeSelectComponent>) {
if (changes.mode) {
this.items = dataAddressTypeSelectItems(this.mode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ function patchAsset(assetId: string, patcher: Patcher<UiAsset>): UiAsset {
}

export const createAsset = (asset: UiAssetCreateRequest): IdResponseDto => {
const assetId = asset.id;
assets.push({
...metadata(asset.id, asset),
assetId,
...metadata(assetId, asset),
connectorEndpoint: 'https://my-connector/api/dsp',
participantId: 'MDSL1234XX.C1234XX',
creatorOrganizationName: 'My Org',
Expand All @@ -54,9 +56,19 @@ export const editAssetMetadata = (
function metadata(
assetId: string,
request: UiAssetCreateRequest | UiAssetEditMetadataRequest,
) {
): Omit<
UiAsset,
| 'assetId'
| 'assetJsonLd'
| 'connectorEndpoint'
| 'creatorOrganizationName'
| 'httpDatasourceHintsProxyBody'
| 'httpDatasourceHintsProxyMethod'
| 'httpDatasourceHintsProxyPath'
| 'httpDatasourceHintsProxyQueryParams'
| 'participantId'
> {
return {
assetId,
title: request.title ?? assetId,
description: request.description,
descriptionShortText: request.description,
Expand Down
21 changes: 10 additions & 11 deletions src/app/core/services/asset-create-request-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,32 @@ export class AssetCreateRequestBuilder {
* Build {@link UiAssetCreateRequest} from {@link AssetEditorDialogFormValue}
*
* @param formValue form value
* @return asset create dto
* @return {@link UiAssetCreateRequest}
*/
buildAssetCreateRequest(
formValue: AssetEditorDialogFormValue,
): UiAssetCreateRequest {
const metadata = this.buildMetadata(formValue);
const id = formValue.metadata?.id!;
const metadata = this.buildEditMetadataRequest(formValue);
const dataAddressProperties =
this.dataAddressMapper.buildDataAddressProperties(formValue.datasource);

return {
id,
...metadata,
dataAddressProperties,
};
}

/**
* Build {@link UiAssetEditMetadataRequest} from {@link AssetEditorDialogFormValue}
*
* @param formValue form value
* @return {@link UiAssetEditMetadataRequest}
*/
buildEditMetadataRequest(
formValue: AssetEditorDialogFormValue,
): UiAssetEditMetadataRequest {
const {id, ...metadata} = this.buildMetadata(formValue);
return metadata;
}

private buildMetadata(
formValue: AssetEditorDialogFormValue,
): Omit<UiAssetCreateRequest, 'dataAddressProperties'> {
const id = formValue.metadata?.id!;
const title = formValue.metadata?.title!;
const version = formValue.metadata?.version;
const description = formValue.metadata?.description;
Expand All @@ -57,7 +57,6 @@ export class AssetCreateRequestBuilder {
const dataModel = formValue.advanced?.dataModel;

return {
id,
title,
language,
description,
Expand Down
15 changes: 11 additions & 4 deletions src/app/core/utils/angular-utils.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import {Input, SimpleChange, SimpleChanges} from '@angular/core';
import {Input, SimpleChanges} from '@angular/core';
import {Subject} from 'rxjs';

/**
* A version of {@link SimpleChanges} with some type safety.
* A type-safe version of {@link SimpleChanges}.
*
* Although it doesn't filter input by whether they are decorated by an {@link Input}, it helps
* Does not contain all {@link Input}s, but only simple fields.
*/
export type SimpleChangesTyped<
Component extends object,
Props = ExcludeFunctions<Component>,
> = {
[Key in keyof Props]: SimpleChange;
[Key in keyof Props]: SimpleChangeTyped<Props[Key]>;
};

export type SimpleChangeTyped<T> = {
previousValue: T;
currentValue: T;
firstChange: boolean;
isFirstChange(): boolean;
};

type MarkFunctionPropertyNames<Component> = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import {
OnDestroy,
OnInit,
Output,
SimpleChanges,
} from '@angular/core';
import {FormControl} from '@angular/forms';
import {Subject} from 'rxjs';
import {map} from 'rxjs/operators';
import {SimpleChangesTyped} from '../../../../core/utils/angular-utils';
import {FilterBoxItem} from './filter-box-item';
import {FilterBoxVisibleState} from './filter-box-visible-state';

Expand Down Expand Up @@ -42,7 +42,7 @@ export class FilterBoxComponent implements OnInit, OnChanges, OnDestroy {
});
}

ngOnChanges(changes: SimpleChanges) {
ngOnChanges(changes: SimpleChangesTyped<FilterBoxComponent>) {
if (changes.state) {
const selectedItems = this.formControl.value ?? [];
if (!this.state.isEqualSelectedItems(selectedItems)) {
Expand Down

0 comments on commit dd36ff4

Please sign in to comment.