Skip to content

Commit 982ced4

Browse files
author
Lukas Ruebbelke
committed
update: ractoring and cleaning up
1 parent a746669 commit 982ced4

18 files changed

+593
-92
lines changed

apps/dashboard/src/app/projects/projects-routing.module.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
import { NgModule } from '@angular/core';
2-
import { Routes, RouterModule } from '@angular/router';
3-
4-
import { AuthGuardService as AuthGuard } from '@workshop/core-data';
2+
import { RouterModule, Routes } from '@angular/router';
53

64
import { ProjectsComponent } from './projects.component';
75

86
const routes: Routes = [
9-
{
10-
path: '',
11-
component: ProjectsComponent,
12-
canActivate: [AuthGuard]
13-
}
7+
{ path: '', component: ProjectsComponent }
148
];
159

1610
@NgModule({

apps/dashboard/src/app/projects/projects.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<div class="col-50">
1111
<app-project-details
12-
[project]="currentProject"
12+
[project]="currentProject$ | async"
1313
[customers]="customers$ | async"
1414
(saved)="saveProject($event)"
1515
(cancelled)="resetCurrentProject($event)">
Lines changed: 84 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,116 @@
1-
import { Observable } from 'rxjs';
21
import { Component, OnInit } from '@angular/core';
3-
import { Customer, Project, ProjectsService, NotificationsService, CustomersService } from '@workshop/core-data';
4-
5-
const emptyProject: Project = {
6-
id: null,
7-
title: '',
8-
details: '',
9-
percentComplete: 0,
10-
approved: false,
11-
customerId: null
12-
}
2+
import { Project, ProjectsFacade, Customer, CustomersFacade } from '@workshop/core-data';
3+
import { Observable } from 'rxjs';
134

145
@Component({
156
selector: 'app-projects',
167
templateUrl: './projects.component.html',
178
styleUrls: ['./projects.component.scss']
189
})
1910
export class ProjectsComponent implements OnInit {
20-
projects$: Observable<Project[]>;
21-
customers$: Observable<Customer[]>;
22-
currentProject: Project;
11+
customers$: Observable<Customer[]> = this.customersFacade.allCustomers$;
12+
projects$: Observable<Project[]> = this.projectsFacade.allProjects$;
13+
currentProject$: Observable<Project> = this.projectsFacade.currentProject$;
2314

2415
constructor(
25-
private projectsService: ProjectsService,
26-
private customerService: CustomersService,
27-
private ns: NotificationsService) { }
16+
private projectsFacade: ProjectsFacade,
17+
private customersFacade: CustomersFacade
18+
) { }
2819

2920
ngOnInit() {
30-
this.getProjects();
31-
this.getCustomers();
21+
this.customersFacade.loadCustomers();
22+
this.projectsFacade.loadProjects();
23+
this.projectsFacade.mutations$.subscribe(_ => this.resetCurrentProject());
3224
this.resetCurrentProject();
3325
}
3426

3527
resetCurrentProject() {
36-
this.currentProject = emptyProject;
28+
this.selectProject({id: null});
3729
}
3830

3931
selectProject(project) {
40-
this.currentProject = project;
41-
}
42-
43-
cancel(project) {
44-
this.resetCurrentProject();
45-
}
46-
47-
getCustomers() {
48-
this.customers$ = this.customerService.all();
49-
}
50-
51-
getProjects() {
52-
this.projects$ = this.projectsService.all();
32+
this.projectsFacade.selectProject(project.id);
5333
}
5434

5535
saveProject(project) {
5636
if (!project.id) {
57-
this.createProject(project);
37+
this.projectsFacade.addProject(project);
5838
} else {
59-
this.updateProject(project);
39+
this.projectsFacade.updateProject(project);
6040
}
6141
}
6242

63-
createProject(project) {
64-
this.projectsService.create(project)
65-
.subscribe(response => {
66-
this.ns.emit('Project created!');
67-
this.getProjects();
68-
this.resetCurrentProject();
69-
});
43+
deleteProject(project) {
44+
this.projectsFacade.deleteProject(project);
7045
}
46+
// projects$: Observable<Project[]>;
47+
// customers$: Observable<Customer[]>;
48+
// currentProject: Project;
7149

72-
updateProject(project) {
73-
this.projectsService.update(project)
74-
.subscribe(response => {
75-
this.ns.emit('Project saved!');
76-
this.getProjects();
77-
this.resetCurrentProject();
78-
});
79-
}
50+
// constructor(
51+
// private projectsService: ProjectsService,
52+
// private customerService: CustomersService,
53+
// private ns: NotificationsService) { }
8054

81-
deleteProject(project) {
82-
this.projectsService.delete(project)
83-
.subscribe(response => {
84-
this.ns.emit('Project deleted!');
85-
this.getProjects();
86-
this.resetCurrentProject();
87-
});
88-
}
55+
// ngOnInit() {
56+
// this.getProjects();
57+
// this.getCustomers();
58+
// this.resetCurrentProject();
59+
// }
60+
61+
// resetCurrentProject() {
62+
// this.currentProject = emptyProject;
63+
// }
64+
65+
// selectProject(project) {
66+
// this.currentProject = project;
67+
// }
68+
69+
// cancel(project) {
70+
// this.resetCurrentProject();
71+
// }
72+
73+
// getCustomers() {
74+
// this.customers$ = this.customerService.all();
75+
// }
76+
77+
// getProjects() {
78+
// this.projects$ = this.projectsService.all();
79+
// }
80+
81+
// saveProject(project) {
82+
// if (!project.id) {
83+
// this.createProject(project);
84+
// } else {
85+
// this.updateProject(project);
86+
// }
87+
// }
88+
89+
// createProject(project) {
90+
// this.projectsService.create(project)
91+
// .subscribe(response => {
92+
// this.ns.emit('Project created!');
93+
// this.getProjects();
94+
// this.resetCurrentProject();
95+
// });
96+
// }
97+
98+
// updateProject(project) {
99+
// this.projectsService.update(project)
100+
// .subscribe(response => {
101+
// this.ns.emit('Project saved!');
102+
// this.getProjects();
103+
// this.resetCurrentProject();
104+
// });
105+
// }
106+
107+
// deleteProject(project) {
108+
// this.projectsService.delete(project)
109+
// .subscribe(response => {
110+
// this.ns.emit('Project deleted!');
111+
// this.getProjects();
112+
// this.resetCurrentProject();
113+
// });
114+
// }
89115
}
90116

libs/core-data/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ export { CustomersService } from './lib/customers/customers.service';
66
export { Customer } from './lib/customers/customer.model';
77
export { Project } from './lib/projects/project.model';
88
export { ProjectsService } from './lib/projects/projects.service';
9+
export { ProjectsFacade } from './lib/state/projects/projects.facade';
10+
export { CustomersFacade } from './lib/state/customers/customers.facade';
Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,26 @@
1-
import { AuthGuardService } from './auth/auth-guard.service';
21
import { CommonModule } from '@angular/common';
3-
import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http';
2+
import { HttpClientModule } from '@angular/common/http';
43
import { NgModule } from '@angular/core';
54

5+
import { AuthGuardService } from './auth/auth-guard.service';
66
import { AuthService } from './auth/auth.service';
7-
import { TokenInterceptor } from './auth/token.interceptor';
7+
import { CustomersService } from './customers/customers.service';
88
import { NotificationsService } from './notifications/notifications.service';
99
import { ProjectsService } from './projects/projects.service';
10-
import { ErrorInterceptor } from './error/error.interceptor';
11-
import { CustomersService } from './customers/customers.service';
10+
import { StateModule } from './state/state.module';
1211

1312
@NgModule({
1413
providers: [
1514
AuthService,
1615
AuthGuardService,
1716
NotificationsService,
1817
CustomersService,
19-
ProjectsService,
20-
{
21-
provide: HTTP_INTERCEPTORS,
22-
useClass: TokenInterceptor,
23-
multi: true
24-
},
25-
{
26-
provide: HTTP_INTERCEPTORS,
27-
useClass: ErrorInterceptor,
28-
multi: true
29-
}
18+
ProjectsService
19+
],
20+
imports: [
21+
CommonModule,
22+
HttpClientModule,
23+
StateModule
3024
],
31-
imports: [CommonModule, HttpClientModule],
3225
})
3326
export class CoreDataModule {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Action } from '@ngrx/store';
2+
3+
export enum CustomersActionTypes {
4+
LoadCustomers = '[Customers] Load Data',
5+
CustomersLoaded = '[Customers] Data Loaded'
6+
}
7+
8+
export class LoadCustomers implements Action {
9+
readonly type = CustomersActionTypes.LoadCustomers;
10+
constructor() {}
11+
}
12+
13+
export class CustomersLoaded implements Action {
14+
readonly type = CustomersActionTypes.CustomersLoaded;
15+
constructor(public payload: any) {}
16+
}
17+
18+
export type CustomersActions = LoadCustomers | CustomersLoaded;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Injectable } from '@angular/core';
2+
import { Actions, Effect } from '@ngrx/effects';
3+
import { DataPersistence } from '@nrwl/nx';
4+
import { map } from 'rxjs/operators';
5+
6+
import { Customer } from '../../customers/customer.model';
7+
import { CustomersService } from '../../customers/customers.service';
8+
import { CustomersActionTypes, CustomersLoaded, LoadCustomers } from './customers.actions';
9+
import { CustomersState } from './customers.reducer';
10+
11+
@Injectable()
12+
export class CustomersEffects {
13+
@Effect()
14+
loadCustomers$ = this.dataPersistence.fetch(CustomersActionTypes.LoadCustomers, {
15+
run: (action: LoadCustomers, state: CustomersState) => {
16+
return this.customersService.all().pipe(map((res: Customer[]) => new CustomersLoaded(res)))
17+
},
18+
19+
onError: (action: LoadCustomers, error) => {
20+
console.error('Error', error);
21+
}
22+
});
23+
24+
constructor(
25+
private actions$: Actions,
26+
private dataPersistence: DataPersistence<CustomersState>,
27+
private customersService: CustomersService
28+
) {}
29+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Injectable } from '@angular/core';
2+
import { select, Store } from '@ngrx/store';
3+
4+
import * as CustomersActions from './customers.actions';
5+
import { CustomersState } from './customers.reducer';
6+
import { selectAllCustomers } from '..';
7+
8+
@Injectable({
9+
providedIn: 'root'
10+
})
11+
export class CustomersFacade {
12+
allCustomers$ = this.store.pipe(select(selectAllCustomers));
13+
14+
constructor(private store: Store<CustomersState>) {}
15+
16+
loadCustomers() {
17+
this.store.dispatch(new CustomersActions.LoadCustomers());
18+
}
19+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { createEntityAdapter, EntityAdapter, EntityState } from '@ngrx/entity';
2+
3+
import { Customer } from '../../customers/customer.model';
4+
import { CustomersActions, CustomersActionTypes } from './customers.actions';
5+
6+
/**
7+
* Interface to the part of the Store containing CustomersState
8+
* and other information related to Customer.
9+
*/
10+
export interface CustomersState extends EntityState<Customer> {}
11+
12+
export const adapter: EntityAdapter<Customer> = createEntityAdapter<Customer>();
13+
export const initialState: CustomersState = adapter.getInitialState();
14+
15+
export function customersReducer(
16+
state = initialState,
17+
action: CustomersActions
18+
): CustomersState {
19+
switch (action.type) {
20+
case CustomersActionTypes.CustomersLoaded: {
21+
return adapter.addAll(action.payload, state);
22+
}
23+
24+
default:
25+
return state;
26+
}
27+
}
28+
29+
// get the selectors
30+
const { selectIds, selectEntities, selectAll } = adapter.getSelectors();
31+
32+
// select the array of widget ids
33+
export const selectCustomerIds = selectIds;
34+
35+
// select the dictionary of widget entities
36+
export const selectCustomerEntities = selectEntities;
37+
38+
// select the array of widgets
39+
export const selectAllCustomers = selectAll;
40+

0 commit comments

Comments
 (0)