|
| 1 | +import Dropdown from 'components/Dropdown/Dropdown.react'; |
| 2 | +import Field from 'components/Field/Field.react'; |
| 3 | +import Label from 'components/Label/Label.react'; |
| 4 | +import Modal from 'components/Modal/Modal.react'; |
| 5 | +import Option from 'components/Dropdown/Option.react'; |
| 6 | +import React from 'react'; |
| 7 | +import TextInput from 'components/TextInput/TextInput.react'; |
| 8 | +import Checkbox from 'components/Checkbox/Checkbox.react'; |
| 9 | + |
| 10 | +function isValidJSON(value) { |
| 11 | + try { |
| 12 | + JSON.parse(value); |
| 13 | + return true; |
| 14 | + } catch { |
| 15 | + return false; |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +export default class EditViewDialog extends React.Component { |
| 20 | + constructor(props) { |
| 21 | + super(); |
| 22 | + const view = props.view || {}; |
| 23 | + this.state = { |
| 24 | + name: view.name || '', |
| 25 | + className: view.className || '', |
| 26 | + query: JSON.stringify(view.query || [], null, 2), |
| 27 | + showCounter: !!view.showCounter, |
| 28 | + }; |
| 29 | + } |
| 30 | + |
| 31 | + valid() { |
| 32 | + return ( |
| 33 | + this.state.name.length > 0 && |
| 34 | + this.state.className.length > 0 && |
| 35 | + isValidJSON(this.state.query) |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + render() { |
| 40 | + const { classes, onConfirm, onCancel } = this.props; |
| 41 | + return ( |
| 42 | + <Modal |
| 43 | + type={Modal.Types.INFO} |
| 44 | + icon="edit-solid" |
| 45 | + iconSize={40} |
| 46 | + title="Edit view?" |
| 47 | + subtitle="Update the custom query." |
| 48 | + confirmText="Save" |
| 49 | + cancelText="Cancel" |
| 50 | + disabled={!this.valid()} |
| 51 | + onCancel={onCancel} |
| 52 | + onConfirm={() => |
| 53 | + onConfirm({ |
| 54 | + name: this.state.name, |
| 55 | + className: this.state.className, |
| 56 | + query: JSON.parse(this.state.query), |
| 57 | + showCounter: this.state.showCounter, |
| 58 | + }) |
| 59 | + } |
| 60 | + > |
| 61 | + <Field |
| 62 | + label={<Label text="Name" />} |
| 63 | + input={ |
| 64 | + <TextInput |
| 65 | + value={this.state.name} |
| 66 | + onChange={name => this.setState({ name })} |
| 67 | + /> |
| 68 | + } |
| 69 | + /> |
| 70 | + <Field |
| 71 | + label={<Label text="Class" />} |
| 72 | + input={ |
| 73 | + <Dropdown |
| 74 | + value={this.state.className} |
| 75 | + onChange={className => this.setState({ className })} |
| 76 | + > |
| 77 | + {classes.map(c => ( |
| 78 | + <Option key={c} value={c}> |
| 79 | + {c} |
| 80 | + </Option> |
| 81 | + ))} |
| 82 | + </Dropdown> |
| 83 | + } |
| 84 | + /> |
| 85 | + <Field |
| 86 | + label={ |
| 87 | + <Label |
| 88 | + text="Query" |
| 89 | + description="An aggregation pipeline that returns an array of items." |
| 90 | + /> |
| 91 | + } |
| 92 | + input={ |
| 93 | + <TextInput |
| 94 | + multiline={true} |
| 95 | + value={this.state.query} |
| 96 | + onChange={query => this.setState({ query })} |
| 97 | + /> |
| 98 | + } |
| 99 | + /> |
| 100 | + <Field |
| 101 | + label={<Label text="Show object counter" />} |
| 102 | + input={ |
| 103 | + <Checkbox |
| 104 | + checked={this.state.showCounter} |
| 105 | + onChange={showCounter => this.setState({ showCounter })} |
| 106 | + /> |
| 107 | + } |
| 108 | + /> |
| 109 | + </Modal> |
| 110 | + ); |
| 111 | + } |
| 112 | +} |
0 commit comments