Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Messages #4926

Merged
merged 19 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/4926.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Messages Component Refactor - @Tishasoumya-02
131 changes: 32 additions & 99 deletions src/components/manage/Messages/Messages.jsx
Original file line number Diff line number Diff line change
@@ -1,107 +1,40 @@
/**
* Messages component.
* @module components/manage/Messages/Messages
*/

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { useDispatch, useSelector, shallowEqual } from 'react-redux';
import { Message, Container } from 'semantic-ui-react';
import { map } from 'lodash';

import { removeMessage } from '@plone/volto/actions';

/**
* Messages container class.
* @class Messages
* @extends Component
*/
class Messages extends Component {
/**
* Property types.
* @property {Object} propTypes Property types.
* @static
*/
static propTypes = {
removeMessage: PropTypes.func.isRequired,
messages: PropTypes.arrayOf(
PropTypes.shape({
title: PropTypes.string,
body: PropTypes.string,
level: PropTypes.string,
}),
).isRequired,
};

/**
* Constructor
* @method constructor
* @param {Object} props Component properties
* @constructs Messages
*/
constructor(props) {
super(props);
this.onDismiss = this.onDismiss.bind(this);
}
const Messages = () => {
const dispatch = useDispatch();

// /**
// * Component will receive props
// * @method componentWillReceiveProps
// * @param {Object} nextProps Next properties
// * @returns {undefined}
// */
// componentWillReceiveProps(nextProps) {
// if (nextProps.messages.length > this.props.messages.length) {
// window.setTimeout(() => {
// if (this.props.messages.length > 0) {
// this.props.removeMessage(-1);
// }
// }, 6000);
// }
// }
const messages = useSelector(
(state) => state.messages.messages,
shallowEqual,
);

/**
* On dismiss
* @method onDismiss
* @param {Object} event Event object
* @param {number} value Index of message
* @returns {undefined}
*/
onDismiss(event, { value }) {
this.props.removeMessage(value);
}

/**
* Render method.
* @method render
* @returns {string} Markup for the component.
*/
render() {
return (
this.props.messages && (
<Container className="messages">
{map(this.props.messages, (message, index) => (
<Message
key={message.id}
value={index}
onDismiss={this.onDismiss}
error={message.level === 'error'}
success={message.level === 'success'}
warning={message.level === 'warning'}
info={message.level === 'info'}
header={message.title}
content={message.body}
/>
))}
</Container>
)
);
}
}
const onDismiss = (event, { value }) => {
dispatch(removeMessage(value));
};

export default connect(
(state) => ({
messages: state.messages.messages,
}),
{ removeMessage },
)(Messages);
return (
messages && (
<Container className="messages">
{map(messages, (message, index) => (
<Message
key={message.id}
value={index}
onDismiss={onDismiss}
error={message.level === 'error'}
success={message.level === 'success'}
warning={message.level === 'warning'}
info={message.level === 'info'}
header={message.title}
content={message.body}
/>
))}
</Container>
)
);
};

export default Messages;
1 change: 0 additions & 1 deletion src/components/manage/Messages/Messages.test.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from 'react';
import renderer from 'react-test-renderer';
import configureStore from 'redux-mock-store';
import { Provider } from 'react-redux';
Expand Down