Fork of the excellent esp8266-react - https://github.com/rjwats/esp8266-react
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.3 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { TextValidator, ValidatorForm, SelectValidator } from 'react-material-ui-form-validator';
  4. import { withStyles } from '@material-ui/core/styles';
  5. import FormControlLabel from '@material-ui/core/FormControlLabel';
  6. import MenuItem from '@material-ui/core/MenuItem';
  7. import Switch from '@material-ui/core/Switch';
  8. import Button from '@material-ui/core/Button';
  9. import SaveIcon from '@material-ui/icons/Save';
  10. import isIP from '../validators/isIP';
  11. import isHostname from '../validators/isHostname';
  12. import or from '../validators/or';
  13. import { timeZoneSelectItems, selectedTimeZone, TIME_ZONES } from '../constants/TZ';
  14. const styles = theme => ({
  15. switchControl: {
  16. width: "100%",
  17. marginTop: theme.spacing(2),
  18. marginBottom: theme.spacing(0.5)
  19. },
  20. textField: {
  21. width: "100%"
  22. },
  23. button: {
  24. marginRight: theme.spacing(2),
  25. marginTop: theme.spacing(2),
  26. }
  27. });
  28. class NTPSettingsForm extends React.Component {
  29. componentWillMount() {
  30. ValidatorForm.addValidationRule('isIPOrHostname', or(isIP, isHostname));
  31. }
  32. changeTimeZone = (event) => {
  33. const { ntpSettings, setData } = this.props;
  34. setData({
  35. ...ntpSettings,
  36. tz_label: event.target.value,
  37. tz_format: TIME_ZONES[event.target.value]
  38. });
  39. }
  40. render() {
  41. const { classes, ntpSettings, handleValueChange, handleCheckboxChange, onSubmit, onReset } = this.props;
  42. return (
  43. <ValidatorForm onSubmit={onSubmit}>
  44. <FormControlLabel className={classes.switchControl}
  45. control={
  46. <Switch
  47. checked={ntpSettings.enabled}
  48. onChange={handleCheckboxChange('enabled')}
  49. value="enabled"
  50. color="primary"
  51. />
  52. }
  53. label="Enable NTP?"
  54. />
  55. <TextValidator
  56. validators={['required', 'isIPOrHostname']}
  57. errorMessages={['Server is required', "Not a valid IP address or hostname"]}
  58. name="server"
  59. label="Server"
  60. className={classes.textField}
  61. value={ntpSettings.server}
  62. onChange={handleValueChange('server')}
  63. margin="normal"
  64. />
  65. <SelectValidator
  66. native
  67. validators={['required']}
  68. errorMessages={['Time zone is required']}
  69. labelId="tz_label"
  70. label="Time zone"
  71. value={selectedTimeZone(ntpSettings.tz_label, ntpSettings.tz_format)}
  72. onChange={this.changeTimeZone}
  73. className={classes.textField}
  74. margin="normal"
  75. >
  76. <MenuItem disabled={true}>Time zone...</MenuItem>
  77. {timeZoneSelectItems()}
  78. </SelectValidator>
  79. <Button startIcon={<SaveIcon />} variant="contained" color="primary" className={classes.button} type="submit">
  80. Save
  81. </Button>
  82. <Button variant="contained" color="secondary" className={classes.button} onClick={onReset}>
  83. Reset
  84. </Button>
  85. </ValidatorForm>
  86. );
  87. }
  88. }
  89. NTPSettingsForm.propTypes = {
  90. classes: PropTypes.object.isRequired,
  91. ntpSettings: PropTypes.object,
  92. onSubmit: PropTypes.func.isRequired,
  93. onReset: PropTypes.func.isRequired,
  94. handleValueChange: PropTypes.func.isRequired,
  95. };
  96. export default withStyles(styles)(NTPSettingsForm);