/app/src/content/components/Notification/Notification.jsx

https://bitbucket.org/Linstructor/sharettenotification · JSX · 116 lines · 105 code · 11 blank · 0 comment · 4 complexity · 4805c6a49967f5cf259ef5dcbef41e27 MD5 · raw file

  1. import React, { Component } from 'react';
  2. import styles from './style.css';
  3. import PropTypes from 'prop-types';
  4. import { ipcRenderer, remote } from 'electron';
  5. import connect from 'react-redux/es/connect/connect';
  6. const closeTO = setTimeout(
  7. () => /*remote.getCurrentWindow().close()*/ console.log('Seems ok'),
  8. 7000,
  9. );
  10. class Notification extends Component {
  11. constructor(props) {
  12. super(props);
  13. this.state = { response: '' };
  14. this.enterPressed = this.enterPressed.bind(this);
  15. this.sendButton = React.createRef();
  16. this.responseInput = React.createRef();
  17. }
  18. sendResponse() {
  19. ipcRenderer.send('notifRespond', {
  20. response: this.state.response,
  21. notifId: this.props.store.notifId,
  22. });
  23. remote.getCurrentWindow().close();
  24. }
  25. handleResponse(event) {
  26. if (event.target.value.length === 1) {
  27. this.setState({ ...this.state, response: event.target.value.toUpperCase() });
  28. } else {
  29. this.setState({ ...this.state, response: event.target.value });
  30. }
  31. }
  32. enterPressed(event) {
  33. if (event.key === 'Enter') {
  34. if (this.state.response !== '') this.sendButton.current.click();
  35. }
  36. }
  37. componentDidMount() {
  38. ipcRenderer.send('notifContent', remote.getCurrentWindow().id);
  39. }
  40. onHover() {
  41. clearTimeout(closeTO);
  42. this.responseInput.current.focus();
  43. }
  44. render() {
  45. return (
  46. <div className={styles.notif} onMouseOver={this.onHover.bind(this)} onKeyPress={this.enterPressed}>
  47. <a
  48. className={styles.close_button + ' material-icons'}
  49. onClick={() => {
  50. const window = remote.getCurrentWindow();
  51. window.close();
  52. }}
  53. >
  54. close
  55. </a>
  56. <div className={styles.flex_row}>
  57. {this.props.store.hasImage ? <img className={styles.img_notif} /> : null}
  58. <div className={styles.notif_text}>
  59. <p className={styles.notif_text_title}>{this.props.store.title}</p>
  60. <p className={styles.notif_text_content}>{this.props.store.description}</p>
  61. </div>
  62. </div>
  63. {this.props.store.isReplyable ? (
  64. <div className={styles.input_container}>
  65. <div className={styles.flex_row}>
  66. <input
  67. type="text"
  68. className={styles.input_notif}
  69. placeholder="Respond"
  70. onChange={this.handleResponse.bind(this)}
  71. ref={this.responseInput}
  72. value={this.state.response}
  73. />
  74. <a
  75. className={styles.send_button + ' ' + 'material-icons'}
  76. onClick={this.sendResponse.bind(this)}
  77. ref={this.sendButton}
  78. >
  79. send
  80. </a>
  81. </div>
  82. </div>
  83. ) : null}
  84. </div>
  85. );
  86. }
  87. }
  88. Notification.propTypes = {
  89. title: PropTypes.string,
  90. content: PropTypes.string,
  91. hasImage: PropTypes.bool,
  92. isReplyable: PropTypes.bool,
  93. };
  94. const mapStateToProps = state => {
  95. return {
  96. store: {
  97. title: state.title,
  98. description: state.description,
  99. hasImage: state.hasImage,
  100. isReplyable: state.isReplyable,
  101. notifId: state.notifId,
  102. },
  103. };
  104. };
  105. export default connect(mapStateToProps)(Notification);