import React, {useState} from 'react'
import Link from 'next/link'
import axios from 'axios'
import ActionBar from '~/components/admin/actionBar'
import Table from '~/components/table'
CategoryItems.getInitialProps = async ({ctx: {axios, query: {slug}}}) => {
const {data: category} = await axios.get(`/api/categories/by-slug/${slug}`)
const {data: allItems} = await axios.get(`/api/items`)
return {category, allItems}
}
export default function CategoryItems({category: _category, allItems}) {
const [category, updateCategory] = useState(_category)
const itemUUIDs = category.items.map(item => item.uuid)
const possibleItems = allItems.filter(item => !itemUUIDs.includes(item.uuid))
const removeItem = uuid => async ev => {
if(ev) ev.preventDefault()
const {data: updated} = await axios.delete(`/api/categories/${category.uuid}/items/${uuid}`)
updateCategory(updated)
}
const addItem = uuid => async ev => {
if(ev) ev.preventDefault()
const {data: updated} = await axios.put(`/api/categories/${category.uuid}/items/${uuid}`)
updateCategory(updated)
}
return (
<>