Skip to content

Commit

Permalink
Test that api.example.add works
Browse files Browse the repository at this point in the history
  • Loading branch information
DemianParkhomenko committed Nov 9, 2023
1 parent 8233872 commit b35e20d
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
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 added global.d.ts
Empty file.
93 changes: 91 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@
"prepare": "husky install"
},
"dependencies": {
"metacom": "^3.1.2",
"next": "13.5.3",
"react": "18.2.0",
"react-dom": "18.2.0"
},
"devDependencies": {
"@flydotio/dockerfile": "^0.4.10",
"@types/react": "^18.2.37",
"@types/react-dom": "^18.2.15",
"autoprefixer": "^10.4.16",
"eslint": "8.53.0",
"eslint-config-next": "14.0.1",
"husky": "^8.0.3",
"lint-staged": "^14.0.1",
"postcss": "^8.4.30",
"prettier": "3.0.3",
"tailwindcss": "^3.3.3"
"tailwindcss": "^3.3.3",
"typescript": "^5.2.2"
}
}
1 change: 1 addition & 0 deletions src/app/layout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ThemeProvider from '@/store/theme-context'
import './global.css'
import '@/utils/api'

export default function RootLayout({ children }) {
return (
Expand Down
8 changes: 7 additions & 1 deletion src/app/page.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use client'

import { useEffect, useState } from 'react'
import { useEffect, useState, useCallback } from 'react'
import Message from '@/components/Message'
import ThemeToggle from '@/components/ThemeToggle'
import { useApiRequest } from '@/utils'

const now = Date.now()
const MOCK_MESSAGES = [
Expand All @@ -17,6 +18,10 @@ const MOCK_MESSAGES = [

export default function Home() {
const [mounted, setMounted] = useState(false)
const { response } = useApiRequest(
useCallback(() => api.example.add({ a: 1, b: 2 }), []),
{ autofetch: true }
)

useEffect(() => {
setMounted(true)
Expand All @@ -34,6 +39,7 @@ export default function Home() {
</Message>
))}
<ThemeToggle />
{response || 'no response yet'}
</div>
)
}
63 changes: 63 additions & 0 deletions src/utils/api.js
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,
}
}
1 change: 1 addition & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './time'
export * from './api'

0 comments on commit b35e20d

Please sign in to comment.