Contact Managment UI With React
Contact Managment UI With React
index.js
// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers: https://bit.ly/CRA-PWA
serviceWorker.unregister();
App.js
// Login Form
class LoginForm extends Component {
constructor(props){
super(props);
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.state = {id:'', name : ''}
}
handleChange(event) {
let tempName = event.target.name;
let tempValue = event.target.value;
this.setState({[tempName]: tempValue});
}
handleSubmit(event) {
event.preventDefault();
this.props.history.push(`/success/${this.state.id}`);
}
render() {
return(<div>
<h3>Login Form!</h3>
<form onSubmit = {this.handleSubmit}>
Id : <input type = "text" name = "id" onChange = {this.handleChange}></input>
<br/>
Name : <input type = "text" name = "name" onChange =
{this.handleChange}></input>
<br/>
componentDidMount() {
console.log('---component did mount---');
console.log(this.props.match.params);
console.log('---component did mount---');
}
render() {
//console.log(this.props.match.url);
return (<div>
<div className = "row">
<div className = "col-3">
<h6 className = "text-center">Profile Summary</h6>
<hr />
<p>Id: {this.props.match.params.id}</p>
</div>
<div className = "col-1">
</div>
<div className = "col-8">
<h6>This is success component : {this.props.match.params.id}</h6>
<hr/>
<Link to = {this.props.match.url+'/dashboard'}>Dashboard</Link> /
<Link to = {this.props.match.url+'/addContacts'}>Add Contact</Link> /
<Link to = {this.props.match.url+'/showAll'}>Show Contacts</Link> /
<Link to = {this.props.match.url+'/update'}>Update</Link>
</div>);
}
}
Partial Output
Link and anchor both produces the url but Link gives instruction to the React BrowserRouter
component to add the component to the Route that matches to the URL without refreshing the
whole document, whereas the anchor gives instruction the Browser directly that will load the whole
page instead of updating the DOM.
Things to understand is <Link> is react component that updates the DOM and <a> updates the whole
Document.
Route: It is like a outlet for the components that has to be rendered when a path matches.
Below is the code with some working UI of contact manager but Dashboard doesn’t show newly
added counts it must be modified, but if you refresh the whole document it will show the newly
added records count.
App.js
render() {
return(<div>
<h4>Hi, {this.props.profile.name}</h4> <hr />
<h6>Your total contacts: {this.props.profile.contactList.length}</h6>
</div>);
}
}
}
}
class UpdateProfile extends Component {
render() {
return(<div>
<h5>Update Profile </h5>
</div>);
}
}
</table>
</div>
<div className = "col-1">
<div style = {{height : '500px', borderLeft : '1px solid silver'}}></div>
</div>
<div className = "col-8">
<nav className = "navbar nav-default navbar-expand-sm">
<ul className = "nav navbar-nav ml-auto">
<li>
<Link to = {this.props.match.url+'/dashboard'} className = "mr-
3">Dashboard</Link>
</li>
<li>
<Link to = {this.props.match.url+'/addContact'} className = "mr-
3">Add Contact</Link>
</li>
<li>
<Link to = {this.props.match.url+'/showAll'} className = "mr-3">Show
My Contacts</Link>
</li>
<li>
<Link to = {this.props.match.url+'/updateProfile'} className = "mr-
3">Update</Link>
</li>
<li>
<Link to = "/login" className = "mr-3">Logout</Link>
</li>
</ul>
</nav>
<Route exact path = {this.props.match.path} component = {() =>
<DashBoardComponent profile = {this.state.profile}/>}></Route>
<Route path = {this.props.match.path+"/dashboard"} component = {() =>
<DashBoardComponent profile = {this.state.profile}/>}></Route>
<Route path = {this.props.match.path+"/addContact"} component = {() =>
<AddContactComponent profile = {this.state.profile}/>}></Route>
<Route path = {this.props.match.path+"/showAll"} component = {() =>
<ShowAllContacts profile = {this.state.profile}/>}></Route>
<Route path = {this.props.match.path+'/updateProfile'} component = {() =>
<UpdateProfile profile = {this.state.profile}/>}></Route>
<Route exact path = "/login" component = {LoginComponent}></Route>
</div>
</div>
</div>
)
}
}
class RegisterComponent extends Component {
constructor(props) {
super(props);
this.state = {name : "", dob : "", password : "", phone : ""};
}
handleChange = (e) => {
let inputName = e.target.name;
let inputValue = e.target.value;
this.setState({[inputName] : inputValue});
}
handleSubmit = (e) => {
e.preventDefault();
let registerURL = "http://localhost:8080/my-contact-app/api/createProfile";
axios.post(registerURL, this.state)
.then(response => {
if(response.status) {
//this.props.history.push(`login/${response.data.userId}`);
alert(`Your profile is registered with an User Id: ${response.data.userId}`)
this.props.history.push(`login`);
}
});
}
render() {
return(<div>
<h3>Register</h3>
<hr />
<form onSubmit = {this.handleSubmit}>
<input type = "text" name = "name" placeholder = "Enter Name" onChange =
{this.handleChange}></input>
<input type = "password" name = "password" placeholder = "Enter Password"
onChange = {this.handleChange}></input>
<br />
Birthday : <input type = "date" name = "dob" onChange = {this.handleChange}
/>
<br />
<input type = "text" name = "phone" placeholder = "Enter Mobile No" onChange
= {this.handleChange}/>
<br />
<input type = "submit" value = "Sign Up" />
</form>
<Link to = "login">Sign In</Link>
</div>);
}
}
class LoginComponent extends Component {
constructor(props) {
super(props);
this.state = {id : "", password : ""};
}
render() {
return(<div>
<h3>Login</h3> <hr />
<form onSubmit = {this.handleSubmit}>
<input type = "text" name = "id" onChange = {this.handleChange}
placeholder = "Enter User Id"></input>
<input type = "text" name = "password" onChange = {this.handleChange}
placeholder = "Enter Password"></input>
<input type = "submit" value = "Sign In"/>
</form>
<Link to = "register">Sign Up</Link>
</div>);
}
}
</div>
</div>);
}
}
export default App;
Output: