frontend/packages/client/src/hooks/useCommunityMembers.js
/**
 * Hook to return community members. Results are paginated
 * @param  {int} communityId community Id to get members from
 * @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 community members. Results are paginated
@param  {int} communityId community Id to get members from
@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 community members . Results are paginated @param { int } communityId community Id to get members from @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 useCommunityMembers({
  communityId,
  start = PAGINATION_INITIAL_STATE.start,
  count = PAGINATION_INITIAL_STATE.count,
} = {}) {
  const [state, dispatch] = useReducer(paginationReducer, {
    ...INITIAL_STATE,
    pagination: {
      ...PAGINATION_INITIAL_STATE,
      start,
      count,
    },
  });
  const { notifyError } = useErrorHandlerContext();
  const fetchMore = useCallback(async () => {
    dispatch({ type: "PROCESSING" });
    const url = `${process.env.REACT_APP_BACK_END_SERVER_API}/communities/${communityId}/users?count=${state.pagination.count}&start=${state.pagination.start}`;
    try {
      const response = await fetch(url);
      const members = await checkResponse(response);
      const membersMocked = members?.data.map((member) => ({
        ...member,
        votingStreak: getRandomInt(50),
        score: getRandomInt(500),
      }));
      dispatch({
        type: "SUCCESS",
        payload: { ...members, data: membersMocked },
      });
    } catch (err) {
      notifyError(err, url);
      dispatch({ type: "ERROR", payload: { errorData: err.message } });
    }
  }, [state.pagination, communityId, notifyError]);
  const getCommunityMembers = useCallback(async () => {
    dispatch({ type: "PROCESSING" });
    const url = `${process.env.REACT_APP_BACK_END_SERVER_API}/communities/${communityId}/users?count=${count}&start=${start}`;
    try {
      const response = await fetch(url);
      const members = await checkResponse(response);
      const membersMocked = members?.data.map((member) => ({
        ...member,
        votingStreak: getRandomInt(50),
        score: getRandomInt(500),
      }));
      dispatch({
        type: "SUCCESS",
        payload: { ...members, data: membersMocked },
      });
    } catch (err) {
      notifyError(err, url);
      dispatch({ type: "ERROR", payload: { errorData: err.message } });
    }
  }, [dispatch, communityId, count, start, notifyError]);
  const resetResults = () => {
    dispatch({ type: "RESET_RESULTS" });
  };
  useEffect(() => {
    getCommunityMembers();
  }, [getCommunityMembers]);
  return {
    ...state,
    getCommunityMembers,
    fetchMore,
    resetResults,
  };
}
function useCommunityMembers ( { communityId , start = PAGINATION_INITIAL_STATE . start , count = PAGINATION_INITIAL_STATE . count , } = { } ) { const [ state , dispatch ] = useReducer ( paginationReducer , { ... INITIAL_STATE , pagination : { ... PAGINATION_INITIAL_STATE , start , count , } , } ) ; const { notifyError } = useErrorHandlerContext ( ) ; const fetchMore = useCallback ( async ( ) => { dispatch ( { type : "PROCESSING" } ) ; const url = ` ${ process . env . REACT_APP_BACK_END_SERVER_API } ${ communityId } ${ state . pagination . count } ${ state . pagination . start } ` ; try { const response = await fetch ( url ) ; const members = await checkResponse ( response ) ; const membersMocked = members ?. data . map ( ( member ) => ( { ... member , votingStreak : getRandomInt ( 50 ) , score : getRandomInt ( 500 ) , } ) ) ; dispatch ( { type : "SUCCESS" , payload : { ... members , data : membersMocked } , } ) ; } catch ( err ) { notifyError ( err , url ) ; dispatch ( { type : "ERROR" , payload : { errorData : err . message } } ) ; } } , [ state . pagination , communityId , notifyError ] ) ; const getCommunityMembers = useCallback ( async ( ) => { dispatch ( { type : "PROCESSING" } ) ; const url = ` ${ process . env . REACT_APP_BACK_END_SERVER_API } ${ communityId } ${ count } ${ start } ` ; try { const response = await fetch ( url ) ; const members = await checkResponse ( response ) ; const membersMocked = members ?. data . map ( ( member ) => ( { ... member , votingStreak : getRandomInt ( 50 ) , score : getRandomInt ( 500 ) , } ) ) ; dispatch ( { type : "SUCCESS" , payload : { ... members , data : membersMocked } , } ) ; } catch ( err ) { notifyError ( err , url ) ; dispatch ( { type : "ERROR" , payload : { errorData : err . message } } ) ; } } , [ dispatch , communityId , count , start , notifyError ] ) ; const resetResults = ( ) => { dispatch ( { type : "RESET_RESULTS" } ) ; } ; useEffect ( ( ) => { getCommunityMembers ( ) ; } , [ getCommunityMembers ] ) ; return { ... state , getCommunityMembers , fetchMore , resetResults , } ; }
Hook to return community members.
Hook to return community members .
/**
   * Function to fetch more results based on pagination configuration
   */ // this needs to be replaced with real data from backend on votingStreak and score // see how these two fields get implemented // this needs to be replaced with real data from backend on votingStreak and score // see how these two fields get implemented