From 237bce2cb238aa48d5af8526640f94d3fdefdc8c Mon Sep 17 00:00:00 2001 From: Mcat12 Date: Thu, 29 Nov 2018 18:36:24 -0500 Subject: [PATCH] Simplify DomainList from a class to a function It should be easier to read now. Signed-off-by: Mcat12 --- src/components/list/DomainList.js | 88 ++++++++++++++------------ src/components/list/DomainList.test.js | 4 +- 2 files changed, 49 insertions(+), 43 deletions(-) diff --git a/src/components/list/DomainList.js b/src/components/list/DomainList.js index fdb011b..b0a0835 100644 --- a/src/components/list/DomainList.js +++ b/src/components/list/DomainList.js @@ -8,54 +8,60 @@ * This file is copyright under the latest version of the EUPL. * Please see LICENSE file for your rights under this license. */ -import React, { Component } from "react"; +import React from "react"; import PropTypes from "prop-types"; import { translate } from "react-i18next"; import api from "../../util/api"; +import { Button } from "reactstrap"; -class DomainList extends Component { - static propTypes = { - domains: PropTypes.arrayOf(PropTypes.string), - onRemove: PropTypes.func.isRequired - }; +const DomainList = ({ domains, onRemove, t }) => { + // Create a button to remove the domain + const removeButton = item => ( + + ); - render() { - const { t } = this.props; + // Map a domain string to a list item + const mapDomainsToListItems = domain => ( +
  • + {api.loggedIn ? removeButton(domain) : null} + + {domain} + +
  • + ); - return ( - + let body; + + if (domains.length > 0) { + body = domains.map(mapDomainsToListItems); + } else { + body = ( +
    + {t("There are no domains in this list")} +
    ); } -} + + return ; +}; + +DomainList.propTypes = { + domains: PropTypes.arrayOf(PropTypes.string), + onRemove: PropTypes.func.isRequired +}; export default translate(["common", "lists"])(DomainList); diff --git a/src/components/list/DomainList.test.js b/src/components/list/DomainList.test.js index 82ba09b..9e41556 100644 --- a/src/components/list/DomainList.test.js +++ b/src/components/list/DomainList.test.js @@ -55,7 +55,7 @@ it("has a delete button when logged in", () => { wrapper .find("ul") .childAt(0) - .find("button") + .find("Button") ).toExist(); }); @@ -68,7 +68,7 @@ it("calls onRemove when a delete button is clicked", () => { wrapper .find("ul") .childAt(0) - .find("button") + .find("Button") .simulate("click"); expect(onRemove).toHaveBeenCalled();