Skip to content

Commit

Permalink
Implemented DELETE /api/v1/observer endpoint in observers list. (#297)
Browse files Browse the repository at this point in the history
* Implemented DELETE /api/v1/observer endpoint in observers list.

* Added localized strings for observer delete dialog.

* Added localized strings for previously implemented observer delete dialog.
  • Loading branch information
VladCuciureanu authored Nov 3, 2020
1 parent 7582e8c commit 12a5fed
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Component, Inject, OnInit } from '@angular/core';
import { ObserverProfileForm } from './observer-profile.form';
import {Component, Inject, OnDestroy, OnInit} from '@angular/core';
import { ObserversService } from '../../../services/observers.service';
import { ActivatedRoute, Router } from '@angular/router';
import { PageState } from '../../../models/page-state.model';
import { Observer } from '../../../models/observer.model';
import { ToastrService } from 'ngx-toastr';
import {
FormBuilder,
FormControl,
FormGroup,
Validators,
} from '@angular/forms';
Expand All @@ -17,6 +15,7 @@ import { Store } from '@ngrx/store';
import { getSelectedObserver } from 'src/app/store/observers/observers.state';
import { switchMap, map, takeUntil } from 'rxjs/operators';
import { of, Subject } from 'rxjs';
import {TranslateService} from '@ngx-translate/core';

const NOT_ONLY_SPACE_LINE = /^(\s*\S+\s*)+$/;

