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

[#8546] Style statistics #5750

Merged
merged 1 commit into from
Nov 19, 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
6 changes: 6 additions & 0 deletions changelog/_8546.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
### Removed
- Removed react_questions_statistics.jsx

### Changed

- Style `StatisticsBox`
25 changes: 25 additions & 0 deletions meinberlin/assets/scss/components_user_facing/_live_questions.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,28 @@
margin-left: auto;
margin-bottom: .625em;
}

.live-question__progress {
display: flex;
gap: 8px;
margin-bottom: 8px;
position: relative;
}

.live-question__progress-bar {
background-color: $gray-lighter;
border-radius: 10px;
height: 100%;
position: absolute;
z-index: 0;
}

.live-question__progress-text {
padding: 0 0.5rem;
position: relative;
z-index: 1;
}

.live-question__progress-percentage {
font-weight: bold;
}
2 changes: 1 addition & 1 deletion meinberlin/react/livequestions/QuestionBox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export default class QuestionBox extends React.Component {
: <StatisticsBox
answeredQuestions={this.state.answeredQuestions}
questions_api_url={this.props.questions_api_url}
categories={this.props.categories}
category_dict={this.props.category_dict}
isModerator={this.props.isModerator}
/>}
</div>
Expand Down
188 changes: 90 additions & 98 deletions meinberlin/react/livequestions/StatisticsBox.jsx
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you convert this to a functional component as well? Or did you plan to do that in another PR?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is now converted.

Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
import django from 'django'
import { updateItem } from '../contrib/helpers.js'
import React from 'react'
import React, { useState, useEffect } from 'react'
import QuestionUser from './QuestionUser'
import QuestionModerator from './QuestionModerator'

export default class StatisticsBox extends React.Component {
constructor (props) {
super(props)
this.state = { answeredQuestions: props.answeredQuestions }
}
const questionAnsweredTag = django.gettext('Questions Answered')
const categoriesTag = django.gettext('Categories')
const categoriesAnsweredTag = django.gettext('Affiliation Of Answered Questions')

UNSAFE_componentWillReceiveProps (props) {
this.setState({ answeredQuestions: props.answeredQuestions })
}
function StatisticsBox (props) {
const [answeredQuestions, setAnsweredQuestions] = useState(props.answeredQuestions)

updateQuestion (data, id) {
const url = this.props.questions_api_url + id + '/'
useEffect(() => {
setAnsweredQuestions(props.answeredQuestions)
}, [props.answeredQuestions])

const updateQuestion = (data, id) => {
const url = props.questions_api_url + id + '/'
return updateItem(data, url, 'PATCH')
}

removeFromList (id, data) {
this.updateQuestion(data, id)
.then(response => this.setState(prevState => ({
answeredQuestions: prevState.answeredQuestions.filter(question => question.id !== id)
})))
const removeFromList = (id, data) => {
updateQuestion(data, id)
.then(() => setAnsweredQuestions(prevQuestions => prevQuestions.filter(question => question.id !== id)
))
}

countCategory (category) {
const countCategory = (category) => {
let countPerCategory = 0
let answeredQuestions = 0
this.props.answeredQuestions.forEach(function (question) {
props.answeredQuestions.forEach(function (question) {
if (question.is_answered && !question.is_hidden) {
answeredQuestions++
if (question.category === category) {
Expand All @@ -40,86 +40,78 @@ export default class StatisticsBox extends React.Component {
return Math.round(countPerCategory * 100 / answeredQuestions) || 0
}

render () {
const questionAnsweredTag = django.gettext('Questions Answered')
const categoriesAnsweredTag = django.gettext('Affiliation Of Answered Questions')

return (
<div className="module-content--light">
<div className="container">
<div className="offset-lg-2 col-lg-8">
{this.props.categories.length > 0 &&
<div>
<h3>{categoriesAnsweredTag}</h3>
<div className="list-item">
{this.props.categories.map((category, index) => {
const countPerCategory = this.countCategory(category)
const style = { width: countPerCategory + '%' }
return (
<div key={index} className="u-spacer-bottom-half">
<div className="progress">
<div
className="progress-bar" style={style} role="progressbar" aria-valuenow="25" aria-valuemin="0"
aria-valuemax="100"
/>
<span className="progress-bar__stats">&nbsp;{category}&nbsp;{countPerCategory}%</span>
</div>
</div>
)
})}
return (
<div className="container">
{Object.keys(props.category_dict).length > 0 && (
<>
<h2>{categoriesAnsweredTag}</h2>
<div className="modul-card card">
<h3>{categoriesTag}</h3>
{Object.keys(props.category_dict).map((id) => {
const countPerCategory = countCategory(props.category_dict[id])
const style = { width: countPerCategory + '%' }
return (
<div className="live-question__progress" key={id}>
<div
className="live-question__progress-bar"
style={style}
role="progressbar"
aria-valuenow={countPerCategory}
aria-valuemin="0"
aria-valuemax="100"
/>
<div className="live-question__progress-text">
<span className="live-question__progress-percentage">
{countPerCategory}%
</span>{' '}
<span>{props.category_dict[id]}</span>
</div>
</div>
</div>}
<h3>{questionAnsweredTag}</h3>
{this.props.isModerator
? (
<div className="list-group">
{this.state.answeredQuestions.map((question, index) => {
return (
<QuestionModerator
updateQuestion={this.updateQuestion.bind(this)}
displayIsOnShortlist={false}
displayIsLive={false}
displayIsHidden={false}
displayIsAnswered={question.is_answered}
removeFromList={this.removeFromList.bind(this)}
key={question.id}
id={question.id}
is_answered={question.is_answered}
is_on_shortlist={question.is_on_shortlist}
is_live={question.is_live}
is_hidden={question.is_hidden}
category={question.category}
likes={question.likes}
>
{question.text}
</QuestionModerator>
)
})}
</div>
)
: (
<div className="list-group">
{this.state.answeredQuestions.map((question, index) => {
return (
<QuestionUser
key={question.id}
id={question.id}
is_answered={question.is_answered}
is_on_shortlist={question.is_on_shortlist}
is_live={question.is_live}
is_hidden={question.is_hidden}
category={question.category}
likes={question.likes}
>
{question.text}
</QuestionUser>
)
})}
</div>
)}
)
})}
</div>
</div>
</div>
)
}
</>
)}
<h2>
{questionAnsweredTag} ({answeredQuestions.length})
</h2>
{props.isModerator
? answeredQuestions.map((question) => (
<QuestionModerator
updateQuestion={updateQuestion}
displayIsOnShortlist={false}
displayIsLive={false}
displayIsHidden={false}
displayIsAnswered={question.is_answered}
removeFromList={removeFromList}
key={question.id}
id={question.id}
is_answered={question.is_answered}
is_on_shortlist={question.is_on_shortlist}
is_live={question.is_live}
is_hidden={question.is_hidden}
category={question.category}
likes={question.likes}
>
{question.text}
</QuestionModerator>
))
: answeredQuestions.map((question) => (
<QuestionUser
key={question.id}
id={question.id}
is_answered={question.is_answered}
is_on_shortlist={question.is_on_shortlist}
is_live={question.is_live}
is_hidden={question.is_hidden}
category={question.category}
likes={question.likes}
>
{question.text}
</QuestionUser>
))}
</div>
)
}

export default StatisticsBox
13 changes: 0 additions & 13 deletions meinberlin/react/livequestions/react_questions_statistics.jsx

This file was deleted.

Loading