From 7455e0abd56a485131dcf7f79be68bcc1d8ddde7 Mon Sep 17 00:00:00 2001 From: Raj Bagaria Date: Mon, 11 Sep 2023 04:57:49 +0530 Subject: [PATCH] Converted the CollectionList from class component to functional component --- .../CollectionList/CollectionList.jsx | 289 +++++++++--------- 1 file changed, 146 insertions(+), 143 deletions(-) diff --git a/client/modules/IDE/components/CollectionList/CollectionList.jsx b/client/modules/IDE/components/CollectionList/CollectionList.jsx index 646b9824b5..7a700aceb7 100644 --- a/client/modules/IDE/components/CollectionList/CollectionList.jsx +++ b/client/modules/IDE/components/CollectionList/CollectionList.jsx @@ -1,5 +1,5 @@ import PropTypes from 'prop-types'; -import React from 'react'; +import React, { useState, useEffect } from 'react'; import { Helmet } from 'react-helmet'; import { withTranslation } from 'react-i18next'; import { connect } from 'react-redux'; @@ -22,113 +22,129 @@ import CollectionListRow from './CollectionListRow'; import ArrowUpIcon from '../../../../images/sort-arrow-up.svg'; import ArrowDownIcon from '../../../../images/sort-arrow-down.svg'; -class CollectionList extends React.Component { - constructor(props) { - super(props); +function CollectionList({ + // user, + // projectId, + // project, + // username, + // sorting, + // getProject, + // getCollections, + // loading, + // collections, + // t, + // toggleDirectionForField, + // resetSorting, + // mobile, - if (props.projectId) { - props.getProject(props.projectId); - } - - this.props.getCollections(this.props.username); - this.props.resetSorting(); + user, + projectId, + getCollections, + getProject, + collections, + username, + loading, + toggleDirectionForField, + resetSorting, + sorting, + project, + t, + mobile +}) { + const [hasLoadedData, setHasLoadedData] = useState(false); + const [ + addingSketchesToCollectionId, + setAddingSketchesToCollectionId + ] = useState(null); - this.state = { - hasLoadedData: false, - addingSketchesToCollectionId: null - }; - } + useEffect(() => { + if (!loading && !hasLoadedData) { + setHasLoadedData(true); + } + }, [loading, hasLoadedData]); - componentDidUpdate(prevProps, prevState) { - if (prevProps.loading === true && this.props.loading === false) { - // eslint-disable-next-line react/no-did-update-set-state - this.setState({ - hasLoadedData: true - }); + useEffect(() => { + if (projectId) { + getProject(projectId); } - } - getTitle() { - if (this.props.username === this.props.user.username) { - return this.props.t('CollectionList.Title'); + getCollections(username); + resetSorting(); + }, [projectId, getCollections, username, resetSorting]); + + const getTitle = () => { + if (username === user.username) { + return t('CollectionList.Title'); } - return this.props.t('CollectionList.AnothersTitle', { - anotheruser: this.props.username + return t('CollectionList.AnothersTitle', { + anotheruser: username }); - } + }; - showAddSketches = (collectionId) => { - this.setState({ - addingSketchesToCollectionId: collectionId - }); + const showAddSketches = (collectionId) => { + setAddingSketchesToCollectionId(collectionId); }; - hideAddSketches = () => { - this.setState({ - addingSketchesToCollectionId: null - }); + const hideAddSketches = () => { + setAddingSketchesToCollectionId(null); }; - hasCollections() { - return ( - (!this.props.loading || this.state.hasLoadedData) && - this.props.collections.length > 0 - ); - } + const hasCollections = () => + (!loading || hasLoadedData) && collections.length > 0; - _renderLoader() { - if (this.props.loading && !this.state.hasLoadedData) return ; + const _renderLoader = () => { + if (loading && !hasLoadedData) return ; return null; - } + }; - _renderEmptyTable() { - if (!this.props.loading && this.props.collections.length === 0) { + const _renderEmptyTable = () => { + if (!loading && collections.length === 0) { return (

- {this.props.t('CollectionList.NoCollections')} + {t('CollectionList.NoCollections')}

); } return null; - } + }; - _getButtonLabel = (fieldName, displayName) => { - const { field, direction } = this.props.sorting; + const _getButtonLabel = (fieldName, displayName) => { + const { field, direction } = sorting; let buttonLabel; if (field !== fieldName) { if (field === 'name') { - buttonLabel = this.props.t('CollectionList.ButtonLabelAscendingARIA', { + buttonLabel = t('CollectionList.ButtonLabelAscendingARIA', { displayName }); } else { - buttonLabel = this.props.t('CollectionList.ButtonLabelDescendingARIA', { + buttonLabel = t('CollectionList.ButtonLabelDescendingARIA', { displayName }); } } else if (direction === SortingActions.DIRECTION.ASC) { - buttonLabel = this.props.t('CollectionList.ButtonLabelDescendingARIA', { + buttonLabel = t('CollectionList.ButtonLabelDescendingARIA', { displayName }); } else { - buttonLabel = this.props.t('CollectionList.ButtonLabelAscendingARIA', { + buttonLabel = t('CollectionList.ButtonLabelAscendingARIA', { displayName }); } return buttonLabel; }; - _renderFieldHeader = (fieldName, displayName) => { - const { field, direction } = this.props.sorting; + const _renderFieldHeader = (fieldName, displayName) => { + const { field, direction } = sorting; const headerClass = classNames({ 'sketches-table__header': true, 'sketches-table__header--selected': field === fieldName }); - const buttonLabel = this._getButtonLabel(fieldName, displayName); + const buttonLabel = _getButtonLabel(fieldName, displayName); return (