mirror of
https://github.com/ecency/ecency-mobile.git
synced 2024-11-26 09:13:33 +03:00
added proposal supportive hive methods and query
This commit is contained in:
parent
58dbe41ed6
commit
658a912590
@ -43,6 +43,7 @@ import { SearchInput } from './searchInput';
|
||||
import { SearchModal } from './searchModal';
|
||||
import { SettingsItem } from './settingsItem';
|
||||
import { SideMenu } from './sideMenu';
|
||||
import { ProposalVoteRequest } from './proposalVoteRequest';
|
||||
|
||||
import CommunityCard from './communityCard';
|
||||
|
||||
@ -272,4 +273,6 @@ export {
|
||||
PostTranslationModal,
|
||||
ImageViewer,
|
||||
WalkthroughMarker,
|
||||
ProposalVoteRequest
|
||||
|
||||
};
|
||||
|
@ -3,28 +3,35 @@ import { Image, Text, View } from "react-native";
|
||||
import styles from "../styles/ProposalVoteRequest.styles";
|
||||
import { TextButton } from "../../../components/buttons";
|
||||
import { MainButton } from "../../../components/mainButton";
|
||||
import { useDispatch } from "react-redux";
|
||||
import { handleDeepLink, showActionModal } from "../../../redux/actions/uiAction";
|
||||
import { useDispatch, useSelector } from "react-redux";
|
||||
import { showActionModal } from "../../../redux/actions/uiAction";
|
||||
import { ButtonTypes } from "../../../components/actionModal/container/actionModalContainer";
|
||||
import { delay } from '../../../utils/editor';
|
||||
|
||||
|
||||
const HIVE_VOTE_OPS = "hive://sign/op/WyJ1cGRhdGVfcHJvcG9zYWxfdm90ZXMiLHsidm90ZXIiOiAiX19zaWduZXIiLCJwcm9wb3NhbF9pZHMiOiBbMjgzXSwiYXBwcm92ZSI6dHJ1ZSwiZXh0ZW5zaW9ucyI6IFtdfV0."
|
||||
|
||||
import { voteProposal } from '../../../providers/hive/dhive';
|
||||
import { useProposalVotedQuery } from '../../../providers/queries';
|
||||
|
||||
const ECENCY_PROPOSAL_ID = 283;
|
||||
|
||||
export const ProposalVoteRequest = () => {
|
||||
|
||||
const dispatch = useDispatch();
|
||||
|
||||
const proposalVotedQuery = useProposalVotedQuery(ECENCY_PROPOSAL_ID);
|
||||
|
||||
const isLoggedIn = useSelector(state => state.application.isLoggedIn);
|
||||
const pinHash = useSelector(state => state.application.pin);
|
||||
const currentAccount = useSelector(state => state.account.currentAccount);
|
||||
|
||||
const [voting, setVoting] = useState(false);
|
||||
const [voteCasted, setVoteCasted] = useState(false);
|
||||
|
||||
//assess if user should be promopted to vote proposal
|
||||
if(!isLoggedIn || proposalVotedQuery.data){
|
||||
return null;
|
||||
}
|
||||
|
||||
const _voteAction = async () => {
|
||||
setVoting(true);
|
||||
// dispatch(handleDeepLink(HIVE_VOTE_OPS));
|
||||
await delay(800);
|
||||
|
||||
await voteProposal(currentAccount, pinHash, ECENCY_PROPOSAL_ID)
|
||||
setVoting(false);
|
||||
setVoteCasted(true);
|
||||
}
|
||||
|
@ -641,6 +641,33 @@ export const ignoreUser = async (currentAccount, pin, data) => {
|
||||
);
|
||||
};
|
||||
|
||||
export const getProposalsVoted = async (username) => {
|
||||
try {
|
||||
if (!username) {
|
||||
throw new Error('invalid parameters');
|
||||
}
|
||||
|
||||
console.log('Getting proposals voted:', username);
|
||||
|
||||
const votedProposals = await client.call(
|
||||
'condenser_api',
|
||||
'list_proposal_votes',
|
||||
[[username], 100, "by_voter_proposal", "ascending", "active"]);
|
||||
|
||||
if (!Array.isArray(votedProposals)) {
|
||||
throw new Error('invalid data');
|
||||
}
|
||||
|
||||
const filteredProposals = votedProposals.filter(item => item.voter === username);
|
||||
|
||||
console.log(`Returning filtered proposals`, filteredProposals);
|
||||
return filteredProposals;
|
||||
} catch (error) {
|
||||
bugsnapInstance.notify(error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export const getActiveVotes = (author, permlink) =>
|
||||
new Promise((resolve, reject) => {
|
||||
try {
|
||||
@ -978,6 +1005,64 @@ const _vote = (currentAccount, pin, author, permlink, weight) => {
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Update Hive Proposal Vote with current account as voter
|
||||
* @param {*} currentAccount
|
||||
* @param {*} pin
|
||||
* @param {*} proposalId
|
||||
* @returns
|
||||
*/
|
||||
export const voteProposal = (currentAccount, pinHash, proposalId) => {
|
||||
const digitPinCode = getDigitPinCode(pinHash);
|
||||
const key = getAnyPrivateKey(currentAccount.local, digitPinCode);
|
||||
|
||||
const voter = currentAccount.name;
|
||||
const opArray = [
|
||||
[
|
||||
"update_proposal_votes",
|
||||
{
|
||||
voter: voter,
|
||||
proposal_ids: [proposalId],
|
||||
approve: true,
|
||||
extensions: []
|
||||
}
|
||||
],
|
||||
];
|
||||
|
||||
if (currentAccount.local.authType === AUTH_TYPE.STEEM_CONNECT) {
|
||||
const token = decryptKey(currentAccount.local.accessToken, digitPinCode);
|
||||
const api = new hsClient({
|
||||
accessToken: token,
|
||||
});
|
||||
|
||||
return api.broadcast(opArray).then((resp) => resp.result);
|
||||
}
|
||||
|
||||
if (key) {
|
||||
const privateKey = PrivateKey.fromString(key);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
sendHiveOperations(opArray, privateKey)
|
||||
.then((result) => {
|
||||
console.log('vote result', result);
|
||||
resolve(result);
|
||||
})
|
||||
.catch((err) => {
|
||||
if (err && get(err, 'jse_info.code') === 4030100) {
|
||||
err.message = getDsteemDateErrorMessage(err);
|
||||
}
|
||||
bugsnagInstance.notify(err);
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return Promise.reject(
|
||||
new Error('Check private key permission! Required private posting key or above.'),
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* @method upvoteAmount estimate upvote amount
|
||||
*/
|
||||
|
@ -59,3 +59,4 @@ export * from './walletQueries';
|
||||
export * from './leaderboardQueries';
|
||||
export * from './settingsQueries';
|
||||
export * from './announcementsQueries';
|
||||
export * from './proposalQueries';
|
||||
|
28
src/providers/queries/proposalQueries.ts
Normal file
28
src/providers/queries/proposalQueries.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { useQuery } from "@tanstack/react-query"
|
||||
import QUERIES from "./queryKeys"
|
||||
import { useSelector } from "react-redux"
|
||||
import { getProposalsVoted } from "../../providers/hive/dhive";
|
||||
|
||||
|
||||
export const useProposalVotedQuery = (proposalId:number) => {
|
||||
|
||||
const currentAccount = useSelector(state => state.account.currentAccount);
|
||||
|
||||
const _getProposalVoteStatus = async () => {
|
||||
const votedProposals = await getProposalsVoted(currentAccount.username);
|
||||
const isVoted = votedProposals.some(item => item.proposal.proposal_id === proposalId)
|
||||
|
||||
console.log('is proposal voted', isVoted);
|
||||
return isVoted;
|
||||
}
|
||||
|
||||
const query = useQuery(
|
||||
[QUERIES.PROPOSALS.GET_VOTES, currentAccount.name, proposalId],
|
||||
_getProposalVoteStatus,
|
||||
{initialData:true}
|
||||
)
|
||||
|
||||
|
||||
return query;
|
||||
|
||||
}
|
@ -50,6 +50,9 @@ const QUERIES = {
|
||||
REDEEM: {
|
||||
GET_BOOST_PLUS_PRICES: 'REDEEM_GET_BOOST_PLUS_PRICES',
|
||||
},
|
||||
PROPOSALS: {
|
||||
GET_VOTES:'QUERY_PROPOSAL_GET_VOTES'
|
||||
}
|
||||
};
|
||||
|
||||
export default QUERIES;
|
||||
|
Loading…
Reference in New Issue
Block a user