diff --git a/web/src/components/MyEMS/Meter/MeterRealtime.js b/web/src/components/MyEMS/Meter/MeterRealtime.js index 29c90dd0..430c0b28 100644 --- a/web/src/components/MyEMS/Meter/MeterRealtime.js +++ b/web/src/components/MyEMS/Meter/MeterRealtime.js @@ -13,6 +13,9 @@ import { Label, Row, Spinner, + Pagination, + PaginationItem, + PaginationLink } from 'reactstrap'; import Cascader from 'rc-cascader'; import RealtimeChart from './RealtimeChart'; @@ -23,8 +26,12 @@ import uuid from 'uuid/v1'; import { toast } from 'react-toastify'; import { APIBaseURL } from '../../../config'; - const MeterRealtime = ({ setRedirect, setRedirectUrl, t }) => { + const [cursor, setCursor] = useState(0); + const [maxCursor, setMaxCursor] = useState(0); + const [selectMeterList, setSelectMeterList] = useState([]); + const len = 8; + useEffect(() => { let is_logged_in = getCookieValue('is_logged_in'); let user_name = getCookieValue('user_name'); @@ -56,64 +63,73 @@ const MeterRealtime = ({ setRedirect, setRedirectUrl, t }) => { fetch(APIBaseURL + '/spaces/tree', { method: 'GET', headers: { - "Content-type": "application/json", - "User-UUID": getCookieValue('user_uuid'), - "Token": getCookieValue('token') + 'Content-type': 'application/json', + 'User-UUID': getCookieValue('user_uuid'), + Token: getCookieValue('token') }, - body: null, - - }).then(response => { - console.log(response); - if (response.ok) { - isResponseOK = true; - } - return response.json(); - }).then(json => { - console.log(json); - if (isResponseOK) { - // rename keys - json = JSON.parse(JSON.stringify([json]).split('"id":').join('"value":').split('"name":').join('"label":')); - setCascaderOptions(json); - // set the default space - setSelectedSpaceName([json[0]].map(o => o.label)); - let selectedSpaceID = [json[0]].map(o => o.value) - //begin of getting meters of the default space - let isSecondResponseOK = false; - fetch(APIBaseURL + '/spaces/' + selectedSpaceID + '/meters', { - method: 'GET', - headers: { - "Content-type": "application/json", - "User-UUID": getCookieValue('user_uuid'), - "Token": getCookieValue('token') - }, - body: null, - - }).then(response => { - if (response.ok) { - isSecondResponseOK = true; - } - return response.json(); - }).then(json => { - if (isSecondResponseOK) { - json = JSON.parse(JSON.stringify([json])); - console.log(json); - setMeterList(json[0]); - setSpinnerHidden(true); - } else { - toast.error(json.description) - } - }).catch(err => { - console.log(err); - }); - //end of getting meters of the default space - } else { - toast.error(json.description); - } - }).catch(err => { - console.log(err); - }); + body: null + }) + .then(response => { + console.log(response); + if (response.ok) { + isResponseOK = true; + } + return response.json(); + }) + .then(json => { + console.log(json); + if (isResponseOK) { + // rename keys + json = JSON.parse( + JSON.stringify([json]) + .split('"id":') + .join('"value":') + .split('"name":') + .join('"label":') + ); + setCascaderOptions(json); + // set the default space + setSelectedSpaceName([json[0]].map(o => o.label)); + let selectedSpaceID = [json[0]].map(o => o.value); + //begin of getting meters of the default space + let isSecondResponseOK = false; + fetch(APIBaseURL + '/spaces/' + selectedSpaceID + '/meters', { + method: 'GET', + headers: { + 'Content-type': 'application/json', + 'User-UUID': getCookieValue('user_uuid'), + Token: getCookieValue('token') + }, + body: null + }) + .then(response => { + if (response.ok) { + isSecondResponseOK = true; + } + return response.json(); + }) + .then(json => { + if (isSecondResponseOK) { + json = JSON.parse(JSON.stringify([json])); + console.log(json); + setMeterList(json[0]); + setSpinnerHidden(true); + } else { + toast.error(json.description); + } + }) + .catch(err => { + console.log(err); + }); + //end of getting meters of the default space + } else { + toast.error(json.description); + } + }) + .catch(err => { + console.log(err); + }); //end of getting space tree - }, []); const labelClasses = 'ls text-uppercase text-600 font-weight-semi-bold mb-0'; @@ -127,42 +143,56 @@ const MeterRealtime = ({ setRedirect, setRedirectUrl, t }) => { fetch(APIBaseURL + '/spaces/' + selectedSpaceID + '/meters', { method: 'GET', headers: { - "Content-type": "application/json", - "User-UUID": getCookieValue('user_uuid'), - "Token": getCookieValue('token') + 'Content-type': 'application/json', + 'User-UUID': getCookieValue('user_uuid'), + Token: getCookieValue('token') }, - body: null, - - }).then(response => { - if (response.ok) { - isResponseOK = true; - } - return response.json(); - }).then(json => { - if (isResponseOK) { + body: null + }) + .then(response => { + if (response.ok) { + isResponseOK = true; + } + return response.json(); + }) + .then(json => { + if (isResponseOK) { json = JSON.parse(JSON.stringify([json])); console.log(json); setMeterList(json[0]); - setSpinnerHidden(true); - } else { - toast.error(json.description) - } - }).catch(err => { - console.log(err); - }); - //end of getting meters of the selected space - } + setSpinnerHidden(true); + } else { + toast.error(json.description); + } + }) + .catch(err => { + console.log(err); + }); + //end of getting meters of the selected space + }; + + useEffect(() => { + const meterLen = meterList.length; + const maxCursor = Math.ceil(meterLen / len); + setCursor(1); + setMaxCursor(maxCursor); + }, [meterList]); + + useEffect(() => { + setSelectMeterList(meterList.slice(cursor * len - 8, cursor * len)); + }, [cursor, meterList]); return (
- {t('Meter Data')}{t('Meter Realtime')} + {t('Meter Data')} + {t('Meter Realtime')}
-
+ @@ -170,33 +200,53 @@ const MeterRealtime = ({ setRedirect, setRedirectUrl, t }) => { {t('Space')}
- + expandTrigger="hover" + >
-

-
- +
- {meterList.map(meter => ( + {selectMeterList.map(meter => ( ))} + + + setCursor(1)} /> + + + (cursor - 1 >= 1 ? setCursor(cursor - 1) : null)} /> + + + {cursor} + + ) + + (cursor + 1 <= maxCursor ? setCursor(cursor + 1) : null)} /> + + + setCursor(maxCursor)} /> + +
); }; -export default withTranslation()(withRedirect(MeterRealtime)); +export default withTranslation()(withRedirect(MeterRealtime)); \ No newline at end of file