|
| 1 | +const app = require('ampersand-app'); |
| 2 | +const shell = require('electron').shell; |
| 3 | +const React = require('react'); |
| 4 | +const Modal = require('react-bootstrap').Modal; |
| 5 | +const TextButton = require('hadron-app-registry').TextButton; |
| 6 | +const NamespaceStore = require('hadron-reflux-store').NamespaceStore; |
| 7 | +const toNS = require('mongodb-ns'); |
| 8 | +const Actions = require('../actions/collections-actions'); |
| 9 | +const CreateCollectionStore = require('../stores/create-collection-store'); |
| 10 | +const CreateCollectionInput = require('./create-collection-input'); |
| 11 | +const CreateCollectionSizeInput = require('./create-collection-size-input'); |
| 12 | +const CreateCollectionCheckbox = require('./create-collection-checkbox'); |
| 13 | + |
| 14 | +/** |
| 15 | + * The help icon for capped collections url. |
| 16 | + */ |
| 17 | +const HELP_URL = 'https://docs.mongodb.com/manual/core/capped-collections/'; |
| 18 | + |
| 19 | +/** |
| 20 | + * The dialog to create a collection. |
| 21 | + */ |
| 22 | +class CreateCollectionDialog extends React.Component { |
| 23 | + |
| 24 | + /** |
| 25 | + * The component constructor. |
| 26 | + * |
| 27 | + * @param {Object} props - The properties. |
| 28 | + */ |
| 29 | + constructor(props) { |
| 30 | + super(props); |
| 31 | + this.state = { open: false }; |
| 32 | + this.ModalStatusMessage = app.appRegistry.getComponent('App.ModalStatusMessage'); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Subscribe to the open dialog store. |
| 37 | + */ |
| 38 | + componentWillMount() { |
| 39 | + this.unsubscribeOpen = Actions.openCreateCollectionDialog.listen(this.onOpenDialog.bind(this)); |
| 40 | + this.unsubscribeCreate = CreateCollectionStore.listen(this.onCollectionCreated.bind(this)); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Unsubscribe from the store. |
| 45 | + */ |
| 46 | + componentWillUnmount() { |
| 47 | + this.unsubscribeOpen(); |
| 48 | + this.unsubscribeCreate(); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * When the open dialog action is fired. |
| 53 | + */ |
| 54 | + onOpenDialog() { |
| 55 | + this.setState({ |
| 56 | + open: true, |
| 57 | + collectionName: '', |
| 58 | + databaseName: toNS(NamespaceStore.ns).database, |
| 59 | + capped: false, |
| 60 | + maxSize: '', |
| 61 | + error: false, |
| 62 | + inProgress: false, |
| 63 | + errorMessage: '' |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * When the cancel button is clicked. |
| 69 | + */ |
| 70 | + onCancelButtonClicked() { |
| 71 | + this.setState({ open: false }); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Initiate the attempt to create a collection. |
| 76 | + */ |
| 77 | + onCreateCollectionButtonClicked() { |
| 78 | + this.setState({ inProgress: true, error: false, errorMessage: '' }); |
| 79 | + Actions.createCollection( |
| 80 | + this.state.databaseName, |
| 81 | + this.state.collectionName, |
| 82 | + this.state.capped, |
| 83 | + this.state.maxSize |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Handle finish collection creation. |
| 89 | + * |
| 90 | + * @param {Error} error - The error, if any. |
| 91 | + */ |
| 92 | + onCollectionCreated(error) { |
| 93 | + if (error) { |
| 94 | + this.setState({ inProgress: false, error: true, errorMessage: error.message }); |
| 95 | + } else { |
| 96 | + this.setState({ inProgress: false, error: false, errorMessage: '', open: false }); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Handle changing the collection name. |
| 102 | + * |
| 103 | + * @param {Event} evt - The change event. |
| 104 | + */ |
| 105 | + onCollectionNameChange(evt) { |
| 106 | + this.setState({ collectionName: evt.target.value }); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Handle clicking the capped checkbox. |
| 111 | + */ |
| 112 | + onCappedClicked() { |
| 113 | + this.setState({ capped: !this.state.capped }); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Handle clicking the help icon. |
| 118 | +
|
| 119 | + * @param {Event} evt - The event. |
| 120 | + */ |
| 121 | + onHelpClicked(evt) { |
| 122 | + evt.preventDefault(); |
| 123 | + evt.stopPropagation(); |
| 124 | + shell.openExternal(HELP_URL); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Change the max collection size. |
| 129 | + * |
| 130 | + * @param {Event} evt - The event. |
| 131 | + */ |
| 132 | + onMaxSizeChange(evt) { |
| 133 | + this.setState({ maxSize: evt.target.value }); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Render the max size component when capped is selected. |
| 138 | + * |
| 139 | + * @returns {React.Component} The component. |
| 140 | + */ |
| 141 | + renderMaxSize() { |
| 142 | + if (this.state.capped) { |
| 143 | + return ( |
| 144 | + <CreateCollectionSizeInput |
| 145 | + name="bytes max" |
| 146 | + placeholder="Enter max bytes" |
| 147 | + value={this.state.maxSize} |
| 148 | + onChangeHandler={this.onMaxSizeChange.bind(this)} /> |
| 149 | + ); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Render the modal dialog. |
| 155 | + * |
| 156 | + * @returns {React.Component} The react component. |
| 157 | + */ |
| 158 | + render() { |
| 159 | + return ( |
| 160 | + <Modal show={this.state.open} backdrop="static" keyboard={false} dialogClassName="create-collection-dialog"> |
| 161 | + <Modal.Header> |
| 162 | + <Modal.Title>Create Collection</Modal.Title> |
| 163 | + </Modal.Header> |
| 164 | + |
| 165 | + <Modal.Body> |
| 166 | + <form name="create-collection-dialog-form"> |
| 167 | + <CreateCollectionInput |
| 168 | + name="Collection Name" |
| 169 | + value={this.state.collectionName} |
| 170 | + onChangeHandler={this.onCollectionNameChange.bind(this)} /> |
| 171 | + <CreateCollectionCheckbox |
| 172 | + name="Capped Collection" |
| 173 | + className="create-collection-dialog-capped" |
| 174 | + checked={this.state.checked} |
| 175 | + onClickHandler={this.onCappedClicked.bind(this)} |
| 176 | + onHelpClickHandler={this.onHelpClicked.bind(this)} /> |
| 177 | + {this.renderMaxSize()} |
| 178 | + {this.state.error ? |
| 179 | + <this.ModalStatusMessage icon="times" message={this.state.errorMessage} type="error" /> |
| 180 | + : null} |
| 181 | + {this.state.inProgress ? |
| 182 | + <this.ModalStatusMessage icon="align-center" message={'Create in Progress'} type="in-progress" /> |
| 183 | + : null} |
| 184 | + </form> |
| 185 | + </Modal.Body> |
| 186 | + |
| 187 | + <Modal.Footer> |
| 188 | + <TextButton |
| 189 | + className="btn btn-default" |
| 190 | + text="Cancel" |
| 191 | + clickHandler={this.onCancelButtonClicked.bind(this)} /> |
| 192 | + <TextButton |
| 193 | + className="btn btn-primary" |
| 194 | + text="Create Collection" |
| 195 | + clickHandler={this.onCreateCollectionButtonClicked.bind(this)} /> |
| 196 | + </Modal.Footer> |
| 197 | + </Modal> |
| 198 | + ); |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +CreateCollectionDialog.displayName = 'CreateCollectionDialog'; |
| 203 | + |
| 204 | +module.exports = CreateCollectionDialog; |
0 commit comments