import React, { useEffect, useRef, useState } from 'react'
import { withAuthSync } from '../utils/auth'
import Layout from '../components/layout'
import { callAPI } from '../utils/api'
const Item = ({ id, text, done }) => {
const handleDone = async (e) => {
await callAPI(null, {
url: `/api/items/${id}`,
method: 'patch',
data: {
done: e.target.checked,
},
})
window.location.reload()
}
const handleDelete = async (e) => {
await callAPI(null, {
url: `/api/items/${id}`,
method: 'delete',
})
window.location.reload()
}
return
|
{text} |
|
{/* language=CSS */}
}
const List = ({ items }) => {
const input = useRef(undefined)
const [text, setText] = useState('')
useEffect(() => {
if (!input.current) return
input.current.focus()
}, [input])
const submit = async () => {
await callAPI(null, {
url: `/api/items/`,
method: 'post',
data: { text },
})
window.location.reload()
}
const deleteAll = async () => {
await callAPI(null, {
url: `/api/items/`,
method: 'delete',
})
window.location.reload()
}
return
{items.map((item, i) => )}
}
List.getInitialProps = async ctx => ({
items: await callAPI(ctx, { url: `/api/items` }),
})
export default withAuthSync(List)