Blame view

components/admin/VendorListing.js 9.61 KB
jaymehta committed
1 2 3 4 5 6 7 8 9 10
import { CheckOutlined, DeleteTwoTone, EyeTwoTone, StopOutlined, StopTwoTone } from "@ant-design/icons";
import { Space, Table, Tag } from "antd";
import { getSession } from "next-auth/react";
import { useRouter } from "next/router";
import { Fragment, useEffect, useState } from "react";
import { Button, Image } from "react-bootstrap";
import { useDispatch, useSelector } from "react-redux";
import { loadUser } from "../../redux/actions/userActions";
import { getAllVendors } from "../../redux/actions/vendorActions";
import VendorDetailModal from "./VendorDetailModal";
Jyotsna committed
11 12 13
import VendorDetails from "./VendorDetails";

const VendorListing = () => {
jaymehta committed
14 15 16 17 18 19 20 21 22 23 24 25
  // Hooks
  const dispatch = useDispatch();
  const router = useRouter();
  const array = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  // useState
  const [session, setSession] = useState();
  const [loading, setLoading] = useState(true);
  const [showMenuIndex, setShowMenuIndex] = useState(null);
  const [restriction, setRestriction] = useState(false);
  const [data, setData] = useState([]);
  const [showDetail, setShowDetail] = useState(false);
  const [detail, setDetail] = useState();
Jyotsna committed
26

jaymehta committed
27 28 29 30 31 32
  // selectors
  const { loadedUser } = useSelector(state => state.loadedUser);
  const { allVendors } = useSelector(state => state.allVendors);
  const toggleMenu = index => {
    setShowMenuIndex(index === showMenuIndex ? null : index);
  };
Jyotsna committed
33

jaymehta committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  // Session
  // useEffect(() => {
  //   const fetchSession = async () => {
  //     setSession(await getSession());
  //   };
  //   fetchSession();
  //   // dispatch(getLoggedInVendor());
  // }, []);
  // useEffects

  useEffect(() => {
    dispatch(loadUser());
    // console.log(">>", loadedUser);
    // if (!loadedUser) {
    //   router.push("/admin/login")
    // }
    if (loadedUser && loadedUser.role.name != "admin") {
      setRestriction(true);
      setLoading(true);
    } else {
      setRestriction(false);

      setLoading(false);
Jyotsna committed
57
    }
jaymehta committed
58 59 60 61 62 63
    // if (!loadedUser) {
    //   setRestriction(true);
    //   return
    // }
  }, []);
  // console.log("session", loadedUser);
Jyotsna committed
64

jaymehta committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
  useEffect(() => {
    if (!loadedUser) return;
    dispatch(getAllVendors());
  }, [loadedUser]);

  console.log("allVendors", allVendors);
  const [vendorId, setVendorId] = useState(null);
  const [showVendorDetails, setShowVendorDetails] = useState(false);
  const handleShowVendorDetails = () => {
    setShowVendorDetails(true);
  };
  const handleCloseVendorDetails = () => {
    setShowVendorDetails(false);
  };

  // useEffect

  useEffect(() => {
    setLoading(true);
    let initialData =
      allVendors &&
      allVendors.map(item => {
.  
jaymehta committed
87
        console.log("item", item.attributes.user.data);
jaymehta committed
88 89 90 91
        return {
          key: item.id,
          name: item.attributes.name,
          businessName: item.attributes.businessName,
.  
jaymehta committed
92 93
          status: [item.attributes.user.data?.attributes.approved],
          confirmed: item.attributes.user.data?.attributes.confirmed
jaymehta committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
        };
      });
    console.log("initialData", initialData);
    setData(initialData);
    setLoading(false);
  }, [allVendors]);

  const columns = [
    {
      title: "Name",
      dataIndex: "name",
      key: "name",
      render: text => <a>{text}</a>
    },
    {
      title: "Business Name",
      dataIndex: "businessName",
      key: "businessName"
    },
    {
      title: "OTP verified",
      dataIndex: "confirmed",
      key: "confirmed",
      render: (_, { confirmed }) => (
        <>
          <>{confirmed ? <CheckOutlined /> : <StopOutlined />}</>
        </>
      )
    },
    {
      title: "Approved",
      key: "status",
      dataIndex: "status",
      render: (_, { status }) => (
        <>
          {status.map(tag => {
            // console.log("tag", tag);
            let color;
            // if (tag === "loser") {
            //   color = "volcano";
            // }
            switch (tag) {
              case "approved":
                color = "green";
                break;
              case "pending":
                color = "orange";
                break;

              case "none":
                color = "red";
                break;

              case "rejected":
                color = "red";
                break;

              default:
                break;
            }
            // console.log("color", color);
            return (
              <Tag color={color} key={tag}>
.  
jaymehta committed
157
                {tag?.toString().toUpperCase()}
jaymehta committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
              </Tag>
            );
          })}
        </>
      )
    },
    {
      title: "Action",
      key: "action",
      render: (_, record) => (
        <Space size="middle">
          {/* <DeleteTwoTone style={{ fontSize: '22px' }}  twoToneColor="red"/> */}
          <EyeTwoTone
            style={{ fontSize: "22px" }}
            onClick={() => {
              setShowDetail(true);
              setDetail(allVendors.filter(item => item.id == record.key));
            }}
          />
.  
jaymehta committed
177
          {/* <StopTwoTone style={{ fontSize: "20px" }} twoToneColor="red" /> */}
jaymehta committed
178 179 180 181
        </Space>
      )
    }
  ];
Jyotsna committed
182

jaymehta committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196
  return (
    <Fragment>
      <div className={restriction ? "overlay" : ""}>
        {restriction ? (
          <div className="d-flex justify-content-center align-items-center" style={{ color: "#FFF", fontSize: "50px", height: "100%" }}>
            Access restricted! Only admin is allowed to view the data.
          </div>
        ) : (
          <div>
            {!showDetail ? (
              <Table loading={loading} columns={columns} dataSource={data} />
            ) : (
              // <VendorDetailModal  />
              <VendorDetails setShowDetail={setShowDetail} showDetail={showDetail} detail={detail} />
Jyotsna committed
197
            )}
jaymehta committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
            <div></div>
          </div>
        )}
      </div>
    </Fragment>
    // <Fragment>
    //   {!showVendorDetails && (
    //     <div className="row">
    //       <div className="col-12 col-lg-12">
    //         <div className="rightContent">
    //           <div className="d-flex align-items-center justify-content-between px-2 mb-2">
    //             <div>
    //               <h2>Vendors</h2>
    //               <p>View all the vendors</p>
    //             </div>
    //             <div>
    //               <Button type="button" variant="" className="btnAdd m-0">
    //                 <Image alt="" width="16" height="16" src="/images/vendor/icon-filter.svg" className="me-2" /> Filter
    //               </Button>
    //             </div>
    //           </div>
    //           <Table responsive className="listingTable">
    //             <thead>
    //               <tr>
    //                 <th>
    //                   <label className="check-container mb-0 ps-2" htmlFor="checkh">
    //                     <input type="checkbox" id="checkh" className="check-box" />
    //                     <span className="checkmark"></span>
    //                   </label>
    //                 </th>
    //                 <th>Vendor</th>
    //                 <th>Vendor Id</th>
    //                 <th>Contact Person</th>
    //                 <th>Phone Number</th>
    //                 <th>Email Address</th>
    //                 <th>Status</th>
    //                 <th></th>
    //               </tr>
    //             </thead>
    //             <tbody>
    //               {array.map((data, index) => (
    //                 <tr key={index}>
    //                   <td>
    //                     <label className="check-container mb-0 ps-2" htmlFor={`check${index}`}>
    //                       <input type="checkbox" id={`check${index}`} className="check-box" />
    //                       <span className="checkmark"></span>
    //                     </label>
    //                   </td>
    //                   <td>
    //                     <span
    //                       onClick={() => {
    //                         setVendorId(1);
    //                         handleShowVendorDetails();
    //                       }}
    //                     >
    //                       M S Adventure
    //                     </span>
    //                   </td>
    //                   <td>VN_ET9797</td>
    //                   <td>Ridge Johnson</td>
    //                   <td>+91 9865 43210</td>
    //                   <td>adventuretrails@example.com</td>
    //                   <td>
    //                     <div className={`statusDiv ${index % 2 ? "inactive" : "active"}`}>{index % 2 ? "Inactive" : "Active"}</div>
    //                   </td>
    //                   <td>
    //                     <div className="actions">
    //                       <div className="ellipse" onClick={() => toggleMenu(index)}>
    //                         <Image alt="" width={20} height={20} src="/images/vendor/icon-more-vertical.svg" />
    //                       </div>
    //                       {showMenuIndex === index && (
    //                         <div className="menu">
    //                           <ul>
    //                             <li>View</li>
    //                             <li>Deactivate</li>
    //                             <li>Delete</li>
    //                           </ul>
    //                         </div>
    //                       )}
    //                     </div>
    //                   </td>
    //                 </tr>
    //               ))}
    //               <tr>
    //                 <td colSpan={7}>Showing Results 10 of 1567</td>
    //               </tr>
    //             </tbody>
    //           </Table>
    //         </div>
    //       </div>
    //     </div>
    //   )}
Jyotsna committed
290

jaymehta committed
291 292 293
    //   {vendorId && showVendorDetails && <VendorDetails id={vendorId} backClick={handleCloseVendorDetails} />}
    // </Fragment>
  );
Jyotsna committed
294 295 296
};

export default VendorListing;