From 5627d6dfbc7563b1139cfe532a0004d77a329280 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Mon, 20 Apr 2020 22:06:47 +0100 Subject: [PATCH] fix(ui): migrate LabelSetList to hooks --- ui/src/Components/LabelSetList/index.js | 117 +++++++++++------------- 1 file changed, 53 insertions(+), 64 deletions(-) diff --git a/ui/src/Components/LabelSetList/index.js b/ui/src/Components/LabelSetList/index.js index 4aebf4276..e8eb7aeda 100644 --- a/ui/src/Components/LabelSetList/index.js +++ b/ui/src/Components/LabelSetList/index.js @@ -1,8 +1,7 @@ -import React, { Component } from "react"; +import React from "react"; import PropTypes from "prop-types"; -import { observer } from "mobx-react"; -import { observable, action } from "mobx"; +import { useLocalStore, useObserver } from "mobx-react"; import hash from "object-hash"; @@ -29,70 +28,60 @@ const GroupListToUniqueLabelsList = (groups) => { return Object.values(alerts); }; -const LabelSetList = observer( - class LabelSetList extends Component { - static propTypes = { - alertStore: PropTypes.instanceOf(AlertStore).isRequired, - labelsList: PropTypes.arrayOf(PropTypes.object).isRequired, - }; +const LabelSetList = ({ alertStore, labelsList }) => { + const pagination = useLocalStore(() => ({ + activePage: 1, + onPageChange(pageNumber) { + pagination.activePage = pageNumber; + }, + })); - maxPerPage = 10; + const maxPerPage = 10; - pagination = observable( - { - activePage: 1, - onPageChange(pageNumber) { - this.activePage = pageNumber; - }, - }, - { - onPageChange: action.bound, - } - ); - - render() { - const { alertStore, labelsList } = this.props; - - return labelsList.length > 0 ? ( + return useObserver(() => + labelsList.length > 0 ? ( +
+

Affected alerts

-

Affected alerts

-
-
    - {labelsList - .slice( - (this.pagination.activePage - 1) * this.maxPerPage, - this.pagination.activePage * this.maxPerPage - ) - .map((labels) => ( -
  • - {Object.entries(labels).map(([name, value]) => ( - - ))} -
  • - ))} -
-
- +
    + {labelsList + .slice( + (pagination.activePage - 1) * maxPerPage, + pagination.activePage * maxPerPage + ) + .map((labels) => ( +
  • + {Object.entries(labels).map(([name, value]) => ( + + ))} +
  • + ))} +
- ) : ( -

No alerts matched

- ); - } - } -); + +
+ ) : ( +

No alerts matched

+ ) + ); +}; +LabelSetList.propTypes = { + alertStore: PropTypes.instanceOf(AlertStore).isRequired, + labelsList: PropTypes.arrayOf(PropTypes.object).isRequired, +}; export { LabelSetList, GroupListToUniqueLabelsList };