Skip to content

Commit

Permalink
Create Position Store action and constants
Browse files Browse the repository at this point in the history
  • Loading branch information
lisamburns committed Feb 16, 2016
1 parent 9d6d520 commit 03d48d1
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 82 deletions.
7 changes: 4 additions & 3 deletions src/js/actions/BallotActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ var BallotConstants = require('../constants/BallotConstants');
// When action calls one of these functions, we are telling the code in the AppDispatcher block to run
module.exports = {

downloadOrganizations: function( we_vote_id) {
positionsRetrieved: function( we_vote_id, payload) {
AppDispatcher.dispatch({
actionType: BallotConstants.DOWNLOAD_ORGANIZATIONS,
we_vote_id
actionType: BallotConstants.POSITIONS_RETRIEVED,
payload: payload,
we_vote_id: we_vote_id
});
},

Expand Down
17 changes: 17 additions & 0 deletions src/js/actions/PositionActions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';
var AppDispatcher = require('../dispatcher/AppDispatcher');
var PositionConstants = require('../constants/BallotConstants');

// In the stores, there are AppDispatcher blocks that listen for these actionType constants (ex/ VOTER_SUPPORTING_SAVE)
// When action calls one of these functions, we are telling the code in the AppDispatcher block to run
module.exports = {

positionRetrieved: function( we_vote_id, payload) {
AppDispatcher.dispatch({
actionType: PositionConstants.POSITION_RETRIEVED,
payload: payload,
we_vote_id: we_vote_id
});
},

};
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
import React, { Component, PropTypes } from 'react';
import { Link } from 'react-router';
import BallotActions from '../../actions/BallotActions';
import BallotStore from '../../stores/BallotStore';
import PositionActions from '../../actions/PositionActions';
import PositionStore from '../../stores/PositionStore';

export default class OrganizationItem extends Component {
export default class PositionItem extends Component {
static propTypes = {
position_we_vote_id: PropTypes.string.isRequired,
};

constructor (props) {
super(props);
this.state = { organization: this.props};
this.state = { position: {} };
}

componentDidMount () {
BallotStore.addChangeListener(this._onChange.bind(this));
console.log("Position Item Component Mounted with wevoteid:")
console.log(this.props.position_we_vote_id);
PositionStore.retrievePositionByWeVoteId(this.props.position_we_vote_id);
PositionStore.addChangeListener(this._onChange.bind(this));
}

componentWillUnmount () {
BallotStore.removeChangeListener(this._onChange.bind(this));
PositionStore.removeChangeListener(this._onChange.bind(this));
}

_onChange () {
// this.setState({ organization: BallotStore.getOrganization(this.props.candidate_we_vote_id, this.props.item.position_we_vote_id) });
this.setState({ position: PositionStore.getLocalPositionByWeVoteId(this.props.position_we_vote_id) });
console.log("This Position Item:")
console.log(this.state.position);
}

render() {
var organization = this.state.organization;
var supportText = organization.is_oppose ? "Opposes" : "Supports";
// console.log(this.state.position);
var position = this.state.position;
var supportText = position.is_oppose ? "Opposes" : "Supports";
return (
<div>
<li className="list-group-item">
Expand All @@ -40,16 +46,15 @@ export default class OrganizationItem extends Component {
<div className="pull-right col-xs-10 col-md-8">
<h4 className="">
<Link className="" to="ballot_candidate_one_org_position"
params={{id: organization.speaker_id, org_id: organization.speaker_we_vote_id}}>
{ organization.speaker_label }
params={{id: position.speaker_id, org_id: position.speaker_we_vote_id}}>
{ position.speaker_label }
</Link>
</h4>
<p className="">{supportText} <span className="small">Yesterday at 7:18 PM</span></p>
</div>
</div>
<div className="row">
Integer ut bibendum ex. Suspendisse eleifend mi accumsan, euismod enim at, malesuada nibh.
Duis a eros fringilla, dictum leo vitae, vulputate mi. Nunc vitae neque nec erat fermentum... (more)
{position.statement_text}
</div>
<br />
23 Likes<br />
Expand Down
37 changes: 37 additions & 0 deletions src/js/components/Ballot/PositionList.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React, { Component, PropTypes } from 'react';
import BallotStore from '../../stores/BallotStore';
import PositionItem from './PositionItem';

export default class PositionList extends Component {
static propTypes = {
we_vote_id: PropTypes.string.isRequired
};

constructor(props) {
super(props);
this.state = { position_list: [] };
}
// no candidate exists... go to ballot
componentDidMount(){
BallotStore.fetchCandidatePositionsByWeVoteId(this.props.we_vote_id)
BallotStore.addChangeListener(this._onChange.bind(this));
}

componentWillUnmount(){
BallotStore.removeChangeListener(this._onChange.bind(this));
}

_onChange(){
this.setState({ position_list: BallotStore.getCandidateByWeVoteId(this.props.we_vote_id).position_list });
}

render() {
return (
<ul className="list-group">
{ this.state.position_list.map( item =>
<PositionItem key={item.position_we_vote_id} position_we_vote_id={item.position_we_vote_id} /> )
}
</ul>
);
}
}
3 changes: 1 addition & 2 deletions src/js/constants/BallotConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ module.exports = require('keymirror')({
VOTER_STOP_OPPOSING_SAVE: null,
VOTER_STAR_ON_SAVE: null,
VOTER_STAR_OFF_SAVE: null,
VOTER_STAR_OFF_SAVE: null,
DOWNLOAD_ORGANIZATIONS: null
POSITIONS_RETRIEVED: null
});
3 changes: 3 additions & 0 deletions src/js/constants/PositionConstants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = require('keymirror')({
POSITION_RETRIEVED: null
});
51 changes: 15 additions & 36 deletions src/js/routes/Ballot/Candidate.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import React, { Component, PropTypes } from 'react';
import { Button, ButtonToolbar, DropdownButton, Input, MenuItem, Navbar } from "react-bootstrap";
import { Link } from 'react-router';

import BallotActions from '../../actions/BallotActions';
import BallotStore from '../../stores/BallotStore';
import CandidateDetail from '../../components/Ballot/CandidateDetail';
import OrganizationItem from '../../components/Ballot/OrganizationItem';
import PositionList from '../../components/Ballot/PositionList';
import ItemActionbar from '../../components/ItemActionbar';
import ItemActionBar2 from '../../components/ItemActionBar2';
import StarAction from '../../components/StarAction';
Expand All @@ -17,38 +16,25 @@ export default class Candidate extends Component {

constructor(props) {
super(props);
this.state = { candidate: BallotStore.getCandidateByWeVoteId(this.props.params.we_vote_id) };
}
// no candidate exists... go to ballot
componentDidMount(){
if (Object.keys(this.state.candidate).length === 0){
this.props.history.replace('/ballot');
}
console.log('Candidate Component Mounted!')
BallotActions.downloadOrganizations(this.props.params.we_vote_id);
BallotStore.addChangeListener(this._onChange.bind(this));
this.state = { candidate: {} };
}

componentWillUnmount(){
BallotStore.removeChangeListener(this._onChange.bind(this));
}

_onChange(){
console.log('onChange!');
var we_vote_id = this.props.params.we_vote_id;
this.setState({ candidate: BallotStore.getCandidateByWeVoteId(we_vote_id) });
componentDidMount(){
this.setState({ candidate: BallotStore.getCandidateByWeVoteId(this.props.params.we_vote_id) });
}

render() {
var candidate = BallotStore.getCandidateByWeVoteId(`${this.props.params.we_vote_id}`);
if (!candidate){return (<div></div>);}
// var candidate = this.state.candidate;
console.log("Candidate Object:");
console.log(candidate);
var position_list = candidate ? candidate.position_list : undefined;
// if (Object.keys(this.state.candidate).length === 0){
// this.props.history.replace('/ballot');
// };
var candidate = this.state.candidate;
var we_vote_id = this.props.params.we_vote_id;
if (!candidate.we_vote_id){
return ( <div></div> );
};

if (Object.keys(candidate).length === 0){
this.props.history.replace('/ballot');}
// if (Object.keys(candidate).length === 0){
// this.props.history.replace('/ballot');}

var support_item;
if (this.props.support_on) {
Expand Down Expand Up @@ -134,14 +120,7 @@ export default class Candidate extends Component {
</li>
</ul>

<ul className="list-group">
{ (position_list) ? position_list.map( item =>
<OrganizationItem key={item.id}
candidate_we_vote_id={candidate.we_vote_id}
{...item} />
) : (<div></div>)
}
</ul>
<PositionList we_vote_id={we_vote_id} />
</div>

</div>
Expand Down
56 changes: 28 additions & 28 deletions src/js/stores/BallotStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { shallowClone } from '../utils/object-utils';

const AppDispatcher = require('../dispatcher/AppDispatcher');
const BallotConstants = require('../constants/BallotConstants');
const BallotActions = require('../actions/BallotActions');

let _ballot_store = {};
let _ballot_order_ids = [];
Expand Down Expand Up @@ -48,16 +49,14 @@ const BallotAPIWorker = {
});
},

positionListForBallotItem : function( we_vote_id, success_func) {
positionListForBallotItem : function( we_vote_id, success) {
return service.get({
endpoint: 'positionListForBallotItem',
query: {
ballot_item_id: _ballot_store[we_vote_id].id,
kind_of_ballot_item: _ballot_store[we_vote_id].kind_of_ballot_item
},
success: function(res){
success_func(res);
}
success
});
},

Expand Down Expand Up @@ -359,30 +358,25 @@ const BallotStore = createStore({
return temp;
},

// emitChange: function () {
// this.emit(BALLOT_CHANGE_EVENT);
// },
//
// addChangeListener: function(callback) {
// console.log("Change listener added");
// this.on(BALLOT_CHANGE_EVENT, callback);
// },
//
// removeChangeListener: function(callback) {
// console.log("Change listener removed!");
// this.removeListener(BALLOT_CHANGE_EVENT, callback);
// },

getCandidatePositionsByWeVoteId: function(candidate_we_vote_id){
BallotAPIWorker.positionListForBallotItem(candidate_we_vote_id, function(res){
var we_vote_id = candidate_we_vote_id;
_ballot_store[we_vote_id].position_list = res.position_list;
BallotStore.emitChange();
}.bind(this))
emitChange: function () {
this.emit(BALLOT_CHANGE_EVENT);
},

addChangeListener: function(callback) {
// console.log("Change listener added");
this.on(BALLOT_CHANGE_EVENT, callback);
},

getOrganization: function(candidate_we_vote_id, organization_we_vote_id){
return _ballot_store[candidate_we_vote_id].position_list.organization_we_vote_id;
removeChangeListener: function(callback) {
// console.log("Change listener removed!");
this.removeListener(BALLOT_CHANGE_EVENT, callback);
},

fetchCandidatePositionsByWeVoteId: function(candidate_we_vote_id){
BallotAPIWorker.positionListForBallotItem(candidate_we_vote_id,
function(res){
BallotActions.positionsRetrieved(candidate_we_vote_id, res);
});
},

/**
Expand Down Expand Up @@ -470,6 +464,11 @@ const BallotStore = createStore({
}
});

function setLocalPositionsList(we_vote_id, position_list) {
_ballot_store[we_vote_id].position_list = position_list;
return true;
}

/**
* toggle the star state of a ballot item by its we_vote_id
* @param {string} we_vote_id identifier for lookup in stored
Expand Down Expand Up @@ -546,8 +545,9 @@ AppDispatcher.register( action => {
var { we_vote_id } = action;
switch (action.actionType) {

case BallotConstants.DOWNLOAD_ORGANIZATIONS:
BallotStore.getCandidatePositionsByWeVoteId(we_vote_id);
case BallotConstants.POSITIONS_RETRIEVED:
setLocalPositionsList(action.we_vote_id, action.payload.position_list)
&& BallotStore.emitChange();
break;

case BallotConstants.VOTER_SUPPORTING_SAVE:
Expand Down
Loading

0 comments on commit 03d48d1

Please # to comment.