import React, {useState} from 'react' import Link from 'next/link' import axios from 'axios' import Table from '~/components/table' Items.getInitialProps = async ({ctx}) => { const {data: items} = await ctx.axios.get('/api/items?showUnpublished=true') return {items} } export default function Items({items: _items}){ const [items, setItems] = useState(_items) const updateItem = newItem => setItems(items=>items.map(i => { if(i.uuid === newItem.uuid) return newItem return i })) const publish = item => async () => { const {data: newItem} = await axios.post(`/api/items/${item.uuid}/publish`) updateItem(newItem) } const unpublish = item => async () => { const {data: newItem} = await axios.post(`/api/items/${item.uuid}/unpublish`) updateItem(newItem) } return ( <>

Items

Create New Item item.name}, {name: 'URL', extractor: item => /store/item/{item.urlslug} }, {name: 'Price', extractor: item => `$${(item.price_cents / 100).toFixed(2)}`}, {name: 'Number in stock', extractor: item => item.number_in_stock}, {name: 'Actions', extractor: item => ( Edit { item.published ? : } )} ]} rows={items} /> ) }