Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
budgeting/assets//templatetags: pipe through voting api url and use a…
Browse files Browse the repository at this point in the history
…ctions on voting button
rine committed Dec 16, 2021
1 parent 98b2a92 commit ec05a1f
Showing 4 changed files with 80 additions and 16 deletions.
7 changes: 7 additions & 0 deletions meinberlin/apps/budgeting/assets/BudgetingProposalList.jsx
Original file line number Diff line number Diff line change
@@ -37,6 +37,10 @@ export const BudgetingProposalList = (props) => {
setQueryString(filterString)
}

const onVoteChange = (selectedPage) => {
fetchProposals(selectedPage)
}

useEffect(fetchProposals, [queryString])

const renderList = (data) => {
@@ -50,6 +54,9 @@ export const BudgetingProposalList = (props) => {
key={`budgeting-proposal-${idx}`}
proposal={proposal}
isVotingPhase={props.is_voting_phase}
tokenvoteApiUrl={props.tokenvote_api_url}
onVoteChange={onVoteChange}
currentPage={meta?.current_page}
/>)}
</ul>
{meta?.is_paginated &&
10 changes: 7 additions & 3 deletions meinberlin/apps/budgeting/assets/BudgetingProposalListItem.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from 'react'
import django from 'django'
import { toLocaleDate } from './helpers'
import { VoteButton } from './VoteButton.jsx'
import VoteButton from './VoteButton'
import { ListItemBadges } from './ListItemBadges'
import { ListItemStats } from './ListItemStats'

export const BudgetingProposalListItem = (props) => {
const { proposal, isVotingPhase } = props
const { proposal, isVotingPhase, tokenvoteApiUrl } = props
const safeLocale = proposal.locale ? proposal.locale : undefined
const date = proposal.modified
? `${django.gettext('modified on')} ${toLocaleDate(proposal.modified, safeLocale)}`
@@ -43,7 +43,11 @@ export const BudgetingProposalListItem = (props) => {
<VoteButton
onClass="btn"
offClass="btn btn--light"
uniqueID={proposal.pk}
objectID={proposal.pk}
tokenvoteApiUrl={tokenvoteApiUrl}
isChecked={proposal.session_token_voted}
onVoteChange={props.onVoteChange}
currentPage={props.currentPage}
/>}
</div>
</li>
72 changes: 59 additions & 13 deletions meinberlin/apps/budgeting/assets/VoteButton.jsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,63 @@
import React from 'react'
import django from 'django'
import CheckboxButton from '../../contrib/assets/CheckboxButton'
import { updateItem } from '../../livequestions/assets/helpers.js'

export const VoteButton = (props) => {
return (
<>
<CheckboxButton
onText={django.gettext('Voted')}
offText={django.gettext('Give my vote')}
onClass="btn btn--full"
offClass="btn btn--full btn--light"
uniqueID={123}
/>
</>
)
export default class VoteButton extends React.Component {
constructor (props) {
super(props)
this.state = {
voted: props.isChecked
}
}

componentDidMount () {
this.setState({ voted: this.props.isChecked })
}

async addVote () {
const data = {
object_id: this.props.objectID
}
await updateItem(data, this.props.tokenvoteApiUrl, 'POST')
}

async deleteVote () {
const url = this.props.tokenvoteApiUrl + this.props.objectID + '/'
await updateItem({}, url, 'DELETE')
}

handleOnChange () {
if (this.state.voted) {
this.deleteVote()
} else {
this.addVote()
}
this.props.onVoteChange(this.props.currentPage)
// this.setState({ voted: !this.state.voted })
}

render () {
const checkedText = django.gettext('Voted')
const uncheckedText = django.gettext('Give my vote')
const checkedClass = 'btn btn--full'
const uncheckedClass = 'btn btn--full btn--light'

return (
<div>
<label
htmlFor={this.props.objectID}
className={this.state.voted ? checkedClass : uncheckedClass}
>
<input
id={this.props.objectID}
className="checkbox-btn__input"
type="checkbox"
checked={this.state.voted}
onChange={(e) => this.handleOnChange(e)}
/>
<span>{this.state.voted ? checkedText : uncheckedText}</span>
</label>
</div>
)
}
}
7 changes: 7 additions & 0 deletions meinberlin/apps/budgeting/templatetags/react_proposals.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json

from django import template
from django.contrib.contenttypes.models import ContentType
from django.urls import reverse
from django.utils.html import format_html

@@ -14,7 +15,13 @@
def react_proposals(context, module):
proposals_api_url = reverse('proposals-list',
kwargs={'module_pk': module.pk})
proposal_ct = ContentType.objects.get(app_label='meinberlin_budgeting',
model='proposal')
tokenvote_api_url = reverse('tokenvotes-list',
kwargs={'content_type': proposal_ct.id})

attributes = {'proposals_api_url': proposals_api_url,
'tokenvote_api_url': tokenvote_api_url,
'is_voting_phase': has_feature_active(module,
Proposal,
'vote')

0 comments on commit ec05a1f

Please sign in to comment.