79 lines
2.1 KiB
TypeScript
79 lines
2.1 KiB
TypeScript
import { useState, useEffect } from 'react'
|
|
|
|
export function useMaterialCategories() {
|
|
const [categories, setCategories] = useState<Array<{ id: string; name: string; count: number }>>([])
|
|
const [scenes, setScenes] = useState<Array<{ id: string; name: string; count: number }>>([])
|
|
const [platforms, setPlatforms] = useState<Array<{ id: string; name: string; count: number }>>([])
|
|
const [productCategories, setProductCategories] = useState<Array<{ id: string; name: string; count: number }>>([])
|
|
|
|
const fetchCategories = async () => {
|
|
try {
|
|
const response = await fetch('/api/categories')
|
|
if (response.ok) {
|
|
const data = await response.json()
|
|
setCategories(data)
|
|
}
|
|
} catch (error) {
|
|
console.error('Error fetching categories:', error)
|
|
}
|
|
}
|
|
|
|
const fetchScenes = async (categoryName?: string) => {
|
|
try {
|
|
const url = categoryName
|
|
? `/api/scenes?category=${encodeURIComponent(categoryName)}`
|
|
: '/api/scenes'
|
|
const response = await fetch(url)
|
|
if (response.ok) {
|
|
const data = await response.json()
|
|
setScenes(data)
|
|
}
|
|
} catch (error) {
|
|
console.error('Error fetching scenes:', error)
|
|
}
|
|
}
|
|
|
|
const fetchPlatforms = async () => {
|
|
try {
|
|
const response = await fetch('/api/platforms')
|
|
if (response.ok) {
|
|
const data = await response.json()
|
|
setPlatforms(data)
|
|
}
|
|
} catch (error) {
|
|
console.error('Error fetching platforms:', error)
|
|
}
|
|
}
|
|
|
|
const fetchProductCategories = async () => {
|
|
try {
|
|
const response = await fetch('/api/product-categories')
|
|
if (response.ok) {
|
|
const data = await response.json()
|
|
setProductCategories(data)
|
|
}
|
|
} catch (error) {
|
|
console.error('Error fetching product categories:', error)
|
|
}
|
|
}
|
|
|
|
useEffect(() => {
|
|
fetchCategories()
|
|
fetchScenes()
|
|
fetchPlatforms()
|
|
fetchProductCategories()
|
|
}, [])
|
|
|
|
return {
|
|
categories,
|
|
scenes,
|
|
platforms,
|
|
productCategories,
|
|
fetchCategories,
|
|
fetchScenes,
|
|
fetchPlatforms,
|
|
fetchProductCategories,
|
|
}
|
|
}
|
|
|