mirror of
https://github.com/optim-enterprises-bv/OptimCloud-gw-ui.git
synced 2025-11-02 19:27:45 +00:00
Fixed re-renders of statistics chart list
This commit is contained in:
@@ -2,20 +2,14 @@ import React from 'react';
|
|||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import Chart from 'react-apexcharts';
|
import Chart from 'react-apexcharts';
|
||||||
|
|
||||||
const DeviceStatisticsChart = ({ data, options }) => (
|
const DeviceStatisticsChart = ({ chart }) => (
|
||||||
<div style={{ height: '360px' }}>
|
<div style={{ height: '360px' }}>
|
||||||
<Chart series={data} options={options} type="line" height="100%" />
|
<Chart series={chart.data} options={chart.options} type="line" height="100%" />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
||||||
DeviceStatisticsChart.propTypes = {
|
DeviceStatisticsChart.propTypes = {
|
||||||
data: PropTypes.instanceOf(Array),
|
chart: PropTypes.instanceOf(Object).isRequired,
|
||||||
options: PropTypes.instanceOf(Object),
|
|
||||||
};
|
|
||||||
|
|
||||||
DeviceStatisticsChart.defaultProps = {
|
|
||||||
data: [],
|
|
||||||
options: {},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default DeviceStatisticsChart;
|
export default DeviceStatisticsChart;
|
||||||
|
|||||||
@@ -5,11 +5,11 @@ import { v4 as createUuid } from 'uuid';
|
|||||||
import axiosInstance from 'utils/axiosInstance';
|
import axiosInstance from 'utils/axiosInstance';
|
||||||
import { getToken } from 'utils/authHelper';
|
import { getToken } from 'utils/authHelper';
|
||||||
import { unixToTime, capitalizeFirstLetter } from 'utils/helper';
|
import { unixToTime, capitalizeFirstLetter } from 'utils/helper';
|
||||||
|
import eventBus from 'utils/eventBus';
|
||||||
import DeviceStatisticsChart from './DeviceStatisticsChart';
|
import DeviceStatisticsChart from './DeviceStatisticsChart';
|
||||||
|
|
||||||
const StatisticsChartList = ({ selectedDeviceId, lastRefresh }) => {
|
const StatisticsChartList = ({ selectedDeviceId }) => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const [loading, setLoading] = useState(false);
|
|
||||||
const [statOptions, setStatOptions] = useState({
|
const [statOptions, setStatOptions] = useState({
|
||||||
interfaceList: [],
|
interfaceList: [],
|
||||||
settings: {},
|
settings: {},
|
||||||
@@ -68,7 +68,6 @@ const StatisticsChartList = ({ selectedDeviceId, lastRefresh }) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
chart: {
|
chart: {
|
||||||
id: 'chart',
|
id: 'chart',
|
||||||
@@ -106,15 +105,13 @@ const StatisticsChartList = ({ selectedDeviceId, lastRefresh }) => {
|
|||||||
interfaceList,
|
interfaceList,
|
||||||
settings: options,
|
settings: options,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (statOptions !== newOptions) {
|
if (statOptions !== newOptions) {
|
||||||
setStatOptions(newOptions);
|
setStatOptions(newOptions);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const getStatistics = () => {
|
const getStatistics = () => {
|
||||||
if (!loading) {
|
|
||||||
setLoading(true);
|
|
||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
headers: {
|
headers: {
|
||||||
Accept: 'application/json',
|
Accept: 'application/json',
|
||||||
@@ -130,11 +127,7 @@ const StatisticsChartList = ({ selectedDeviceId, lastRefresh }) => {
|
|||||||
.then((response) => {
|
.then((response) => {
|
||||||
transformIntoDataset(response.data.data);
|
transformIntoDataset(response.data.data);
|
||||||
})
|
})
|
||||||
.catch(() => {})
|
.catch(() => {});
|
||||||
.finally(() => {
|
|
||||||
setLoading(false);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -144,42 +137,41 @@ const StatisticsChartList = ({ selectedDeviceId, lastRefresh }) => {
|
|||||||
}, [selectedDeviceId]);
|
}, [selectedDeviceId]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!loading && lastRefresh !== '' && selectedDeviceId) {
|
eventBus.on('refreshInterfaceStatistics', () => getStatistics());
|
||||||
getStatistics();
|
|
||||||
}
|
return () => {
|
||||||
}, [lastRefresh]);
|
eventBus.remove('refreshInterfaceStatistics');
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
{statOptions.interfaceList.map((data) => (
|
{statOptions.interfaceList.map((data) => {
|
||||||
<div key={createUuid()}>
|
const options = {
|
||||||
<DeviceStatisticsChart
|
data,
|
||||||
key={createUuid()}
|
options: {
|
||||||
data={data}
|
...statOptions.settings,
|
||||||
options={{
|
title: {
|
||||||
...statOptions.settings,
|
text: capitalizeFirstLetter(data[0].titleName),
|
||||||
title: {
|
align: 'left',
|
||||||
text: capitalizeFirstLetter(data[0].titleName),
|
style: {
|
||||||
align: 'left',
|
fontSize: '25px',
|
||||||
style: {
|
|
||||||
fontSize: '25px',
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}}
|
},
|
||||||
/>
|
}
|
||||||
</div>
|
}
|
||||||
))}
|
return (
|
||||||
|
<div key={createUuid()}>
|
||||||
|
<DeviceStatisticsChart chart={ options } />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
StatisticsChartList.propTypes = {
|
StatisticsChartList.propTypes = {
|
||||||
lastRefresh: PropTypes.string,
|
|
||||||
selectedDeviceId: PropTypes.string.isRequired,
|
selectedDeviceId: PropTypes.string.isRequired,
|
||||||
};
|
};
|
||||||
|
|
||||||
StatisticsChartList.defaultProps = {
|
|
||||||
lastRefresh: '',
|
|
||||||
};
|
|
||||||
|
|
||||||
export default React.memo(StatisticsChartList);
|
export default React.memo(StatisticsChartList);
|
||||||
|
|||||||
@@ -14,13 +14,13 @@ import {
|
|||||||
} from '@coreui/react';
|
} from '@coreui/react';
|
||||||
import { cilOptions } from '@coreui/icons';
|
import { cilOptions } from '@coreui/icons';
|
||||||
import CIcon from '@coreui/icons-react';
|
import CIcon from '@coreui/icons-react';
|
||||||
|
import eventBus from 'utils/eventBus';
|
||||||
import StatisticsChartList from './StatisticsChartList';
|
import StatisticsChartList from './StatisticsChartList';
|
||||||
import LatestStatisticsModal from './LatestStatisticsModal';
|
import LatestStatisticsModal from './LatestStatisticsModal';
|
||||||
import styles from './index.module.scss';
|
import styles from './index.module.scss';
|
||||||
|
|
||||||
const DeviceStatisticsCard = ({ selectedDeviceId }) => {
|
const DeviceStatisticsCard = ({ selectedDeviceId }) => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const [lastRefresh, setLastRefresh] = useState(new Date().toString());
|
|
||||||
const [showLatestModal, setShowLatestModal] = useState(false);
|
const [showLatestModal, setShowLatestModal] = useState(false);
|
||||||
|
|
||||||
const toggleLatestModal = () => {
|
const toggleLatestModal = () => {
|
||||||
@@ -28,7 +28,7 @@ const DeviceStatisticsCard = ({ selectedDeviceId }) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const refresh = () => {
|
const refresh = () => {
|
||||||
setLastRefresh(new Date().toString());
|
eventBus.dispatch('refreshInterfaceStatistics', { message: 'Refresh interface statistics' });
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -55,7 +55,7 @@ const DeviceStatisticsCard = ({ selectedDeviceId }) => {
|
|||||||
</CRow>
|
</CRow>
|
||||||
</CCardHeader>
|
</CCardHeader>
|
||||||
<CCardBody className={styles.statsBody}>
|
<CCardBody className={styles.statsBody}>
|
||||||
<StatisticsChartList selectedDeviceId={selectedDeviceId} lastRefresh={lastRefresh} />
|
<StatisticsChartList selectedDeviceId={selectedDeviceId} />
|
||||||
</CCardBody>
|
</CCardBody>
|
||||||
</CCard>
|
</CCard>
|
||||||
<LatestStatisticsModal
|
<LatestStatisticsModal
|
||||||
|
|||||||
Reference in New Issue
Block a user