-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Task/FP-1375: Admin controlled messages (#615)
* Add CustomMessages and CustomMessageTemplate models. * Add sagas and reducers for custom messages. * Add frontend components for CustomMessage. * Add tests for custom messages. * Fix tests. * Fix bug for template state. * Fix style * Correct dismissible spelling. * Refactor to embed template inside message. * Fix tests. * Remove TODO comments. * Revert whitespace changes. * Revert whitespace changes. * Allow info messages to have dismiss button. * Fix linting. * Remove unused css import. * Change TextField to CharField for sanity. * Use class selector instead of attribute selector. * Refactor messages. * Fix prettier linting. * Fix server linting. * Fix server testing. * Rename message to portal_messages. * Fix model. * Refactor put request for custom messages. * Refactor introMessageName to messageName. * Fix prettier linting. * Fix incorrect import. * Fix test. * Simplify custom message payload. * Rename messageName to introMessageName. * Fix linting. * Rename introMessages to introMessageComponents. * Fix linting. * Fix backend testing. Co-authored-by: Sal Tijerina <r.sal.tijerina@gmail.com>
- Loading branch information
1 parent
b3aa730
commit 6040fa4
Showing
47 changed files
with
752 additions
and
298 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
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
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
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
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
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
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
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
64 changes: 64 additions & 0 deletions
64
client/src/components/_common/CustomMessage/CustomMessage.jsx
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,64 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { SectionMessage } from '_common'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
import styles from './CustomMessage.module.scss'; | ||
|
||
/** | ||
* A message which, is created by the admin. Like IntroMessage, when dismissed, will not appear again. | ||
* | ||
* _This message is designed for custom messages from the admin. | ||
* | ||
* @example | ||
* // message with identifier | ||
* <CustomMessage | ||
* messageComponentName={identifierForMessageLikeRouteName} | ||
* > | ||
* </CustomMessage> | ||
*/ | ||
function CustomMessage({ messageComponentName }) { | ||
const dispatch = useDispatch(); | ||
const messages = useSelector((state) => { | ||
return state.customMessages | ||
? state.customMessages.messages.filter((message) => { | ||
return ( | ||
message.unread && | ||
message.template.component === messageComponentName | ||
); | ||
}) | ||
: []; | ||
}); | ||
|
||
function onDismiss(dismissMessage) { | ||
const payload = { | ||
templateId: dismissMessage.template.id, | ||
unread: false, | ||
}; | ||
dispatch({ | ||
type: 'SAVE_CUSTOM_MESSAGES', | ||
payload, | ||
}); | ||
} | ||
|
||
return messages.map((message) => { | ||
const template = message.template; | ||
return ( | ||
<div key={template.id} className={styles.message}> | ||
<SectionMessage | ||
type={template.message_type} | ||
canDismiss={template.dismissible} | ||
onDismiss={() => onDismiss(message)} | ||
> | ||
{template.message} | ||
</SectionMessage> | ||
</div> | ||
); | ||
}); | ||
} | ||
|
||
CustomMessage.propTypes = { | ||
/** A unique identifier for the message */ | ||
messageComponentName: PropTypes.string.isRequired, | ||
}; | ||
|
||
export default CustomMessage; |
4 changes: 4 additions & 0 deletions
4
client/src/components/_common/CustomMessage/CustomMessage.module.scss
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,4 @@ | ||
.message { | ||
margin-top: 12px; | ||
margin-bottom: 12px; | ||
} |
41 changes: 41 additions & 0 deletions
41
client/src/components/_common/CustomMessage/CustomMessage.test.jsx
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,41 @@ | ||
import React from 'react'; | ||
import { render } from '@testing-library/react'; | ||
import configureStore from 'redux-mock-store'; | ||
import { Provider } from 'react-redux'; | ||
|
||
import CustomMessage from './CustomMessage'; | ||
|
||
const mockStore = configureStore(); | ||
const store = mockStore({ | ||
customMessages: { | ||
messages: [ | ||
{ | ||
template: { | ||
id: 1, | ||
component: 'TEST', | ||
message_type: 'warning', | ||
dismissible: true, | ||
message: 'Test Message', | ||
}, | ||
unread: true, | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
describe('CustomMessage', () => { | ||
describe('elements', () => { | ||
it('renders message text, message type, and dismissability correctly', () => { | ||
const { container, getByText } = render( | ||
<Provider store={store}> | ||
<CustomMessage messageComponentName="TEST"></CustomMessage> | ||
</Provider> | ||
); | ||
expect(container.getElementsByClassName('is-warn').length).toEqual(1); | ||
expect(container.getElementsByClassName('close-button').length).toEqual( | ||
1 | ||
); | ||
expect(getByText('Test Message')).not.toEqual(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './CustomMessage'; |
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
Oops, something went wrong.