Skip to content

Commit

Permalink
userdashboard/AiReportExplanation: add switch component to display/hi…
Browse files Browse the repository at this point in the history
…de ai explanation on frontend with test
  • Loading branch information
philli-m authored and goapunk committed Aug 10, 2023
1 parent e893b96 commit c0f6b65
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 22 deletions.
32 changes: 32 additions & 0 deletions adhocracy-plus/assets/scss/components/_button.scss
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,35 @@
padding-right: 0.5rem;
}
}

.btn-switch {
display: flex;
}

.btn-switch__switch {
display: inline-block;
width: 36px;
height: 22px;
margin-inline-end: 0.5 * $spacer;
border: 2px solid $brand-primary;
background-color: $brand-primary;
border-radius: 20px;

}

.btn-switch__adjuster {
display: inline-block;
border: 9px solid $body-bg;
border-radius: 50px;
}

[aria-pressed="false"] .btn-switch__switch,
[aria-checked="false"] .btn-switch__switch {
border-color: $brand-secondary;
background-color: $brand-secondary;
}

[aria-pressed="true"] .btn-switch__adjuster,
[aria-checked="true"] .btn-switch__adjuster {
transform: translate(14px);
}
29 changes: 29 additions & 0 deletions apps/contrib/assets/SwitchButton.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React, { useState } from 'react'

export const SwitchButton = (props) => {
const [isChecked, setIsChecked] = useState(false)

const toggleIsChecked = () => {
setIsChecked(!isChecked)
}

return (
<button
id={props.id}
className="btn-switch"
aria-labelledby={'switch-id-' + props.id}
role="switch"
aria-checked={isChecked}
onClick={toggleIsChecked}
>
<span className="btn-switch__switch">
<span className="btn-switch__adjuster" />
</span>
<span
id={'switch-id-' + props.id}
>
{props.switchLabel}
</span>
</button>
)
}
25 changes: 25 additions & 0 deletions apps/contrib/assets/__tests__/SwitchButton.jest.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react'
import { render, screen, fireEvent } from '@testing-library/react'
import '@testing-library/jest-dom'
import { SwitchButton } from '../SwitchButton'

test('Show button correctly', () => {
render(
<SwitchButton
switchLabel="This is the label for the switch"
/>
)
expect(screen.getByText('This is the label for the switch')).toBeInTheDocument()
})

test('Switch button should change state onclick', () => {
const { getByRole } = render(<SwitchButton />)
const switchButton = getByRole('switch')

expect(switchButton).toHaveAttribute('aria-checked', 'false')
fireEvent.click(switchButton)
expect(switchButton).toHaveAttribute('aria-checked', 'true')
fireEvent.click(switchButton)
// State should be updated back to false after the second click
expect(switchButton).toHaveAttribute('aria-checked', 'false')
})
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useState } from 'react'
import django from 'django'
import { SwitchButton } from '../../../../../apps/contrib/assets/SwitchButton'

const translated = {
intro: django.gettext('This comment contains disinformation. Defakts uses an Artificial ' +
Expand All @@ -11,31 +12,39 @@ const translated = {
readLess: django.gettext('Read less')
}

export const AIReportExplanation = ({ AIReport }) => {
export const AiReportExplanation = ({ AIReport, notificationPk }) => {
const [isExpanded, setIsExpanded] = useState()

const toggleExpand = () => {

Check warning on line 18 in apps/userdashboard/assets/js/a4_candy_userdashboard/AiReportExplanation.jsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹 Function is not covered

Warning! Not covered function
setIsExpanded(!isExpanded)

Check warning on line 19 in apps/userdashboard/assets/js/a4_candy_userdashboard/AiReportExplanation.jsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

return (
<li className="alert alert--danger d-flex text-start mb-3">
<i
className="fas fa-exclamation-circle text-danger pt-1 pe-2"
aria-hidden="True"
/>
<p>
{translated.intro + ' '}
{isExpanded && AIReport + ' '}
<button
className="btn--link text-danger"
type="button"
onClick={toggleExpand}
aria-label={isExpanded ? translated.ariaReadLess : translated.ariaReadMore}
>
{isExpanded ? translated.readLess : translated.readMore}
</button>
</p>
<li className="alert alert--danger mb-3">
<div className="d-flex text-start mb-4">
<i
className="fas fa-exclamation-circle text-danger pt-1 pe-2"
aria-hidden="True"
/>
<p>
{translated.intro + ' '}
{isExpanded && AIReport + ' '}

Check warning on line 31 in apps/userdashboard/assets/js/a4_candy_userdashboard/AiReportExplanation.jsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
<button
className="btn--link text-danger"
type="button"
onClick={toggleExpand}
aria-label={isExpanded ? translated.ariaReadLess : translated.ariaReadMore}

Check warning on line 36 in apps/userdashboard/assets/js/a4_candy_userdashboard/AiReportExplanation.jsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
>
{isExpanded ? translated.readLess : translated.readMore}

Check warning on line 38 in apps/userdashboard/assets/js/a4_candy_userdashboard/AiReportExplanation.jsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch
</button>
</p>
</div>
<div className="d-flex text-start">
<SwitchButton
id={notificationPk}
switchLabel={translated.readLess}
/>
</div>
</li>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export const ModerationFeedback = (props) => {
delete: django.gettext('delete'),
edit: django.gettext('edit'),
feedbackTitle: django.gettext('Moderator\'s feedback'),
editWasOn: django.gettext('Last edit was on ')
editWasOn: django.gettext('Last edit was on '),
feedbackMenu: django.gettext('Feedback menu')

}

return (
Expand All @@ -21,7 +23,7 @@ export const ModerationFeedback = (props) => {
<div className="text-end">
<div className="dropdown">
<button
title="{% trans 'Feedback menu' %}"
title={translated.feedbackMenu}
type="button"
className="dropdown-toggle btn btn--none"
aria-haspopup="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const translated = {
notificationUnread: django.gettext('Notification successfully marked as unread.'),
aiClassified: django.gettext('AI'),
postedComment: django.gettext('posted a {}comment{}'),
notificationMenu: django.gettext('Notification menu'),
dummyText: django.gettext('Dummy text')

}
Expand Down Expand Up @@ -272,7 +273,7 @@ export const ModerationNotification = (props) => {
<div className="ms-auto">
<div className="dropdown">
<button
title="{% trans 'Notification menu' %}"
title={translated.notificationMenu}
type="button"
className="dropdown-toggle btn btn--none"
aria-haspopup="true"
Expand Down Expand Up @@ -307,7 +308,10 @@ export const ModerationNotification = (props) => {
<p>{commentText}</p>
</li>
{/* FIXME add if once report added to serializer */}
<AiReportExplanation AiReport={translated.dummyText} />
<AiReportExplanation
AiReport={translated.dummyText}
notificationPk={notification.pk}
/>
<ModerationNotificationActionsBar
itemPk={notification.pk}
isEditing={notification.moderator_feedback}
Expand Down
4 changes: 4 additions & 0 deletions changelog/7542.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### Added

- general switch button react component
- switch button to AIreportExplanation component
1 change: 1 addition & 0 deletions docs/ai_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ FIXME add docs for AI report, for now just list coresponding files

### React components
apps/userdashboard/assets/js/a4_candy_userdashboard/AiReportExplanation.jsx
apps/contrib/assets/SwitchButton.jsx

0 comments on commit c0f6b65

Please sign in to comment.