import type { NextPage } from 'next';
import React from 'react';
import { useSelector } from 'react-redux'
import { MainLayout } from '../layouts/Customer';
import { CardStock, SkeletonStock, BoxScroll, MottoBlock, CategoriesButton } from '../components';
import { useAppDispatch } from '../redux/store';
import { fetchStoke } from '../redux/stoke/stock';
import { selectStokeData } from '../redux/stoke/selectors';

const Home: NextPage = () => {
  const dispatch = useAppDispatch();

  React.useEffect(() => {
    dispatch(
      fetchStoke(),
    );
  }, [1]);

  const { items, status } = useSelector(selectStokeData);

  const stokes = items.map((obj, index) => <CardStock key={index} {...obj} />);
  const skeletonsStoke = [...new Array(4)].map((_, index) => <SkeletonStock key={index} />);

  const categories = items.map((obj, index) => <CategoriesButton key={index} {...obj}/> );

  return (
    <MainLayout>
      <BoxScroll>
        {status === 'loading' ? skeletonsStoke : stokes}
      </BoxScroll>
      <MottoBlock/>

    </MainLayout>
  )
}

export default Home
