Skip to content

Commit

Permalink
Refactor Anontools (#4845)
Browse files Browse the repository at this point in the history
Co-authored-by: Victor Fernandez de Alba <sneridagh@gmail.com>
Co-authored-by: Nilesh <nileshgulia@gmail.com>
  • Loading branch information
3 people authored Jun 19, 2023
1 parent 7e5783b commit ac3c119
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 74 deletions.
1 change: 1 addition & 0 deletions news/4845.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Refactored Anontools components. @Tishasoumya-02
117 changes: 45 additions & 72 deletions src/components/theme/Anontools/Anontools.jsx
Original file line number Diff line number Diff line change
@@ -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 && (
<Menu pointing secondary floated="right">
const { settings } = config;
return (
!token && (
<Menu pointing secondary floated="right">
<Menu.Item>
<Link
aria-label="login"
to={`/login${
content?.['@id']
? `?return_url=${flattenToAppURL(content['@id'])}`
: ''
}`}
>
<FormattedMessage id="Log in" defaultMessage="Log in" />
</Link>
</Menu.Item>
{settings.showSelfRegistration && (
<Menu.Item>
<Link
aria-label="login"
to={`/login${
this.props.content?.['@id']
? `?return_url=${this.props.content['@id'].replace(
settings.apiPath,
'',
)}`
: ''
}`}
>
<FormattedMessage id="Log in" defaultMessage="Log in" />
<Link aria-label="register" to="/register">
<FormattedMessage id="Register" defaultMessage="Register" />
</Link>
</Menu.Item>
{settings.showSelfRegistration && (
<Menu.Item>
<Link aria-label="register" to="/register">
<FormattedMessage id="Register" defaultMessage="Register" />
</Link>
</Menu.Item>
)}
</Menu>
)
);
}
}
)}
</Menu>
)
);
};

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,
},
};
18 changes: 16 additions & 2 deletions src/components/theme/Anontools/Anontools.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
Expand All @@ -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: {},
Expand Down
31 changes: 31 additions & 0 deletions src/hooks/content/useContent.js
Original file line number Diff line number Diff line change
@@ -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 };
// }

0 comments on commit ac3c119

Please sign in to comment.