From b22af6def6042f3d93a0bbba312dcaec37671191 Mon Sep 17 00:00:00 2001 From: Kha Date: Mon, 20 Dec 2021 14:04:54 +0000 Subject: [PATCH] apps/budgeting/proposal-item-detail/voting: passing same information to template tag for detail proposal view. distuingish between if VoteButton used as list-item(React) or as widget(Django) and fetch differently --- .../apps/budgeting/assets/VoteButton.jsx | 14 +++++++++++- .../assets/react_vote_button_init.jsx | 22 +++++++++++++------ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/meinberlin/apps/budgeting/assets/VoteButton.jsx b/meinberlin/apps/budgeting/assets/VoteButton.jsx index bed5443176..85f8e8034e 100644 --- a/meinberlin/apps/budgeting/assets/VoteButton.jsx +++ b/meinberlin/apps/budgeting/assets/VoteButton.jsx @@ -15,13 +15,25 @@ export default class VoteButton extends React.Component { await updateItem({}, url, 'DELETE') } + triggerRender = () => { + // FIXME: this distuingishes between if + // it has a parent that handles onVoteChange + // or if it is used as widget, and therefore page + // has to be reloaded --> fix would be one asynchronous way + if (this.props.asWidget) { + window.location.reload() + } else { + this.props.onVoteChange(this.props.currentPage) + } + } + async handleOnChange () { if (this.props.isChecked) { await this.deleteVote() } else { await this.addVote() } - this.props.onVoteChange(this.props.currentPage) + this.triggerRender() } render () { diff --git a/meinberlin/apps/budgeting/assets/react_vote_button_init.jsx b/meinberlin/apps/budgeting/assets/react_vote_button_init.jsx index 53778f4c24..17ef79892a 100644 --- a/meinberlin/apps/budgeting/assets/react_vote_button_init.jsx +++ b/meinberlin/apps/budgeting/assets/react_vote_button_init.jsx @@ -1,15 +1,23 @@ import React from 'react' import ReactDOM from 'react-dom' import { widget as ReactWidget } from 'adhocracy4' -import { VoteButton } from './VoteButton.jsx' +import VoteButton from './VoteButton.jsx' function init () { - ReactWidget.initialise('mb', 'vote_button', - function (el) { - const props = el.getAttribute('data-attributes') - ReactDOM.render(, el) - } - ) + ReactWidget.initialise('mb', 'vote_button', function (el) { + const props = JSON.parse(el.getAttribute('data-attributes')) + const votesLeft = props.token_info ? props.token_info.votes_left : false + ReactDOM.render( + , + el + ) + }) } document.addEventListener('DOMContentLoaded', init, false)