Skip to content

Commit 8e44164

Browse files
Dan WahlinDan Wahlin
Dan Wahlin
authored and
Dan Wahlin
committed
Various updates
1 parent 0b8658f commit 8e44164

31 files changed

+547
-29
lines changed

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ npm-debug.log
77
.vs
88

99
# Build Generated Files
10-
public/app/**/*.js
11-
public/app/**/*.js.map
12-
public/lib
10+
src/public/app/**/*.js
11+
src/public/app/**/*.js.map
12+
src/public/lib
1313

1414

1515

.vscode/settings.json

Lines changed: 0 additions & 9 deletions
This file was deleted.
File renamed without changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
3+
class CustomersController {
4+
5+
constructor(router) {
6+
7+
8+
9+
}
10+
11+
12+
13+
}
14+
15+
module.exports = CustomersController;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { RouterModule, Routes } from '@angular/router';
2+
3+
import { CustomersComponent } from './customers/customers.component';
4+
import { CustomersGridComponent } from './customers/customers-grid.component';
5+
import { CustomerEditComponent } from './customers/customer-edit.component';
6+
import { CustomerEditReactiveComponent } from './customers/customer-edit-reactive.component';
7+
import { IRouting } from './shared/interfaces';
8+
9+
const routes: Routes = [
10+
{ path: 'customers', component: CustomersComponent},
11+
{ path: 'customers/:id', component: CustomerEditComponent},
12+
//{ path: 'customers/:id', component: CustomerEditReactiveComponent },
13+
{ path: '**', pathMatch:'full', redirectTo: '/customers' } //catch any unfound routes and redirect to home page
14+
];
15+
16+
export const appRouting: IRouting = {
17+
routes: RouterModule.forRoot(routes),
18+
components: [ CustomersComponent, CustomerEditComponent, CustomerEditReactiveComponent, CustomersGridComponent ]
19+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { NgModule, Optional, SkipSelf } from '@angular/core';
2+
import { HttpModule } from '@angular/http';
3+
4+
5+
import { DataFilterService } from './data-filter.service';
6+
import { Sorter } from './sorter';
7+
import { TrackByService } from './trackby.service';
8+
import { EnsureModuleLoadedOnceGuard } from '../shared/ensureModuleLoadedOnceGuard';
9+
10+
@NgModule({
11+
imports: [ HttpModule ],
12+
providers: [DataFilterService, Sorter, TrackByService] // these should be singleton
13+
})
14+
export class CoreModule extends EnsureModuleLoadedOnceGuard { //Ensure that CoreModule is only loaded into AppModule
15+
16+
//Looks for the module in the parent injector to see if it's already been loaded (only want it loaded once)
17+
constructor( @Optional() @SkipSelf() parentModule: CoreModule) {
18+
super(parentModule);
19+
}
20+
21+
}
22+
23+
24+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Injectable } from '@angular/core';
2+
3+
@Injectable()
4+
export class DataService {
5+
6+
constructor() {
7+
8+
}
9+
10+
11+
12+
13+
calculateCustomersOrderTotal(customers: ICustomer[]) {
14+
for (let customer of customers) {
15+
if (customer && customer.orders) {
16+
let total = 0;
17+
for (let order of customer.orders) {
18+
total += (order.price * order.quantity);
19+
}
20+
customer.orderTotal = total;
21+
}
22+
}
23+
}
24+
25+
private handleError(error: any) {
26+
console.error('server error:', error);
27+
if (error instanceof Response) {
28+
let errMessage = '';
29+
try {
30+
errMessage = error.json().error;
31+
} catch(err) {
32+
errMessage = error.statusText;
33+
}
34+
return Observable.throw(errMessage);
35+
// Use the following instead if using lite-server
36+
//return Observable.throw(err.text() || 'backend server error');
37+
}
38+
return Observable.throw(error || 'Node.js server error');
39+
}
40+
41+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { Component, OnInit } from '@angular/core';
2+
import { Router, ActivatedRoute } from '@angular/router';
3+
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
4+
5+
import { DataService } from '../core/data.service';
6+
import { ICustomer, IState } from '../shared/interfaces';
7+
import { ValidationService } from '../shared/validation.service';
8+
9+
@Component({
10+
moduleId: module.id,
11+
selector: 'customer-edit-reactive',
12+
templateUrl: 'customer-edit-reactive.component.html'
13+
})
14+
export class CustomerEditReactiveComponent implements OnInit {
15+
16+
customerForm: FormGroup;
17+
customer: ICustomer = {
18+
_id: '',
19+
firstName: '',
20+
lastName: '',
21+
gender: '',
22+
address: '',
23+
email: '',
24+
city: '',
25+
stateId: 0,
26+
zip: 0
27+
};
28+
states: IState[];
29+
errorMessage: string;
30+
deleteMessageEnabled: boolean;
31+
operationText: string = 'Insert';
32+
33+
constructor(private router: Router,
34+
private route: ActivatedRoute,
35+
private dataService: DataService,
36+
private formBuilder: FormBuilder) { }
37+
38+
ngOnInit() {
39+
let id = this.route.snapshot.params['id'];
40+
if (id !== '0') {
41+
this.operationText = 'Update';
42+
this.getCustomer(id);
43+
}
44+
45+
this.getStates();
46+
this.buildForm();
47+
}
48+
49+
getCustomer(id: string) {
50+
this.dataService.getCustomer(id)
51+
.subscribe((customer: ICustomer) => {
52+
//Quick and dirty clone used in case user cancels out of form
53+
const cust = JSON.stringify(customer);
54+
this.customer = JSON.parse(cust);
55+
this.buildForm();
56+
},
57+
(err) => console.log(err));
58+
}
59+
60+
buildForm() {
61+
this.customerForm = this.formBuilder.group({
62+
firstName: [this.customer.firstName, Validators.required],
63+
lastName: [this.customer.lastName, Validators.required],
64+
gender: [this.customer.gender, Validators.required],
65+
email: [this.customer.email, [Validators.required, ValidationService.emailValidator]],
66+
address: [this.customer.address, Validators.required],
67+
city: [this.customer.city, Validators.required],
68+
stateId: [this.customer.stateId, Validators.required]
69+
});
70+
}
71+
72+
getStates() {
73+
this.dataService.getStates().subscribe((states: IState[]) => this.states = states);
74+
}
75+
76+
submit({ value, valid }: { value: ICustomer, valid: boolean }) {
77+
78+
value._id = this.customer._id;
79+
value.zip = this.customer.zip || 0;
80+
// var customer: ICustomer = {
81+
// _id: this.customer._id,
82+
// };
83+
84+
if (value._id) {
85+
86+
87+
88+
} else {
89+
90+
91+
92+
}
93+
}
94+
95+
cancel(event: Event) {
96+
event.preventDefault();
97+
this.router.navigate(['/customers']);
98+
}
99+
100+
delete(event: Event) {
101+
102+
}
103+
104+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<div>
2+
<header>
3+
<h3>
4+
<span class="glyphicon glyphicon-user"></span>
5+
{{ customer.firstName }} {{ customer.lastName }}
6+
</h3>
7+
</header>
8+
<br />
9+
<form class="editForm" novalidate>
10+
<div class="form-group">
11+
<label>Name</label>
12+
<input type="text" class="form-control" required>
13+
<div class="alert alert-danger" [hidden]="firstName.untouched || firstName.valid">First Name is required</div>
14+
</div>
15+
<div class="form-group">
16+
<label>Last Name</label>
17+
<input type="text" class="form-control" name="lastName" [(ngModel)]="customer.lastName" #lastName="ngModel" required>
18+
<div class="alert alert-danger" [hidden]="lastName.untouched || lastName.valid">Last Name is required</div>
19+
</div>
20+
<div class="form-group">
21+
<label>Gender</label>
22+
<br />
23+
<div class="radio">
24+
<label>
25+
<input type="radio" name="gender" [(ngModel)]="customer.gender" #gender="ngModel" value="Male" required />
26+
Male
27+
</label>
28+
</div>
29+
<div class="radio">
30+
<label>
31+
<input type="radio" name="gender" [(ngModel)]="customer.gender" #gender="ngModel" value="Female" required />
32+
Female
33+
</label>
34+
</div>
35+
</div>
36+
<div class="form-group">
37+
<label>Email</label>
38+
<input type="email" class="form-control" name="email" [(ngModel)]="customer.email" #email="ngModel" required pattern="^[^\s@]+@[^\s@]+\.[^\s@]{2,}$" />
39+
<div class="alert alert-danger" [hidden]="email.untouched || email.valid">Email is required and must be valid</div>
40+
</div>
41+
<div class="form-group">
42+
<label>Address</label>
43+
<input type="text" class="form-control" name="address" [(ngModel)]="customer.address" #address="ngModel" required>
44+
<div class="alert alert-danger" [hidden]="address.untouched || address.valid">Address is required</div>
45+
</div>
46+
<div class="form-group">
47+
<label>City</label>
48+
<input type="text" class="form-control" name="city" [(ngModel)]="customer.city" #city="ngModel" required>
49+
<div class="alert alert-danger" [hidden]="city.untouched || city.valid">City is required</div>
50+
</div>
51+
<div class="form-group">
52+
<label>State</label>
53+
<select class="form-control"
54+
[(ngModel)]="customer.stateId"
55+
name="state"
56+
required>
57+
<option *ngFor="let state of states" [ngValue]="state.id">{{state.name}}</option>
58+
</select>
59+
</div>
60+
<br />
61+
62+
<div *ngIf="customer">
63+
<div class="alert alert-warning" *ngIf="customer._id && deleteMessageEnabled">
64+
Delete Customer?&nbsp;&nbsp;<button class="btn btn-danger" (click)="delete($event)">Yes</button>&nbsp;&nbsp;
65+
<button class="btn btn-default" (click)="deleteMessageEnabled = false">No</button>
66+
</div>
67+
<button class="btn btn-danger" *ngIf="customer._id && !deleteMessageEnabled" (click)="deleteMessageEnabled = true">Delete</button>&nbsp;&nbsp;
68+
69+
<div class="pull-right" *ngIf="!deleteMessageEnabled">
70+
<button class="btn btn-default" (click)="cancel($event)">Cancel</button>&nbsp;&nbsp;
71+
<button type="submit" class="btn btn-success" [disabled]="!customerForm.valid">{{ operationText }}</button>
72+
</div>
73+
</div>
74+
<br />
75+
<br />
76+
<div class="alert alert-danger" *ngIf="errorMessage != null">{{ errorMessage }}</div>
77+
78+
</form>
79+
</div>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { Component, OnInit } from '@angular/core';
2+
import { Router, ActivatedRoute } from '@angular/router';
3+
4+
import { DataService } from '../core/data.service';
5+
import { ICustomer, IState } from '../shared/interfaces';
6+
7+
@Component({
8+
moduleId: module.id,
9+
selector: 'customer-edit',
10+
templateUrl: 'customer-edit.component.html'
11+
})
12+
export class CustomerEditComponent implements OnInit {
13+
14+
customer: ICustomer = {
15+
_id: '',
16+
firstName: '',
17+
lastName: '',
18+
gender: '',
19+
address: '',
20+
email: '',
21+
city: '',
22+
state: {
23+
abbreviation: '',
24+
name: ''
25+
},
26+
stateId: 0,
27+
zip: 0
28+
};
29+
states: IState[];
30+
errorMessage: string;
31+
deleteMessageEnabled: boolean;
32+
operationText: string = 'Insert';
33+
34+
constructor(private router: Router,
35+
private route: ActivatedRoute,
36+
private dataService: DataService) { }
37+
38+
ngOnInit() {
39+
let id = this.route.snapshot.params['id'];
40+
if (id !== '0') {
41+
this.operationText = 'Update';
42+
this.getCustomer(id);
43+
}
44+
45+
this.getStates();
46+
}
47+
48+
getCustomer(id: string) {
49+
50+
}
51+
52+
getStates() {
53+
this.dataService.getStates().subscribe((states: IState[]) => this.states = states);
54+
}
55+
56+
submit() {
57+
58+
}
59+
60+
cancel(event: Event) {
61+
event.preventDefault();
62+
this.router.navigate(['/']);
63+
}
64+
65+
delete(event: Event) {
66+
67+
}
68+
69+
}

0 commit comments

Comments
 (0)