useCallAPI hook

This commit is contained in:
cupcakearmy 2019-05-24 15:26:06 +02:00
parent 3ef6382d54
commit a053ec72f5

View File

@ -1,3 +1,4 @@
import { useEffect, useState } from 'react'
import nextCookie from 'next-cookies' import nextCookie from 'next-cookies'
import axios from 'axios' import axios from 'axios'
import cookie from 'js-cookie' import cookie from 'js-cookie'
@ -26,4 +27,18 @@ export const callAPI = async (ctxOrNull, { url, method, data }) => {
if (response.status === 401) redirect() if (response.status === 401) redirect()
else return response.data.body else return response.data.body
}
export const useCallAPI = (call) => {
const [data, setData] = useState(undefined)
const refresh = () => {
callAPI(null, call).then(setData)
}
useEffect(() => {
refresh()
}, [0])
return [data, refresh]
} }