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

static/Alert.jsx: refactor #1735

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions adhocracy4/comments_async/static/comments_async/comment.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import CommentList from './comment_list'
import { ModeratorFeedback } from './moderator_feedback'
import AiReport from './ai_report'
import RatingBox from '../../../ratings/static/ratings/RatingBox'
import Alert from '../../../static/Alert'

const translated = {
reportTitle: django.gettext('You want to report this content? Your message will be sent to the moderation. The moderation will look at the reported content. The content will be deleted if it does not meet our discussion rules (netiquette).'),
Expand Down Expand Up @@ -309,8 +310,7 @@ export default class Comment extends React.Component {

return (
<li>
{this.props.displayNotification &&
<div className="alert alert--success a4-comments__success-notification"><i className="fas fa-check" /> {translated.successMessage}</div>}
Copy link
Contributor Author

@hom3mad3 hom3mad3 Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this icon is such an edge case that i removed it (also inconsistently used)

{this.props.displayNotification && <Alert type="success" message={translated.successMessage} />}
<div className={(this.props.is_users_own_comment ? 'a4-comments__comment a4-comments__comment-owner' : 'a4-comments__comment')}>
<a className="a4-comments__anchor" id={'comment_' + this.props.id} href={'./?comment=' + this.props.id}>{'Comment ' + this.props.id}</a>
{this.renderDeleteModal()}
Expand Down
2 changes: 0 additions & 2 deletions adhocracy4/polls/static/PollDashboard/EditPollManagement.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ export const EditPollManagement = (props) => {
.done((data) => {
setQuestions(data.questions)
setAlert({
alertAttribute: 'polite',
type: 'success',
message: django.gettext('The poll has been updated.')
})
Expand All @@ -199,7 +198,6 @@ export const EditPollManagement = (props) => {
}

setAlert({
alertAttribute: 'assertive',
type: 'danger',
message: django.gettext(
'The poll could not be updated. Please check the data you entered again.'
Expand Down
50 changes: 29 additions & 21 deletions adhocracy4/static/Alert.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useRef, useEffect } from 'react'
import django from 'django'

const Alert = ({ type, alertAttribute, message, onClick, timeInMs }) => {
const Alert = ({ type = 'info', message, onClick, timeInMs }) => {
const timer = useRef()
const closeTag = django.gettext('Close')

Expand All @@ -14,28 +14,36 @@ const Alert = ({ type, alertAttribute, message, onClick, timeInMs }) => {
}
}, [timeInMs, onClick])

if (type) {
return (
<div
id="alert"
className={'alert alert--' + type}
aria-atomic="true"
aria-live={alertAttribute}
>
<div className="container a4-alert__container">
<div className="a4-alert__content">
{message}
</div>
<button className="alert__close" title={closeTag} onClick={onClick}>
<i className="fa fa-times" aria-label={closeTag} />
</button>
</div>

</div>
)
// Only check for message now since type has a default
if (!message) {
return null
}

return <div aria-live="assertive" aria-atomic="true" id="alert" />
// Use alert role for danger/warning, status for others
const ariaRole = ['danger', 'warning'].includes(type) ? 'alert' : 'status'

return (
<div
id="alert"
role={ariaRole}
className={'alert alert--' + type}
aria-atomic="true"
>
<div className="alert__content">
{message}
{onClick && (
<button
type="button"
className="alert__close"
aria-label={closeTag}
onClick={onClick}
>
<span className="fa fa-times" aria-hidden="true" />
</button>
)}
</div>
</div>
)
}

module.exports = Alert
2 changes: 1 addition & 1 deletion adhocracy4/static/__tests__/Alert.jest.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test('Alert is showing', () => {
test('Alert is not showing', () => {
render(<Alert type="" message="" />)
const alert = document.querySelector('#alert')
expect(alert).toBeTruthy()
expect(alert).toBeFalsy()
})

test('Invoke callback through click', () => {
Expand Down
2 changes: 2 additions & 0 deletions changelog/_8471.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
### Changed
- refactored static/Alert.jsx