Skip to content

Commit 25811ac

Browse files
committed
New examples
1 parent 8196a39 commit 25811ac

File tree

3 files changed

+158
-27
lines changed

3 files changed

+158
-27
lines changed

README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,39 @@ Object containing:
108108
- `error: object`: Request error.
109109
- `dispatchFetch: function`: Dispatched request if `controlledFetch` property is `true`.
110110

111+
#### Example
112+
113+
```js
114+
import { useAxiosPost } from "react-hooks-toolbox";
115+
116+
const AddPost = () => {
117+
const [title, setTitle] = useState("");
118+
const [author, setAuthor] = useState("");
119+
120+
const { status, response, dispatchFetch } = useAxiosPost({
121+
url: "http://localhost:3001/posts",
122+
controlledFetch: true,
123+
options: {
124+
data: {
125+
title: title,
126+
author: author
127+
}
128+
},
129+
successCb: response => {
130+
console.log(response);
131+
}
132+
});
133+
134+
return (
135+
<div>
136+
<input value={title} onChange={e => setTitle(e.target.value)} />
137+
<input value={author} onChange={e => setAuthor(e.target.value)} />
138+
<button onClick={() => dispatchFetch()}>Add Post</button>
139+
</div>
140+
);
141+
};
142+
```
143+
111144
## Google-API
112145

113146
### `useGoogleApiInit()`
@@ -197,7 +230,8 @@ const DymoLabelPreview = () => {
197230
}
198231
};
199232
```
233+
200234
[npm-image]: https://img.shields.io/npm/v/react-hooks-toolbox.svg?style=flat-square
201235
[npm-url]: https://npmjs.org/package/react-hooks-toolbox
202236
[download-image]: https://img.shields.io/npm/dm/react-hooks-toolbox.svg?style=flat-square
203-
[download-url]: https://npmjs.org/package/react-hooks-toolbox
237+
[download-url]: https://npmjs.org/package/react-hooks-toolbox

db.json

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,34 @@
11
{
22
"posts": [
3-
{ "id": 1, "title": "json-server", "author": "typicode" },
4-
{ "id": 2, "title": "client", "author": "client" },
5-
{ "id": 3, "title": "hybrid", "author": "hybrid" }
3+
{
4+
"id": 1,
5+
"title": "Harry Potter and the Sorcerer's Stone",
6+
"author": "J. K. Rowling"
7+
},
8+
{
9+
"id": 2,
10+
"title": "The Hobbit",
11+
"author": "J. R. R. Tolkien"
12+
},
13+
{
14+
"id": 3,
15+
"title": "The Fellowship of the Ring",
16+
"author": "J. R. R. Tolkien"
17+
},
18+
{
19+
"title": "Catching Fire",
20+
"author": "Suzanne Collins",
21+
"id": 4
22+
}
623
],
7-
"comments": [{ "id": 1, "body": "some comment", "postId": 1 }],
8-
"profile": { "name": "typicode" }
9-
}
24+
"comments": [
25+
{
26+
"id": 1,
27+
"body": "some comment",
28+
"postId": 1
29+
}
30+
],
31+
"profile": {
32+
"name": "typicode"
33+
}
34+
}
Lines changed: 92 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,111 @@
1-
import React from "react";
1+
import React, {useState, useReducer, useContext, createContext} from "react";
2+
import produce from "immer";
23

3-
import {useAxiosGet} from "../../../src";
4+
import {useAxiosGet, useAxiosPost, useAxios} from "../../../src";
45

56

7+
const Context = createContext();
8+
69
function ListPosts() {
7-
const {status, response} = useAxiosGet({
8-
url: "http://localhost:3001/posts"
10+
const {state, dispatch} = useContext(Context);
11+
const [postId, setPostId] = useState(null);
12+
const {status} = useAxiosGet({
13+
url: "http://localhost:3001/posts",
14+
successCb: response => {
15+
dispatch({type: "FETCHED_POSTS", payload: response});
16+
}
17+
});
18+
const {dispatchFetch} = useAxios({
19+
url: `http://localhost:3001/posts/${postId}`,
20+
method: "DELETE",
21+
controlledFetch: true
922
});
1023
if (status === "loading") {
1124
return <h5>Loading posts...</h5>;
1225
}
1326
else if (status === "error") {
14-
return <h5>Error in loading posts.</h5>;
27+
return <h5 style={{color: "red"}}>Error in loading posts</h5>;
1528
}
16-
else if (status === "success") {
17-
return (
18-
<ul>
19-
{" "}
20-
{response.map(item => (
21-
<li key={item.id}>
22-
{item.title}, {item.author}
23-
</li>
24-
))}{" "}
25-
</ul>
26-
);
29+
30+
function handleDeletePost(post) {
31+
setPostId(post.id);
32+
dispatchFetch();
33+
dispatch({type: "DELETED_POST", id: post.id});
2734
}
28-
else {
29-
return null;
35+
36+
return (
37+
<ul>
38+
{state.posts.map(post => (
39+
<li key={post.id}>
40+
{post.title} by <strong>{post.author}</strong>{" "}
41+
<a href="javascript:void(0)" onClick={() => handleDeletePost(post)}>
42+
Delete Post
43+
</a>
44+
</li>
45+
))}
46+
</ul>
47+
);
48+
}
49+
50+
function AddPost() {
51+
const {dispatch} = useContext(Context);
52+
const [title, setTitle] = useState("");
53+
const [author, setAuthor] = useState("");
54+
const {dispatchFetch, status} = useAxiosPost({
55+
url: "http://localhost:3001/posts",
56+
controlledFetch: true,
57+
options: {
58+
data: {
59+
title: title,
60+
author: author
61+
}
62+
},
63+
successCb: response => {
64+
dispatch({type: "ADD_POST", payload: response});
65+
}
66+
});
67+
68+
const loading = status === "loading";
69+
return (
70+
<div>
71+
<input value={title} onChange={e => setTitle(e.target.value)} disabled={loading}/>
72+
<input value={author} onChange={e => setAuthor(e.target.value)} disabled={loading}/>
73+
<button onClick={() => dispatchFetch()} disabled={loading}>
74+
{loading ? "Loading..." : "Add Post"}
75+
</button>
76+
</div>
77+
);
78+
}
79+
80+
const initState = {posts: []};
81+
82+
function reducer(state, action) {
83+
switch (action.type) {
84+
case "ADD_POST":
85+
return produce(state, draftState => {
86+
draftState.posts.push(action.payload);
87+
});
88+
case "FETCHED_POSTS":
89+
return produce(state, draftState => {
90+
draftState.posts = action.payload;
91+
});
92+
case "DELETED_POST":
93+
return produce(state, draftState => {
94+
const {posts} = state,
95+
index = posts.findIndex(post => post["id"] === action.id);
96+
draftState.posts.splice(index, 1);
97+
});
98+
default:
99+
return state;
30100
}
31101
}
32102

33103
export default function AxiosAPIExample(props) {
104+
const [state, dispatch] = useReducer(reducer, initState);
34105
return (
35-
<div>
106+
<Context.Provider value={{state, dispatch}}>
36107
<ListPosts/>
37-
</div>
108+
<AddPost/>
109+
</Context.Provider>
38110
);
39111
}

0 commit comments

Comments
 (0)