File tree Expand file tree Collapse file tree 5 files changed +14
-13
lines changed Expand file tree Collapse file tree 5 files changed +14
-13
lines changed Original file line number Diff line number Diff line change 1
- ## React API Integration with example of Fetch Axios
1
+ ## React API Integration with Example using Axios
2
+ ### Integrate API from [ flask-pymongo-validator] ( https://github.com/mrxmamun/flask-pymongo-validator.git )
2
3
#### List of Todo API Routes
3
4
| Request | Endpoint | Details | Example Source |
4
5
| --- | --- | --- | --- |
5
- | ` GET ` | ` http://127.0.0.1:5000/todos ` | Get All| [ Todo List] ( src/components/todo/TodoList.js ) |
6
- | ` GET ` | ` http://127.0.0.1:5000/todos/todo_id ` | Get Single Id| [ Todo] ( src/components/todo/Todo.js ) |
7
- | ` POST ` | ` http://127.0.0.1:5000/todos ` | Insert One| [ Add Todo] ( src/components/todo/AddTodo.js ) |
8
- | ` PUT ` | ` http://127.0.0.1:5000/todos/todo_id ` | Update One| [ Update Todo] ( src/components/todo/Edit.js ) |
9
- | ` DELETE ` | ` http://127.0.0.1:5000/todos/todo_id ` | Delete One| [ Delete Todo] ( src/components/todo/Todo.js ) |
6
+ | ` GET ` | ` http://127.0.0.1:5000/api/v1/ todos ` | Get All| [ Todo List] ( src/components/todo/TodoList.js ) |
7
+ | ` GET ` | ` http://127.0.0.1:5000/api/v1/ todos/todo_id ` | Get Single Id| [ Todo] ( src/components/todo/Todo.js ) |
8
+ | ` POST ` | ` http://127.0.0.1:5000/api/v1/ todos ` | Insert One| [ Add Todo] ( src/components/todo/AddTodo.js ) |
9
+ | ` PUT ` | ` http://127.0.0.1:5000/api/v1/ todos/todo_id ` | Update One| [ Update Todo] ( src/components/todo/Edit.js ) |
10
+ | ` DELETE ` | ` http://127.0.0.1:5000/api/v1/ todos/todo_id ` | Delete One| [ Delete Todo] ( src/components/todo/Todo.js ) |
10
11
11
12
### In [ Api] ( src/api.js ) File Define all routes
12
13
``` js
Original file line number Diff line number Diff line change @@ -6,23 +6,22 @@ import { faEdit, faTrash } from '@fortawesome/free-solid-svg-icons'
6
6
import Navbar from './components/Navbar' ;
7
7
import TodoList from "./components/todo/TodoList" ;
8
8
import Todo from './components/todo/Todo' ;
9
+ import AddTodo from './components/todo/AddTodo' ;
9
10
import TodoEdit from './components/todo/Edit' ;
10
11
11
12
library . add ( faEdit , faTrash )
12
13
13
-
14
14
class App extends Component {
15
15
render ( ) {
16
16
return (
17
17
< React . Fragment >
18
18
< Navbar />
19
19
< Switch >
20
20
< Route path = "/" component = { TodoList } exact />
21
+ < Route path = "/todo/add" component = { AddTodo } exact />
21
22
< Route path = "/todo/:id" component = { Todo } exact />
22
23
< Route path = "/todo/edit/:id" component = { TodoEdit } exact />
23
- { /* <Route path="/add/todo" component={AddTodo} exact /> */ }
24
- { /*
25
- <Route path="/form" component={MyForm} exact /> */ }
24
+
26
25
</ Switch >
27
26
</ React . Fragment >
28
27
) ;
Original file line number Diff line number Diff line change @@ -23,7 +23,7 @@ class AddTodo extends Component {
23
23
const { title, body} = this . state ;
24
24
// define state variable for use in return
25
25
//and store in state value
26
- api . todos ( ) . create ( { title : title , body : body } )
26
+ api . todos ( ) . create ( { title : title , body : body } )
27
27
. then ( response => response . data )
28
28
. then ( this . setState ( { //clear inputs after submit
29
29
title : "" ,
Original file line number Diff line number Diff line change @@ -18,7 +18,6 @@ class Todo extends Component {
18
18
title : response . data . title ,
19
19
body : response . data . body
20
20
} ) ) ;
21
-
22
21
}
23
22
onChance = ( e ) => {
24
23
this . setState ( { [ e . target . name ] : e . target . value } ) ;
@@ -30,6 +29,8 @@ class Todo extends Component {
30
29
const { title, body } = this . state ; // define state variable for use in return and store in state value
31
30
api . todos ( ) . update ( todoId , { title : title , body : body } )
32
31
. then ( response => response . data ) ;
32
+
33
+ this . props . history . push ( '/' ) ;
33
34
}
34
35
35
36
Original file line number Diff line number Diff line change @@ -17,10 +17,10 @@ class Todo extends Component {
17
17
api . todos ( ) . getOne ( todoId )
18
18
. then ( response => this . setState ( { todo : response . data } ) ) ;
19
19
}
20
-
21
20
onDelete = ( ) => {
22
21
const todoId = this . props . match . params ;
23
22
api . todos ( ) . delete ( todoId ) ;
23
+ this . props . history . push ( '/' ) ;
24
24
}
25
25
26
26
render ( ) {
You can’t perform that action at this time.
0 commit comments