Commit 95fdc0c7 authored by Efe's avatar Efe Committed by Jonathan Porta
Browse files

add 'voted' filter, move code to separate file

parent 92512005
import React from 'react';
import { connect } from 'react-redux';
import { actions as fetchDataSagaActions } from 'app/redux/FetchDataSaga';
import { actions as proposalActions } from 'app/redux/ProposalSaga';
import PropTypes from 'prop-types';
import { List } from 'immutable';
import tt from 'counterpart';
......@@ -14,36 +14,12 @@ class SteemProposalSystem extends React.Component {
pages = new Map();
filterDropdownItems = [
{
value: 'all',
onClick: () => {
this.onFilterListProposals('all');
},
},
{
value: 'active',
onClick: () => {
this.onFilterListProposals('active');
},
},
{
value: 'inactive',
onClick: () => {
this.onFilterListProposals('inactive');
},
},
{
value: 'expired',
onClick: () => {
this.onFilterListProposals('expired');
},
},
{
value: 'votable',
onClick: () => {
this.onFilterListProposals('votable');
},
},
'all',
'active',
'inactive',
'expired',
'votable',
'voted',
];
orderedProposalKeys = [
......@@ -116,6 +92,9 @@ class SteemProposalSystem extends React.Component {
order_direction = 'direction_descending',
start = ''
) {
if (status === 'voted') {
start = this.props.currentUser;
}
this.props.listProposals({
start,
order_by,
......@@ -193,6 +172,17 @@ class SteemProposalSystem extends React.Component {
return false;
}
getDropdownItems() {
return this.filterDropdownItems
.filter(
value => (this.props.currentUser ? true : value !== 'voted')
)
.map(value => ({
value,
onClick: () => this.onFilterListProposals(value),
}));
}
formatTableDiv(
key,
value,
......@@ -320,7 +310,7 @@ class SteemProposalSystem extends React.Component {
<div className="proposals-filter-wrapper">
<div className="dropdowns">
<DropdownMenu
items={this.filterDropdownItems}
items={this.getDropdownItems()}
el="div"
key="proposals-filter"
selected={status}
......@@ -439,7 +429,7 @@ module.exports = {
),
successCallback: () => {
dispatch(
fetchDataSagaActions.listProposals({
proposalActions.listProposals({
start: '',
order_by: 'by_total_votes',
order_direction: 'direction_descending',
......@@ -449,7 +439,7 @@ module.exports = {
})
);
dispatch(
fetchDataSagaActions.listVoterProposals({
proposalActions.listVoterProposals({
start: proposal_owner,
order_by: 'by_creator',
order_direction: 'direction_ascending',
......@@ -464,7 +454,7 @@ module.exports = {
listProposals: payload =>
new Promise((resolve, reject) => {
dispatch(
fetchDataSagaActions.listProposals({
proposalActions.listProposals({
...payload,
resolve,
reject,
......@@ -474,7 +464,7 @@ module.exports = {
listVoterProposals: payload =>
new Promise((resolve, reject) => {
dispatch(
fetchDataSagaActions.listVoterProposals({
proposalActions.listVoterProposals({
...payload,
resolve,
reject,
......
......@@ -8,11 +8,6 @@ import {
} from 'redux-saga/effects';
import { api } from '@blocktradesdev/steem-js';
import { loadFollows } from 'app/redux/FollowSaga';
import {
getContent,
listProposals,
listVoterProposals,
} from 'app/redux/SagaShared';
import * as globalActions from './GlobalReducer';
import * as appActions from './AppReducer';
import constants from './constants';
......@@ -20,8 +15,6 @@ import { fromJS, Map, Set } from 'immutable';
import { getStateAsync } from 'app/utils/steemApi';
const REQUEST_DATA = 'fetchDataSaga/REQUEST_DATA';
const LIST_PROPOSALS = 'fetchDataSaga/LIST_PROPOSALS';
const LIST_VOTER_PROPOSALS = 'fetchDataSaga/LIST_VOTER_PROPOSALS';
const GET_CONTENT = 'fetchDataSaga/GET_CONTENT';
const FETCH_STATE = 'fetchDataSaga/FETCH_STATE';
......@@ -29,18 +22,8 @@ export const fetchDataWatches = [
takeLatest('@@router/LOCATION_CHANGE', fetchState),
takeLatest(FETCH_STATE, fetchState),
takeEvery('global/FETCH_JSON', fetchJson),
takeEvery(LIST_PROPOSALS, listProposalsCaller),
takeEvery(LIST_VOTER_PROPOSALS, listVoterProposalsCaller),
];
export function* listProposalsCaller(action) {
yield listProposals(action.payload);
}
export function* listVoterProposalsCaller(action) {
yield listVoterProposals(action.payload);
}
let is_initial_state = true;
export function* fetchState(location_change_action) {
const { pathname } = location_change_action.payload;
......@@ -174,13 +157,4 @@ export const actions = {
type: FETCH_STATE,
payload,
}),
listProposals: payload => ({
type: LIST_PROPOSALS,
payload,
}),
listVoterProposals: payload => ({
type: LIST_VOTER_PROPOSALS,
payload,
}),
};
import { api } from '@blocktradesdev/steem-js';
import { call, put, takeEvery } from 'redux-saga/effects';
import * as globalActions from './GlobalReducer';
const LIST_PROPOSALS = 'fetchDataSaga/LIST_PROPOSALS';
const LIST_VOTER_PROPOSALS = 'fetchDataSaga/LIST_VOTER_PROPOSALS';
export const proposalWatches = [
takeEvery(LIST_PROPOSALS, listProposalsCaller),
takeEvery(LIST_VOTER_PROPOSALS, listVoterProposalsCaller),
];
export function* listProposalsCaller(action) {
yield listProposals(action.payload);
}
export function* listVoterProposalsCaller(action) {
yield listVoterProposals(action.payload);
}
export function* listProposals({
start,
order_by,
order_direction,
limit,
status,
last_id,
resolve,
reject,
}) {
let proposals;
while (!proposals) {
if (status === 'voted') {
const voterProposals = yield call(
[api, api.listVoterProposalsAsync],
start,
order_by,
order_direction,
limit,
'votable',
last_id
);
proposals = voterProposals[start];
} else {
proposals = yield call(
[api, api.listProposalsAsync],
start,
order_by,
order_direction,
limit,
status,
last_id
);
}
}
yield put(globalActions.receiveListProposals({ proposals }));
if (resolve && proposals) {
resolve(proposals);
} else if (reject && !proposals) {
reject();
}
}
export function* listVoterProposals({
start,
order_by,
order_direction,
limit,
status,
resolve,
reject,
}) {
let voterProposals = { [start]: [] };
let last_id = null;
let isLast = false;
while (!isLast) {
const data = yield call(
[api, api.listVoterProposalsAsync],
start,
order_by,
order_direction,
limit,
status,
last_id
);
if (data) {
if (!data.hasOwnProperty(start)) {
isLast = true;
} else {
let proposals = [];
if (data[start].length < limit) {
proposals = [...voterProposals[start], ...data[start]];
isLast = true;
} else {
const nextProposals = [...data[start]];
last_id = nextProposals[nextProposals.length - 1]['id'];
nextProposals.splice(-1, 1);
proposals = [...voterProposals[start], ...nextProposals];
}
voterProposals = { [start]: proposals };
}
}
}
yield put(globalActions.receiveListVoterProposals({ voterProposals }));
if (resolve && voterProposals[start].length > 0) {
resolve(voterProposals);
} else if (reject && !voterProposals) {
reject();
}
}
// Action creators
export const actions = {
listProposals: payload => ({
type: LIST_PROPOSALS,
payload,
}),
listVoterProposals: payload => ({
type: LIST_VOTER_PROPOSALS,
payload,
}),
};
......@@ -68,90 +68,6 @@ function* showTransactionErrorNotification() {
}
}
export function* listProposals({
start,
order_by,
order_direction,
limit,
status,
last_id,
resolve,
reject,
}) {
let proposals;
while (!proposals) {
proposals = yield call(
[api, api.listProposalsAsync],
start,
order_by,
order_direction,
limit,
status,
last_id
);
}
yield put(globalActions.receiveListProposals({ proposals }));
if (resolve && proposals) {
resolve(proposals);
} else if (reject && !proposals) {
reject();
}
}
export function* listVoterProposals({
start,
order_by,
order_direction,
limit,
status,
resolve,
reject,
}) {
let voterProposals = { [start]: [] };
let last_id = null;
let isLast = false;
while (!isLast) {
const data = yield call(
[api, api.listVoterProposalsAsync],
start,
order_by,
order_direction,
limit,
status,
last_id
);
if (data) {
if (!data.hasOwnProperty(start)) {
isLast = true;
} else {
let proposals = [];
if (data[start].length < limit) {
proposals = [...voterProposals[start], ...data[start]];
isLast = true;
} else {
const nextProposals = [...data[start]];
last_id = nextProposals[nextProposals.length - 1]['id'];
nextProposals.splice(-1, 1);
proposals = [...voterProposals[start], ...nextProposals];
}
voterProposals = { [start]: proposals };
}
}
}
yield put(globalActions.receiveListVoterProposals({ voterProposals }));
if (resolve && voterProposals[start].length > 0) {
resolve(voterProposals);
} else if (reject && !voterProposals) {
reject();
}
}
/**
* Save this user's preferences, either directly from the submitted payload or from whatever's saved in the store currently.
*
......
......@@ -7,7 +7,7 @@ import secureRandom from 'secure-random';
import { PrivateKey, PublicKey } from '@blocktradesdev/steem-js/lib/auth/ecc';
import { api, broadcast, auth, memo } from '@blocktradesdev/steem-js';
import { getAccount, listVoterProposals } from 'app/redux/SagaShared';
import { getAccount, listVoterProposals } from 'app/redux/ProposalSaga';
import { findSigningKey } from 'app/redux/AuthSaga';
import * as appActions from 'app/redux/AppReducer';
import * as globalActions from 'app/redux/GlobalReducer';
......
......@@ -5,6 +5,7 @@ import { sharedWatches } from 'app/redux/SagaShared';
import { userWatches } from 'app/redux/UserSaga';
import { authWatches } from 'app/redux/AuthSaga';
import { transactionWatches } from 'app/redux/TransactionSaga';
import { proposalWatches } from 'app/redux/ProposalSaga';
export default function* rootSaga() {
yield all([
......@@ -14,5 +15,6 @@ export default function* rootSaga() {
...authWatches,
...transactionWatches,
...marketWatches,
...proposalWatches,
]);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment