Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdanconstantinescu committed Aug 26, 2019
2 parents 8571e17 + 6359308 commit 2ae6537
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 19 deletions.
9 changes: 8 additions & 1 deletion src/app/core/http/addresses-api.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { throwError as observableThrowError, Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { catchError, defaultIfEmpty, filter } from 'rxjs/operators';

import { environment } from '@env/environment';
import { AddressRequest, AddressResponse } from '../models';
Expand All @@ -28,6 +28,13 @@ export class AddressesApiService {
public getAddress$(aAssetId: number): Observable<AddressResponse> {
return this.http.get<AddressResponse>(`${environment.api_url}/assets/${aAssetId}/address`)
.pipe(
/**
* Currently the backend responds with an address with id 0 if there is no address for an
* asset. I'm not exactly sure why is this happening, but I just hate fake data, so we'll
* filter it away and just return a nice and simple empty object.
*/
filter(aAddress => aAddress && aAddress.id !== 0),
defaultIfEmpty({} as AddressResponse),
catchError(aError => observableThrowError(aError))
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export class Address extends AssetProperty {
return {
id: this.id,
countyId: this.countyId,
county: this.county.toJson(),
county: this.county ? this.county.toJson() : undefined,
street: this.street,
city: this.city,
building: this.building,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Defendant } from './defendant.model';

export interface DefendantPayload {
id: number;
defendants: Defendant[];
}
1 change: 1 addition & 0 deletions src/app/core/models/asset/asset-properties/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from './county.model';
export * from './county-response.interface';
export * from './defendant.model';
export * from './defendant-form.model';
export * from './defendant-payload.interface';
export * from './defendant-request.interface';
export * from './defendant-response.interface';
export * from './storage-space.model';
Expand Down
6 changes: 4 additions & 2 deletions src/app/core/services/addresses.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@angular/core';

import { Observable } from 'rxjs';
import { map, mergeMap, toArray } from 'rxjs/operators';
import { map } from 'rxjs/operators';

import { AddressesApiService } from '../http';
import { Address, AddressResponse, Asset, County } from '../models';
Expand Down Expand Up @@ -31,7 +31,9 @@ export class AddressesService {
const theAddress = new Address();
theAddress.fromResponse(aNewAddress);
theAddress.setAsset(aAsset);
theAddress.setCounty(aCounties[aNewAddress.countyId]);
if (aNewAddress.countyId) {
theAddress.setCounty(aCounties[aNewAddress.countyId]);
}
return theAddress;
})
)
Expand Down
6 changes: 3 additions & 3 deletions src/app/core/store/actions/defendants.action.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Action } from '@ngrx/store';
import { Defendant } from '../../models';
import { Defendant, DefendantPayload } from '../../models';

export enum DefendantsActionTypes {
CreateDefendant = '[Defendants] Create Defendant',
Expand Down Expand Up @@ -37,12 +37,12 @@ export class LoadDefendants implements Action {

export class LoadDefendantsFail implements Action {
readonly type: string = DefendantsActionTypes.LoadDefendantsFail;
constructor(public payload: any) {}
constructor(public payload: number) {}
}

export class LoadDefendantsSuccess implements Action {
readonly type: string = DefendantsActionTypes.LoadDefendantsSuccess;
constructor(public payload: Defendant[]) {}
constructor(public payload: DefendantPayload) {}
}

export class DeleteDefendant implements Action {
Expand Down
4 changes: 2 additions & 2 deletions src/app/core/store/effects/defendants.effect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@angular/core';
import { ofType, Actions, Effect } from '@ngrx/effects';
import { of } from 'rxjs';
import { catchError, filter, map, mergeMap, switchMap, tap } from 'rxjs/operators';
import { catchError, filter, map, mergeMap, switchMap } from 'rxjs/operators';

import { Defendant } from '../../models';
import { DefendantsService } from '../../services';
Expand Down Expand Up @@ -45,7 +45,7 @@ export class DefendantsEffects {
filter(aAsset => aAsset !== undefined),
switchMap((aAsset) => {
return this.defendantsService.getDefendants$(aAsset).pipe(
map(aDefendants => new defendantsActions.LoadDefendantsSuccess(aDefendants)),
map(aDefendants => new defendantsActions.LoadDefendantsSuccess({ id: aAsset.id, defendants: aDefendants })),
catchError(() => of(new defendantsActions.LoadDefendantsFail(aAsset.id)))
);
})
Expand Down
15 changes: 6 additions & 9 deletions src/app/core/store/reducers/defendants.reducer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { first } from 'lodash';
import { Defendant, IDefendant } from '../../models';
import * as fromDefendants from '../actions/defendants.action';

Expand Down Expand Up @@ -38,7 +37,6 @@ export function reducer(

case fromDefendants.DefendantsActionTypes.LoadDefendants: {
const theAssetId = action.payload;

return {
...state,
loading: {
Expand All @@ -65,11 +63,11 @@ export function reducer(
}

case fromDefendants.DefendantsActionTypes.LoadDefendantsSuccess: {
const theDefendants: Defendant[] = action.payload;
let theAssetId;
const thePayload = (action as fromDefendants.LoadDefendantsSuccess).payload;
const theAssetId = thePayload.id;
const theDefendants: Defendant[] = thePayload.defendants;

const entities = theDefendants.reduce((aEntities: { [id: number]: Defendant }, aDefendant: Defendant) => {
theAssetId = aDefendant.getAssetId();
return {
...aEntities,
[aDefendant.id]: aDefendant.toJson(),
Expand All @@ -78,6 +76,7 @@ export function reducer(

let loading = { ...state.loading };
let loaded = { ...state.loaded };

if (theAssetId) {
loading = {
...state.loading,
Expand All @@ -89,7 +88,6 @@ export function reducer(
[theAssetId]: true,
};
}

return {
...state,
entities,
Expand All @@ -99,13 +97,12 @@ export function reducer(
}

case fromDefendants.DefendantsActionTypes.DeleteDefendant: {
const theDefendantId = (action.payload as Defendant).id;

const theDefendant = (action.payload as Defendant);
return {
...state,
deleting: {
...state.deleting,
[theDefendantId]: true,
[theDefendant.id]: true,
},
} as DefendantsState
}
Expand Down
5 changes: 4 additions & 1 deletion src/app/core/store/selectors/addresses.selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export const getAddressesEntities = createSelector(
(aAddresses, aAssetEntities) => {
const theAddresses = Object.assign({}, aAddresses);
const theResult: { [id: number]: Address } = {};
if (Object.keys(theAddresses).length === 0) {
return theResult;
}

Object.keys(theAddresses).map((aKey) => {
const theAddress = new Address(theAddresses[aKey]);
Expand All @@ -39,7 +42,7 @@ export const getAllAddresses = createSelector(
);
export const getAllAddressesForAssetId = (aAssetId: number) => createSelector(
getAllAddresses,
(aAddresses) => aAddresses.filter(aAddress => aAddress.getAssetId() === +aAssetId)
(aAddresses) => aAddresses.filter(aAddress => aAddress && aAddress.getAssetId() === +aAssetId)
);

export const getAddressesLoaded = createSelector(getAddressesState, fromAddresses.getAddressesLoaded);
Expand Down

0 comments on commit 2ae6537

Please sign in to comment.