import React, { useState } from 'react';
import { useSelector } from 'react-redux';
import {
makeStyles, Typography, Container, Paper, AppBar, Toolbar, IconButton, Table, TableHead, TableRow, TableCell, TableBody,
} from '@material-ui/core';
import ArrowBackIcon from '@material-ui/icons/ArrowBack';
import { useHistory, useParams } from 'react-router-dom';
import { useEffectAsync } from './reactHelper';
import { prefixString } from './common/stringUtils';
import { useTranslation } from './LocalizationProvider';
import PositionValue from './components/PositionValue';
const useStyles = makeStyles((theme) => ({
root: {
paddingTop: theme.spacing(1),
paddingBottom: theme.spacing(1),
},
}));
const PositionPage = () => {
const classes = useStyles();
const history = useHistory();
const t = useTranslation();
const { id } = useParams();
const [item, setItem] = useState();
useEffectAsync(async () => {
if (id) {
const response = await fetch(`/api/positions?id=${id}`);
if (response.ok) {
const positions = await response.json();
if (positions.length > 0) {
setItem(positions[0]);
}
}
} else {
setItem({});
}
}, [id]);
const deviceName = useSelector((state) => {
if (item) {
const device = state.devices.items[item.deviceId];
if (device) {
return device.name;
}
}
return null;
});
return (
<>
history.push('/')}>
{deviceName}
{t('stateName')}
{t('sharedName')}
{t('stateValue')}
{item && Object.getOwnPropertyNames(item).filter((it) => it !== 'attributes').map((property) => (
{property}
{t(prefixString('position', property))}
))}
{item && Object.getOwnPropertyNames(item.attributes).map((attribute) => (
{attribute}
{t(prefixString('position', attribute)) || t(prefixString('device', attribute))}
))}
>
);
};
export default PositionPage;