Blame view

components/vendor/ActivityListingRBAC.js 14.3 KB
jaymehta committed
1
import React, { useEffect, useRef, useState } from "react";
.  
jaymehta committed
2
import { Button, Dropdown, Input, Space, Table, Tag } from "antd";
jaymehta committed
3
import { useDispatch, useSelector } from "react-redux";
jaymehta committed
4
import Highlighter from "react-highlight-words";
jaymehta committed
5
import { DeleteTwoTone, DownCircleOutlined, EditTwoTone, SearchOutlined } from "@ant-design/icons";
jaymehta committed
6
import { useRouter } from "next/router";
jaymehta committed
7 8 9 10
import { getActivitiesByVendor } from "../../redux/actions/activityAction";
import { loadUser, updateActivityStatusAdmin } from "../../redux/actions/userActions";
import { Modal } from "react-bootstrap";
import { toast } from "react-toastify";
jaymehta committed
11 12 13 14 15

const onChange = (pagination, filters, sorter, extra) => {
  console.log("params", pagination, filters, sorter, extra);
};

.  
jaymehta committed
16
export const ActivityListingRBAC = ({ setactivityDetailInfo, setShowActivityDetailsModal }) => {
jaymehta committed
17 18
  const router = useRouter();
  const dispatch = useDispatch();
jaymehta committed
19 20 21
  // let columns = []
  // useSelectors
  const { allActivitiesData } = useSelector(state => state.allActivitiesData);
jaymehta committed
22
  const { loadedUser } = useSelector(state => state.loadedUser);
jaymehta committed
23 24
  const { categories } = useSelector(state => state.categories);
  const { subCategories } = useSelector(state => state.subCategories);
.  
jaymehta committed
25
  // console.log("loadedUser", loadedUser);
jaymehta committed
26 27 28 29
  // useStates
  const [selectedRowKeys, setSelectedRowKeys] = useState([]);
  const [columns, setcolumns] = useState([]);
  const [data, setdata] = useState([]);
jaymehta committed
30
  const [rejectPopUp, setrejectPopUp] = useState(false);
jaymehta committed
31 32
  const [searchText, setSearchText] = useState("");
  const [searchedColumn, setSearchedColumn] = useState("");
jaymehta committed
33 34
  const [rejectionReasonText, setrejectionReasonText] = useState("");
  const [rejectionId, setrejectionId] = useState("");
jaymehta committed
35

jaymehta committed
36 37 38 39 40 41
  // functions
  const onSelectChange = newSelectedRowKeys => {
    console.log("selectedRowKeys changed: ", newSelectedRowKeys);
    setSelectedRowKeys(newSelectedRowKeys);
  };

jaymehta committed
42 43 44 45 46 47
  const searchInput = useRef(null);
  const handleSearch = (selectedKeys, confirm, dataIndex) => {
    confirm();
    setSearchText(selectedKeys[0]);
    setSearchedColumn(dataIndex);
  };
jaymehta committed
48
  const handleReset = clearFilters => {
jaymehta committed
49
    clearFilters();
jaymehta committed
50
    setSearchText("");
jaymehta committed
51
  };
jaymehta committed
52
  const getColumnSearchProps = dataIndex => ({
jaymehta committed
53 54 55
    filterDropdown: ({ setSelectedKeys, selectedKeys, confirm, clearFilters, close }) => (
      <div
        style={{
jaymehta committed
56
          padding: 8
jaymehta committed
57
        }}
jaymehta committed
58
        onKeyDown={e => e.stopPropagation()}
jaymehta committed
59 60 61 62 63
      >
        <Input
          ref={searchInput}
          placeholder={`Search ${dataIndex}`}
          value={selectedKeys[0]}
jaymehta committed
64
          onChange={e => setSelectedKeys(e.target.value ? [e.target.value] : [])}
jaymehta committed
65 66 67
          onPressEnter={() => handleSearch(selectedKeys, confirm, dataIndex)}
          style={{
            marginBottom: 8,
jaymehta committed
68
            display: "block"
jaymehta committed
69 70 71 72 73 74 75 76 77
          }}
        />
        <Space>
          <Button
            type="primary"
            onClick={() => handleSearch(selectedKeys, confirm, dataIndex)}
            icon={<SearchOutlined />}
            size="small"
            style={{
jaymehta committed
78
              width: 90
jaymehta committed
79 80 81 82 83 84 85 86
            }}
          >
            Search
          </Button>
          <Button
            onClick={() => clearFilters && handleReset(clearFilters)}
            size="small"
            style={{
jaymehta committed
87
              width: 90
jaymehta committed
88 89 90 91 92 93 94 95 96
            }}
          >
            Reset
          </Button>
          <Button
            type="link"
            size="small"
            onClick={() => {
              confirm({
jaymehta committed
97
                closeDropdown: false
jaymehta committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
              });
              setSearchText(selectedKeys[0]);
              setSearchedColumn(dataIndex);
            }}
          >
            Filter
          </Button>
          <Button
            type="link"
            size="small"
            onClick={() => {
              close();
            }}
          >
            close
          </Button>
        </Space>
      </div>
    ),
jaymehta committed
117
    filterIcon: filtered => (
jaymehta committed
118 119
      <SearchOutlined
        style={{
jaymehta committed
120
          color: filtered ? "#1677ff" : undefined
jaymehta committed
121 122 123
        }}
      />
    ),
jaymehta committed
124 125
    onFilter: (value, record) => record[dataIndex].toString().toLowerCase().includes(value.toLowerCase()),
    onFilterDropdownOpenChange: visible => {
jaymehta committed
126 127 128 129
      if (visible) {
        setTimeout(() => searchInput.current?.select(), 100);
      }
    },
jaymehta committed
130
    render: text =>
jaymehta committed
131 132 133
      searchedColumn === dataIndex ? (
        <Highlighter
          highlightStyle={{
jaymehta committed
134 135
            backgroundColor: "#ffc069",
            padding: 0
jaymehta committed
136 137 138
          }}
          searchWords={[searchText]}
          autoEscape
jaymehta committed
139
          textToHighlight={text ? text.toString() : ""}
jaymehta committed
140 141 142
        />
      ) : (
        text
jaymehta committed
143
      )
jaymehta committed
144
  });
jaymehta committed
145
  let IS_ADMIN = false;
jaymehta committed
146
  // useEffects
jaymehta committed
147

jaymehta committed
148
  useEffect(() => {
jaymehta committed
149 150 151 152 153 154 155 156 157 158 159 160 161 162
    if (loadedUser && loadedUser.role.type == "admin") {
      IS_ADMIN = true;
    }
  }, [loadedUser]);

  useEffect(() => {
    // dispatch(loadUser());

    let initialData =
      allActivitiesData &&
      allActivitiesData.data.map((item, index) => {
        return {
          key: item.id,
          name: item.attributes.name,
.  
jaymehta committed
163
          category: item.attributes.category?.data.attributes.name,
jaymehta committed
164 165 166 167
          subCategory: item.attributes.subCategory?.data.attributes.name,
          location: item.attributes.masterPincode.data.attributes.name,
          price: item.attributes.pricePerPerson,
          place: item.attributes.activityType,
.  
jaymehta committed
168 169
          gift: item.attributes.giftSomeone ? "Yes" : "No",
          status: item.attributes.approved
jaymehta committed
170 171
        };
      });
jaymehta committed
172

jaymehta committed
173
    const categoryFilterArray = categories?.data.map((item, index) => {
jaymehta committed
174 175
      return { text: item.attributes.name, value: item.attributes.name };
    });
jaymehta committed
176
    const subCategoryFilterArray = subCategories?.data.map((item, index) => {
jaymehta committed
177 178 179
      return { text: item.attributes.name, value: item.attributes.name };
    });
    setdata(initialData);
jaymehta committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    if (loadedUser && loadedUser.role.type == "admin") {
      setcolumns([
        {
          title: "Category",
          dataIndex: "category",
          filters: categoryFilterArray,
          filterMode: "tree",
          filterSearch: true,
          onFilter: (value, record) => record.category.startsWith(value),
          width: "15%"
        },
        {
          title: "Sub-category",
          dataIndex: "subCategory",
          filters: subCategoryFilterArray,
          filterSearch: true,
          onFilter: (value, record) => record.subCategory.startsWith(value),
          width: "15%"
        },
        {
          title: "Name",
          dataIndex: "name",
          key: "name",
          ...getColumnSearchProps("name"),
          width: "15%"
        },
        {
          title: "Location",
          dataIndex: "location",
          width: "15%"
        },
        {
          title: "Price",
          dataIndex: "price",
          width: "15%"
        },
        {
          title: "Place",
          dataIndex: "place",
jaymehta committed
219

jaymehta committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
          width: "15%"
        },
        {
          title: "Action",
          render: (_, record) => (
            <Space size="middle">
              {/* <a>Invite {record.name}</a> */}
              <Dropdown
                menu={{
                  items: [
                    {
                      key: "1",
                      label: (
                        <a
                          rel="noopener noreferrer"
                          onClick={() => {
.  
jaymehta committed
236
                            adminActions({ type: "view", activityId: record.key });
jaymehta committed
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
                            // console.log(record);
                          }}
                        >
                          View Details
                        </a>
                      )
                    },
                    {
                      key: "2",
                      label: (
                        <a
                          rel="noopener noreferrer"
                          onClick={() => {
                            setrejectionId(record.key);
                            adminActions({ type: "reject", activityId: record.key });
                          }}
                        >
                          Reject
                        </a>
                      )
                    },
                    {
                      key: "3",
                      label: (
                        <a
                          target="_blank"
                          rel="noopener noreferrer"
                          onClick={() => {
                            // setrejectionId(record.key);
                            adminActions({ type: "approve", activityId: record.key });
                          }}
                        >
                          Approve
                        </a>
                      )
                    }
                  ]
                }}
                placement="bottomLeft"
              >
                <Button style={{ border: "none" }}>
                  <DownCircleOutlined style={{ fontSize: "20px", color: "#08c" }} onClick={() => {}} />
                </Button>
              </Dropdown>
            </Space>
          ),
          width: "10%"
        }
      ]);
.  
jaymehta committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
    } else {
      setcolumns([
        {
          title: "Category",
          dataIndex: "category",
          filters: categoryFilterArray,
          filterMode: "tree",
          filterSearch: true,
          onFilter: (value, record) => record.category.startsWith(value),
          width: "15%"
        },
        {
          title: "Sub-category",
          dataIndex: "subCategory",
          filters: subCategoryFilterArray,
          filterSearch: true,
          onFilter: (value, record) => record.subCategory.startsWith(value),
          width: "15%"
        },
        {
          title: "Name",
          dataIndex: "name",
          key: "name",
          ...getColumnSearchProps("name"),
          width: "15%"
        },
        {
          title: "Location",
          dataIndex: "location",
          width: "15%"
        },
        {
          title: "Price",
          dataIndex: "price",
          width: "15%"
        },
        {
          title: "Place",
          dataIndex: "place",

          width: "12%"
        },
        {
          title: "Approved",
          key: "status",
          dataIndex: "status",
          render: (_, { status }) => (
            <>
.  
jaymehta committed
334
              {status == "approved" && (
.  
jaymehta committed
335 336 337
                <Tag color={"green"} key={1}>
                  Approved
                </Tag>
.  
jaymehta committed
338 339 340 341 342 343 344
              )}
              {status == "pending" && (
                <Tag color={"orange"} key={1}>
                  Pending
                </Tag>
              )}
              {status == "rejected" && (
.  
jaymehta committed
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
                <Tag color={"red"} key={1}>
                  Rejected
                </Tag>
              )}
            </>
          )
        },
        {
          title: "Edit record",
          render: (_, record) => (
            <Space size="middle">
              {/* <a>Invite {record.name}</a> */}
              <EditTwoTone
                style={{ fontSize: "22px" }}
                onClick={() => {
                  router.push(`/vendor/activities/${record.key}`);
                  console.log(record);
                }}
              />
              {/* <DeleteTwoTone
                twoToneColor="#FF0000"
                style={{ fontSize: "22px" }}
                onClick={() => {
                  console.log(record);
                }}
              /> */}
            </Space>
          ),
          width: "10%"
        }
      ]);
jaymehta committed
376 377
    }
  }, [allActivitiesData, loadedUser]);
jaymehta committed
378

jaymehta committed
379
  // console.log("allActivitiesData", allActivitiesData);
jaymehta committed
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417

  const rowSelection = {
    selectedRowKeys,
    onChange: onSelectChange,
    selections: [
      Table.SELECTION_ALL,
      Table.SELECTION_INVERT,
      Table.SELECTION_NONE,
      {
        key: "odd",
        text: "Select Odd Row",
        onSelect: changeableRowKeys => {
          let newSelectedRowKeys = [];
          newSelectedRowKeys = changeableRowKeys.filter((_, index) => {
            if (index % 2 !== 0) {
              return false;
            }
            return true;
          });
          setSelectedRowKeys(newSelectedRowKeys);
        }
      },
      {
        key: "even",
        text: "Select Even Row",
        onSelect: changeableRowKeys => {
          let newSelectedRowKeys = [];
          newSelectedRowKeys = changeableRowKeys.filter((_, index) => {
            if (index % 2 !== 0) {
              return true;
            }
            return false;
          });
          setSelectedRowKeys(newSelectedRowKeys);
        }
      }
    ]
  };
jaymehta committed
418 419 420

  const adminActions = async ({ type, activityId }) => {
    if (type == "approve") {
.  
jaymehta committed
421
      await updateActivityStatusAdmin({ status: "approved", activityId, rejectionReason: "" });
jaymehta committed
422 423 424 425 426
    }
    if (type == "reject") {
      setrejectPopUp(true);
    }
    if (type == "view") {
.  
jaymehta committed
427 428 429
      let data = allActivitiesData.data.filter(item => item.id == activityId);
      setactivityDetailInfo(data);
      setShowActivityDetailsModal(true);
jaymehta committed
430 431
    }
  };
jaymehta committed
432
  return (
Chetan committed
433
    <div className="p-5">
jaymehta committed
434
      <Table rowSelection={rowSelection} columns={columns} dataSource={data} onChange={onChange} />
jaymehta committed
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
      <Modal
        show={rejectPopUp}
        onHide={() => {
          setrejectionId(null);
          setrejectPopUp(false);
        }}
      >
        <Modal.Header closeButton>Please mention reason for rejecting the activity.</Modal.Header>
        <Modal.Body>
          <div className="input-group">
            <textarea
              style={{ width: "100%" }}
              rows={4}
              className="p-2"
              placeholder="Eg: Not appropriate for this age group."
              name="comments"
              onChange={e => {
                e.preventDefault();
                setrejectionReasonText(e.target.value);
              }}
            />
          </div>
          <div className="my-4">
            <div className="d-flex justify-content-center">
              <Button
                variant=""
                className="btnAdd btnApprove m-0"
                disabled={rejectionReasonText == ""}
                onClick={async () => {
.  
jaymehta committed
464
                  await updateActivityStatusAdmin({ status: "rejected", activityId: rejectionId, rejectionReason: rejectionReasonText });
jaymehta committed
465 466
                  setrejectionId(null);
                  setrejectPopUp(false);
.  
jaymehta committed
467
                  toast.success("Activity rejected.");
jaymehta committed
468 469 470 471 472 473 474 475
                }}
              >
                Submit
              </Button>
            </div>
          </div>
        </Modal.Body>
      </Modal>
jaymehta committed
476 477 478 479 480
    </div>
  );
};

export default ActivityListingRBAC;