github.com/stampzilla/stampzilla-go@v2.0.0-rc9+incompatible/nodes/stampzilla-server/web/src/routes/automation/Schedule.js (about) 1 import React, { Component } from 'react'; 2 import { Button } from 'reactstrap'; 3 import { connect } from 'react-redux'; 4 import Form from 'react-jsonschema-form'; 5 6 import { add, save } from '../../ducks/schedules'; 7 import Card from '../../components/Card'; 8 import SavedStateWidget from './components/SavedStatePicker'; 9 import { 10 ArrayFieldTemplate, 11 CustomCheckbox, 12 ObjectFieldTemplate, 13 } from './components/formComponents'; 14 15 const schema = { 16 type: 'object', 17 required: ['name'], 18 properties: { 19 name: { 20 type: 'string', 21 title: 'Name', 22 }, 23 enabled: { 24 type: 'boolean', 25 title: 'Enabled', 26 description: 'Turn on and off this schedule', 27 }, 28 when: { 29 type: 'string', 30 title: 'When', 31 description: 'Cron formated when line [second minute hour day month dow]', 32 }, 33 actions: { 34 type: 'array', 35 title: 'Actions', 36 items: { 37 type: 'string', 38 }, 39 }, 40 }, 41 }; 42 const uiSchema = { 43 config: { 44 'ui:options': { 45 rows: 15, 46 }, 47 }, 48 actions: { 49 items: { 50 'ui:widget': 'SavedStateWidget', 51 'ui:options': { 52 hideDelay: true, 53 }, 54 }, 55 }, 56 }; 57 58 class Automation extends Component { 59 constructor(props) { 60 super(); 61 62 const { schedules, match } = props; 63 const schedule = schedules.find(n => n.get('uuid') === match.params.uuid); 64 this.state = { 65 formData: schedule && schedule.toJS(), 66 isValid: true, 67 }; 68 } 69 70 componentWillReceiveProps(nextProps) { 71 const { schedules, match } = nextProps; 72 if ( 73 !this.props || 74 match.params.uuid !== this.props.match.params.uuid || 75 schedules !== this.props.schedules 76 ) { 77 const schedule = schedules.find(n => n.get('uuid') === match.params.uuid); 78 this.setState({ 79 formData: schedule && schedule.toJS(), 80 }); 81 } 82 } 83 84 onChange = () => (data) => { 85 const { errors, formData } = data; 86 this.setState({ 87 isValid: errors.length === 0, 88 formData, 89 }); 90 }; 91 92 onSubmit = () => ({ formData }) => { 93 const { dispatch } = this.props; 94 95 if (formData.uuid) { 96 dispatch(save(formData)); 97 } else { 98 dispatch(add(formData)); 99 } 100 101 const { history } = this.props; 102 history.push('/aut'); 103 }; 104 105 render() { 106 const { match, devices } = this.props; 107 108 const params = devices.reduce((acc, dev) => { 109 (dev.get('state') || []).forEach((value, key) => { 110 acc[`devices["${dev.get('id')}"].${key}`] = value; 111 }); 112 return acc; 113 }, {}); 114 115 return ( 116 <React.Fragment> 117 <div className="row"> 118 <div className="col-md-12"> 119 <Card 120 title={match.params.uuid ? 'Edit schedule ' : 'New schedule'} 121 bodyClassName="p-0" 122 > 123 <div className="card-body"> 124 <Form 125 schema={schema} 126 uiSchema={uiSchema} 127 showErrorList={false} 128 liveValidate 129 onChange={this.onChange()} 130 formData={this.state.formData} 131 onSubmit={this.onSubmit()} 132 // onError={log('errors')} 133 // disabled={this.props.disabled} 134 // transformErrors={this.props.transformErrors} 135 ObjectFieldTemplate={ObjectFieldTemplate} 136 ArrayFieldTemplate={ArrayFieldTemplate} 137 widgets={{ 138 CheckboxWidget: CustomCheckbox, 139 SavedStateWidget, 140 }} 141 > 142 <button 143 ref={(btn) => { 144 this.submitButton = btn; 145 }} 146 style={{ display: 'none' }} 147 type="submit" 148 /> 149 </Form> 150 </div> 151 <div className="card-footer"> 152 <Button 153 color="primary" 154 disabled={!this.state.isValid || this.props.disabled} 155 onClick={() => this.submitButton.click()} 156 > 157 {'Save'} 158 </Button> 159 </div> 160 </Card> 161 162 <pre> 163 {Object.keys(params).map(key => ( 164 <div key={key}> 165 {key}: <strong>{JSON.stringify(params[key])}</strong> 166 </div> 167 ))} 168 </pre> 169 </div> 170 </div> 171 </React.Fragment> 172 ); 173 } 174 } 175 176 const mapToProps = state => ({ 177 schedules: state.getIn(['schedules', 'list']), 178 devices: state.getIn(['devices', 'list']), 179 }); 180 181 export default connect(mapToProps)(Automation);