diff --git a/src/js/actions/BallotActions.js b/src/js/actions/BallotActions.js
index 536bd14c4..cc93597c7 100644
--- a/src/js/actions/BallotActions.js
+++ b/src/js/actions/BallotActions.js
@@ -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,
diff --git a/src/js/components/Ballot/PositionList.jsx b/src/js/components/Ballot/PositionList.jsx
index 3aea58c65..6c3deed2c 100644
--- a/src/js/components/Ballot/PositionList.jsx
+++ b/src/js/components/Ballot/PositionList.jsx
@@ -27,6 +27,11 @@ export default class PositionList extends Component {
}
render () {
+ if (!this.state.position_list){
+ return (
+
+ );
+ }
return (
{ this.state.position_list.map( item =>
diff --git a/src/js/constants/BallotConstants.js b/src/js/constants/BallotConstants.js
index 52c3bf062..f7903e4f6 100644
--- a/src/js/constants/BallotConstants.js
+++ b/src/js/constants/BallotConstants.js
@@ -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
});
diff --git a/src/js/routes/Ballot/Candidate.jsx b/src/js/routes/Ballot/Candidate.jsx
index 36c3dad64..d7e5228a5 100644
--- a/src/js/routes/Ballot/Candidate.jsx
+++ b/src/js/routes/Ballot/Candidate.jsx
@@ -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 ( );
};
// var candidate = BallotStore.getCandidateByWeVoteId(`${this.props.params.we_vote_id}`);
@@ -128,7 +144,9 @@ export default class Candidate extends Component {
*/}
-
+ {
+
+ }
diff --git a/src/js/stores/BallotStore.js b/src/js/stores/BallotStore.js
index baa0a56e6..fe7559541 100644
--- a/src/js/stores/BallotStore.js
+++ b/src/js/stores/BallotStore.js
@@ -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';
@@ -41,6 +42,14 @@ 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',
@@ -48,7 +57,7 @@ const BallotAPIWorker = {
},
positionListForBallotItem : function( we_vote_id, success) {
- return service.get({
+ return get({
endpoint: 'positionListForBallotItem',
query: {
ballot_item_id: _ballot_store[we_vote_id].id,
@@ -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
});
},
@@ -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 ]
@@ -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
@@ -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
*/
@@ -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();