import React, { useState } from 'react';
import TextField from '@material-ui/core/TextField';
import {
Accordion, AccordionSummary, AccordionDetails, makeStyles, Typography, FormControl, InputLabel, Select, MenuItem, FormControlLabel, Checkbox, InputAdornment, IconButton, FilledInput,
} from '@material-ui/core';
import ExpandMoreIcon from '@material-ui/icons/ExpandMore';
import CachedIcon from '@material-ui/icons/Cached';
import { useDispatch, useSelector } from 'react-redux';
import moment from 'moment';
import EditItemView from './components/EditItemView';
import EditAttributesView from './components/EditAttributesView';
import LinkField from '../common/components/LinkField';
import { useTranslation } from '../common/components/LocalizationProvider';
import useUserAttributes from '../common/attributes/useUserAttributes';
import { sessionActions } from '../store';
import SelectField from '../common/components/SelectField';
import SettingsMenu from './components/SettingsMenu';
import useCommonUserAttributes from '../common/attributes/useCommonUserAttributes';
import { useAdministrator, useManager } from '../common/util/permissions';
import { prefixString } from '../common/util/stringUtils';
const useStyles = makeStyles(() => ({
details: {
flexDirection: 'column',
},
}));
const UserPage = () => {
const classes = useStyles();
const dispatch = useDispatch();
const t = useTranslation();
const admin = useAdministrator();
const manager = useManager();
const currentUserId = useSelector((state) => state.session.user.id);
const commonUserAttributes = useCommonUserAttributes(t);
const userAttributes = useUserAttributes(t);
const [item, setItem] = useState();
const onItemSaved = (result) => {
if (result.id === currentUserId) {
dispatch(sessionActions.updateUser(result));
}
};
const validate = () => item && item.name && item.email && (item.id || item.password);
return (
}
breadcrumbs={['settingsTitle', 'settingsUser']}
>
{item && (
<>
}>
{t('sharedRequired')}
setItem({ ...item, name: event.target.value })}
label={t('sharedName')}
variant="filled"
/>
setItem({ ...item, email: event.target.value })}
label={t('userEmail')}
variant="filled"
/>
setItem({ ...item, password: event.target.value })}
label={t('userPassword')}
variant="filled"
/>
}>
{t('sharedPreferences')}
setItem({ ...item, phone: event.target.value })}
label={t('sharedPhone')}
variant="filled"
/>
setItem({ ...item, latitude: Number(event.target.value) })}
label={t('positionLatitude')}
variant="filled"
/>
setItem({ ...item, longitude: Number(event.target.value) })}
label={t('positionLongitude')}
variant="filled"
/>
setItem({ ...item, zoom: Number(event.target.value) })}
label={t('serverZoom')}
variant="filled"
/>
{t('settingsCoordinateFormat')}
{t('settingsSpeedUnit')}
{t('settingsDistanceUnit')}
{t('settingsVolumeUnit')}
setItem({ ...item, attributes: { ...item.attributes, timezone: e.target.value } })}
endpoint="/api/server/timezones"
keyGetter={(it) => it}
titleGetter={(it) => it}
label={t('sharedTimezone')}
variant="filled"
/>
setItem({ ...item, poiLayer: event.target.value })}
label={t('mapPoiLayer')}
variant="filled"
/>
setItem({ ...item, twelveHourFormat: event.target.checked })} />}
label={t('settingsTwelveHourFormat')}
/>
}>
{t('sharedPermissions')}
{t('userToken')}
setItem({ ...item, token: e.target.value })}
endAdornment={(
{
const token = [...Array(30)].map(() => Math.random().toString(36)[2]).join('');
setItem({ ...item, token });
}}
>
)}
/>
setItem({ ...item, expirationTime: moment(e.target.value, moment.HTML5_FMT.DATE) })}
disabled={!manager}
/>
setItem({ ...item, deviceLimit: Number(e.target.value) })}
label={t('userDeviceLimit')}
variant="filled"
disabled={!admin}
/>
setItem({ ...item, userLimit: Number(e.target.value) })}
label={t('userUserLimit')}
variant="filled"
disabled={!admin}
/>
setItem({ ...item, disabled: e.target.checked })} />}
label={t('sharedDisabled')}
disabled={!manager}
/>
setItem({ ...item, administrator: e.target.checked })} />}
label={t('userAdmin')}
disabled={!admin}
/>
setItem({ ...item, readonly: e.target.checked })} />}
label={t('serverReadonly')}
disabled={!manager}
/>
setItem({ ...item, deviceReadonly: e.target.checked })} />}
label={t('userDeviceReadonly')}
disabled={!manager}
/>
setItem({ ...item, limitCommands: e.target.checked })} />}
label={t('userLimitCommands')}
disabled={!manager}
/>
setItem({ ...item, disableReports: e.target.checked })} />}
label={t('userDisableReports')}
disabled={!manager}
/>
}>
{t('sharedAttributes')}
setItem({ ...item, attributes })}
definitions={{ ...commonUserAttributes, ...userAttributes }}
/>
{item.id && manager && (
}>
{t('sharedConnections')}
t(prefixString('event', it.type))}
label={t('sharedNotifications')}
variant="filled"
/>
it.description}
label={t('sharedComputedAttributes')}
variant="filled"
/>
it.description}
label={t('sharedSavedCommands')}
variant="filled"
/>
)}
>
)}
);
};
export default UserPage;