|
| 1 | +import * as React from 'react'; |
| 2 | +import { |
| 3 | + Alert, Button, Table, |
| 4 | +} from 'antd'; |
| 5 | +import Paragraph from 'antd/es/typography/Paragraph'; |
| 6 | +import { getLabels, setLanguage } from '../utils/Localization'; |
| 7 | +import { fetchBackend, sendToBackend } from '../utils/restCalls'; |
| 8 | + |
| 9 | +export class CronSettings extends React.Component { |
| 10 | + state = { |
| 11 | + settings: {}, |
| 12 | + changed: false, |
| 13 | + error: '', |
| 14 | + updateDevices: '', |
| 15 | + syncDevices: '', |
| 16 | + loading: false, |
| 17 | + }; |
| 18 | + |
| 19 | + async componentDidMount() { |
| 20 | + await this.reload(); |
| 21 | + } |
| 22 | + |
| 23 | + async onSaveClick() { |
| 24 | + const { |
| 25 | + settings, |
| 26 | + updateDevices, |
| 27 | + syncDevices, |
| 28 | + } = this.state; |
| 29 | + this.setState({ loading: true }); |
| 30 | + const copyConfig = JSON.parse(JSON.stringify(settings.data)); |
| 31 | + if (updateDevices) { |
| 32 | + copyConfig.cron.updateDevices = updateDevices; |
| 33 | + } |
| 34 | + if (syncDevices) { |
| 35 | + copyConfig.cron.syncDevices = syncDevices; |
| 36 | + } |
| 37 | + try { |
| 38 | + const res = await sendToBackend('/ui/settings', 'POST', copyConfig); |
| 39 | + const status = JSON.parse(res.data); |
| 40 | + if (status.status === 'OK') { |
| 41 | + const event = { changed: false }; |
| 42 | + await this.props.reload(); |
| 43 | + await this.reload(); |
| 44 | + this.setState(event); |
| 45 | + } else { |
| 46 | + this.setState({ error: status.message }); |
| 47 | + } |
| 48 | + } finally { |
| 49 | + this.setState({ loading: false }); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + getColumns() { |
| 54 | + return [ |
| 55 | + { |
| 56 | + title: 'Name', |
| 57 | + dataIndex: 'name', |
| 58 | + key: 'name', |
| 59 | + render: (text) => <a>{getLabels()[text] || text}</a>, |
| 60 | + }, |
| 61 | + { |
| 62 | + title: 'Value', |
| 63 | + dataIndex: 'value', |
| 64 | + key: 'value', |
| 65 | + render: (text, data) => { |
| 66 | + const value = this.state[data.name]; |
| 67 | + return ( |
| 68 | + <Paragraph editable={{ |
| 69 | + onChange: (newValue) => { |
| 70 | + if (newValue) { |
| 71 | + const newState = { changed: true }; |
| 72 | + newState[data.name] = newValue; |
| 73 | + this.setState(newState); |
| 74 | + } |
| 75 | + }, |
| 76 | + }} |
| 77 | + > |
| 78 | + {value} |
| 79 | + </Paragraph> |
| 80 | + ); |
| 81 | + }, |
| 82 | + }, |
| 83 | + ]; |
| 84 | + } |
| 85 | + |
| 86 | + async reload() { |
| 87 | + const { data } = await fetchBackend('/ui/settings'); |
| 88 | + const settings = JSON.parse(data); |
| 89 | + setLanguage(settings.data.language || 'English'); |
| 90 | + this.setState({ |
| 91 | + settings, |
| 92 | + updateDevices: settings.data.cron.updateDevices, |
| 93 | + syncDevices: settings.data.cron.syncDevices, |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + render() { |
| 98 | + const { |
| 99 | + settings, changed, loading, error, |
| 100 | + } = this.state; |
| 101 | + if (settings.status === 'OK') { |
| 102 | + const data = [{ |
| 103 | + name: 'updateDevices', |
| 104 | + value: settings.data.cron.updateDevices, |
| 105 | + }, |
| 106 | + { |
| 107 | + name: 'syncDevices', |
| 108 | + value: settings.data.cron.syncDevices, |
| 109 | + }]; |
| 110 | + return ( |
| 111 | + <div> |
| 112 | + {error ? ( |
| 113 | + <Alert |
| 114 | + message={error} |
| 115 | + showIcon |
| 116 | + type="error" |
| 117 | + closable |
| 118 | + /> |
| 119 | + ) : null} |
| 120 | + <Table pagination={false} columns={this.getColumns()} dataSource={data} /> |
| 121 | + <Button |
| 122 | + type="primary" |
| 123 | + loading={loading} |
| 124 | + block |
| 125 | + disabled={!changed} |
| 126 | + onClick={async () => { |
| 127 | + await this.onSaveClick(); |
| 128 | + }} |
| 129 | + > |
| 130 | + {getLabels().save || 'Save'} |
| 131 | + </Button> |
| 132 | + </div> |
| 133 | + ); |
| 134 | + } |
| 135 | + return null; |
| 136 | + } |
| 137 | +} |
0 commit comments