Skip to content

Commit

Permalink
Merge branch 'oproiustefan-develop-stefan' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdanconstantinescu committed Jun 11, 2019
2 parents b414a2d + 2f3fe25 commit 9fddbd3
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,12 @@ <h2>Inculpat</h2>
></app-detailed-text>
</div>
</div>
<section class="buttons">
<button
mat-raised-button
(click)="onDefendantDeleted()"
color="warn"
[disabled]="deleting$ | async"
>Sterge</button>
</section>
</section>
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@
flex-basis: 50%;
}
}

.buttons {
display: flex;
justify-content: flex-end;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { Component, EventEmitter, Input, OnInit, 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 {
export class ViewDefendantComponent implements OnInit {
@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)));
}

getDefendantType(aIsPerson: boolean) {
return aIsPerson ? DefendantType.Person.toString() : DefendantType.Company.toString();
Expand All @@ -17,4 +30,12 @@ export class ViewDefendantComponent {
getIdentifier(aIdentifierType: number): Identifier {
return this.identifiers.find(aIdentifier => aIdentifier.id === aIdentifierType);
}

onDefendantDeleted() {
const payload: fromDefendants.DeleteDefendantPayload = {
defendantId: this.defendant.id,
assetId: this.defendant.getAsset().id,
};
this.defendantDeleted.emit(payload);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ <h2>{{theAsset.name}}</h2>
<app-view-defendant
[defendant]="theDefendant"
[identifiers]="identifiers$ | async"
(defendantDeleted)="onDefendantDeleted($event)"
></app-view-defendant>
</div>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ export class AssetDetailComponent implements OnInit {
this.setStateView();
}

onDefendantDeleted(payload: fromStore.DeleteDefendantPayload) {
this.store.dispatch(new fromStore.DeleteDefendant(payload));
}

private resetSelectedProperty() {
this.selectedProperty = undefined;
}
Expand Down
1 change: 1 addition & 0 deletions src/app/core/error-strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export class ErrorStrings {
static ERROR_FETCH_ASSETS = 'Could not fetch assets';
static ERROR_SEARCH_DECISIONS = 'Could not search decisions';
static ERROR_ADD_MINIMAL_ASSET = 'Could not add asset';
static ERROR_DELETE_DEFENDANT = 'Could not delete defendant';
}
7 changes: 7 additions & 0 deletions src/app/core/http/defendants-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@ export class DefendantsApiService {
catchError(aError => observableThrowError(aError))
);
}

public deleteDefendant(assetId: number, defendantId: number): Observable<number> {
return this.http.delete<number>(`${environment.api_url}/assets/${assetId}/defendants/${defendantId}`)
.pipe(
catchError(aError => observableThrowError(aError))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class Defendant extends AssetProperty {
identification: this.identification,
isPerson: this.isPerson,
name: this.name,
birthdate: this.birthdate.isValid() ? this.birthdate.toISOString() : undefined,
birthdate: this.birthdate && this.birthdate.isValid() ? this.birthdate.toISOString() : undefined,
firstName: this.firstName,
identifierId: this.identifierId,
nationality: this.nationality,
Expand Down
6 changes: 5 additions & 1 deletion src/app/core/services/defendants.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export class DefendantsService {
)
}

public deleteDefendant$(assetId: number, defendantId: number): Observable<any> {
return this.defendantsApiService.deleteDefendant(assetId, defendantId);
}

private toRequest(aDefendant: Defendant): DefendantRequest {
return {
id: aDefendant.id,
Expand All @@ -43,7 +47,7 @@ export class DefendantsService {
identification: aDefendant.identification,
isPerson: aDefendant.isPerson,
name: aDefendant.name,
birthdate: aDefendant.birthdate.isValid() ? aDefendant.birthdate.format() : undefined,
birthdate: aDefendant.birthdate && aDefendant.birthdate.isValid() ? aDefendant.birthdate.format() : undefined,
firstName: aDefendant.firstName,
identifierId: aDefendant.identifierId,
nationality: aDefendant.nationality,
Expand Down
29 changes: 28 additions & 1 deletion src/app/core/store/actions/defendants.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,38 @@ export interface DefendantsSuccessPayload {
asset: Asset;
}

export interface DeleteDefendantPayload {
assetId: number;
defendantId: number;
}

export const DEFENDANT_DELETE = '[Defendants] Delete Defendant';
export const DEFENDANT_DELETE_FAIL = '[Defendants] Delete Defendant Fail';
export const DEFENDANT_DELETE_SUCCESS = '[Defendants] Delete Defendant Success';

export class DeleteDefendant implements Action {
readonly type: string = DEFENDANT_DELETE;
constructor(public payload: DeleteDefendantPayload) {}
}

export class DeleteDefendantFail implements Action {
readonly type: string = DEFENDANT_DELETE_FAIL;
constructor(public payload: number) {}
}

export class DeleteDefendantSuccess implements Action {
readonly type: string = DEFENDANT_DELETE_SUCCESS;
constructor(public payload: number) {}
}

// action types
export type DefendantsAction =
CreateDefendant
| CreateDefendantFail
| CreateDefendantSuccess
| LoadDefendants
| LoadDefendantsFail
| LoadDefendantsSuccess;
| LoadDefendantsSuccess
| DeleteDefendant
| DeleteDefendantFail
| DeleteDefendantSuccess;
16 changes: 15 additions & 1 deletion 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 } from 'rxjs/operators';
import { catchError, filter, map, mergeMap, switchMap, tap } from 'rxjs/operators';

import { Defendant } from '../../models';
import { DefendantsService } from '../../services';
Expand Down Expand Up @@ -54,6 +54,20 @@ export class DefendantsEffects {
})
);

@Effect()
deleteDefendant$ = this.actions$
.pipe(
ofType(defendantsActions.DEFENDANT_DELETE),
map((action: defendantsActions.DeleteDefendant) => action.payload),
switchMap((aPayload: defendantsActions.DeleteDefendantPayload) =>
this.defendantsService.deleteDefendant$(aPayload.assetId, aPayload.defendantId)
.pipe(
map((aResponse: number) => new defendantsActions.DeleteDefendantSuccess(aResponse)),
catchError(() => of(new defendantsActions.DeleteDefendantFail(aPayload.defendantId)))
)
)
);

constructor(
private actions$: Actions,
private defendantsService: DefendantsService,
Expand Down
59 changes: 59 additions & 0 deletions src/app/core/store/reducers/defendants.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ export interface DefendantsState {
entities: { [id: number]: Defendant };
loaded: { [id: number]: boolean };
loading: { [id: number]: boolean };
deleted: { [id: number]: boolean };
deleting: { [id: number]: boolean };
}

export const initialState: DefendantsState = {
entities: {},
loaded: {},
loading: {},
deleted: {},
deleting: {},
};

export function reducer(
Expand Down Expand Up @@ -85,6 +89,59 @@ export function reducer(
} as DefendantsState;
}

case fromDefendants.DEFENDANT_DELETE: {
const theDefendantId = (action.payload as fromDefendants.DeleteDefendantPayload).defendantId;

return {
...state,
deleting: {
...state.deleting,
[theDefendantId]: true,
},
} as DefendantsState
}

case fromDefendants.DEFENDANT_DELETE_FAIL: {
const theDefendantId = action.payload;

return {
...state,
deleting: {
...state.deleting,
[theDefendantId]: false,
},
deleted: {
...state.deleted,
[theDefendantId]: false,
},
} as DefendantsState
}

case fromDefendants.DEFENDANT_DELETE_SUCCESS: {
const theDefendantId: number = action.payload;
// const entities = state.entities;
// var theDefendants: Defendant[] = []
// for(var key in Object.keys(entities)) {
// theDefendants = [...theDefendants, entities[key]];
// }
// theDefendants = theDefendants.filter(d => d.id != theDefendantId);
const entities: { [id: number]: Defendant } = { ...state.entities }
delete entities[theDefendantId];

return {
...state,
entities,
deleting: {
...state.deleting,
[theDefendantId]: false,
},
deleted: {
...state.deleted,
[theDefendantId]: true,
},
} as DefendantsState
}

default: {
return {
...state,
Expand All @@ -96,3 +153,5 @@ export function reducer(
export const getDefendantsEntities = (state: DefendantsState) => state.entities;
export const getDefendantsLoading = (state: DefendantsState) => state.loading;
export const getDefendantsLoaded = (state: DefendantsState) => state.loaded;
export const getDefendantsDeleting = (state: DefendantsState) => state.deleting;
export const getDefendantsDeleted = (state: DefendantsState) => state.deleted;
12 changes: 12 additions & 0 deletions src/app/core/store/selectors/defendants.selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,15 @@ export const getDefendantsLoadingForAssetId = (aAssetId: number) => createSelect
getDefendantsLoading,
(aLoading) => aLoading[aAssetId] || false
);

export const getDefendantsDeleted = createSelector(getDefendantsState, fromDefendants.getDefendantsDeleted);
export const getDefendantDeleting = (aDefendantId: number) => createSelector(
getDefendantsDeleted,
(aDeleted) => aDeleted[aDefendantId] || false
);

export const getDefendantsDeleting = createSelector(getDefendantsState, fromDefendants.getDefendantsDeleting);
export const getDefendantDeletingById = (aDefendantId: number) => createSelector(
getDefendantsDeleting,
(aDeleting) => aDeleting[aDefendantId] || false
);

0 comments on commit 9fddbd3

Please sign in to comment.