|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import './App.css'; |
| 3 | +var client = require('./connection.js'); |
| 4 | +var indexName = "crud"; |
| 5 | +var docType = "doc"; |
| 6 | +var payload; |
| 7 | +class App extends Component { |
| 8 | + constructor(props) { |
| 9 | + super(props); |
| 10 | + this.state = { |
| 11 | + id: "", |
| 12 | + nameValue: "", |
| 13 | + dataList: [] |
| 14 | + } |
| 15 | + } |
| 16 | + onChangeFunc = (event) => { |
| 17 | + this.setState({ |
| 18 | + nameValue: event.target.value |
| 19 | + }) |
| 20 | + } |
| 21 | + componentDidMount() { |
| 22 | + this.getElasticSearchData(); |
| 23 | + } |
| 24 | + getElasticSearchData = () => { |
| 25 | + var that = this; |
| 26 | + client.search({ |
| 27 | + index: indexName, |
| 28 | + type: docType, |
| 29 | + // refresh: true, |
| 30 | + // body: payload |
| 31 | + }).then(function (resp) { |
| 32 | + console.log(resp.hits.hits); |
| 33 | + that.setState({ |
| 34 | + dataList: resp.hits.hits |
| 35 | + }) |
| 36 | + }, function (err) { |
| 37 | + console.log(err.message); |
| 38 | + }); |
| 39 | + } |
| 40 | + onEditClick = (ch) => { |
| 41 | + this.setState({ |
| 42 | + nameValue: ch._source.message, |
| 43 | + id: ch._id |
| 44 | + }); |
| 45 | + } |
| 46 | + onDeleteClick = (ch) => { |
| 47 | + // this.setState({ |
| 48 | + // nameValue: ch._source.message, |
| 49 | + // id: ch._id |
| 50 | + // }); |
| 51 | + console.log("onDeleteClick :", ch) |
| 52 | + var that = this; |
| 53 | + client.delete({ |
| 54 | + index: indexName, |
| 55 | + type: docType, |
| 56 | + refresh: true, |
| 57 | + id: ch._id, |
| 58 | + }, function (err, resp) { |
| 59 | + if (err) { |
| 60 | + console.log(err) |
| 61 | + } else { |
| 62 | + that.getElasticSearchData(); |
| 63 | + } |
| 64 | + }); |
| 65 | + } |
| 66 | + handleKeyUp = (evt) => { |
| 67 | + var that = this; |
| 68 | + if (evt.keyCode === 13) { |
| 69 | + evt.persist(); |
| 70 | + if (this.state.id !== "") { |
| 71 | + payload = { |
| 72 | + "doc": { // for update this is required |
| 73 | + "message": evt.target.value |
| 74 | + } |
| 75 | + } |
| 76 | + var _id = this.state.id; |
| 77 | + client.update({ |
| 78 | + index: indexName, |
| 79 | + type: docType, |
| 80 | + refresh: true, |
| 81 | + id: _id, |
| 82 | + body: payload |
| 83 | + }, function (err, resp) { |
| 84 | + if (!err) { |
| 85 | + that.getElasticSearchData(); |
| 86 | + that.setState({ |
| 87 | + id: "", |
| 88 | + nameValue:"" |
| 89 | + }) |
| 90 | + } else { |
| 91 | + console.log(err); |
| 92 | + } |
| 93 | + }) |
| 94 | + } else { |
| 95 | + payload = { |
| 96 | + "user": "elastic", |
| 97 | + "post_date": "2010-01-15T01:46:38", |
| 98 | + "message": evt.target.value |
| 99 | + } |
| 100 | + //create documents |
| 101 | + client.index({ |
| 102 | + index: indexName, |
| 103 | + type: docType, |
| 104 | + refresh: true, |
| 105 | + // id: _id, |
| 106 | + body: payload |
| 107 | + }).then(function (resp) { |
| 108 | + that.getElasticSearchData(); |
| 109 | + that.setState({ |
| 110 | + id: "", |
| 111 | + nameValue:"" |
| 112 | + }) |
| 113 | + }, function (err) { |
| 114 | + console.log(err.message); |
| 115 | + }); |
| 116 | + } |
| 117 | + }; |
| 118 | + } |
| 119 | + render() { |
| 120 | + return ( |
| 121 | + <div className="container"> |
| 122 | + <div className="row justify-content-md-center"> |
| 123 | + <div className="col-md-8"> |
| 124 | + <center><h3 className="main-title">Channel List</h3></center> |
| 125 | + <hr /> |
| 126 | + <div className="input-group mb-3"> |
| 127 | + <input |
| 128 | + type="text" |
| 129 | + className="form-control" |
| 130 | + placeholder="Enter Channel" |
| 131 | + onKeyUp={this.handleKeyUp} |
| 132 | + value={this.state.nameValue} |
| 133 | + onChange={this.onChangeFunc} /> |
| 134 | + </div> |
| 135 | + |
| 136 | + </div> |
| 137 | + </div> |
| 138 | + <ul className="list-group"> |
| 139 | + {this.state.dataList.map(ch => |
| 140 | + <li key={"div_" + ch._id} className="list-group-item"> |
| 141 | + <label htmlFor="checkbox5"> |
| 142 | + {ch._source.message} |
| 143 | + </label> |
| 144 | + <div className="pull-right action-buttons"> |
| 145 | + <a onClick={() => this.onEditClick(ch)} href="#"><span className="fa fa-pencil"></span></a> |
| 146 | + <a onClick={() => this.onDeleteClick(ch)} className="trash" href="#" ><span className="fa fa-trash"></span></a> |
| 147 | + </div> |
| 148 | + </li> |
| 149 | + )} |
| 150 | + </ul> |
| 151 | + </div> |
| 152 | + |
| 153 | + ); |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +export default App; |
0 commit comments