|
| 1 | +const app = require('ampersand-app'); |
| 2 | +const React = require('react'); |
| 3 | +const Modal = require('react-bootstrap').Modal; |
| 4 | +const NamespaceStore = require('hadron-reflux-store').NamespaceStore; |
| 5 | +const toNS = require('mongodb-ns'); |
| 6 | +const TextButton = require('hadron-app-registry').TextButton; |
| 7 | +const Actions = require('../actions/collections-actions'); |
| 8 | +const DropCollectionStore = require('../stores/drop-collection-store'); |
| 9 | + |
| 10 | +/** |
| 11 | + * The dialog to drop a database. |
| 12 | + */ |
| 13 | +class DropCollectionDialog extends React.Component { |
| 14 | + |
| 15 | + /** |
| 16 | + * The component constructor. |
| 17 | + * |
| 18 | + * @param {Object} props - The properties. |
| 19 | + */ |
| 20 | + constructor(props) { |
| 21 | + super(props); |
| 22 | + this.state = { name: '', confirmName: '' }; |
| 23 | + this.ModalStatusMessage = app.appRegistry.getComponent('App.ModalStatusMessage'); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Subscribe to the open dialog store. |
| 28 | + */ |
| 29 | + componentWillMount() { |
| 30 | + this.unsubscribeOpen = Actions.openDropCollectionDialog.listen(this.onOpenDialog.bind(this)); |
| 31 | + this.unsubscribeDrop = DropCollectionStore.listen(this.onCollectionDropped.bind(this)); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Unsubscribe from the store. |
| 36 | + */ |
| 37 | + componentWillUnmount() { |
| 38 | + this.unsubscribeOpen(); |
| 39 | + this.unsubscribeDrop(); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * When the open dialog action is fired. |
| 44 | + * |
| 45 | + * @param {String} name - The name of the database to drop. |
| 46 | + */ |
| 47 | + onOpenDialog(name) { |
| 48 | + this.setState({ |
| 49 | + open: true, |
| 50 | + name: name, |
| 51 | + confirmName: '', |
| 52 | + databaseName: toNS(NamespaceStore.ns).database |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * When the cancel button is clicked. |
| 58 | + */ |
| 59 | + onCancelButtonClicked() { |
| 60 | + this.setState({ open: false }); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Initiate the attempt to drop a database. |
| 65 | + */ |
| 66 | + onDropCollectionButtonClicked() { |
| 67 | + this.setState({ inProgress: true, error: false, errorMessage: '' }); |
| 68 | + Actions.dropCollection(this.state.databaseName, this.state.name); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Handle finish database dropping. |
| 73 | + * |
| 74 | + * @param {Error} error - The error, if any. |
| 75 | + */ |
| 76 | + onCollectionDropped(error) { |
| 77 | + if (error) { |
| 78 | + this.setState({ inProgress: false, error: true, errorMessage: error.message }); |
| 79 | + } else { |
| 80 | + this.setState({ inProgress: false, error: false, errorMessage: '', open: false }); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Fires when the confirmation name is changed. |
| 86 | + * |
| 87 | + * @param {Event} evt - The change event. |
| 88 | + */ |
| 89 | + onConfirmNameChanged(evt) { |
| 90 | + this.setState({ confirmName: evt.target.value }); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Render the modal dialog. |
| 95 | + * |
| 96 | + * @returns {React.Component} The react component. |
| 97 | + */ |
| 98 | + render() { |
| 99 | + return ( |
| 100 | + <Modal show={this.state.open} backdrop="static" keyboard={false} dialogClassName="drop-collection-dialog"> |
| 101 | + <Modal.Header> |
| 102 | + <Modal.Title>Drop Collection</Modal.Title> |
| 103 | + </Modal.Header> |
| 104 | + |
| 105 | + <Modal.Body> |
| 106 | + <div> |
| 107 | + <p className="drop-confirm-message"> |
| 108 | + <i className="drop-confirm-icon fa fa-exclamation-triangle" aria-hidden="true"></i> |
| 109 | + Type the collection name |
| 110 | + <strong> {this.state.name} </strong> |
| 111 | + to drop |
| 112 | + </p> |
| 113 | + </div> |
| 114 | + <form> |
| 115 | + <div className="form-group"> |
| 116 | + <input |
| 117 | + type="text" |
| 118 | + className="drop-confirm-input form-control" |
| 119 | + value={this.state.confirmName} |
| 120 | + onChange={this.onConfirmNameChanged.bind(this)} /> |
| 121 | + </div> |
| 122 | + {this.state.error ? |
| 123 | + <this.ModalStatusMessage icon="times" message={this.state.errorMessage} type="error" /> |
| 124 | + : null} |
| 125 | + {this.state.inProgress ? |
| 126 | + <this.ModalStatusMessage icon="align-center" message={'Drop in Progress'} type="in-progress" /> |
| 127 | + : null} |
| 128 | + </form> |
| 129 | + </Modal.Body> |
| 130 | + |
| 131 | + <Modal.Footer> |
| 132 | + <TextButton |
| 133 | + className="btn btn-default" |
| 134 | + text="Cancel" |
| 135 | + clickHandler={this.onCancelButtonClicked.bind(this)} /> |
| 136 | + <button |
| 137 | + className="btn btn-primary" |
| 138 | + disabled={this.state.confirmName !== this.state.name} |
| 139 | + onClick={this.onDropCollectionButtonClicked.bind(this)}> |
| 140 | + Drop Collection |
| 141 | + </button> |
| 142 | + </Modal.Footer> |
| 143 | + </Modal> |
| 144 | + ); |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +DropCollectionDialog.displayName = 'DropCollectionDialog'; |
| 149 | + |
| 150 | +module.exports = DropCollectionDialog; |
0 commit comments