Skip to content

Commit

Permalink
Load elements of candidate page on load
Browse files Browse the repository at this point in the history
  • Loading branch information
lisamburns committed Feb 18, 2016
1 parent 6537cc8 commit 5657d34
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 17 deletions.
18 changes: 18 additions & 0 deletions src/js/actions/BallotActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@ module.exports = {
});
},

starStatusRetrieved: function (payload, we_vote_id) {
console.log("action payload:");
console.log(payload);
AppDispatcher.dispatch({
actionType: BallotConstants.STAR_STATUS_RETRIEVED,
payload: payload,
we_vote_id: we_vote_id
});
},

candidateRetrieved: function (payload) {
AppDispatcher.dispatch({
actionType: BallotConstants.CANDIDATE_RETRIEVED,
payload: payload,
we_vote_id: payload.we_vote_id
});
},

voterSupportingSave: function (we_vote_id) { // VOTER_SUPPORTING_SAVE
AppDispatcher.dispatch({
actionType: BallotConstants.VOTER_SUPPORTING_SAVE,
Expand Down
5 changes: 5 additions & 0 deletions src/js/components/Ballot/PositionList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export default class PositionList extends Component {
}

render () {
if (!this.state.position_list){
return (
<div></div>
);
}
return (
<ul className="list-group">
{ this.state.position_list.map( item =>
Expand Down
4 changes: 3 additions & 1 deletion src/js/constants/BallotConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ module.exports = require('keymirror')({
VOTER_STOP_OPPOSING_SAVE: null,
VOTER_STAR_ON_SAVE: null,
VOTER_STAR_OFF_SAVE: null,
POSITIONS_RETRIEVED: null
POSITIONS_RETRIEVED: null,
CANDIDATE_RETRIEVED: null,
STAR_STATUS_RETRIEVED: null
});
40 changes: 29 additions & 11 deletions src/js/routes/Ballot/Candidate.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,50 @@ import StarAction from '../../components/StarAction';
export default class Candidate extends Component {
static propTypes = {
//history: PropTypes.func.isRequired,
history: PropTypes.string,
oppose_on: PropTypes.boolean,
params: PropTypes.object.isRequired,
support_on: PropTypes.boolean
// history: PropTypes.string,
// oppose_on: PropTypes.boolean,
params: PropTypes.object.isRequired
// support_on: PropTypes.boolean
};

constructor(props) {
console.log(props);
super(props);
this.state = { candidate: {} };
}

componentWillMount(){
// Redirects to root if candidate isn't fetched yet; TODO: just fetch params to enable sending links to candidate page.
var candidate = BallotStore.getCandidateByWeVoteId(this.props.params.we_vote_id);
if (Object.keys(candidate).length === 0)
{
this.props.history.replace('/ballot');
}
// var candidate = BallotStore.getCandidateByWeVoteId(this.props.params.we_vote_id);
// if (Object.keys(candidate).length === 0)
// {
// this.props.history.replace('/ballot');
// }

}

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

componentDidMount(){
BallotStore.addChangeListener(this._onChange.bind(this));
var candidate = BallotStore.getOrFetchCandidateByWeVoteId(this.props.params.we_vote_id);
if (candidate) {
this.setState({ candidate: candidate });
}
}

_onChange(){
this.setState({ candidate: BallotStore.getCandidateByWeVoteId(this.props.params.we_vote_id) });
console.log('on_Change!');
console.log(this.state.candidate);
}

render() {
var candidate = this.state.candidate;
var we_vote_id = this.props.params.we_vote_id;
if (!candidate.we_vote_id){
if (!candidate || !candidate.we_vote_id){
return ( <div></div> );
};
// var candidate = BallotStore.getCandidateByWeVoteId(`${this.props.params.we_vote_id}`);
Expand Down Expand Up @@ -128,7 +144,9 @@ export default class Candidate extends Component {
</li>
</ul>
*/}
<PositionList we_vote_id={we_vote_id} />
{
<PositionList we_vote_id={we_vote_id} />
}
</div>

</div>
Expand Down
81 changes: 76 additions & 5 deletions src/js/stores/BallotStore.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { get } from '../utils/service';
// import service from '../utils/service';
import { createStore } from '../utils/createStore';
import { shallowClone } from '../utils/object-utils';

Expand Down Expand Up @@ -41,14 +42,22 @@ const BallotAPIWorker = {
success: success || defaultSuccess });
},

candidateRetrieve: function (we_vote_id, success ) {
return get({
endpoint: 'candidateRetrieve',
query: { candidate_we_vote_id: we_vote_id },
success: success
});
},

// get the ballot items
voterBallotItemsRetrieve: function ( success ) {
return get({ endpoint: 'voterBallotItemsRetrieve',
success: success || defaultSuccess });
},

positionListForBallotItem : function( we_vote_id, success) {
return service.get({
return get({
endpoint: 'positionListForBallotItem',
query: {
ballot_item_id: _ballot_store[we_vote_id].id,
Expand Down Expand Up @@ -93,8 +102,8 @@ const BallotAPIWorker = {
return get({
endpoint: 'voterStarStatusRetrieve',
query: {
ballot_item_id: _ballot_store[we_vote_id].id,
kind_of_ballot_item: _ballot_store[we_vote_id].kind_of_ballot_item
ballot_item_we_vote_id: we_vote_id,
kind_of_ballot_item: _ballot_store[we_vote_id].kind_of_ballot_item || 'CANDIDATE'
}, success: success || defaultSuccess
});
},
Expand Down Expand Up @@ -408,6 +417,38 @@ const BallotStore = createStore({
return _ballot_store[we_vote_id].is_starred;
},

getOrFetchCandidateByWeVoteId: function (candidate_we_vote_id) {
var candidate = this.getCandidateByWeVoteId(candidate_we_vote_id);
if (candidate && candidate.is_oppose) { return candidate; } //candidate already retrieved
_ballot_store[candidate_we_vote_id] = {};
_ballot_store[candidate_we_vote_id].kind_of_ballot_item = 'CANDIDATE';

BallotAPIWorker.candidateRetrieve(candidate_we_vote_id, function(res){
BallotActions.candidateRetrieved(res);
BallotStore.fetchCandidateDetails(candidate_we_vote_id);
});

return _ballot_store[candidate_we_vote_id];
},

fetchCandidateDetails: function(candidate_we_vote_id){
BallotAPIWorker.voterStarStatusRetrieve(candidate_we_vote_id, function(res){
BallotActions.starStatusRetrieved(res, candidate_we_vote_id);
});

BallotStore.fetchCandidatePositionsByWeVoteId(candidate_we_vote_id);

BallotAPIWorker.positionOpposeCountForBallotItem (candidate_we_vote_id, function(res){
_ballot_store[candidate_we_vote_id].opposeCount = res.count;
BallotStore.emitChange();
});

BallotAPIWorker.positionSupportCountForBallotItem (candidate_we_vote_id, function(res){
_ballot_store [candidate_we_vote_id].supportCount = res.count;
BallotStore.emitChange();
});
},

getCandidateByWeVoteId: function (candidate_we_vote_id) {
return shallowClone (
_ballot_store [ candidate_we_vote_id ]
Expand Down Expand Up @@ -453,6 +494,11 @@ function setLocalPositionsList(we_vote_id, position_list) {
return true;
}

function addCandidateToStore (res) {
_ballot_store[res.we_vote_id] = res;
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 All @@ -465,6 +511,13 @@ function toggleStarState(we_vote_id) {
return true;
}

function setStarState(we_vote_id, status) {
if (status){
_ballot_store[we_vote_id].is_starred = status;
}
return true;
}

/**
* toggle the support state of a ballot item to on by its we_vote_id
*/
Expand Down Expand Up @@ -525,10 +578,28 @@ function setLocalOpposeOffState(we_vote_id) {
return true;
}

AppDispatcher.register( action => {
var { we_vote_id } = action;

BallotStore.dispatchToken = AppDispatcher.register( action => {
var { we_vote_id } = action;
switch (action.actionType) {

case BallotConstants.CANDIDATE_RETRIEVED:
console.log('action:');
console.log(action);
addCandidateToStore(action.payload)
&& BallotStore.emitChange();
break;

case BallotConstants.STAR_STATUS_RETRIEVED:
// AppDispatcher.waitFor([BallotStore.dispatchToken]);
console.log("REGISTERING");
console.log(action);
console.log("Ballot Store:");
console.log(_ballot_store[action.we_vote_id]);
setStarState(action.we_vote_id, action.payload.is_starred)
&& BallotStore.emitChange();
break;

case BallotConstants.POSITIONS_RETRIEVED:
setLocalPositionsList(action.we_vote_id, action.payload.position_list)
&& BallotStore.emitChange();
Expand Down

0 comments on commit 5657d34

Please sign in to comment.