import React, { Component, Fragment } from 'react'; import { WithTheme, withTheme } from '@material-ui/core/styles'; import { Avatar, Divider, List, ListItem, ListItemAvatar, ListItemText, Button } from '@material-ui/core'; import { Dialog, DialogTitle, DialogContent, DialogActions, Box, TextField } from '@material-ui/core'; import SwapVerticalCircleIcon from '@material-ui/icons/SwapVerticalCircle'; import AccessTimeIcon from '@material-ui/icons/AccessTime'; import DNSIcon from '@material-ui/icons/Dns'; import UpdateIcon from '@material-ui/icons/Update'; import AvTimerIcon from '@material-ui/icons/AvTimer'; import RefreshIcon from '@material-ui/icons/Refresh'; import { RestFormProps, FormButton, HighlightAvatar } from '../components'; import { isNtpActive, ntpStatusHighlight, ntpStatus } from './NTPStatus'; import { formatDuration, formatDateTime, formatLocalDateTime } from './TimeFormat'; import { NTPStatus, Time } from './types'; import { redirectingAuthorizedFetch, withAuthenticatedContext, AuthenticatedContextProps } from '../authentication'; import { TIME_ENDPOINT } from '../api'; type NTPStatusFormProps = RestFormProps & WithTheme & AuthenticatedContextProps; interface NTPStatusFormState { settingTime: boolean; localTime: string; processing: boolean; } class NTPStatusForm extends Component { constructor(props: NTPStatusFormProps) { super(props); this.state = { settingTime: false, localTime: '', processing: false }; } updateLocalTime = (event: React.ChangeEvent) => { this.setState({ localTime: event.target.value }); } openSetTime = () => { this.setState({ localTime: formatLocalDateTime(new Date()), settingTime: true }); } closeSetTime = () => { this.setState({ settingTime: false }); } createTime = (): Time => ({ local_time: formatLocalDateTime(new Date(this.state.localTime)) }); configureTime = () => { this.setState({ processing: true }); redirectingAuthorizedFetch(TIME_ENDPOINT, { method: 'POST', body: JSON.stringify(this.createTime()), headers: { 'Content-Type': 'application/json' } }) .then(response => { if (response.status === 200) { this.props.enqueueSnackbar("Time set successfully", { variant: 'success' }); this.setState({ processing: false, settingTime: false }, this.props.loadData); } else { throw Error("Error setting time, status code: " + response.status); } }) .catch(error => { this.props.enqueueSnackbar(error.message || "Problem setting the time", { variant: 'error' }); this.setState({ processing: false, settingTime: false }); }); } renderSetTimeDialog() { return ( Set Time Enter local date and time below to set the device's time. ) } render() { const { data, theme } = this.props const me = this.props.authenticatedContext.me; return ( {isNtpActive(data) && ( )} } variant="contained" color="secondary" onClick={this.props.loadData}> Refresh {me.admin && !isNtpActive(data) && ( )} {this.renderSetTimeDialog()} ); } } export default withAuthenticatedContext(withTheme(NTPStatusForm));