|
| 1 | +import React, { Component } from "react"; |
| 2 | +import "./Filter.scss"; |
| 3 | +import Form from "react-bootstrap/Form"; |
| 4 | +import Button from "react-bootstrap/Button"; |
| 5 | +import Dropdown from "react-bootstrap/Dropdown"; |
| 6 | +import CheckOutlinedIcon from "@material-ui/icons/CheckOutlined"; |
| 7 | +import SearchOutlinedIcon from "@material-ui/icons/SearchOutlined"; |
| 8 | + |
| 9 | +// Author, tags and status are the three filters that we want |
| 10 | +// only one author can be selected at a time, clicking on a different authoir will unclick the first author |
| 11 | +// implement search bar in author filter |
| 12 | +// implement |
| 13 | + |
| 14 | +class Filter extends Component { |
| 15 | + constructor(props) { |
| 16 | + super(props); |
| 17 | + this.state = { |
| 18 | + author: null, |
| 19 | + status: [], |
| 20 | + tags: [], |
| 21 | + search: "", |
| 22 | + allTags: Array.from( |
| 23 | + new Set( |
| 24 | + props.tickets.reduce( |
| 25 | + (acc, curr) => [...acc, ...(curr.tags || [])], |
| 26 | + [] |
| 27 | + ) |
| 28 | + ) |
| 29 | + ), |
| 30 | + allAuthors: Array.from( |
| 31 | + new Set( |
| 32 | + props.tickets.map((ticket) => |
| 33 | + JSON.stringify({ |
| 34 | + id: ticket.createdBy.id, |
| 35 | + name: ticket.createdBy.name, |
| 36 | + }) |
| 37 | + ) |
| 38 | + ) |
| 39 | + ), |
| 40 | + tickets: props.tickets, |
| 41 | + }; |
| 42 | + this.allTickets = props.tickets; |
| 43 | + } |
| 44 | + |
| 45 | + filter = (remove) => { |
| 46 | + let filtered = this.props.tickets; |
| 47 | + if (this.state.search) { |
| 48 | + filtered = filtered.filter( |
| 49 | + (ele) => ele.title.toLowerCase().indexOf(this.state.search) !== -1 |
| 50 | + ); |
| 51 | + } |
| 52 | + if (this.state.author) { |
| 53 | + filtered = filtered.filter( |
| 54 | + (ele) => this.state.author === JSON.stringify(ele.createdBy) |
| 55 | + ); |
| 56 | + } |
| 57 | + if (this.state.status.length) { |
| 58 | + filtered = filtered.filter( |
| 59 | + (ele) => this.state.status.indexOf(ele.status) !== -1 |
| 60 | + ); |
| 61 | + } |
| 62 | + console.log(filtered); |
| 63 | + this.props.setFiltered(filtered); |
| 64 | + }; |
| 65 | + |
| 66 | + handleSearchBarChange = (evt) => { |
| 67 | + const currentValue = evt.target.value; |
| 68 | + this.setState( |
| 69 | + { |
| 70 | + search: currentValue, |
| 71 | + }, |
| 72 | + this.filter |
| 73 | + ); |
| 74 | + }; |
| 75 | + |
| 76 | + handleAuthorChange = (author) => { |
| 77 | + if (author === this.state.author) { |
| 78 | + console.log("Same author clicked twice"); |
| 79 | + this.setState({ author: null }, this.filter); |
| 80 | + } else { |
| 81 | + this.setState({ author: author }, this.filter); |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + handleTagsChange = (tag) => { |
| 86 | + // in not present then add if already present then clicking on it will remove it |
| 87 | + if (this.state.tags.indexOf(tag) === -1) { |
| 88 | + this.setState({ |
| 89 | + tags: [...this.state.tags, tag], |
| 90 | + }); |
| 91 | + } else { |
| 92 | + this.setState({ |
| 93 | + tags: [...this.state.tags.filter((ele) => ele === tag)], |
| 94 | + }); |
| 95 | + } |
| 96 | + }; |
| 97 | + |
| 98 | + handleStatusChange = (status) => { |
| 99 | + // in not present then add if already present then clicking on it will remove it |
| 100 | + if (this.state.status.indexOf(status) === -1) { |
| 101 | + this.setState( |
| 102 | + { |
| 103 | + status: [...this.state.status, status], |
| 104 | + }, |
| 105 | + this.filter |
| 106 | + ); |
| 107 | + } else { |
| 108 | + this.setState( |
| 109 | + { |
| 110 | + status: [...this.state.status.filter((ele) => ele !== status)], |
| 111 | + }, |
| 112 | + this.filter |
| 113 | + ); |
| 114 | + } |
| 115 | + }; |
| 116 | + |
| 117 | + clearFilters = () => { |
| 118 | + this.setState( |
| 119 | + { |
| 120 | + search: "", |
| 121 | + author: null, |
| 122 | + status: ["OPEN", "CLOSED", "PENDING", "SOLVED", "ON_HOLD"], |
| 123 | + }, |
| 124 | + this.props.clear |
| 125 | + ); |
| 126 | + }; |
| 127 | + |
| 128 | + componentDidMount() { |
| 129 | + // fetch all labels and all |
| 130 | + } |
| 131 | + |
| 132 | + render() { |
| 133 | + // console.log(this.state.allAuthors); |
| 134 | + const allStatus = [ |
| 135 | + { label: "Open", status: "OPEN" }, |
| 136 | + { label: "Closed", status: "CLOSED" }, |
| 137 | + { label: "Pending", status: "PENDING" }, |
| 138 | + { label: "Solved", status: "SOLVED" }, |
| 139 | + { label: "On Hold", status: "ON_HOLD" }, |
| 140 | + ]; |
| 141 | + return ( |
| 142 | + <div className="tickets-dashboard-filter"> |
| 143 | + <div className="searchbar-container"> |
| 144 | + <div className="searchbar"> |
| 145 | + <span className="searchbar-icon"> |
| 146 | + <SearchOutlinedIcon /> |
| 147 | + </span> |
| 148 | + <Form> |
| 149 | + <Form.Control |
| 150 | + as="input" |
| 151 | + value={this.state.search} |
| 152 | + placeholder="Search Tickets" |
| 153 | + onChange={this.handleSearchBarChange} |
| 154 | + /> |
| 155 | + </Form> |
| 156 | + </div> |
| 157 | + <Button onClick={() => this.toggleNewTicketEditor(true)}> |
| 158 | + New Ticket |
| 159 | + </Button> |
| 160 | + </div> |
| 161 | + <div className="filters"> |
| 162 | + <div onClick={this.clearFilters} className="clear-filters"> |
| 163 | + {this.state.search |
| 164 | + ? "Clear Search and Filters" |
| 165 | + : !this.state.author || |
| 166 | + !this.state.tags.length || |
| 167 | + (!this.state.status && "Clear Filters")} |
| 168 | + </div> |
| 169 | + <Dropdown size="sm" alignRight> |
| 170 | + <Dropdown.Toggle variant="light" id="dropdown-basic"> |
| 171 | + Authors |
| 172 | + </Dropdown.Toggle> |
| 173 | + <Dropdown.Menu> |
| 174 | + {this.state.allAuthors.map((ele, index) => ( |
| 175 | + <Dropdown.Item |
| 176 | + key={index} |
| 177 | + onClick={() => this.handleAuthorChange(ele)} |
| 178 | + > |
| 179 | + {this.state.author === ele && <CheckOutlinedIcon />} |
| 180 | + {JSON.parse(ele).name} |
| 181 | + </Dropdown.Item> |
| 182 | + ))} |
| 183 | + </Dropdown.Menu> |
| 184 | + </Dropdown> |
| 185 | + <Dropdown size="sm" alignRight> |
| 186 | + <Dropdown.Toggle variant="light" id="dropdown-basic"> |
| 187 | + Status |
| 188 | + </Dropdown.Toggle> |
| 189 | + <Dropdown.Menu> |
| 190 | + {allStatus.map((ele, index) => ( |
| 191 | + <Dropdown.Item |
| 192 | + key={index} |
| 193 | + onClick={() => this.handleStatusChange(ele.status)} |
| 194 | + > |
| 195 | + {this.state.status.indexOf(ele.status) !== -1 && ( |
| 196 | + <CheckOutlinedIcon /> |
| 197 | + )} |
| 198 | + {ele.label} |
| 199 | + </Dropdown.Item> |
| 200 | + ))} |
| 201 | + </Dropdown.Menu> |
| 202 | + </Dropdown> |
| 203 | + </div> |
| 204 | + </div> |
| 205 | + ); |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +export default Filter; |
0 commit comments