Skip to content

Commit 87b79a8

Browse files
committed
Searching for Hello
1 parent 7f07c99 commit 87b79a8

File tree

7 files changed

+493
-3
lines changed

7 files changed

+493
-3
lines changed

djsr/frontend/src/axiosApi.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// djsr/frontend/src/axiosApi.js
2+
3+
import axios from 'axios'
4+
5+
const axiosInstance = axios.create({
6+
baseURL: 'http://127.0.0.1:8000/api/',
7+
timeout: 5000,
8+
headers: {
9+
'Authorization': "JWT " + localStorage.getItem('access_token'),
10+
'Content-Type': 'application/json',
11+
'accept': 'application/json'
12+
}
13+
});
14+
15+
// axiosInstance.interceptors.response.use(
16+
// response => response,
17+
// error => {
18+
// const originalRequest = error.config;
19+
//
20+
// if (error.response.status === 401) {
21+
// const refresh_token = localStorage.getItem('refresh_token');
22+
//
23+
// return axiosInstance
24+
// .post('/token/refresh/', {refresh: refresh_token})
25+
// .then((response) => {
26+
//
27+
// localStorage.setItem('access_token', response.data.access);
28+
// localStorage.setItem('refresh_token', response.data.refresh);
29+
//
30+
// axiosInstance.defaults.headers['Authorization'] = "JWT " + response.data.access;
31+
// originalRequest.headers['Authorization'] = "JWT " + response.data.access;
32+
//
33+
// return axios(originalRequest);
34+
// })
35+
// .catch(err => {
36+
// console.log(err)
37+
// });
38+
// }
39+
// return Promise.reject(error);
40+
// }
41+
// );
42+
43+
export default axiosInstance

djsr/frontend/src/components/App.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { Component} from "react";
22
import { Switch, Route, Link } from "react-router-dom";
33
import Login from "./login";
44
import Signup from "./signup";
5+
import Hello from "./hello";
56

67
class App extends Component {
78
render() {
@@ -11,13 +12,15 @@ class App extends Component {
1112
<Link className={"nav-link"} to={"/"}>Home</Link>
1213
<Link className={"nav-link"} to={"/login/"}>Login</Link>
1314
<Link className={"nav-link"} to={"/signup/"}>Signup</Link>
15+
<Link className={"nav-link"} to={"/hello/"}>Hello</Link>
1416
</nav>
1517
<main>
1618
<h1>Ahhh after 10,000 years I'm free. Time to conquer the Earth!</h1>
1719

1820
<Switch>
1921
<Route exact path={"/login/"} component={Login}/>
2022
<Route exact path={"/signup/"} component={Signup}/>
23+
<Route exact path={"/hello/"} component={Hello}/>
2124
<Route path={"/"} render={() => <div>Home again</div>}/>
2225
</Switch>
2326
</main>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import React, { Component } from "react";
2+
import axiosInstance from "../axiosApi";
3+
4+
class Hello extends Component {
5+
constructor(props) {
6+
super(props);
7+
this.state = {
8+
message:"",
9+
};
10+
11+
this.getMessage = this.getMessage.bind(this)
12+
}
13+
14+
getMessage(){
15+
try {
16+
let response = axiosInstance.get('/hello/');
17+
const message = response.data.hello;
18+
this.setState({
19+
message: message,
20+
});
21+
return message;
22+
}catch(error){
23+
console.log("Error: ", JSON.stringify(error, null, 4));
24+
throw error;
25+
}
26+
}
27+
28+
componentDidMount(){
29+
// It's not the most straightforward thing to run an async method in componentDidMount
30+
31+
// Version 1 - no async: Console.log will output something undefined.
32+
const messageData1 = this.getMessage();
33+
console.log("messageData1: ", JSON.stringify(messageData1, null, 4));
34+
35+
36+
}
37+
38+
render(){
39+
return (
40+
<div>
41+
<p>{this.state.message}</p>
42+
</div>
43+
)
44+
}
45+
}
46+
47+
export default Hello;

djsr/frontend/src/components/login.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { Component } from "react";
2+
import axiosInstance from "../axiosApi";
23

34
class Login extends Component {
45
constructor(props) {
@@ -14,8 +15,19 @@ class Login extends Component {
1415
}
1516

1617
handleSubmit(event) {
17-
alert('A username and password was submitted: ' + this.state.username + " " + this.state.password);
1818
event.preventDefault();
19+
try {
20+
const data = axiosInstance.post('/token/obtain/', {
21+
username: this.state.username,
22+
password: this.state.password
23+
});
24+
axiosInstance.defaults.headers['Authorization'] = "JWT " + data.access;
25+
localStorage.setItem('access_token', data.access);
26+
localStorage.setItem('refresh_token', data.refresh);
27+
return data;
28+
} catch (error) {
29+
throw error;
30+
}
1931
}
2032

2133
render() {

djsr/frontend/static/frontend/public/main.js

Lines changed: 347 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"webpack-cli": "^3.3.4"
1919
},
2020
"dependencies": {
21+
"axios": "^0.19.0",
2122
"react": "^16.8.6",
2223
"react-dom": "^16.8.6",
2324
"react-router-dom": "^5.0.1"

0 commit comments

Comments
 (0)