|
1 | 1 | // @flow |
2 | 2 |
|
3 | 3 | import React from 'react' |
4 | | -import { Modal } from 'react-bootstrap' |
| 4 | +import { |
| 5 | + Button, |
| 6 | + Col, |
| 7 | + ControlLabel, |
| 8 | + FormControl, |
| 9 | + FormGroup, |
| 10 | + HelpBlock, |
| 11 | + ListGroup, |
| 12 | + ListGroupItem, |
| 13 | + Panel, |
| 14 | + Row |
| 15 | +} from 'react-bootstrap' |
| 16 | +import update from 'react-addons-update' |
5 | 17 |
|
6 | | -type LabelEditorProps = { |
7 | | - label?: Label, |
8 | | -} |
| 18 | +import {validationState} from '../util' |
| 19 | +import type {ManagerUserState} from '../../types/reducers' |
9 | 20 |
|
10 | | -type LabelEditorState = { |
11 | | - showModal: boolean, |
12 | | -} |
| 21 | +// TODO: props |
| 22 | +export default class LabelEditor extends React.Component { |
| 23 | + state = { |
| 24 | + validation: { |
| 25 | + name: true |
| 26 | + }, |
| 27 | + newLabel: {...this.props.label} |
| 28 | + } |
13 | 29 |
|
14 | | -export function LabelEditor ({label}) { |
15 | | - return <div>{label.name}</div> |
16 | | -} |
17 | | -export class LabelEditorModal extends React.Component<LabelEditorProps, LabelEditorState> { |
18 | | - state = { |
19 | | - showModal: false |
20 | | - } |
| 30 | + componentDidMount = () => { |
| 31 | + // If we didn't get a label passed in, declare the label to be new |
| 32 | + if (!this.props.label) { |
| 33 | + this.setState({ |
| 34 | + labelIsNew: true |
| 35 | + }) |
| 36 | + } |
| 37 | + } |
21 | 38 |
|
22 | | - close = () => { |
23 | | - this.setState({ |
24 | | - showModal: false |
25 | | - }) |
26 | | - } |
| 39 | + userIsAdmin = (user: ManagerUserState) => { |
| 40 | + const {permissions} = user |
| 41 | + if (!permissions) return false |
| 42 | + else return permissions.isApplicationAdmin() || permissions.canAdministerAnOrganization() |
| 43 | + } |
| 44 | + |
| 45 | + _onFormChange = ({target}: {target: HTMLInputElement}) => { |
| 46 | + const {name, value, type, checked} = target |
| 47 | + const universalValue = type === 'checkbox' ? checked : value |
| 48 | + const valid = type === 'checkbox' ? true : universalValue.length > 0 |
| 49 | + |
| 50 | + this.setState( |
| 51 | + update(this.state, { |
| 52 | + newLabel: { $merge: {[name]: universalValue} }, |
| 53 | + validation: { [name]: { $set: universalValue !== undefined && valid } } |
| 54 | + }) |
| 55 | + ) |
| 56 | + } |
27 | 57 |
|
28 | | - open = () => { |
29 | | - this.setState({ |
30 | | - showModal: true |
| 58 | + _getChanges = () => { |
| 59 | + const newLabel = this.state.newLabel |
| 60 | + const oldLabel = this.props.label || {} |
| 61 | + const changes: any = {} |
| 62 | + Object.keys(oldLabel).map(k => { |
| 63 | + if (oldLabel[k] !== newLabel[k]) { |
| 64 | + changes[k] = oldLabel[k] |
| 65 | + } |
31 | 66 | }) |
| 67 | + return changes |
32 | 68 | } |
| 69 | + _settingsAreUnedited = () => Object.keys(this._getChanges()).length === 0 |
33 | 70 |
|
34 | | - ok = () => { |
35 | | - this.close() |
| 71 | + _formIsValid = () => { |
| 72 | + const {validation} = this.state |
| 73 | + return Object.keys(validation).every(k => validation[k]) |
| 74 | + } |
| 75 | + |
| 76 | + _onSaveSettings = () => { |
| 77 | + const {labelIsNew, newLabel: label} = this.state |
| 78 | + const {createLabel, updateLabel} = this.props |
| 79 | + |
| 80 | + if (labelIsNew) { |
| 81 | + createLabel(label) |
| 82 | + } else { |
| 83 | + updateLabel(label) |
| 84 | + } |
| 85 | + |
| 86 | + // This passed method will close the editor |
| 87 | + this.props.onDone() |
36 | 88 | } |
37 | 89 |
|
38 | 90 | render () { |
39 | | - const {Body, Header, Title} = Modal |
40 | | - const {label} = this.props |
41 | | - return ( |
42 | | - <Modal show={this.state.showModal} onHide={this.close}> |
43 | | - <Header> |
44 | | - <Title>Editing {label.name ? label.name : 'New Label'}</Title> |
45 | | - </Header> |
46 | | - |
47 | | - <Body> |
48 | | - <LabelEditor label={label} /> |
49 | | - </Body> |
50 | | - </Modal> |
51 | | - ) |
| 91 | + const {newLabel, validation} = this.state |
| 92 | + const {user} = this.props |
| 93 | + |
| 94 | + return <div> |
| 95 | + <Panel> |
| 96 | + |
| 97 | + <ListGroup fill> |
| 98 | + <ListGroupItem> |
| 99 | + <FormGroup |
| 100 | + data-test-id='label-name' |
| 101 | + validationState={validationState(validation.name)} |
| 102 | + > |
| 103 | + |
| 104 | + <ControlLabel>Name</ControlLabel> |
| 105 | + <FormControl |
| 106 | + value={newLabel.name} |
| 107 | + name={'name'} |
| 108 | + onChange={this._onFormChange} |
| 109 | + /> |
| 110 | + <FormControl.Feedback /> |
| 111 | + <HelpBlock>Required.</HelpBlock> |
| 112 | + </FormGroup> |
| 113 | + </ListGroupItem> |
| 114 | + <ListGroupItem> |
| 115 | + <Row> |
| 116 | + <Col xs={3}> |
| 117 | + <FormGroup |
| 118 | + data-test-id='label-color' |
| 119 | + > |
| 120 | + <ControlLabel>Color</ControlLabel> |
| 121 | + <FormControl |
| 122 | + value={newLabel.color} |
| 123 | + name={'color'} |
| 124 | + type='color' |
| 125 | + onChange={this._onFormChange} |
| 126 | + /> |
| 127 | + <FormControl.Feedback /> |
| 128 | + </FormGroup> |
| 129 | + </Col> |
| 130 | + <Col xs={9}> |
| 131 | + {this.userIsAdmin(user) |
| 132 | + ? <FormGroup |
| 133 | + data-test-id='label-adminOnly' |
| 134 | + > |
| 135 | + <ControlLabel>Visible to admins only?</ControlLabel> |
| 136 | + <Row> |
| 137 | + <Col xs={3}> |
| 138 | + <FormControl |
| 139 | + checked={newLabel.adminOnly} |
| 140 | + name={'adminOnly'} |
| 141 | + type='checkbox' |
| 142 | + onChange={this._onFormChange} |
| 143 | + /> |
| 144 | + </Col> |
| 145 | + <Col xs={9}> |
| 146 | + <div>If checked, this label will only be visible to project and application admins. Users with feed-specific priveleges will not see this label applied to any feed sources.</div> |
| 147 | + </Col> |
| 148 | + </Row> |
| 149 | + </FormGroup> |
| 150 | + : ''} |
| 151 | + </Col> |
| 152 | + </Row> |
| 153 | + </ListGroupItem> |
| 154 | + </ListGroup> |
| 155 | + </Panel> |
| 156 | + <Row> |
| 157 | + <Col xs={12}> |
| 158 | + {/* Cancel button */} |
| 159 | + <Button |
| 160 | + onClick={this.props.onDone} |
| 161 | + style={{marginRight: 10}} |
| 162 | + > |
| 163 | + Cancel |
| 164 | + </Button> |
| 165 | + {/* Save button */} |
| 166 | + <Button |
| 167 | + bsStyle='primary' |
| 168 | + data-test-id='label-form-save-button' |
| 169 | + disabled={ |
| 170 | + this._settingsAreUnedited() || |
| 171 | + !this._formIsValid() |
| 172 | + } |
| 173 | + onClick={this._onSaveSettings}> |
| 174 | + Save |
| 175 | + </Button> |
| 176 | + </Col> |
| 177 | + </Row> |
| 178 | + </div> |
52 | 179 | } |
53 | 180 | } |
0 commit comments