BlogItem.js 8.16 KB
import React, { useEffect, useState } from "react";
import { fadeIn, zoomIn, slideFromLeft, slideFromRight } from "../animationvariants.js";
import { motion } from "framer-motion";
import Image from "next/image.js";
import { Accordion, Button, Form } from "react-bootstrap";
import { cleanImage } from "../../services/imageHandling.js";
import { useDispatch, useSelector } from "react-redux";
import { getBlogsData } from "../../redux/actions/blogAction.js";
import { Skeleton } from "antd";

const BlogsItem = ({ blogs, loading }) => {
  const dispatch = useDispatch();
  const { categories } = useSelector(state => state.categories);
  // const { subCategoires } = useSelector(state => state.subCategoires);
  const [showContent, setShowContent] = useState(false);
  const [isGridViewOpen, setIsGridViewOpen] = useState(false);
  const [isOpen, setIsOpen] = useState(false);
  const [subCategoryFilter, setsubCategoryFilter] = useState([]);

  console.log("categoires", categories);
  // console.log("subCategoires", subCategoires);
  const toggleContent = () => {
    setShowContent(!showContent);
  };
  const toggleGridViewDropdown = () => {
    setIsGridViewOpen(!isGridViewOpen);
  };
  const toggleFilterViewDropdown = () => {
    setIsFilterViewOpen(!isFilterViewOpen);
  };
  const toggleDropdown = () => {
    setIsOpen(!isOpen);
  };
  console.log("subCategoryFilter", subCategoryFilter);
  useEffect(() => {
    let filters = [];
    filters =
      subCategoryFilter.length > 0 &&
      subCategoryFilter.map(item => {
        return {
          subCategory: {
            id: {
              $eq: item
            }
          }
        };
      });
    if (subCategoryFilter.length > 0) {
      dispatch(getBlogsData({ subCategories: filters }));
    } else {
      dispatch(getBlogsData({}));
    }
  }, [subCategoryFilter]);

  return (
    <section className="blogs-session">
      <div className="container">
        <div className="row">
          <div className="col-12">
            <div className="head-btn">
              <motion.div variants={slideFromRight(0.4)} initial={"hidden"} whileInView={"show"} viewport={{ once: false, amount: 0.2 }}>
                <div className="head01">
                  <div className="title">Most Read</div>
                  <h2>Blogs</h2>
                </div>
              </motion.div>
            </div>
          </div>
        </div>
        <div className="row">
          <div className="col-12">
            <div className="blogs-filter">
              <a onClick={toggleGridViewDropdown}>
                <span className="image-container">
                  <Image layout="fill" alt="" className="image img-fluid" src="/images/icons/filter-icon.svg" />
                </span>
              </a>
              <div className={`inner-content ${isGridViewOpen ? "open" : ""}`}>
                <div className="top-head">
                  <div className="">Select</div>
                  <div className="close-btn" onClick={toggleGridViewDropdown}>
                    <span className="image-container">
                      <Image layout="fill" alt="" className="image img-fluid" src="/images/icons/close-icon.svg" />
                    </span>
                  </div>
                </div>
                <div className="listing-filter">
                  <div className="box-01">
                    <div className="inner">
                      <Accordion className="accordion-filter" defaultActiveKey="0" flush>
                        {categories.data.map((item, key) => {
                          return (
                            <Accordion.Item eventKey={key} key={key}>
                              <Accordion.Header>{item.attributes.name}</Accordion.Header>
                              <Accordion.Body>
                                {item.attributes.subCategories.data.map((data, key) => {
                                  return (
                                    <div className="data-filters-item">
                                      <label>
                                        <input
                                          type="checkbox"
                                          onChange={e => {
                                            console.log(e);
                                            if (e.target.checked) {
                                              setsubCategoryFilter([...subCategoryFilter, data.id]);
                                            } else {
                                              if (subCategoryFilter.length == 0) {
                                                return;
                                              }
                                              let arr = subCategoryFilter.filter(i => i != data.id);
                                              setsubCategoryFilter(arr);
                                            }
                                          }}
                                        />
                                        {data.attributes.name}
                                      </label>
                                    </div>
                                  );
                                })}
                              </Accordion.Body>
                            </Accordion.Item>
                          );
                        })}
                      </Accordion>
                    </div>
                  </div>
                  <div></div>
                </div>
                {/* <div className="text-center">
                  <Button variant="primary">Submit</Button>
                </div> */}
              </div>
            </div>
          </div>
        </div>
        <div className="row">
          <div className="col-12">
            {!loading ? (
              <div className="row">
                {blogs &&
                  blogs?.map(data => {
                    return (
                      <div className="col-md-4">
                        <div className="blog-item">
                          <div className="img-wrapper">
                            <span className="image-container">
                              <Image layout="fill" alt="" className="image img-fluid" src={cleanImage(data?.attributes?.image?.data?.attributes)} />
                            </span>
                            {/* <div className="top-rated">{data?.attributes?.categories}</div> */}
                          </div>
                          <div className="info">
                            {/* <div className="read-row">
                            <span className="image-container">
                              <Image layout="fill" alt="" className="image img-fluid" src="/images/icons/clock.svg" />
                            </span>
                            <div className="">{data?.attributes?.read} Read</div>
                          </div> */}
                            <div className="title">{data?.attributes?.title}</div>
                            <div className="discription" dangerouslySetInnerHTML={{ __html: data?.attributes?.description }}></div>
                            <div className="read-more">
                              <Button href={`/blog/${data.attributes.slug}`} variant="primary">
                                Read More
                              </Button>
                            </div>
                          </div>
                        </div>
                      </div>
                    );
                  })}
              </div>
            ) : (
              <div className="container">
                <div className="py-5">
                  <div className="row">
                    {[1, 2, 3, 5, 5, 5].map((_, index) => (
                      <div key={index} className="col-lg-3 col-md-3 col-sm-12 my-3">
                        <div className="d-inline-flex flex-column">
                          <Skeleton.Button active style={{ height: 250, width: 270 }} />
                          <Skeleton.Button active style={{ marginTop: 10, width: 120 }} />
                        </div>
                      </div>
                    ))}
                  </div>
                </div>
              </div>
            )}
          </div>
        </div>
      </div>
    </section>
  );
};

export default BlogsItem;