Skip to content

Commit a58086b

Browse files
committed
Handle the response and the data
1 parent 5bb2bb6 commit a58086b

File tree

7 files changed

+19
-12
lines changed

7 files changed

+19
-12
lines changed

src/__tests__/use-post.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ test('should make a POST', async () => {
2525

2626
await waitForNextUpdate()
2727

28-
expect(result.current.data).toBe('12345')
28+
expect(result.current.data).toBe(12345)
2929
})
3030

3131
test('should make handle error', async () => {

src/__tests__/use-request.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ test('should make a request', async () => {
2222

2323
await waitForNextUpdate()
2424

25-
expect(result.current.data).toBe('12345')
25+
expect(result.current.data).toBe(12345)
2626
})
2727

2828
test('should make handle error', async () => {

src/reducer.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
export const getData = obj => {
2-
if (obj && obj.data && obj.data.data) return obj.data.data
3-
if (obj && obj.data) return obj.data
4-
if (obj.data === null) return null
5-
return obj
2+
const data = obj && typeof obj.data === 'string' ? JSON.parse(obj.data) : obj
3+
4+
if (data && data.data) {
5+
return data.data
6+
} else if (Array.isArray(data)) {
7+
return data
8+
}
9+
return data
610
}
711
export const getError = obj => {
812
if (obj && obj.error && obj.error.Error) return obj.error.Error
@@ -21,15 +25,15 @@ export const reducer = (state, action) => {
2125
case 'success':
2226
return {
2327
...state,
24-
data: action.payload.data,
28+
data: getData(action.payload),
2529
error: null,
2630
loading: false,
2731
complete: true,
2832
}
2933
case 'error':
3034
return {
3135
...state,
32-
data: getData(action.payload),
36+
data: getData(action.payload.data),
3337
error: true,
3438
errorDetails: getError(action.payload),
3539
loading: false,

src/use-graphql.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export const useQuery = (
8787
method: 'POST',
8888
}
8989
) => {
90-
const { data, error, loading, makeRequest } = useRequest(url, {
90+
const { makeRequest, ...props } = useRequest(url, {
9191
data: null,
9292
error: null,
9393
loading: false,
@@ -106,5 +106,5 @@ export const useQuery = (
106106
})
107107
}
108108

109-
return { data, error, loading, makeQuery }
109+
return { makeQuery, ...props }
110110
}

src/use-request.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ const useRequest = (url = '', config = { method: 'GET' }) => {
6969
payload: result,
7070
})
7171
}
72+
7273
return dispatch({ type: 'success', payload: { data: result } })
7374
}
7475
const result = await resp.text()

working/hooks/graph.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export default () => {
2525
useEffect(() => {
2626
makeQuery()
2727
}, [])
28+
console.log('data', data)
2829
if (loading) {
2930
return (
3031
<div className="App">

working/hooks/grapher.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ export default () => {
2323
}`,
2424
{ id: 1 }
2525
)
26-
26+
console.log('data', data)
2727
if (data.length === 0 && !loading) {
2828
makeQuery({ id: 1 })
2929
}
30+
3031
if (loading) {
3132
return (
3233
<div className="App">
@@ -48,7 +49,7 @@ export default () => {
4849
}
4950
return (
5051
<div className="App content">
51-
<h2>{data.person[0].name} - er</h2>
52+
<h2>{data.person[0].name}</h2>
5253
<h3>Pick a number</h3>
5354
<div className="field is-grouped">
5455
<p className="control">

0 commit comments

Comments
 (0)