TableRiwayat.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import DataTable from 'react-data-table-component';
  2. import Datatable from "@/components/Tables/Datatable";
  3. import Link from "next/link";
  4. import moment from "moment";
  5. import React, { Component } from "react";
  6. import { getLog2 } from "@/actions/log";
  7. import { connect } from "react-redux";
  8. import { Row, Col, Input, FormGroup, Label, Progress, Button } from "reactstrap";
  9. import Datetime from "react-datetime";
  10. class TableRiwayat extends Component {
  11. constructor(props) {
  12. super(props);
  13. this.state = {
  14. dataRiwayat: [],
  15. dataRiwayatsort: [],
  16. fromDate: "",
  17. toDate: "",
  18. updateTable: false,
  19. search: "",
  20. dataFilterSearch: []
  21. };
  22. }
  23. componentDidMount = async () => {
  24. const { token } = this.props;
  25. const data = await getLog2(this.props.token)
  26. const dataRiwayat = data.data
  27. this.setState({ dataRiwayat });
  28. }
  29. fetchData = async () => {
  30. const date1 = moment(this.state.fromDate).format("YYYY-MM-DD")
  31. const date2 = moment(this.state.toDate).format("YYYY-MM-DD")
  32. const data = await getLog2(this.props.token, { fromDate: date1, toDate: date2 })
  33. const dataFilterSearch = data.data
  34. this.setState({ dataFilterSearch: dataFilterSearch, dataRiwayat: dataFilterSearch });
  35. // this.setState({ dataRiwayat });
  36. }
  37. onFilter = () => {
  38. this.setState({ loading: true });
  39. this.fetchData(); 21
  40. this.setState({ loading: false });
  41. if (this.state.dataRiwayat && this.state.dataRiwayat.length) {
  42. // this.renderTableData();
  43. this.setState({ noData: true });
  44. }
  45. }
  46. handleSearchChange = (e) => {
  47. const { dataRiwayat } = this.state;
  48. const searchValue = e.target.value;
  49. const dataNoFilter = dataRiwayat
  50. const result = dataRiwayat.filter((item) =>
  51. item.aktivitas.toLowerCase().includes(searchValue.toLowerCase())
  52. || item?.timestamp?.toLowerCase().includes(searchValue.toLowerCase())
  53. || item?.username?.toLowerCase().includes(searchValue.toLowerCase())
  54. || item?.ipv4?.toLowerCase().includes(searchValue.toLowerCase())
  55. || item?.menu?.toLowerCase().includes(searchValue.toLowerCase())
  56. );
  57. this.setState({
  58. search: searchValue,
  59. })
  60. if (result.length) {
  61. this.setState({
  62. dataFilterSearch: result
  63. });
  64. }
  65. else {
  66. this.setState({
  67. dataFilterSearch: dataNoFilter
  68. })
  69. }
  70. }
  71. render() {
  72. const { dataRiwayat, search, dataFilterSearch } = this.state
  73. const columns = [
  74. {
  75. name: 'Timestamp',
  76. // selector: row => row.timestamp,
  77. cell: row => (
  78. <div>
  79. <div>
  80. <div
  81. data-tag="allowRowEvents"
  82. style={{ color: 'grey', whiteSpace: 'wrap', textOverflow: 'ellipses', width: '100px' }}
  83. >
  84. { }
  85. {row.timestamp}
  86. </div>
  87. </div>
  88. </div>
  89. )
  90. },
  91. {
  92. name: 'UserName',
  93. // selector: row => row.username,
  94. cell: row => (
  95. <div>
  96. <div>
  97. <div
  98. data-tag="allowRowEvents"
  99. style={{ color: 'grey', whiteSpace: 'wrap', textOverflow: 'ellipses', width: '100px' }}
  100. >
  101. { }
  102. {row.username}
  103. </div>
  104. </div>
  105. </div>
  106. )
  107. },
  108. {
  109. name: 'IPv4',
  110. // selector: row => row.ipv4,
  111. cell: row => (
  112. <div>
  113. <div>
  114. <div
  115. data-tag="allowRowEvents"
  116. style={{ color: 'grey', whiteSpace: 'wrap', textOverflow: 'ellipses', width: '100px' }}
  117. >
  118. { }
  119. {row.ipv4}
  120. </div>
  121. </div>
  122. </div>
  123. )
  124. },
  125. {
  126. name: 'Menu',
  127. // selector: row => row.menu,
  128. cell: row => (
  129. <div>
  130. <div>
  131. <div
  132. data-tag="allowRowEvents"
  133. style={{ color: 'grey', whiteSpace: 'wrap', width: '100px' }}
  134. >
  135. {row.menu||"-"}
  136. </div>
  137. </div>
  138. </div>
  139. )
  140. },
  141. {
  142. name: 'Deskripsi',
  143. // selector: row => row.aktivitas,
  144. cell: row => (
  145. <div>
  146. <div>
  147. <div
  148. data-tag="allowRowEvents"
  149. style={{ color: 'grey', overflow: 'hidden', whiteSpace: 'wrap', textOverflow: 'ellipses', width: '300px' }}
  150. >
  151. { }
  152. {row.aktivitas}
  153. </div>
  154. </div>
  155. </div>
  156. ),
  157. },
  158. ];
  159. const data = dataRiwayat?.map((value, index) => (
  160. {
  161. timestamp: value.createdAt,
  162. username: value.user?.nama,
  163. ipv4: value?.ipv4,
  164. menu: value?.menu,
  165. aktivitas: value?.aktivitas,
  166. }
  167. ))
  168. const datasearch = dataFilterSearch?.map((value, index) => (
  169. {
  170. timestamp: value.createdAt,
  171. username: value.user?.nama,
  172. ipv4: value?.ipv4,
  173. menu: value?.menu,
  174. aktivitas: value?.aktivitas,
  175. }
  176. ))
  177. return (
  178. <div className="card b ">
  179. <div className="row card-body card-over">
  180. <div className=" pl-4 col-2 float-left">
  181. <label className="col-form-label">Range Filter : </label>
  182. <span>
  183. <Datetime
  184. timeFormat={false}
  185. inputProps={{ className: "form-control" }}
  186. closeOnSelect={true}
  187. value={this.state.fromDate ? moment(this.state.fromDate).format("DD-MM-YYYY") : "DD/MM/YYYY"}
  188. onChange={(fromDate) => {
  189. this.setState({ fromDate })
  190. }}
  191. className=' mb-3'
  192. />
  193. </span>
  194. <span>
  195. <Datetime
  196. timeFormat={false}
  197. inputProps={{ className: "form-control" }}
  198. closeOnSelect={true}
  199. value={this.state.toDate ? moment(this.state.toDate).format("DD-MM-YYYY") : "DD/MM/YYYY"}
  200. onChange={(toDate) => {
  201. this.setState({ toDate })
  202. }}
  203. // className=' w-75'
  204. />
  205. </span>
  206. <Button color="info" className=" mt-4" onClick={(e) => this.onFilter()}>Filter</Button>
  207. </div>
  208. <div className="col-10">
  209. <DataTable data={datasearch.length ? datasearch : data} columns={columns}
  210. defaultSortField="title"
  211. pagination
  212. // selectableRows
  213. subHeader
  214. subHeaderComponent={
  215. <input
  216. type="text"
  217. className="w-25 form-control"
  218. placeholder="Search..."
  219. value={search}
  220. onChange={this.handleSearchChange}
  221. />
  222. }
  223. subHeaderAlign="right"
  224. />
  225. </div>
  226. </div>
  227. </div >
  228. );
  229. }
  230. }
  231. const mapStateToProps = (state) => ({ token: state.token });
  232. export default connect(mapStateToProps)(TableRiwayat);