Skip to content

Commit b2bf9a4

Browse files
committed
Troca de eventos dos paineis, routing para edição e adição de entidade, http service genérico.
1 parent d959c49 commit b2bf9a4

File tree

9 files changed

+176
-142
lines changed

9 files changed

+176
-142
lines changed

src/app/modules/core/http-backend.interceptor.ts

Lines changed: 68 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -12,78 +12,83 @@ export class HttpBackendInterceptor implements HttpInterceptor {
1212
constructor() { }
1313

1414
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
15-
if (req.url.startsWith('http://localhost:5000/identity') ||
16-
req.url.startsWith('http://localhost:5000/users') ||
17-
req.url.startsWith('http://localhost:5000/error') ||
18-
req.url.startsWith('http://localhost:5000/cards')) {
19-
20-
return of(null).pipe(mergeMap(() => {
21-
if (req.url.startsWith('http://localhost:5000/identity') && req.method === 'POST') {
22-
var data = new Date();
23-
data.setDate(data.getDate() + 1);
24-
25-
let jwt: Jwt = {
26-
Token: "eaea23424asdfaefwr52asdfasdf32s",
27-
Timeout: data,
28-
Username: req.body.Email.split("@")[0],
29-
Name: req.body.Email.split("@")[0],
30-
Email: req.body.Email
31-
};
32-
33-
return of(new HttpResponse({ status: 200, body: jwt }));
34-
}
15+
return of(null).pipe(mergeMap(() => {
16+
if (req.url.includes('identity') && req.method === 'POST') {
17+
var data = new Date();
18+
data.setDate(data.getDate() + 1);
19+
20+
let jwt: Jwt = {
21+
Token: "eaea23424asdfaefwr52asdfasdf32s",
22+
Timeout: data,
23+
Username: req.body.Email.split("@")[0],
24+
Name: req.body.Email.split("@")[0],
25+
Email: req.body.Email
26+
};
3527

36-
if (req.url.startsWith('http://localhost:5000/cards') && req.method === 'GET') {
37-
let cards: Card[] = [];
38-
39-
var card: Card = {
40-
Title: 'Card title',
41-
Description: 'Some quick example text to build on the card title and make up the bulk of the card\'s content.',
42-
Link: 'Dapibus ac facilisis in'
43-
}
28+
return of(new HttpResponse({ status: 200, body: jwt }));
29+
}
4430

45-
for (let i = 0; i < 9; i++) {
46-
cards.push(card);
47-
}
48-
49-
return of(new HttpResponse({ status: 200, body: cards }));
31+
if (req.url.includes('cards') && req.method === 'GET') {
32+
let cards: Card[] = [];
33+
34+
var card: Card = {
35+
Title: 'Card title',
36+
Description: 'Some quick example text to build on the card title and make up the bulk of the card\'s content.',
37+
Link: 'Dapibus ac facilisis in'
5038
}
5139

52-
if (req.url.startsWith('http://localhost:5000/users') && req.method === 'GET') {
53-
let users: User[] = [];
54-
55-
var data = new Date();
40+
for (let i = 0; i < 9; i++) {
41+
cards.push(card);
42+
}
43+
44+
return of(new HttpResponse({ status: 200, body: cards }));
45+
}
5646

57-
var user: User = {
58-
Name: 'Gabriel Lucena',
59-
60-
Document: '023.437.673-27',
61-
Birthdate: new Date('1991-04-28T12:00:00')
62-
}
47+
if (req.url.includes('users?') && req.method === 'GET') {
48+
let users: User[] = [];
49+
50+
var data = new Date();
6351

64-
for (let i = 0; i < 10; i++) {
65-
users.push(user);
66-
}
52+
var user: User = {
53+
Name: 'Gabriel Lucena',
54+
55+
Document: '023.437.673-27',
56+
Birthdate: new Date('1991-04-28T12:00:00')
57+
}
6758

68-
let pagination: Pagination<User> = {
69-
Items: users,
70-
Limit: 10,
71-
Offset: 0,
72-
Total: 192
73-
};
74-
75-
return of(new HttpResponse({ status: 200, body: pagination }));
59+
for (let i = 0; i < 10; i++) {
60+
users.push(user);
7661
}
62+
63+
let pagination: Pagination<User> = {
64+
Items: users,
65+
Limit: 10,
66+
Offset: 0,
67+
Total: 192
68+
};
7769

78-
if (req.url.startsWith('http://localhost:5000/error') && req.method === 'GET') {
79-
return throwError({ error: { message: 'Error thrown.' } });
70+
return of(new HttpResponse({ status: 200, body: pagination }));
71+
}
72+
73+
if (req.url.includes('users/') && req.method === 'GET') {
74+
var user: User = {
75+
Name: 'Gabriel Lucena',
76+
77+
Document: '023.437.673-27',
78+
Birthdate: new Date('1991-04-28T12:00:00')
8079
}
81-
82-
return next.handle(req);
83-
}))
84-
.pipe(materialize())
85-
.pipe(delay(500))
86-
.pipe(dematerialize());
87-
}
80+
81+
return of(new HttpResponse({ status: 200, body: user }));
82+
}
83+
84+
if (req.url.includes('error') && req.method === 'GET') {
85+
return throwError({ error: { message: 'Error thrown.' } });
86+
}
87+
88+
return next.handle(req);
89+
}))
90+
.pipe(materialize())
91+
.pipe(delay(500))
92+
.pipe(dematerialize());
8893
}
8994
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Component, ViewEncapsulation, Input } from '@angular/core';
22
import { PanelAnimation } from 'src/app/modules/animations/panel.animation';
3+
import { BehaviorSubject } from 'rxjs';
4+
import { User } from 'src/app/domain/user';
35

46
@Component({
57
selector: 'users-add',
@@ -8,7 +10,7 @@ import { PanelAnimation } from 'src/app/modules/animations/panel.animation';
810
animations: [ PanelAnimation ],
911
})
1012
export class UsersAddComponent {
11-
@Input() visible: boolean;
13+
public user = new BehaviorSubject<User>(null);
1214

1315
constructor() { }
1416
}

src/app/routes/users/users-edit/users-edit.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
<div [@overlay-transition]="(visible | async) ? 'visible' : 'hidden'" class="overlay" (click)="close()"></div>
1+
<div [@overlay-transition]="(user | async) ? 'visible' : 'hidden'" class="overlay" (click)="close()"></div>
22

3-
<div #panel [@panel-transition]="(visible | async) ? 'visible' : 'hidden'" class="panel">
3+
<div #panel [@panel-transition]="(user | async) ? 'visible' : 'hidden'" class="panel">
44
<div class="float-left">
55
<h2 class="mb-4">Edit a user</h2>
66
</div>
Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
1-
import { Component, ViewEncapsulation, Renderer2, ViewChild, ElementRef } from '@angular/core';
1+
import { Component, ViewEncapsulation, Renderer2, ViewChild, ElementRef, Input } from '@angular/core';
22
import { faTimes, IconDefinition } from '@fortawesome/free-solid-svg-icons';
33
import { PanelAnimation } from 'src/app/modules/animations/panel.animation';
44
import { OverlayAnimation } from 'src/app/modules/animations/overlay.animation';
55
import { BehaviorSubject } from 'rxjs';
6+
import { Location } from '@angular/common';
7+
import { Router } from '@angular/router';
8+
import { User } from 'src/app/domain/user';
69

710
@Component({
811
selector: 'users-edit',
912
templateUrl: './users-edit.component.html',
1013
encapsulation: ViewEncapsulation.None,
11-
animations: [ PanelAnimation, OverlayAnimation ],
14+
animations: [ PanelAnimation, OverlayAnimation ]
1215
})
1316
export class UsersEditComponent {
14-
@ViewChild('panel') panel: ElementRef;
17+
@ViewChild('panel') panel: ElementRef;
1518

16-
public visible = new BehaviorSubject<Boolean>(false);
19+
public user = new BehaviorSubject<User>(null);
1720
public faTimes: IconDefinition = faTimes;
1821

19-
constructor(private renderer: Renderer2) {
20-
this.visible.subscribe((visible: boolean) => {
21-
if (visible) {
22-
this.renderer.addClass(document.body, 'overflow');
23-
this.renderer.setProperty(this.panel.nativeElement, 'scrollTop', '0');
24-
} else {
25-
this.renderer.removeClass(document.body, 'overflow');
26-
}
27-
});
22+
constructor(private renderer: Renderer2,
23+
private location: Location,
24+
private router: Router) {
25+
26+
this.user.subscribe((user: User) => {
27+
if (user) {
28+
this.renderer.addClass(document.body, 'overflow');
29+
this.renderer.setProperty(this.panel.nativeElement, 'scrollTop', '0');
30+
this.location.go(this.router.url + "/edit/");
31+
} else {
32+
this.renderer.removeClass(document.body, 'overflow');
33+
this.location.go(this.router.url);
34+
}
35+
});
2836
}
2937

3038
close() {
31-
this.visible.next(false);
39+
this.user.next(null);
3240
}
3341
}
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { Component, ViewEncapsulation, OnInit, Output, EventEmitter } from '@angular/core';
1+
import { Component, ViewEncapsulation, Output, EventEmitter } from '@angular/core';
22
import { faTrash, faEdit } from '@fortawesome/free-solid-svg-icons';
33
import { ListAnimation } from './../../../modules/animations/list.animation';
44
import { User } from '../../../domain/user';
55
import { Observable } from 'rxjs';
6-
import { UsersService } from './../users.service';
76
import { Pagination } from 'src/app/domain/pagination';
87
import { FormGroup } from '@angular/forms';
98
import { IconDefinition } from '@fortawesome/fontawesome-svg-core';
9+
import { HttpService } from 'src/app/services/http.service';
1010

1111
@Component({
1212
selector: 'users-list',
@@ -15,20 +15,15 @@ import { IconDefinition } from '@fortawesome/fontawesome-svg-core';
1515
animations: [ ListAnimation ],
1616
})
1717

18-
export class UsersListComponent implements OnInit {
18+
export class UsersListComponent {
1919
@Output() editEvent: EventEmitter<User> = new EventEmitter<User>();
2020
@Output() deleteEvent: EventEmitter<User> = new EventEmitter<User>();
2121

2222
public faTrash: IconDefinition = faTrash;
2323
public faEdit: IconDefinition = faEdit;
2424
public pagination: Observable<Pagination<User>>;
2525

26-
constructor(
27-
public userService: UsersService) { }
28-
29-
ngOnInit(): void {
30-
this.list(0, 10, null);
31-
}
26+
constructor(public httpService: HttpService<User>) { }
3227

3328
edit(user: User): void {
3429
this.editEvent.emit(user);
@@ -38,7 +33,9 @@ export class UsersListComponent implements OnInit {
3833
this.deleteEvent.emit(user);
3934
}
4035

41-
list(offset: Number, limit: Number, filters: FormGroup): void {
42-
this.pagination = this.userService.get(offset, limit, filters);
36+
list(offset: Number, limit: Number, filters: FormGroup): Observable<Pagination<User>> {
37+
this.pagination = this.httpService.list('users', offset, limit, filters);
38+
39+
return this.pagination;
4340
}
4441
}
Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,72 @@
1-
import { Component, ViewEncapsulation, ChangeDetectionStrategy, ViewChild } from '@angular/core';
1+
import { Component, ViewEncapsulation, ChangeDetectionStrategy, ViewChild, OnInit } from '@angular/core';
22
import { UsersListComponent } from '../users-list/users-list.component';
33
import { UsersFilterComponent } from '../users-filter/users-filter.component';
44
import { FormGroup } from '@angular/forms';
55
import { UsersAddComponent } from '../users-add/users-add.component';
66
import { User } from 'src/app/domain/user';
77
import { UsersEditComponent } from '../users-edit/users-edit.component';
88
import { ConfirmationModalComponent } from 'src/app/modules/modals/confirmation-modal.component';
9+
import { Router } from '@angular/router';
10+
import { HttpService } from 'src/app/services/http.service';
11+
import { forkJoin } from 'rxjs';
912

1013
@Component({
1114
selector: 'users-page',
1215
templateUrl: './users-page.component.html',
1316
encapsulation: ViewEncapsulation.None,
1417
changeDetection: ChangeDetectionStrategy.OnPush
1518
})
16-
export class UsersPageComponent {
19+
export class UsersPageComponent implements OnInit {
1720
@ViewChild('userFilter') userFilter: UsersFilterComponent;
1821
@ViewChild('userList') userList: UsersListComponent;
1922
@ViewChild('userAdd') userAdd: UsersAddComponent;
2023
@ViewChild('userEdit') userEdit: UsersEditComponent;
2124
@ViewChild('userDelete') userDelete: ConfirmationModalComponent;
2225

26+
constructor(
27+
private router: Router,
28+
private httpService: HttpService<User>) { }
29+
30+
ngOnInit(): void {
31+
if (this.router.url.includes('users/')) {
32+
let get = this.httpService.get('users', 1);
33+
let list = this.userList.list(0, 10, null);
34+
35+
forkJoin([get, list]).subscribe(results => {
36+
this.userEdit.user.next(results[0]);
37+
});
38+
} else if (this.router.url.includes('new')) {
39+
this.userAdd.user.next(new User());
40+
} else {
41+
this.userList.list(0, 10, null);
42+
}
43+
}
44+
2345
filter(filters: FormGroup): void {
2446
this.userList.list(0, 10, filters);
2547
}
2648

2749
add(): void {
28-
this.userEdit.visible.next(true);
50+
this.userAdd.user.next(new User());
2951
}
3052

3153
edit(user: User): void {
32-
this.userEdit.visible.next(true);
54+
this.userEdit.user.next(user);
3355
}
3456

3557
delete(user: User): void {
3658
this.userDelete.visible.next(true);
3759
}
3860

39-
applyAdd() {
61+
applyAdd(user: User): void {
4062

4163
}
4264

43-
applyEdit() {
65+
applyEdit(user: User): void {
4466

4567
}
4668

47-
applyDelete() {
69+
applyDelete(user: User): void {
4870

4971
}
5072
}

src/app/routes/users/users.module.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,16 @@ import { UsersFormComponent } from './users-form/users-form.component';
1414
import { UsersFilterComponent } from './users-filter/users-filter.component';
1515
import { UsersEditComponent } from './users-edit/users-edit.component';
1616
import { UsersAddComponent } from './users-add/users-add.component';
17-
import { UsersService } from './users.service';
18-
import { CommonModule } from '@angular/common';
17+
import { CommonModule, PathLocationStrategy, LocationStrategy, Location } from '@angular/common';
1918
import { ReactiveFormsModule, FormsModule } from '@angular/forms';
19+
import { HttpService } from 'src/app/services/http.service';
2020

2121
const appRoutes: Routes = [
2222
{ path: '', component: UsersPageComponent, canActivate: [AuthenticationGuard] },
23+
{ path: ':id', component: UsersPageComponent, canActivate: [AuthenticationGuard] },
24+
{ path: '/new', component: UsersPageComponent, canActivate: [AuthenticationGuard] }
2325
];
24-
25-
// users-add -> users.form
26-
// [{
27-
// path: 'team/:id',
28-
// component: Team,
29-
// children: [{
30-
// path: 'user/:name',
31-
// component: User
32-
// }]
33-
// }]
34-
26+
3527
@NgModule({
3628
declarations: [
3729
UsersPageComponent,
@@ -51,7 +43,8 @@ const appRoutes: Routes = [
5143
RouterModule.forChild(appRoutes)
5244
],
5345
providers: [
54-
UsersService
46+
HttpService,
47+
Location, {provide: LocationStrategy, useClass: PathLocationStrategy }
5548
]
5649
})
5750
export class UsersModule { }

0 commit comments

Comments
 (0)