Skip to content

Commit 09cf1f7

Browse files
authored
React - Add view only contact detail (#20)
1 parent f70081b commit 09cf1f7

39 files changed

+883
-652
lines changed

ASP.NET Core Basics/ASP.NET Core Basics.sln

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26730.16
4+
VisualStudioVersion = 15.0.27004.2002
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{6446DEF2-D423-47E2-85CC-691A52915EC2}"
77
EndProject
@@ -13,7 +13,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Aurelia", "src\Aurelia\Aure
1313
EndProject
1414
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Angular", "src\Angular\Angular.csproj", "{F27EF346-050A-4AC6-BCAA-D2446246240C}"
1515
EndProject
16-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "React", "React\React.csproj", "{309AB54C-C4ED-4E7E-870C-9032A970E3CF}"
16+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "React", "src\React\React.csproj", "{56EC1AA3-0060-4349-921F-59AAB8A93129}"
1717
EndProject
1818
Global
1919
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -33,10 +33,10 @@ Global
3333
{F27EF346-050A-4AC6-BCAA-D2446246240C}.Debug|Any CPU.Build.0 = Debug|Any CPU
3434
{F27EF346-050A-4AC6-BCAA-D2446246240C}.Release|Any CPU.ActiveCfg = Release|Any CPU
3535
{F27EF346-050A-4AC6-BCAA-D2446246240C}.Release|Any CPU.Build.0 = Release|Any CPU
36-
{309AB54C-C4ED-4E7E-870C-9032A970E3CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
37-
{309AB54C-C4ED-4E7E-870C-9032A970E3CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
38-
{309AB54C-C4ED-4E7E-870C-9032A970E3CF}.Release|Any CPU.ActiveCfg = Release|Any CPU
39-
{309AB54C-C4ED-4E7E-870C-9032A970E3CF}.Release|Any CPU.Build.0 = Release|Any CPU
36+
{56EC1AA3-0060-4349-921F-59AAB8A93129}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
37+
{56EC1AA3-0060-4349-921F-59AAB8A93129}.Debug|Any CPU.Build.0 = Debug|Any CPU
38+
{56EC1AA3-0060-4349-921F-59AAB8A93129}.Release|Any CPU.ActiveCfg = Release|Any CPU
39+
{56EC1AA3-0060-4349-921F-59AAB8A93129}.Release|Any CPU.Build.0 = Release|Any CPU
4040
EndGlobalSection
4141
GlobalSection(SolutionProperties) = preSolution
4242
HideSolutionNode = FALSE
@@ -45,7 +45,7 @@ Global
4545
{A1208011-947A-475C-9011-E2657FAB5A31} = {6446DEF2-D423-47E2-85CC-691A52915EC2}
4646
{EB45A937-E3EF-4B0F-A674-92B44BEA3FDA} = {6446DEF2-D423-47E2-85CC-691A52915EC2}
4747
{F27EF346-050A-4AC6-BCAA-D2446246240C} = {6446DEF2-D423-47E2-85CC-691A52915EC2}
48-
{309AB54C-C4ED-4E7E-870C-9032A970E3CF} = {6446DEF2-D423-47E2-85CC-691A52915EC2}
48+
{56EC1AA3-0060-4349-921F-59AAB8A93129} = {6446DEF2-D423-47E2-85CC-691A52915EC2}
4949
EndGlobalSection
5050
GlobalSection(ExtensibilityGlobals) = postSolution
5151
SolutionGuid = {810FF428-282C-444B-9C2A-660B9AAE9D8F}

ASP.NET Core Basics/React/wwwroot/dist/main.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import * as React from 'react';
2+
import { RouteComponentProps } from 'react-router';
3+
import { Link, NavLink } from 'react-router-dom';
4+
import 'isomorphic-fetch';
5+
import { Contact } from './contact';
6+
import { ContactService } from './contactService';
7+
8+
interface ContactDetailState {
9+
id: string;
10+
contact: Contact | undefined;
11+
loading: boolean;
12+
}
13+
14+
export class ContactDetail extends React.Component<RouteComponentProps<{}>, ContactDetailState> {
15+
16+
constructor(props: any) {
17+
super();
18+
this.state = { id: props.match.params.id, contact: undefined, loading: true };
19+
20+
let contactService = new ContactService();
21+
contactService.getById(this.state.id)
22+
.then(data => {
23+
this.setState({ contact: data, loading: false });
24+
});
25+
}
26+
27+
public render() {
28+
let contents = this.state.loading
29+
? <p><em>Loading...</em></p>
30+
: this.state.contact
31+
? ContactDetail.renderContactsTable(this.state.contact)
32+
: <p>No contacts</p>;
33+
34+
return <div>
35+
<h1>Contact Detail</h1>
36+
<hr />
37+
{contents}
38+
<NavLink to={'/contactlist'}>Back to List</NavLink>
39+
<hr />
40+
</div>;
41+
}
42+
43+
private static renderContactsTable(contact: Contact) {
44+
return <dl className="dl-horizontal">
45+
<dt>ID</dt>
46+
<dd>{contact.id}</dd>
47+
<dt>Name</dt>
48+
<dd>{contact.name}</dd>
49+
<dt>Address</dt>
50+
<dd>{contact.getAddress()}</dd>
51+
<dt>Phone</dt>
52+
<dd>{contact.phone}</dd>
53+
<dt>Email</dt>
54+
<dd>{contact.email}</dd>
55+
</dl>;
56+
}
57+
}

ASP.NET Core Basics/React/ClientApp/components/ContactList.tsx renamed to ASP.NET Core Basics/src/React/ClientApp/components/contacts/ContactList.tsx

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import * as React from 'react';
22
import { RouteComponentProps } from 'react-router';
3+
import { Link } from 'react-router-dom';
34
import 'isomorphic-fetch';
5+
import { Contact } from './contact';
6+
import { ContactService } from './contactService';
47

58
interface ContactListState {
69
contacts: Contact[];
@@ -12,8 +15,8 @@ export class ContactList extends React.Component<RouteComponentProps<{}>, Contac
1215
super();
1316
this.state = { contacts: [], loading: true };
1417

15-
fetch('http://localhost:13322/api/contactsApi/')
16-
.then(response => response.json() as Promise<Contact[]>)
18+
let contactService = new ContactService();
19+
contactService.getAll()
1720
.then(data => {
1821
this.setState({ contacts: data, loading: false });
1922
});
@@ -41,22 +44,11 @@ export class ContactList extends React.Component<RouteComponentProps<{}>, Contac
4144
<tbody>
4245
{contacts.map(contact =>
4346
<tr key={contact.id}>
44-
<td>{contact.id}</td>
47+
<td><Link to={`contactdetail/${contact.id}`}>{contact.id}</Link></td>
4548
<td>{contact.name}</td>
4649
</tr>
4750
)}
4851
</tbody>
4952
</table>;
5053
}
5154
}
52-
53-
interface Contact {
54-
id: number;
55-
name: string;
56-
address: string;
57-
city: string;
58-
state: string;
59-
postalCode: string;
60-
phone: string;
61-
email: string;
62-
}

0 commit comments

Comments
 (0)