import StoriesPreview from './StoriesPreview'
import { UserCategoriesNames, UserCategory, composeUserCategoriesFilters } from '../assets/userCategories'
import { useAnnouncements } from '../hooks/api'
import { gotError, gotResponse } from '../hooks/useFetch'

type CategoryPreviewProps = {
    category: UserCategory,
}

function CategoryPreview({ category }: CategoryPreviewProps) {
    const announcements = useAnnouncements(composeUserCategoriesFilters[category]())

    return (
        <section>
            <h4 className='fw-bold'>{UserCategoriesNames[category]}</h4>
            {gotError(announcements) ? (
                <p className='text-danger'>{announcements.error}</p>
            ) : (
                gotResponse(announcements) ? (
                    <StoriesPreview announcements={announcements.data} category={category} />
                ) : (
                    'Загрузка...'
                )
            )}
        </section>
    )
}

export default CategoryPreview