Expand All @@ -25,7 +24,7 @@ const NOT_ONLY_SPACE_LINE = /^(\s*\S+\s*)+$/;
templateUrl: './observer-profile.component.html',
styleUrls: ['./observer-profile.component.scss'],
})
export class ObserverProfileComponent implements OnInit {
export class ObserverProfileComponent implements OnInit, OnDestroy {
error: string;
fileData: File;

Expand All @@ -43,6 +42,7 @@ export class ObserverProfileComponent implements OnInit {
private fb: FormBuilder,
private router: Router,
private store: Store,
private translateService: TranslateService,
@Inject(BASE_BUTTON_VARIANTS) public BaseButtonVariants: typeof Variants
) {
this.observerProfileForm = this.fb.group({
Expand All @@ -62,11 +62,11 @@ export class ObserverProfileComponent implements OnInit {
}

deleteObserver() {
if (confirm('Are you sure you want to remove this observer?')) {
if (confirm(this.translateService.instant('OBSERVER_DELETE_CONFIRMATION'))) {
this.observerService
.deleteObserver(this.observer.id)
.subscribe((data) => {
this.toastr.warning('Success', 'Observer has been removed');
.subscribe(() => {
this.toastr.warning(this.translateService.instant('SUCCESS'), this.translateService.instant('OBSERVER_DELETE_SUCCESS'));
this.router.navigateByUrl('/observatori');
});
}
Expand All @@ -81,7 +81,7 @@ export class ObserverProfileComponent implements OnInit {

onSubmit() {
const sanitizedValues = this.trimFormValuesOutsideAndInside(this.observerProfileForm.value);

if (this.pageState === PageState.EDIT) {
this.saveChanges(sanitizedValues);
} else if (this.pageState === PageState.NEW) {
Expand All @@ -91,7 +91,7 @@ export class ObserverProfileComponent implements OnInit {

private initRouteListener() {
this.route.params.subscribe((params) => {
this.pageState = params['state'];
this.pageState = params.state;
this.handleFormState();
this.getObserver(params);
});
Expand All @@ -100,7 +100,7 @@ export class ObserverProfileComponent implements OnInit {
private saveChanges(values: { [k: string]: string }) {
this.observerService
.saveChanges(values, this.observer)
.subscribe((data) => {
.subscribe(() => {
this.toastr.success('Success', 'Changes have been saved');
});
}
Expand All @@ -111,7 +111,7 @@ export class ObserverProfileComponent implements OnInit {
observerToAdd.pin = values.password;
observerToAdd.name = values.name;

this.observerService.addNewObserver(observerToAdd).subscribe((value) => {
this.observerService.addNewObserver(observerToAdd).subscribe(() => {
this.toastr.success('Success', 'Observer has been added');
this.router.navigateByUrl('/observatori');
});
Expand All @@ -129,13 +129,13 @@ export class ObserverProfileComponent implements OnInit {
if (this.pageState === PageState.NEW) {
return;
}
this.store.select(getSelectedObserver, params['id'])

this.store.select(getSelectedObserver, params.id)
.pipe(
switchMap(
obs => obs
? of(obs)
: this.observerService.getObserver(params['id']).pipe(
obs => obs
? of(obs)
: this.observerService.getObserver(params.id).pipe(
map((o: ApiListResponse<Observer>) => o ? o.data[0] : {})
)
),
Expand Down
36 changes: 21 additions & 15 deletions src/app/components/observers/observers.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import {
TemplateRef,
Inject,
InjectionToken,
ViewContainerRef,
ViewChildDecorator,
} from '@angular/core';
import {
LoadObserversAction,
Expand All @@ -27,12 +25,12 @@ import { ObserversFilterForm } from './observers-filter.form';
import { ObserversService } from '../../services/observers.service';
import { ToastrService } from 'ngx-toastr';
import { NgbModalRef, NgbModal, NgbModalOptions } from '@ng-bootstrap/ng-bootstrap';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { HttpClient } from '@angular/common/http';
import { BASE_BUTTON_VARIANTS, Variants } from 'src/app/shared/base-button/base-button.component';
import { SelectedZoneEvents, SortedColumnEvent, TableColumn } from 'src/app/table/table-container/table-container.component';
import { DropdownConfigItem } from 'src/app/shared/base-dropdown/base-dropdown.component';
import { ActivatedRoute, Router } from '@angular/router';
import { TranslatePipe, TranslateService } from '@ngx-translate/core';
import { TranslateService } from '@ngx-translate/core';

const ACTIONS_COLUMN_NAME = 'Actions';

Expand All @@ -54,14 +52,12 @@ const TABLE_COLUMNS = new InjectionToken('TABLE_COLUMNS', {
const DROPDOWN_CONFIG = new InjectionToken('DROPDOWN_CONFIG', {
providedIn: 'root',
factory: () => {
const config = [
{ name: 'Edit', isMain: true, eventType: DropdownEvents.EDIT },
{ name: 'Delete', eventType: DropdownEvents.DELETE },
{ name: 'Notification', eventType: DropdownEvents.NOTIFICATION },
{ name: 'Reset Password', eventType: DropdownEvents.RESET_PASSWORD },
return [
{name: 'Edit', isMain: true, eventType: DropdownEvents.EDIT},
{name: 'Delete', eventType: DropdownEvents.DELETE},
{name: 'Notification', eventType: DropdownEvents.NOTIFICATION},
{name: 'Reset Password', eventType: DropdownEvents.RESET_PASSWORD},
];

return config;
}
})

Expand All @@ -75,7 +71,7 @@ enum DropdownEvents {
DELETE,
NOTIFICATION,
RESET_PASSWORD,
};
}

type TableColumnTranslated = Omit<TableColumn, 'name'> & { name: Observable<any> }

Expand Down Expand Up @@ -107,7 +103,7 @@ export class ObserversComponent implements OnInit, OnDestroy {
keyboard: false,
};
observerToEdit: Observer;

actionsColumnName = ACTIONS_COLUMN_NAME;
tableColumns: TableColumnTranslated[] = [];
crtResetPasswordRow = null;
Expand Down Expand Up @@ -165,7 +161,7 @@ export class ObserversComponent implements OnInit, OnDestroy {
this.router.navigate(['profil/edit/', row.phone], { relativeTo: this.route });
break;
case DropdownEvents.DELETE:
console.warn('TO BE IMPLEMENTED');
this.deleteObserver(row);
break;
case DropdownEvents.NOTIFICATION:
console.warn('TO BE IMPLEMENTED');
Expand Down Expand Up @@ -263,6 +259,17 @@ export class ObserversComponent implements OnInit, OnDestroy {
this.observersCountSubscription.unsubscribe();
}

deleteObserver(observer: Observer) {
if (confirm(this.translateService.instant('OBSERVER_DELETE_CONFIRMATION'))) {
this.observersService
.deleteObserver(observer.id)
.subscribe(() => {
this.toastrService.warning(this.translateService.instant('SUCCESS'), this.translateService.instant('OBSERVER_DELETE_SUCCESS'));
this.loadObservers(1);
});
}
}

isPasswordValid(): boolean {
return (
this.newPassword &&
Expand Down Expand Up @@ -301,7 +308,6 @@ export class ObserversComponent implements OnInit, OnDestroy {

private onResetPasswordEvent (row: any) {
this.crtResetPasswordRow = row;

from(this.modalService.open(this.resetPasswordContent).result)
.subscribe(
newPassword => {
Expand Down
5 changes: 4 additions & 1 deletion src/assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@
"OBSERVER_GUIDE": "Observer's guide",
"OBSERVER_ADD": "Add Observer",
"OBSERVER_EDIT": "Edit Observer",
"OBSERVER_DELETE_CONFIRMATION": "Are you sure you want to remove this observer?",
"OBSERVER_DELETE_SUCCESS": "Observer has been removed",
"OBSERVER_POLLING_STATIONS": "Visited Polling Stations",
"OBSERVERS_SELECTED": "Observers Selected",
"OBSERVERS_IMPORT": "Import Observers",
Expand All @@ -121,5 +123,6 @@
"CONFIRM": "Confirm",
"TYPE": "Type",
"SELECT_FILE": "Select file",
"IMPORT": "Import"
"IMPORT": "Import",
"SUCCESS": "Success"
}
7 changes: 5 additions & 2 deletions src/assets/i18n/ro.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@
"OBSERVERS": "Observatori",
"OBSERVER_ADD": "Adaugă Observator",
"OBSERVER_EDIT": "Editaţi Observator",
"OBSERVER_DELETE_CONFIRMATION": "Sunteti sigur ca doriti sa stergeti acest observator?",
"OBSERVER_DELETE_SUCCESS": "Observatorul a fost sters",
"OBSERVER_POLLING_STATIONS": "Secţii De Votare Vizitate",
"ENTER_NEW_PASSWORD": "Introduceţi noul Pin pentru: ",
"OBSERVERS_SELECTED": "Observatori Selectaţi",
Expand All @@ -91,7 +93,7 @@
"LAST_NAME": "Nume",
"PHONE": "Telefon",
"LAST_LOGIN": "Ultima logare",


"//": "COMMON_STUFF",
"EDIT": "Editaţi",
Expand All @@ -107,5 +109,6 @@
"RESET_PASSWORD": "Resetare Parolă",
"TYPE": "Scrieţi",
"SELECT_FILE": "Selectaţi fișierul",
"IMPORT": "Importaţi"
"IMPORT": "Importaţi",
"SUCCESS": "Succes"
}

1 comment on commit 12a5fed

@vercel
Copy link

@vercel vercel bot commented on 12a5fed Nov 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.