import React, { useEffect, useRef, useState } from 'react' import { withAuthSync } from '../utils/auth' import Layout from '../components/layout' import { callAPI, useCallAPI } from '../utils/api' import Item from '../components/item' const List = ({ fetched }) => { const input = useRef(undefined) const [text, setText] = useState('') const [data, refresh] = useCallAPI({ url: `/api/items` }) useEffect(() => { if (!input.current) return input.current.focus() }, [input]) const _submit = async (e) => { e.preventDefault() await callAPI(null, { url: `/api/items/`, method: 'post', data: { text }, }) setText('') refresh() } const _clear = () => callAPI(null, { url: `/api/items/`, method: 'delete', }).then(refresh) const items = data || fetched return
setText(e.target.value)}/>

{items.map((item, i) => )}

} List.getInitialProps = async ctx => ({ fetched: await callAPI(ctx, { url: `/api/items` }), }) export default withAuthSync(List)