import { Box } from '@mui/material';
import React, { useEffect, useState } from 'react'
import { useGetOwnerOrderQuery } from '../api/api';
import Loader from '../components/Loader';
import Title from '../components/Title';
import OrderCard from '../components/OrderCard';
import { useParams } from 'react-router-dom';

export default function UserPage() {
  const { categoryId } = useParams();
  const { data, error, isFetching} = useGetOwnerOrderQuery(categoryId);
  const [sortedData, setSortedData] = useState();
  useEffect(() => {
    if (data) {
      const dataForSort = [...data?.OrderFind];
      setSortedData(dataForSort?.sort((a, b) => b.createdAt - a.createdAt));
    }
  }, [data]);

  return (
    <>
      <Title>My orders</Title>
      {isFetching ? <Loader /> :
        <Box>
          {sortedData?.map(order => <OrderCard key={order._id} order={order} />)}
        </Box>}
    </>
  )
}
