mirror of
https://github.com/cupcakearmy/obolus.git
synced 2024-12-22 16:16:27 +00:00
useCallAPI hook
This commit is contained in:
parent
3ef6382d54
commit
a053ec72f5
@ -1,3 +1,4 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import nextCookie from 'next-cookies'
|
||||
import axios from 'axios'
|
||||
import cookie from 'js-cookie'
|
||||
@ -26,4 +27,18 @@ export const callAPI = async (ctxOrNull, { url, method, data }) => {
|
||||
|
||||
if (response.status === 401) redirect()
|
||||
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]
|
||||
}
|
Loading…
Reference in New Issue
Block a user