From 5657d34acafcb7f7790f256bd31e4781fb83dfa8 Mon Sep 17 00:00:00 2001 From: Lisa Cho Date: Wed, 17 Feb 2016 16:37:25 -0800 Subject: [PATCH] Load elements of candidate page on load --- src/js/actions/BallotActions.js | 18 +++++ src/js/components/Ballot/PositionList.jsx | 5 ++ src/js/constants/BallotConstants.js | 4 +- src/js/routes/Ballot/Candidate.jsx | 40 ++++++++--- src/js/stores/BallotStore.js | 81 +++++++++++++++++++++-- 5 files changed, 131 insertions(+), 17 deletions(-) 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 ( */} - + { + + } 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();