import { useState, useRef } from 'react'; import { useNavigate } from 'react-router-dom'; import { useSelector } from 'react-redux'; import { useGetSitePromoListQuery } from '/src/api/apiSitePromo'; import { selectRegions } from '../store/slice/regionSlice'; import { useOutsideClick } from '../hooks/useOutsideClick'; import { LoadingComponent } from '../components/Placeholders/LoadingComponent'; import { ErrorComponent } from '../components/Placeholders/ErrorComponent'; export const SitePromoListPage = () => { const [ searchValue, setSearchValue ] = useState( '' ); const [ currentPage, setCurrentPage ] = useState( 1 ); const [ expandedId, setExpandedId ] = useState( '' ); const navigate = useNavigate(); const regions = useSelector(selectRegions); const tableRef = useRef( null ); useOutsideClick( tableRef, () => setExpandedId( null )); const { data: response = {}, isFetching, error: queryError } = useGetSitePromoListQuery( { search: searchValue, page: currentPage } ); const pagination = response.pagination || {}; const items = response.data ? response.data : []; const renderPagination = () => { const total = pagination.total_pages || 1; const current = pagination.current_page || 1; const pages = new Set( [ 1, total ] ); for ( let page = current - 2; page <= current + 2; page += 1 ) { if ( page > 1 && page < total ) pages.add( page ); } const sorted = Array.from( pages ).sort( ( a, b ) => a - b ); const elements = []; let last = 0; sorted.forEach( page => { if ( last && page - last > 1 ) { elements.push(
  • ); } elements.push(
  • ); last = page; }); return ( ); }; return (

    Промо (контент)

    { e.stopPropagation(); navigate(`/site-promo/create`); } } />
    { setSearchValue( e.target.value ); setCurrentPage( 1 ); } } />
    { isFetching ? : queryError ? : ( <>
    {items.map( item => ( <> setExpandedId( expandedId === item.id ? null : item.id ) }> { expandedId === item.id && ( )} ))}
    ID Название Alias Активно Регион
    { item.id } { item.name } { item.alias } { item.active ? 'Да' : 'Нет' } { regions[item.regionId] ?? item.regionId }
    { e.stopPropagation(); navigate(`/site-promo/edit/${item.id}`) } } />
    { renderPagination() } )}
    ); };