Skip to main content

11.27 Working With Guards

Blog.js

Blog.js
// import axios from "axios";
import React, { Component } from "react";
import { NavLink, Redirect, Route, Switch } from "react-router-dom";
import "./Blog.css";
import NewPost from "./NewPost/NewPost";
import Posts from "./Posts/Posts";

class Blog extends Component {
state = {
auth: false,
};
render() {
return (
<div className="Blog">
<header>
<nav>
<ul>
<li>
<NavLink
to="/posts/"
exact
activeClassName="my-active"
activeStyle={{
color: "#fa923f",
textDecoration: "underline",
}}
>
Posts
</NavLink>
</li>
<li>
<NavLink
to={{
pathname: "/new-post",
hash: "#submit",
search: "?quick-submit=true",
}}
>
New Post
</NavLink>
</li>
</ul>
</nav>
</header>
{/* <Route path="/" exact render={() => <h1>Home</h1>} />
<Route path="/" render={() => <h1>Home2</h1>} /> */}

<Switch>
{this.state.auth ? (
<Route path="/new-post" component={NewPost} />
) : null}

<Route path="/posts" component={Posts} />
<Redirect from="/" to="/posts" />
{/* <Route path="/" component={Posts} /> */}
</Switch>
</div>
);
}
}

export default Blog;

NewPost.js

NewPost.js
import axios from "axios";
import React, { Component } from "react";
import { Redirect } from "react-router-dom";
import "./NewPost.css";

class NewPost extends Component {
state = {
title: "",
content: "",
author: "Pash",
submitted: false,
};
componentDidMount() {
// if unauth => this.props.history.replace("/posts");
console.log(this.props);
}
postDataHandler = () => {
const post = {
title: this.state.title,
body: this.state.content,
author: this.state.author,
};
axios.post("/posts", post).then((response) => {
console.log(response);
this.props.history.replace("/posts");
// this.setState({ submitted: true });
});
};

render() {
let redirect = null;
if (this.state.submitted) {
redirect = <Redirect to="/posts" />;
}
return (
<div className="NewPost">
{redirect}
<h1>Add a Post</h1>
<label>Title</label>
<input
type="text"
value={this.state.title}
onChange={(event) => this.setState({ title: event.target.value })}
/>
<label>Content</label>
<textarea
rows="4"
value={this.state.content}
onChange={(event) => this.setState({ content: event.target.value })}
/>
<label>Author</label>
<select
value={this.state.author}
onChange={(event) => this.setState({ author: event.target.value })}
>
<option value="Pash">Pash</option>
<option value="Mash">Mash</option>
<option value="Chel">Chel</option>
</select>
<button onClick={this.postDataHandler}>Add Post</button>
</div>
);
}
}

export default NewPost;