-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Users can add a post to a post list of selected category. When users navigate to different category, category field is auto-updated based on path States of all input fields are component state. `PostForm` receives: - `categories` props to display category selection list - `category` props to set initial state for `category`. This state is updated every time there is a modified props Generating new post id with `UUID` package
- Loading branch information
1 parent
55362a4
commit 0230af7
Showing
6 changed files
with
98 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Link } from 'react-router-dom'; | ||
const uuidv4 = require('uuid/v4'); | ||
|
||
class PostForm extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = ({ | ||
openForm: false, | ||
title: '', | ||
body: '', | ||
author: '', | ||
category: this.props.currentCategory | ||
}) | ||
this.handleForm = this.handleForm.bind(this) | ||
this.handleSubmit = this.handleSubmit.bind(this) | ||
} | ||
handleForm() { | ||
this.setState({openForm: !this.state.openForm}) | ||
} | ||
handleSubmit(e) { | ||
e.preventDefault(); | ||
const newPost = { | ||
id: uuidv4(), | ||
title: this.state.title, | ||
category: this.state.category, | ||
body: this.state.body, | ||
author: this.state.author, | ||
timestamp: Date.now(), | ||
} | ||
console.log(newPost) | ||
} | ||
componentWillReceiveProps(nextProps) { | ||
if (this.props.category !== nextProps.category) { | ||
this.setState({ | ||
openForm: false, | ||
title: '', | ||
body: '', | ||
author: '', | ||
category: nextProps.category | ||
}) | ||
} | ||
} | ||
render() { | ||
const { categories } = this.props.categories | ||
const { title, author, body, category } = this.state | ||
return ( | ||
<div> | ||
<button onClick={this.handleForm}>New Post</button> | ||
{this.state.openForm && | ||
<form onSubmit={this.handleSubmit}> | ||
<input type="text" name="title" placeholder="New Title" value={title} onChange={(e) => this.setState({ title: e.target.value })} required/> | ||
<label> | ||
Current category: | ||
<select value={this.state.category} onChange={(e) => this.setState({ category: e.target.value })}> | ||
<option value=''>Select a category</option> | ||
{categories && categories.map((category) => | ||
<option key={category.name} value={`${category.name}`}>{category.name}</option> | ||
)} | ||
</select> | ||
</label> | ||
<input type="text" name="body" placeholder="New Content" value={body} onChange={(e) => this.setState({ body: e.target.value })} required/> | ||
<input type="text" name="author" placeholder="Your Name" value={author} onChange={(e) => this.setState({ author: e.target.value })} required/> | ||
<input type='submit' value='Submit'/> | ||
</form> | ||
} | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
PostForm.PropTypes = { | ||
categories: PropTypes.array.isRequired | ||
} | ||
|
||
export default PostForm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters