diff --git a/news/4845.feature b/news/4845.feature new file mode 100644 index 0000000000..e8599a0811 --- /dev/null +++ b/news/4845.feature @@ -0,0 +1 @@ +Refactored Anontools components. @Tishasoumya-02 diff --git a/src/components/theme/Anontools/Anontools.jsx b/src/components/theme/Anontools/Anontools.jsx index 2b9e9fb5b6..ccff3638b0 100644 --- a/src/components/theme/Anontools/Anontools.jsx +++ b/src/components/theme/Anontools/Anontools.jsx @@ -1,83 +1,56 @@ -/** - * Anontools component. - * @module components/theme/Anontools/Anontools - */ - -import React, { Component } from 'react'; import PropTypes from 'prop-types'; -import { connect } from 'react-redux'; import { Link } from 'react-router-dom'; import { Menu } from 'semantic-ui-react'; import { FormattedMessage } from 'react-intl'; +import { flattenToAppURL } from '@plone/volto/helpers'; +import { useToken } from '@plone/volto/hooks/userSession/useToken'; +import { useContent } from '@plone/volto/hooks/content/useContent'; import config from '@plone/volto/registry'; -/** - * Anontools container class. - */ -export class Anontools extends Component { - /** - * Property types. - * @property {Object} propTypes Property types. - * @static - */ - static propTypes = { - token: PropTypes.string, - content: PropTypes.shape({ - '@id': PropTypes.string, - }), - }; - - /** - * Default properties. - * @property {Object} defaultProps Default properties. - * @static - */ - static defaultProps = { - token: null, - content: { - '@id': null, - }, - }; +const Anontools = () => { + const token = useToken(); + const { data: content } = useContent(); - /** - * Render method. - * @method render - * @returns {string} Markup for the component. - */ - render() { - const { settings } = config; - return ( - !this.props.token && ( - + const { settings } = config; + return ( + !token && ( + + + + + + + {settings.showSelfRegistration && ( - - + + - {settings.showSelfRegistration && ( - - - - - - )} - - ) - ); - } -} + )} + + ) + ); +}; + +export default Anontools; + +Anontools.propTypes = { + token: PropTypes.string, + content: PropTypes.shape({ + '@id': PropTypes.string, + }), +}; -export default connect((state) => ({ - token: state.userSession.token, - content: state.content.data, -}))(Anontools); +Anontools.defaultProps = { + token: null, + content: { + '@id': null, + }, +}; diff --git a/src/components/theme/Anontools/Anontools.test.jsx b/src/components/theme/Anontools/Anontools.test.jsx index 628e214925..28f54f9544 100644 --- a/src/components/theme/Anontools/Anontools.test.jsx +++ b/src/components/theme/Anontools/Anontools.test.jsx @@ -12,7 +12,14 @@ describe('Anontools', () => { it('renders an anontools component when no token is specified', () => { const store = mockStore({ userSession: { token: null }, - content: { data: { '@id': 'myid' } }, + content: { + data: { '@id': 'myid' }, + get: { + loading: false, + loaded: true, + error: null, + }, + }, intl: { locale: 'en', messages: {}, @@ -32,7 +39,14 @@ describe('Anontools', () => { it('should not render an anontools component when a token is specified', () => { const store = mockStore({ userSession: { token: '1234' }, - content: { data: {} }, + content: { + data: {}, + get: { + loading: false, + loaded: true, + error: null, + }, + }, intl: { locale: 'en', messages: {}, diff --git a/src/hooks/content/useContent.js b/src/hooks/content/useContent.js new file mode 100644 index 0000000000..3001c1d7a4 --- /dev/null +++ b/src/hooks/content/useContent.js @@ -0,0 +1,31 @@ +import { useSelector, shallowEqual } from 'react-redux'; + +/** + * useContent hook + * + * This hook returns the current content that is stored in the Redux store in the + * `content` reducer, and returns it along with the related state (loading/loaded/error). + * + * @export + * @return {{ data: ContentData, loading: boolean, loaded: boolean, error: Error }} + */ +export function useContent() { + const data = useSelector((state) => state.content.data, shallowEqual); + const loading = useSelector((state) => state.content.get.loading); + const loaded = useSelector((state) => state.content.get.loaded); + const error = useSelector((state) => state.content.get.error, shallowEqual); + + return { data, loading, loaded, error }; +} + +// For reference purposes: Potential future useQuery version +// export function useContent() { +// // the cache will need to know the current location +// const pathname = useLocation(); +// const query = useQuery(getContentQuery({ path })) + +// // This might not be needed if we rename the properties +// const {isLoading: loading, isSuccess: loaded, ...rest} = query; + +// return { loading, loaded, ...rest }; +// }