-
-
Notifications
You must be signed in to change notification settings - Fork 714
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Victor Fernandez de Alba <sneridagh@gmail.com> Co-authored-by: Nilesh <nileshgulia@gmail.com>
- Loading branch information
1 parent
7e5783b
commit ac3c119
Showing
4 changed files
with
93 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Refactored Anontools components. @Tishasoumya-02 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
// } |