-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8233872
commit b35e20d
Showing
8 changed files
with
169 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"extends": ["eslint:recommended", "next/core-web-vitals"] | ||
"extends": ["eslint:recommended", "next"] | ||
} |
Empty file.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
'use client' | ||
|
||
import { useState, useCallback, useEffect } from 'react' | ||
import { Metacom } from 'metacom' | ||
|
||
const metacom = Metacom.create('ws://localhost:8000') | ||
|
||
;(async () => { | ||
const { api } = metacom | ||
await metacom.load('example') | ||
window.api = api | ||
console.log('api', api) | ||
})() | ||
|
||
// TODO: enhance this function | ||
export const useApiRequest = ( | ||
fetcher, | ||
{ onError, autofetch = false, onSuccess, onBeforeFetch, onResponse } | ||
) => { | ||
const [isLoading, setLoading] = useState(false) | ||
const [response, setResponse] = useState(null) | ||
|
||
const refetch = useCallback( | ||
async (args) => { | ||
setLoading(true) | ||
await onBeforeFetch?.() | ||
const res = await fetcher(args) | ||
if (!res) { | ||
setLoading(false) | ||
return | ||
} | ||
await onResponse?.(res) | ||
if (!res) { | ||
if (onError) { | ||
await onError?.(res, args) | ||
} else { | ||
console.error('Api request error') | ||
} | ||
} else { | ||
setResponse(res) | ||
try { | ||
await onSuccess?.(res, args) | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
} | ||
setLoading(false) | ||
}, | ||
[onBeforeFetch, fetcher, onResponse, onError, setResponse, onSuccess] | ||
) | ||
|
||
useEffect(() => { | ||
if (autofetch) { | ||
refetch({}) | ||
} | ||
}, [refetch, fetcher, autofetch]) | ||
|
||
return { | ||
isLoading, | ||
response, | ||
fetch, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './time' | ||
export * from './api' |