NotAvailableForGuest.jsx 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { UncontrolledTooltip } from 'reactstrap';
  4. import AppContainer from '../services/AppContainer';
  5. import { withUnstatedContainers } from './UnstatedUtils';
  6. const NotAvailableForGuest = (props) => {
  7. const { appContainer, children } = props;
  8. const isLoggedin = appContainer.currentUser != null;
  9. if (isLoggedin) {
  10. return props.children;
  11. }
  12. const id = children.props.id || `grw-not-available-for-guest-${Math.random().toString(32).substring(2)}`;
  13. // clone and add className
  14. const clonedChild = React.cloneElement(children, {
  15. id,
  16. className: `${children.props.className} grw-not-available-for-guest`,
  17. onClick: () => { /* do nothing */ },
  18. });
  19. return (
  20. <>
  21. { clonedChild }
  22. <UncontrolledTooltip placement="top" target={id}>Not available for guest</UncontrolledTooltip>
  23. </>
  24. );
  25. };
  26. NotAvailableForGuest.propTypes = {
  27. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  28. children: PropTypes.node.isRequired,
  29. };
  30. export default withUnstatedContainers(NotAvailableForGuest, [AppContainer]);