import React, {useState} from 'react' import router from 'next/router' import Link from 'next/link' import axios from 'axios' import {Button} from '@rmwc/button' import ActionBar from '~/components/admin/actionBar' 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 ( <> 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 ? : } )} ]} // Map in an id property so the table can use array.map rows={items.map(item => ({id: item.uuid, ...item}))} /> ) }