Skip to content

Commit c148d3a

Browse files
authored
Aurelia contact creation
1 parent f0df5c6 commit c148d3a

File tree

4 files changed

+81
-9
lines changed

4 files changed

+81
-9
lines changed

ASP.NET Core Basics/src/Aurelia/ClientApp/app/components/contacts/contact.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
phone: string;
99
email: string;
1010

11-
constructor(data) {
11+
constructor(data?) {
12+
if (data == null) return;
1213
Object.assign(this, data);
1314
}
1415

ASP.NET Core Basics/src/Aurelia/ClientApp/app/components/contacts/contactDetail.html

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<h1>Contact Details</h1>
33
<hr />
4-
<div if.bind="contact">
4+
<div if.bind="hasContactId">
55
<dl class="dl-horizontal">
66
<dt>ID</dt>
77
<dd>${contact.id}</dd>
@@ -15,9 +15,60 @@ <h1>Contact Details</h1>
1515
<dd>${contact.email}</dd>
1616
</dl>
1717
</div>
18-
<h3 if.bind="!hasContactId">
19-
Place holder for creating a new contact
20-
</h3>
21-
<a route-href="route: contactlist">Back to List</a>
18+
<div if.bind="!hasContactId">
19+
<div>
20+
<form role="form" class="form-horizontal">
21+
<div class="form-group">
22+
<label class="col-sm-2 control-label">Name</label>
23+
<div class="col-sm-10">
24+
<input type="text" placeholder="name" class="form-control" value.bind="contact.name" />
25+
</div>
26+
</div>
27+
<div class="form-group">
28+
<label class="col-sm-2 control-label">Address</label>
29+
<div class="col-sm-10">
30+
<input type="text" placeholder="address" class="form-control" value.bind="contact.address" />
31+
</div>
32+
</div>
33+
<div class="form-group">
34+
<label class="col-sm-2 control-label">City</label>
35+
<div class="col-sm-10">
36+
<input type="text" placeholder="city" class="form-control" value.bind="contact.city" />
37+
</div>
38+
</div>
39+
<div class="form-group">
40+
<label class="col-sm-2 control-label">State</label>
41+
<div class="col-sm-10">
42+
<input type="text" placeholder="state" class="form-control" value.bind="contact.state" />
43+
</div>
44+
</div>
45+
<div class="form-group">
46+
<label class="col-sm-2 control-label">Zip</label>
47+
<div class="col-sm-10">
48+
<input type="text" placeholder="zip" class="form-control" value.bind="contact.postalCode" />
49+
</div>
50+
</div>
51+
<div class="form-group">
52+
<label class="col-sm-2 control-label">Phone</label>
53+
<div class="col-sm-10">
54+
<input type="text" placeholder="phone" class="form-control" value.bind="contact.phone" />
55+
</div>
56+
</div>
57+
<div class="form-group">
58+
<label class="col-sm-2 control-label">Email</label>
59+
<div class="col-sm-10">
60+
<input type="email" placeholder="email" class="form-control" value.bind="contact.email" />
61+
</div>
62+
</div>
63+
</form>
64+
</div>
65+
<div class="text-center">
66+
<button class="btn btn-success btn-lg" click.delegate="save()">Save</button>
67+
<button class="btn btn-danger btn-lg" click.delegate="reset()">Reset</button>
68+
</div>
69+
</div>
70+
<div>
71+
<a route-href="route: contactlist">Back to List</a>
72+
</div>
2273
<hr />
2374
</template>

ASP.NET Core Basics/src/Aurelia/ClientApp/app/components/contacts/contactDetail.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ContactService } from './contactService';
44

55
@inject(ContactService)
66
export class ContactDetail {
7-
contact: Contact;
7+
contact: Contact = new Contact();
88
hasContactId: boolean;
99

1010
constructor(private contactService: ContactService) { }
@@ -18,7 +18,16 @@ export class ContactDetail {
1818
}
1919

2020
return null;
21+
}
22+
23+
reset() {
24+
this.contact = new Contact();
25+
}
2126

27+
save() {
28+
this.contactService.save(this.contact)
29+
.then(contact => this.contact = contact)
30+
.then(() => this.hasContactId = true);
2231
}
2332

2433
}

ASP.NET Core Basics/src/Aurelia/ClientApp/app/components/contacts/contactService.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HttpClient } from 'aurelia-fetch-client';
1+
import { HttpClient, json } from 'aurelia-fetch-client';
22
import { inject } from 'aurelia-framework';
33
import { Contact } from './contact';
44

@@ -12,7 +12,7 @@ export class ContactService {
1212
});
1313
}
1414

15-
getAll() : Promise<Contact[]> {
15+
getAll(): Promise<Contact[]> {
1616
return this.http.fetch('')
1717
.then(response => response.json())
1818
.then(contacts => Array.from(contacts, c => new Contact(c)))
@@ -26,4 +26,15 @@ export class ContactService {
2626
.catch(error => console.log(error));
2727
}
2828

29+
save(contact: Contact): Promise<Contact> {
30+
return this.http.fetch('',
31+
{
32+
method: 'post',
33+
body: json(contact)
34+
})
35+
.then(response => response.json())
36+
.then(contact => new Contact(contact))
37+
.catch(error => console.log(error));
38+
}
39+
2940
}

0 commit comments

Comments
 (0)