Skip to main content

9.15 Setting Default Global Configuration For Axios

index.js

index.js
import axios from "axios";
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import "./index.css";
import registerServiceWorker from "./registerServiceWorker";

axios.defaults.baseURL = "https://jsonplaceholder.typicode.com";
axios.defaults.headers["Authorization"] = "AUTH TOKEN";
axios.defaults.headers.post["Content-Type"] = "application/json";

axios.interceptors.request.use(
(request) => {
console.log(request);
// Edit request config
return request;
},
(error) => {
console.log(error);
return Promise.reject(error);
}
);

axios.interceptors.response.use(
(response) => {
console.log(response);
// Edit response config
return response;
},
(error) => {
console.log(error);
return Promise.reject(error);
}
);

ReactDOM.render(<App />, document.getElementById("root"));
registerServiceWorker();

FullPost.js

FullPost.js
import axios from "axios";
import React, { Component } from "react";
import "./FullPost.css";

class FullPost extends Component {
state = {
loadedPost: null,
};
componentDidUpdate() {
if (this.props.id) {
if (
!this.state.loadedPost ||
(this.state.loadedPost && this.state.loadedPost.id !== this.props.id)
) {
axios.get("/posts/" + this.props.id).then((response) => {
// console.log(response);
this.setState({ loadedPost: response.data });
});
}
}
}

deletePostHandler = () => {
axios.delete("/posts/" + this.props.id).then((response) => {
// console.log(response);
});
};
render() {
let post = <p style={{ textAlign: "center" }}>Please select a Post!</p>;

if (this.props.id) {
<p style={{ textAlign: "center" }}>Loading...!</p>;
}
if (this.state.loadedPost) {
post = (
<div className="FullPost">
<h1>{this.state.loadedPost.title}</h1>
<p>{this.state.loadedPost.body}</p>
<div className="Edit">
<button className="Delete" onClick={this.deletePostHandler}>
Delete
</button>
</div>
</div>
);
}

return post;
}
}

export default FullPost;

NewPost.js

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

class NewPost extends Component {
state = {
title: "",
content: "",
author: "Pash",
};

postDataHandler = () => {
const post = {
title: this.state.title,
body: this.state.content,
author: this.state.author,
};
axios.post("/posts", post).then((response) => {
console.log(response);
});
};

render() {
return (
<div className="NewPost">
<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;

Blog.js

Blog.js
import axios from "axios";
import React, { Component } from "react";
import FullPost from "../../components/FullPost/FullPost";
import NewPost from "../../components/NewPost/NewPost";
import Post from "../../components/Post/Post";
import "./Blog.css";

class Blog extends Component {
state = {
posts: [],
selectedPostId: null,
error: false,
};
componentDidMount() {
axios
.get("/posts")
.then((response) => {
const posts = response.data.slice(0, 4);
const updatedPosts = posts.map((post) => {
return {
...post,
author: "Pash",
};
});
this.setState({ posts: updatedPosts });
// console.log(response);
})
.catch((error) => {
// console.log(error);
this.setState({ error: true });
});
}

postSelectedHandler = (id) => {
this.setState({ selectedPostId: id });
};
render() {
let posts = <p style={{ textAlign: "center" }}>Something went wrong!</p>;
if (!this.state.error) {
posts = this.state.posts.map((post) => {
return (
<Post
key={post.id}
title={post.title}
author={post.author}
clicked={() => this.postSelectedHandler(post.id)}
/>
);
});
}
return (
<div>
<section className="Posts">{posts}</section>
<section>
<FullPost id={this.state.selectedPostId} />
</section>
<section>
<NewPost />
</section>
</div>
);
}
}

export default Blog;