Skip to content

Commit

Permalink
Merge branch 'Wydder-ListAddressOfAsset' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdanconstantinescu committed Jun 29, 2019
2 parents 9fddbd3 + 9d4b844 commit 8557351
Show file tree
Hide file tree
Showing 27 changed files with 443 additions and 126 deletions.
8 changes: 6 additions & 2 deletions src/app/assets/assets-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ import * as fromContainers from './containers';
const routes: Routes = [
{
path: '',
canActivate: [fromGuards.AssetsGuard],
canActivate: [
fromGuards.AssetsGuard,
fromGuards.LoadCountiesGuard,
],
component: fromContainers.AssetsComponent,
},
{
path: 'detail/:assetId',
canActivate: [
fromGuards.AssetsGuard,
fromGuards.AssetDetailGuard,
fromGuards.LoadDefendantsGuard,
fromGuards.LoadCountiesGuard,
fromGuards.LoadDefendantsGuard,
fromGuards.LoadAddressesGuard,
],
component: fromContainers.AssetDetailComponent,
},
Expand Down
2 changes: 2 additions & 0 deletions src/app/assets/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { EditAssetComponent } from './edit-asset/edit-asset.component';
import { EditDefendantComponent } from './edit-defendant/edit-defendant.component';
import { EditSolutionComponent } from './edit-solution/edit-solution.component';
import { EditStorageSpaceComponent } from './edit-storage/edit-storage.component';
import { ViewAddressComponent } from './view-address/view-address.component';
import { ViewDefendantComponent } from './view-defendant/view-defendant.component';

export const components: any[] = [
Expand All @@ -11,5 +12,6 @@ export const components: any[] = [
EditDefendantComponent,
EditSolutionComponent,
EditStorageSpaceComponent,
ViewAddressComponent,
ViewDefendantComponent,
];
32 changes: 32 additions & 0 deletions src/app/assets/components/view-address/view-address.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<section class="view-address" *ngIf="address">
<h2>Adresa</h2>

<div class="double">
<app-detailed-text
description="Oras"
[text]="address.city"
></app-detailed-text>
<app-detailed-text
description="Judet"
[text]="address.getCountyName()"
></app-detailed-text>
</div>

<div class="double">
<app-detailed-text
description="Strada"
[text]="address.street"
></app-detailed-text>
<app-detailed-text
description="Bloc"
[text]="address.building"
></app-detailed-text>
</div>

<app-detailed-text
*ngIf="address.description"
description="Detalii"
[text]="address.description"
></app-detailed-text>
</section>

22 changes: 22 additions & 0 deletions src/app/assets/components/view-address/view-address.component.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@import 'mixins';

.view-address {
h2 {
@include material-card-title();
}

app-detailed-text {
@include flex-column();
@include flex-grow();
margin-bottom: 16px;
}

.double {
display: flex;
flex-direction: row;

app-detailed-text {
flex-basis: 50%;
}
}
}
11 changes: 11 additions & 0 deletions src/app/assets/components/view-address/view-address.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Component, Input } from '@angular/core';
import { Address } from '@app/core';

@Component({
selector: 'app-view-address',
templateUrl: './view-address.component.html',
styleUrls: ['./view-address.component.scss'],
})
export class ViewAddressComponent {
@Input() address: Address;
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ <h2>Inculpat</h2>
mat-raised-button
(click)="onDefendantDeleted()"
color="warn"
[disabled]="deleting$ | async"
[disabled]="isDeleting"
>Sterge</button>
</section>
</section>
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { Defendant, DefendantType, Identifier } from '@app/core';
import * as fromDefendants from '@app/core/store';
import { select, Store } from '@ngrx/store';
import { Observable } from 'rxjs';

@Component({
selector: 'app-view-defendant',
templateUrl: './view-defendant.component.html',
styleUrls: ['./view-defendant.component.scss'],
})
export class ViewDefendantComponent implements OnInit {
export class ViewDefendantComponent {
@Input() defendant: Defendant;
@Input() identifiers: Identifier[];
@Output() defendantDeleted: EventEmitter<fromDefendants.DeleteDefendantPayload>
= new EventEmitter<fromDefendants.DeleteDefendantPayload>();

deleting$: Observable<boolean>;

constructor(private store: Store<fromDefendants.DefendantsState>) { }

ngOnInit(): void {
this.deleting$ = this.store.pipe(select(fromDefendants.getDefendantDeletingById(this.defendant.id)));
}
@Input() isDeleting: boolean;
@Output() defendantDeleted: EventEmitter<Defendant> = new EventEmitter<Defendant>();

getDefendantType(aIsPerson: boolean) {
return aIsPerson ? DefendantType.Person.toString() : DefendantType.Company.toString();
Expand All @@ -32,10 +21,6 @@ export class ViewDefendantComponent implements OnInit {
}

onDefendantDeleted() {
const payload: fromDefendants.DeleteDefendantPayload = {
defendantId: this.defendant.id,
assetId: this.defendant.getAsset().id,
};
this.defendantDeleted.emit(payload);
this.defendantDeleted.emit(this.defendant);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,20 @@ <h2>{{theAsset.name}}</h2>
<app-view-defendant
[defendant]="theDefendant"
[identifiers]="identifiers$ | async"
[isDeleting]="(isDefendantDeleting$(theDefendant.id) | async)"
(defendantDeleted)="onDefendantDeleted($event)"
></app-view-defendant>
</div>
</section>

<section class="asset-details">
<div class="asset-details-card mat-elevation-z8" *ngFor="let theAddress of (addresses$ | async)">
<app-view-address
[address]="theAddress"
></app-view-address>
</div>
</section>

<section class="asset-details" *ngIf="isStateView()">
<div class="asset-details-card mat-elevation-z2" *ngIf="!(isEditingAssetProperty$() | async); else editProperty">
<h2>Adauga proprietati</h2>
Expand Down
28 changes: 17 additions & 11 deletions src/app/assets/containers/asset-detail/asset-detail.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export enum AssetDetailState {
styleUrls: ['asset-detail.component.scss'],
})
export class AssetDetailComponent implements OnInit {
private asset$: Observable<Asset>;
asset$: Observable<Asset>;

institutions$: Observable<Institution[]> = this.store.pipe(select(fromStore.getAllInstitutions));
decisions$: Observable<Decision[]> = this.store.pipe(select(fromStore.getAllDecisions));
Expand All @@ -59,6 +59,7 @@ export class AssetDetailComponent implements OnInit {
assetProperty$: Observable<AssetProperty>;
subcategories$: Observable<Category[]>;
defendants$: Observable<Defendant[]>;
addresses$: Observable<Address[]>;

measurements: AssetMeasurement[];
currencies: AssetCurrency[];
Expand Down Expand Up @@ -87,6 +88,7 @@ export class AssetDetailComponent implements OnInit {
this.asset$ = this.store.pipe(select(fromStore.getAssetById(theId)));
this.assetProperty$ = this.store.pipe(select(fromStore.getAssetPropertiesByAssetId(theId)));
this.defendants$ = this.store.pipe(select(fromStore.getAllDefendantsForAssetId(theId)));
this.addresses$ = this.store.pipe(select(fromStore.getAllAddressesForAssetId(theId)));
});

this.asset$.pipe(take(1))
Expand Down Expand Up @@ -175,8 +177,20 @@ export class AssetDetailComponent implements OnInit {
this.setStateView();
}

onDefendantDeleted(payload: fromStore.DeleteDefendantPayload) {
this.store.dispatch(new fromStore.DeleteDefendant(payload));
isDefendantDeleting$(aDefendantId: number) {
return this.store.pipe(select(fromStore.getDefendantDeletingById(aDefendantId)));
}

onDefendantDeleted(aDefendant: Defendant) {
this.store.dispatch(new fromStore.DeleteDefendant(aDefendant));
}

isStateView(): boolean {
return this.state === AssetDetailState.View;
}

isStateEdit(): boolean {
return this.state === AssetDetailState.Edit;
}

private resetSelectedProperty() {
Expand All @@ -190,12 +204,4 @@ export class AssetDetailComponent implements OnInit {
private setStateView() {
this.state = AssetDetailState.View;
}

isStateView(): boolean {
return this.state === AssetDetailState.View;
}

isStateEdit(): boolean {
return this.state === AssetDetailState.Edit;
}
}
3 changes: 3 additions & 0 deletions src/app/assets/guards/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { AssetDetailGuard } from './asset-detail.guard';
import { AssetsGuard } from './assets.guard';
import { LoadAddressesGuard } from './load-addresses.guard';
import { LoadCountiesGuard } from './load-counties.guard';
import { LoadDefendantsGuard } from './load-defendants.guard';

export const guards: any[] = [
AssetsGuard,
AssetDetailGuard,
LoadAddressesGuard,
LoadCountiesGuard,
LoadDefendantsGuard,
];

export * from './assets.guard';
export * from './asset-detail.guard';
export * from './load-addresses.guard';
export * from './load-counties.guard';
export * from './load-defendants.guard';
36 changes: 36 additions & 0 deletions src/app/assets/guards/load-addresses.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate } from '@angular/router';
import { of, Observable } from 'rxjs';

import { select, Store } from '@ngrx/store';
import { catchError, filter, switchMap, take, tap } from 'rxjs/operators';
import * as fromStore from '../../core/store';

@Injectable()
export class LoadAddressesGuard implements CanActivate {
constructor(private store: Store<fromStore.AddressesState>) {
}

canActivate(route: ActivatedRouteSnapshot): Observable<boolean> {
const theAssetId = parseInt((route.params as any).assetId, 10);

return this.hasAddressesLoaded(theAssetId)
.pipe(
switchMap(() => of(true)),
catchError(() => of(false))
);
}

hasAddressesLoaded(aId: number): Observable<boolean> {
return this.store.pipe(select(fromStore.getAddressesLoadedForAssetId(aId)))
.pipe(
tap((aLoaded: boolean) => {
if (!aLoaded) {
this.store.dispatch(new fromStore.LoadAddresses(aId));
}
}),
filter((aLoaded: boolean) => aLoaded),
take(1)
);
}
}
34 changes: 34 additions & 0 deletions src/app/core/http/addresses-api.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { throwError as observableThrowError, Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';

import { environment } from '@env/environment';
import { AddressRequest, AddressResponse } from '../models';

@Injectable()
export class AddressesApiService {
constructor(private http: HttpClient) {
}

public createAddress(id: number, address: AddressRequest): Observable<AddressResponse> {
return this.http.post<AddressResponse>(`${environment.api_url}/assets/${id}/address`, address)
.pipe(
catchError(aError => observableThrowError(aError.error.errors))
);
}

public updateAddress(id: number, address: AddressRequest): Observable<AddressResponse> {
return this.http.put<AddressResponse>(`${environment.api_url}/assets/${id}/address`, address)
.pipe(
catchError(aError => observableThrowError(aError.error.errors))
);
}

public getAddress$(aAssetId: number): Observable<AddressResponse> {
return this.http.get<AddressResponse>(`${environment.api_url}/assets/${aAssetId}/address`)
.pipe(
catchError(aError => observableThrowError(aError))
);
}
}
14 changes: 0 additions & 14 deletions src/app/core/http/assets-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,6 @@ export class AssetsApiService {
);
}

public createAddress(id: number, address: AddressRequest): Observable<AddressResponse> {
return this.http.post<AddressResponse>(`${environment.api_url}/assets/${id}/address`, address)
.pipe(
catchError(aError => observableThrowError(aError.error.errors))
);
}

public updateAddress(id: number, address: AddressRequest): Observable<AddressResponse> {
return this.http.put<AddressResponse>(`${environment.api_url}/assets/${id}/address`, address)
.pipe(
catchError(aError => observableThrowError(aError.error.errors))
);
}

public create(asset: AssetRequest): Observable<AssetDetailResponse> {
return this.http.post<AssetDetailResponse>(`${environment.api_url}/assets/addminimalasset`, asset)
.pipe(
Expand Down
3 changes: 3 additions & 0 deletions src/app/core/http/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AddressesApiService } from './addresses-api.service';
import { AssetsApiService } from './assets-api.service';
import { CategoriesApiService } from './categories-api.service';
import { CountiesApiService } from './counties-api.service';
Expand All @@ -13,6 +14,7 @@ import { StagesApiService } from './stages-api.service';
import { StorageSpacesApiService } from './storage-spaces-api.service';

export const httpServices: any[] = [
AddressesApiService,
AssetsApiService,
CategoriesApiService,
CountiesApiService,
Expand All @@ -28,6 +30,7 @@ export const httpServices: any[] = [
StorageSpacesApiService,
];

export * from './addresses-api.service';
export * from './assets-api.service';
export * from './categories-api.service';
export * from './counties-api.service';
Expand Down
4 changes: 4 additions & 0 deletions src/app/core/models/asset/asset-properties/address.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,8 @@ export class Address extends AssetProperty {
this.county = aCounty;
this.countyId = aCounty.id;
}

getCountyName() {
return this.county ? this.county.name : '';
}
}
Loading

0 comments on commit 8557351

Please sign in to comment.