const ProductPage = () => {
  const [products, setProducts] = useState<Product[]>();
  const [filter, setFilter] = useState<Product[]>();
  const [quantity, setQuantity] = useState(1);
  const location = useLocation();
  const id = location.pathname.split("/")[2];
  const dispatch = useAppDispatch();
  const handleQuantity = (type: string) => {
    if (quantity > 1 && type === "dec") setQuantity(quantity - 1);
    if (type === "inc") setQuantity(quantity + 1);
  };
  const handleClick = () => {
    dispatch(addProduct({ products, quantity }));
  };
  useEffect(() => {
    const getProducts = async () => {
      try {
        const res = await publicRequest.get(`/products/find/${id}`);
        setProducts(res.data);
      } catch (err) {
        console.log(err);
      }
    };
    getProducts();
  }, [id]);
  useEffect(() => {
    const getFilter = async () => {
      try {
        const res = await axios.get(`http://localhost:5000/api/products`);
        setFilter(res.data);
      } catch (err) {
        console.log(err);
      }
    };
    getFilter();
  }, [products?.color]);
  return (
    <Container>
      {products && filter ? (
        <>
          <Navbar />
          <PromoInfo />
          <Wrapper>
            <ImageContainer>
              <Image src={products.img} />
            </ImageContainer>
            <InfoContainer>
              <Title>{products.title}</Title>
              <Description>{products?.description}</Description>
              <Price>{products.price}PLN</Price>
              <FilterContainer>
                <Filter>
                  <FilterTitle>Color</FilterTitle>
                  {filter
                    .filter((item) => item.color[0] !== products.color[0])
                    .map((item, index) => (
                      <Link key={index} to={`/product/${item._id}`}>
                        <FilterColor color={item.color[0]} />
                      </Link>
                    ))}
                </Filter>
              </FilterContainer>
              <AddContainer>
                <AmountContainer>
                  <RemoveIcon onClick={() => handleQuantity("dec")} />
                  <Amount>{quantity}</Amount>
                  <AddIcon onClick={() => handleQuantity("inc")} />
                </AmountContainer>
                <Button onClick={() => handleClick()}>Add to cart</Button>
              </AddContainer>
            </InfoContainer>
          </Wrapper>
          <Footer />
        </>
      ) : null}
    </Container>
  );
};
const ProductPage = () => {
  const [products, setProducts] = useState<Product[]>();
  const [filter, setFilter] = useState<Product[]>();
  const [quantity, setQuantity] = useState(1);
  const location = useLocation();
  const id = location.pathname.split("/")[2];
  const dispatch = useAppDispatch();
  const handleQuantity = (type: string) => {
    if (quantity > 1 && type === "dec") setQuantity(quantity - 1);
    if (type === "inc") setQuantity(quantity + 1);
  };
  const handleClick = () => {
    dispatch(addProduct({ products, quantity }));
  };
  useEffect(() => {
    const getProducts = async () => {
      try {
        const res = await publicRequest.get(`/products/find/${id}`);
        setProducts(res.data);
      } catch (err) {
        console.log(err);
      }
    };
    getProducts();
  }, [id]);
  useEffect(() => {
    const getFilter = async () => {
      try {
        const res = await axios.get(`http://localhost:5000/api/products`);
        setFilter(res.data);
      } catch (err) {
        console.log(err);
      }
    };
    getFilter();
  }, [products?.color]);
  return (
    <Container>
      {products && filter ? (
        <>
          <Navbar />
          <PromoInfo />
          <Wrapper>
            <ImageContainer>
              <Image src={products.img} />
            </ImageContainer>
            <InfoContainer>
              <Title>{products.title}</Title>
              <Description>{products?.description}</Description>
              <Price>{products.price}PLN</Price>
              <FilterContainer>
                <Filter>
                  <FilterTitle>Color</FilterTitle>
                  {filter
                    .filter((item) => item.color[0] !== products.color[0])
                    .map((item, index) => (
                      <Link key={index} to={`/product/${item._id}`}>
                        <FilterColor color={item.color[0]} />
                      </Link>
                    ))}
                </Filter>
              </FilterContainer>
              <AddContainer>
                <AmountContainer>
                  <RemoveIcon onClick={() => handleQuantity("dec")} />
                  <Amount>{quantity}</Amount>
                  <AddIcon onClick={() => handleQuantity("inc")} />
                </AmountContainer>
                <Button onClick={() => handleClick()}>Add to cart</Button>
              </AddContainer>
            </InfoContainer>
          </Wrapper>
          <Footer />
        </>
      ) : null}
    </Container>
  );
};
const ProductPage = () => {
  const [products, setProducts] = useState<Product[]>();
  const [filter, setFilter] = useState<Product[]>();
  const [quantity, setQuantity] = useState(1);
  const location = useLocation();
  const id = location.pathname.split("/")[2];
  const dispatch = useAppDispatch();
  const handleQuantity = (type: string) => {
    if (quantity > 1 && type === "dec") setQuantity(quantity - 1);
    if (type === "inc") setQuantity(quantity + 1);
  };
  const handleClick = () => {
    dispatch(addProduct({ products, quantity }));
  };
  useEffect(() => {
    const getProducts = async () => {
      try {
        const res = await publicRequest.get(`/products/find/${id}`);
        setProducts(res.data);
      } catch (err) {
        console.log(err);
      }
    };
    getProducts();
  }, [id]);
  useEffect(() => {
    const getFilter = async () => {
      try {
        const res = await axios.get(`http://localhost:5000/api/products`);
        setFilter(res.data);
      } catch (err) {
        console.log(err);
      }
    };
    getFilter();
  }, [products?.color]);
  return (
    <Container>
      {products && filter ? (
        <>
          <Navbar />
          <PromoInfo />
          <Wrapper>
            <ImageContainer>
              <Image src={products.img} />
            </ImageContainer>
            <InfoContainer>
              <Title>{products.title}</Title>
              <Description>{products?.description}</Description>
              <Price>{products.price}PLN</Price>
              <FilterContainer>
                <Filter>
                  <FilterTitle>Color</FilterTitle>
                  {filter
                    .filter((item) => item.color[0] !== products.color[0])
                    .map((item, index) => (
                      <Link key={index} to={`/product/${item._id}`}>
                        <FilterColor color={item.color[0]} />
                      </Link>
                    ))}
                </Filter>
              </FilterContainer>
              <AddContainer>
                <AmountContainer>
                  <RemoveIcon onClick={() => handleQuantity("dec")} />
                  <Amount>{quantity}</Amount>
                  <AddIcon onClick={() => handleQuantity("inc")} />
                </AmountContainer>
                <Button onClick={() => handleClick()}>Add to cart</Button>
              </AddContainer>
            </InfoContainer>
          </Wrapper>
          <Footer />
        </>
      ) : null}
    </Container>
  );
};
Type 'Product[] | undefined' is not assignable to type 'Product[]'.
  Type 'undefined' is not assignable to type 'Product[]'.ts(2322)