frontend/packages/client/src/hooks/useProposalVotes.js
/**
 * Hook to return proposal votes for a proposal. Results are paginated
 * @param  {int} count page size, used for pagination limiting the number of elements returned. Defaults to 10. Max value is 25.
 * @param  {int} start indicates the start index for paginated results. Defaults to 0.
 */
Hook to return proposal votes for a proposal. Results are paginated
@param  {int} count page size, used for pagination limiting the number of elements returned. Defaults to 10. Max value is 25.
@param  {int} start indicates the start index for paginated results. Defaults to 0.
Hook to return proposal votes for a proposal . Results are paginated @param { int } count page size used for pagination limiting the number of elements returned . Defaults to 10 . Max value is 25 . @param { int } start indicates the start index for paginated results . Defaults to 0 .
function useProposalVotes({
  proposalId,
  count = PAGINATION_INITIAL_STATE.count,
  start = PAGINATION_INITIAL_STATE.start,
} = {}) {
  const [state, dispatch] = useReducer(paginationReducer, {
    ...INITIAL_STATE,
    pagination: {
      ...PAGINATION_INITIAL_STATE,
      count,
      start,
    },
  });
  const { notifyError } = useErrorHandlerContext();
  const resetResults = useCallback(() => {
    dispatch({ type: "RESET_RESULTS" });
  }, []);
  const fetchMore = useCallback(async () => {
    dispatch({ type: "PROCESSING" });
    const url = `${process.env.REACT_APP_BACK_END_SERVER_API}/proposals/${proposalId}/votes?count=${state.pagination.count}&start=${state.pagination.start}`;
    try {
      const response = await fetch(url);
      const proposalVotes = await response.json();
      dispatch({ type: "SUCCESS", payload: proposalVotes });
    } catch (err) {
      notifyError(err, url);
      dispatch({ type: "ERROR", payload: { errorData: err.message } });
    }
  }, [state.pagination, proposalId, notifyError]);
  const getProposalVotes = useCallback(async () => {
    dispatch({ type: "PROCESSING" });
    const url = `${process.env.REACT_APP_BACK_END_SERVER_API}/proposals/${proposalId}/votes?count=${count}&start=${start}`;
    try {
      const response = await fetch(url);
      const proposalVotes = await checkResponse(response);
      dispatch({ type: "SUCCESS", payload: proposalVotes });
    } catch (err) {
      notifyError(err, url);
      dispatch({
        type: "ERROR",
        payload: { errorData: err.message },
      });
    }
  }, [proposalId, count, start, notifyError]);
  useEffect(() => {
    (async () => {
      await getProposalVotes();
    })();
  }, [proposalId, getProposalVotes]);
  return {
    ...state,
    getProposalVotes,
    resetResults,
    fetchMore,
  };
}
function useProposalVotes ( { proposalId , count = PAGINATION_INITIAL_STATE . count , start = PAGINATION_INITIAL_STATE . start , } = { } ) { const [ state , dispatch ] = useReducer ( paginationReducer , { ... INITIAL_STATE , pagination : { ... PAGINATION_INITIAL_STATE , count , start , } , } ) ; const { notifyError } = useErrorHandlerContext ( ) ; const resetResults = useCallback ( ( ) => { dispatch ( { type : "RESET_RESULTS" } ) ; } , [ ] ) ; const fetchMore = useCallback ( async ( ) => { dispatch ( { type : "PROCESSING" } ) ; const url = ` ${ process . env . REACT_APP_BACK_END_SERVER_API } ${ proposalId } ${ state . pagination . count } ${ state . pagination . start } ` ; try { const response = await fetch ( url ) ; const proposalVotes = await response . json ( ) ; dispatch ( { type : "SUCCESS" , payload : proposalVotes } ) ; } catch ( err ) { notifyError ( err , url ) ; dispatch ( { type : "ERROR" , payload : { errorData : err . message } } ) ; } } , [ state . pagination , proposalId , notifyError ] ) ; const getProposalVotes = useCallback ( async ( ) => { dispatch ( { type : "PROCESSING" } ) ; const url = ` ${ process . env . REACT_APP_BACK_END_SERVER_API } ${ proposalId } ${ count } ${ start } ` ; try { const response = await fetch ( url ) ; const proposalVotes = await checkResponse ( response ) ; dispatch ( { type : "SUCCESS" , payload : proposalVotes } ) ; } catch ( err ) { notifyError ( err , url ) ; dispatch ( { type : "ERROR" , payload : { errorData : err . message } , } ) ; } } , [ proposalId , count , start , notifyError ] ) ; useEffect ( ( ) => { ( async ( ) => { await getProposalVotes ( ) ; } ) ( ) ; } , [ proposalId , getProposalVotes ] ) ; return { ... state , getProposalVotes , resetResults , fetchMore , } ; }
Hook to return proposal votes for a proposal.
Hook to return proposal votes for a proposal .
/**
   * Function to reset results from Api call stored in hook state
   */ /**
   * Function to fetch more results based on pagination configuration
   * @param  {int} proposalId id used to fetch votes from
   */ // Initial Load to fetch from API