From d146c1fcf9249267988455bed5415854c49090ae Mon Sep 17 00:00:00 2001 From: olav Date: Fri, 4 Feb 2022 13:07:45 +0100 Subject: [PATCH] refactor: detach ApplicationList from global settings (#666) * refactor: add missing prop-types dependency * refactor: detach ApplicationList from global settings * refactor: destructure props inline Co-authored-by: Fredrik Strand Oseberg --- frontend/package.json | 1 + .../application/application-list-component.js | 78 +++++++++---------- .../application/application-list-container.js | 18 ++--- frontend/yarn.lock | 9 +++ 4 files changed, 53 insertions(+), 53 deletions(-) diff --git a/frontend/package.json b/frontend/package.json index ad01c5c44e..4ae3bffda7 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -71,6 +71,7 @@ "lodash.flow": "3.5.0", "node-fetch": "2.6.7", "prettier": "2.5.1", + "prop-types": "^15.8.1", "react": "17.0.2", "react-dnd": "14.0.5", "react-dnd-html5-backend": "14.1.0", diff --git a/frontend/src/component/application/application-list-component.js b/frontend/src/component/application/application-list-component.js index 518bbb1dc5..65da487cfe 100644 --- a/frontend/src/component/application/application-list-component.js +++ b/frontend/src/component/application/application-list-component.js @@ -1,4 +1,4 @@ -import React, { Component } from 'react'; +import React, { useEffect, useMemo, useState } from 'react'; import PropTypes from 'prop-types'; import { CircularProgress } from '@material-ui/core'; import { Warning } from '@material-ui/icons'; @@ -25,49 +25,45 @@ const Empty = () => ( ); -class ClientStrategies extends Component { - static propTypes = { - applications: PropTypes.array, - fetchAll: PropTypes.func.isRequired, - settings: PropTypes.object.isRequired, - updateSetting: PropTypes.func.isRequired, - }; +const ClientStrategies = ({ fetchAll, applications }) => { + const [filter, setFilter] = useState(''); - componentDidMount() { - this.props.fetchAll(); + useEffect(() => { + fetchAll(); + }, [fetchAll]); + + const filteredApplications = useMemo(() => { + const regExp = new RegExp(filter, 'i'); + return filter + ? applications?.filter(a => regExp.test(a.appName)) + : applications; + }, [applications, filter]); + + if (!filteredApplications) { + return ; } - render() { - const { applications } = this.props; - - if (!applications) { - return ; - } - return ( - <> -
- + return ( + <> +
+ +
+ }> +
+ {filteredApplications.length > 0 ? ( + + ) : ( + + )}
- } - > -
- {applications.length > 0 ? ( - - ) : ( - - )} -
-
- - ); - } -} +
+ + ); +}; + +ClientStrategies.propTypes = { + applications: PropTypes.array, + fetchAll: PropTypes.func.isRequired, +}; export default ClientStrategies; diff --git a/frontend/src/component/application/application-list-container.js b/frontend/src/component/application/application-list-container.js index e8d3ff6f88..201ceece2d 100644 --- a/frontend/src/component/application/application-list-container.js +++ b/frontend/src/component/application/application-list-container.js @@ -1,20 +1,14 @@ import { connect } from 'react-redux'; import ApplicationList from './application-list-component'; -import { fetchAll } from './../../store/application/actions'; -import { updateSettingForGroup } from '../../store/settings/actions'; +import { fetchAll } from '../../store/application/actions'; -const mapStateToProps = state => { - const applications = state.applications.get('list').toJS(); - const settings = state.settings.toJS().application || {}; +const mapStateToProps = state => ({ + applications: state.applications.get('list').toJS(), +}); - const regex = new RegExp(settings.filter, 'i'); - - return { - applications: settings.filter ? applications.filter(a => regex.test(a.appName)) : applications, - settings, - }; +const mapDispatchToProps = { + fetchAll, }; -const mapDispatchToProps = { fetchAll, updateSetting: updateSettingForGroup('application') }; const Container = connect(mapStateToProps, mapDispatchToProps)(ApplicationList); diff --git a/frontend/yarn.lock b/frontend/yarn.lock index 64ce1f2837..b8f8f65918 100644 --- a/frontend/yarn.lock +++ b/frontend/yarn.lock @@ -10062,6 +10062,15 @@ prop-types@^15.6.2, prop-types@^15.7.2: object-assign "^4.1.1" react-is "^16.8.1" +prop-types@^15.8.1: + version "15.8.1" + resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.8.1.tgz#67d87bf1a694f48435cf332c24af10214a3140b5" + integrity sha512-oj87CgZICdulUohogVAR7AjlC0327U4el4L6eAvOqCeudMDVU0NThNaV+b9Df4dXgSP1gXMTnPdhfe/2qDH5cg== + dependencies: + loose-envify "^1.4.0" + object-assign "^4.1.1" + react-is "^16.13.1" + proxy-addr@~2.0.5: version "2.0.6" resolved "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.6.tgz"