Skip to content

Commit

Permalink
Merge pull request #3209 from processing/react-fc-conversion-2
Browse files Browse the repository at this point in the history
React component conversion(class to functional) - 2
  • Loading branch information
nahbee10 authored Aug 11, 2024
2 parents 87b4199 + 4b19f9e commit 2b9cb0a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 38 deletions.
17 changes: 15 additions & 2 deletions client/modules/IDE/components/QuickAddList/QuickAddList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,21 @@ const Item = ({ isAdded, onSelect, name, url }) => {
const buttonLabel = isAdded
? t('QuickAddList.ButtonRemoveARIA')
: t('QuickAddList.ButtonAddToCollectionARIA');

const handleKeyDown = (event) => {
if (event.key === 'Enter' || event.key === ' ') {
onSelect(event);
}
};

return (
<li className="quick-add__item" onClick={onSelect}> { /* eslint-disable-line */ }
<div
role="button"
className="quick-add__item"
onClick={onSelect}
onKeyDown={handleKeyDown}
tabIndex={0}
>
<button
className="quick-add__item-toggle"
onClick={onSelect}
Expand All @@ -28,7 +41,7 @@ const Item = ({ isAdded, onSelect, name, url }) => {
>
{t('QuickAddList.View')}
</Link>
</li>
</div>
);
};

Expand Down
36 changes: 22 additions & 14 deletions client/modules/IDE/components/Searchbar/Collection.jsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import React from 'react';
import { useDispatch, useSelector } from 'react-redux';
import i18next from 'i18next';
import * as SortingActions from '../../actions/sorting';

import Searchbar from './Searchbar';

const scope = 'collection';

function mapStateToProps(state) {
return {
searchLabel: i18next.t('Searchbar.SearchCollection'),
searchTerm: state.search[`${scope}SearchTerm`]
const SearchbarContainer = () => {
const dispatch = useDispatch();
const searchLabel = i18next.t('Searchbar.SearchCollection');
const searchTerm = useSelector((state) => state.search[`${scope}SearchTerm`]);

const setSearchTerm = (term) => {
dispatch(SortingActions.setSearchTerm(scope, term));
};
}

function mapDispatchToProps(dispatch) {
const actions = {
setSearchTerm: (term) => SortingActions.setSearchTerm(scope, term),
resetSearchTerm: () => SortingActions.resetSearchTerm(scope)
const resetSearchTerm = () => {
dispatch(SortingActions.resetSearchTerm(scope));
};
return bindActionCreators(Object.assign({}, actions), dispatch);
}

export default connect(mapStateToProps, mapDispatchToProps)(Searchbar);
return (
<Searchbar
searchLabel={searchLabel}
searchTerm={searchTerm}
setSearchTerm={setSearchTerm}
resetSearchTerm={resetSearchTerm}
/>
);
};

export default SearchbarContainer;
13 changes: 6 additions & 7 deletions client/modules/IDE/components/Searchbar/Searchbar.jsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import React, { useState, useCallback, useEffect } from 'react';
import PropTypes from 'prop-types';
import { throttle } from 'lodash';
import { withTranslation } from 'react-i18next';
import { useTranslation } from 'react-i18next';
import i18next from 'i18next';
import SearchIcon from '../../../../images/magnifyingglass.svg';

const Searchbar = ({
searchTerm,
setSearchTerm,
resetSearchTerm,
searchLabel,
t
searchLabel
}) => {
const { t } = useTranslation();
const [searchValue, setSearchValue] = useState(searchTerm);

const throttledSearchChange = useCallback(
throttle((value) => {
setSearchTerm(value.trim());
}, 500),
[]
[setSearchTerm]
);

const handleResetSearch = () => {
Expand Down Expand Up @@ -65,12 +65,11 @@ Searchbar.propTypes = {
searchTerm: PropTypes.string.isRequired,
setSearchTerm: PropTypes.func.isRequired,
resetSearchTerm: PropTypes.func.isRequired,
searchLabel: PropTypes.string,
t: PropTypes.func.isRequired
searchLabel: PropTypes.string
};

Searchbar.defaultProps = {
searchLabel: i18next.t('Searchbar.SearchSketch')
};

export default withTranslation()(Searchbar);
export default Searchbar;
37 changes: 22 additions & 15 deletions client/modules/IDE/components/Searchbar/Sketch.jsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import React from 'react';
import { useDispatch, useSelector } from 'react-redux';
import i18next from 'i18next';
import * as SortingActions from '../../actions/sorting';

import Searchbar from './Searchbar';

const scope = 'sketch';

function mapStateToProps(state) {
return {
searchLabel: i18next.t('Searchbar.SearchSketch'),
searchTerm: state.search[`${scope}SearchTerm`]
const SearchbarContainer = () => {
const dispatch = useDispatch();
const searchLabel = i18next.t('Searchbar.SearchSketch');
const searchTerm = useSelector((state) => state.search[`${scope}SearchTerm`]);

const setSearchTerm = (term) => {
dispatch(SortingActions.setSearchTerm(scope, term));
};
}

function mapDispatchToProps(dispatch) {
const actions = {
setSearchTerm: (term) => SortingActions.setSearchTerm(scope, term),
resetSearchTerm: () => SortingActions.resetSearchTerm(scope)
const resetSearchTerm = () => {
dispatch(SortingActions.resetSearchTerm(scope));
};
return bindActionCreators(Object.assign({}, actions), dispatch);
}

export default connect(mapStateToProps, mapDispatchToProps)(Searchbar);
return (
<Searchbar
searchLabel={searchLabel}
searchTerm={searchTerm}
setSearchTerm={setSearchTerm}
resetSearchTerm={resetSearchTerm}
/>
);
};

export default SearchbarContainer;

0 comments on commit 2b9cb0a

Please sign in to comment.