-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
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…
…ctions on voting button
rine
committed
Dec 16, 2021
1 parent
98b2a92
commit ec05a1f
Showing
4 changed files
with
80 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters