Home Reference Source

application/components/common/delete-button.js

  1. import React from 'react';
  2. import RaisedButton from 'material-ui/RaisedButton';
  3. import LinearProgress from 'material-ui/LinearProgress';
  4. import FontIcon from 'material-ui/FontIcon';
  5. import BaseView from './../base-view';
  6. import {language} from '../../lang/en/delete-button';
  7.  
  8. /**
  9. * Hold to delete button. Eschews traditional "Are you sure" box
  10. * by forcing the user to hold the button for about 10 seconds before deleteing.
  11. *
  12. * Parent component should provide onDelete property to handle when deletion is confirmed
  13. */
  14. class DeleteButton extends BaseView {
  15.  
  16. constructor(props) {
  17. super(props);
  18.  
  19. this.state = {
  20. 'active': false,
  21. 'progress': 0,
  22. 'interval': false,
  23. 'counter': 0
  24. };
  25.  
  26. this.handleDown = this.handleDown.bind(this);
  27. this.handleUp = this.handleUp.bind(this);
  28. this.handleInterval = this.handleInterval.bind(this);
  29. }
  30.  
  31. componentWillMount() {
  32. this.setState({'label': this.props.label ? this.props.label : language.rest()});
  33. }
  34.  
  35. handleInterval() {
  36. let counter = this.state.counter;
  37. counter++;
  38.  
  39. this.setState({'progress': counter*4});
  40.  
  41. if (counter === 30) {
  42. this.handleComplete();
  43. }
  44.  
  45. this.setState({'counter': counter});
  46. }
  47.  
  48. handleComplete() {
  49.  
  50. clearInterval(this.state.interval);
  51. this.setState({'label': language.complete(), 'active': false, 'progress': 100, 'interval': false});
  52.  
  53. const handler = (() => this.setState({'label': this.props.label ? this.props.label : language.rest(), 'counter': 0, 'progress': 0, 'active': false})).bind(this);
  54. setTimeout(() => {
  55. this.props.onDelete(handler);
  56. }, 1000);
  57. }
  58.  
  59. handleDown(e) {
  60.  
  61. if (this.props.disabled) {
  62. return;
  63. }
  64.  
  65. this.setState({'label': language.active(), 'active': true});
  66.  
  67. let interval = setInterval(this.handleInterval, 100)
  68. this.setState({'interval': interval});
  69. }
  70.  
  71. handleUp(e) {
  72. if (!this.state.active) {
  73. return;
  74. }
  75.  
  76. this.setState({'active': false, 'label': this.props.label ? this.props.label : language.rest()});
  77.  
  78. clearInterval(this.state.interval);
  79. this.setState({'interval': false, 'counter': 0, 'progress': 0});
  80. }
  81.  
  82. render() {
  83. return (
  84. <div className={'delete-button-root ' + this.props.className}>
  85. <RaisedButton
  86. className='delete-button'
  87. fullWidth={this.props.fullWidth}
  88. onMouseDown={this.handleDown}
  89. onMouseOut={this.handleUp}
  90. onMouseUp={this.handleUp}
  91. disabled={this.props.disabled}
  92. backgroundColor='#B71C1C'
  93. labelColor='#fff'
  94. label={this.state.label ? this.state.label : language.rest()}
  95. icon={this.props.icon ? <FontIcon className='mui-icons'>delete</FontIcon> : false}
  96. />
  97. {this.props.disabled ? '' : <LinearProgress mode="determinate" value={this.state.progress} /> }
  98. </div>
  99. );
  100. }
  101.  
  102. }
  103.  
  104. export default DeleteButton;