Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Replace hard code page size. #2622

Merged
merged 16 commits into from
Oct 18, 2021
9 changes: 8 additions & 1 deletion src/components/Proposal/RawProposal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ import get from "lodash/fp/get";
import { classNames } from "pi-ui";
import { getMarkdownContent } from "src/containers/Proposal/helpers";
import { useProposal } from "src/containers/Proposal/Detail/hooks";
import usePolicy from "src/hooks/api/usePolicy";
import styles from "./Proposal.module.css";

const RawProposal = ({ match }) => {
const {
policyTicketVote: { summariespagesize: proposalPageSize }
} = usePolicy();
const tokenFromUrl = get("params.token", match);
const { proposal, loading, error } = useProposal(tokenFromUrl);
const { proposal, loading, error } = useProposal(
tokenFromUrl,
proposalPageSize
);
const rawMarkdown = proposal ? getMarkdownContent(proposal.files) : null;
return !loading && !error ? (
<div>
Expand Down
14 changes: 13 additions & 1 deletion src/containers/Proposal/Admin/Admin.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import useProposalsStatusChangeUser from "src/hooks/api/useProposalsStatusChange
import { PROPOSAL_STATUS_CENSORED } from "src/constants";
import { tabValues, statusByTab } from "./helpers";
import { mapProposalsTokensByTab } from "src/containers/Proposal/helpers";
import usePolicy from "src/hooks/api/usePolicy";
// XXX change to AdminActionsProvider
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be it is generated automatically from code editor. I will remove it now. Thanks for your review @amass01


import {
UnvettedActionsProvider,
PublicActionsProvider
Expand All @@ -26,6 +29,12 @@ const AdminProposals = ({ TopBanner, PageDetails, Main }) => {
0,
tabLabels
);
const {
policyTicketVote: {
summariespagesize: proposalPageSize,
inventorypagesize: inventoryPageSize
}
} = usePolicy();
const {
proposals: batchProposals,
proposalsTokens,
Expand All @@ -39,7 +48,9 @@ const AdminProposals = ({ TopBanner, PageDetails, Main }) => {
fetchVoteSummary: false,
fetchProposalSummary: true,
unvetted: true,
proposalStatus: statusByTab[tabLabels[tabIndex]]
proposalStatus: statusByTab[tabLabels[tabIndex]],
proposalPageSize: proposalPageSize,
inventoryPageSize: inventoryPageSize
});

const { proposals, loading: mdLoading } = useProposalsStatusChangeUser(
Expand Down Expand Up @@ -78,6 +89,7 @@ const AdminProposals = ({ TopBanner, PageDetails, Main }) => {
dropdownTabsForMobile={true}
hasMore={hasMoreProposals}
isLoading={loading || verifying || mdLoading}
pageSize={proposalPageSize}
getEmptyMessage={getEmptyMessage}>
{({ tabs, content }) => (
<>
Expand Down
6 changes: 5 additions & 1 deletion src/containers/Proposal/Detail/Detail.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { shortRecordToken } from "src/helpers";
import ModalLogin from "src/components/ModalLogin";
import useModalContext from "src/hooks/utils/useModalContext";
import { usePaywall, useIdentity } from "src/hooks";
import usePolicy from "src/hooks/api/usePolicy";

const COMMENTS_LOGIN_MODAL_ID = "commentsLoginModal";

Expand All @@ -51,6 +52,9 @@ const SetPageTitle = ({ title }) => {
const ProposalDetail = ({ Main, match, history }) => {
const tokenFromUrl = shortRecordToken(get("params.token", match));
const threadParentCommentID = get("params.commentid", match);
const {
policyTicketVote: { summariespagesize: proposalPageSize }
} = usePolicy();
const {
proposal: fetchedProposal,
loading,
Expand All @@ -65,7 +69,7 @@ const ProposalDetail = ({ Main, match, history }) => {
commentsError,
commentsLoading,
onReloadProposalDetails
} = useProposal(tokenFromUrl, threadParentCommentID);
} = useProposal(tokenFromUrl, proposalPageSize, threadParentCommentID);
const { userid } = currentUser || {};
const isSingleThread = !!threadParentID;
const { proposals, loading: mdLoading } = useProposalsStatusChangeUser(
Expand Down
8 changes: 5 additions & 3 deletions src/containers/Proposal/Detail/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const getProposalRfpLinksTokens = (proposal) => {
return isSubmission ? [proposal.linkto] : proposal.linkedfrom;
};

export function useProposal(token, threadParentID) {
export function useProposal(token, proposalPageSize, threadParentID) {
const tokenShort = shortRecordToken(token);
const onFetchProposalDetails = useAction(act.onFetchProposalDetails);
const onFetchProposalsBatch = useAction(act.onFetchProposalsBatch);
Expand Down Expand Up @@ -148,8 +148,10 @@ export function useProposal(token, threadParentID) {
},
verify: () => {
if (hasRemainingTokens) {
const [tokensBatch, next] =
getTokensForProposalsPagination(remainingTokens);
const [tokensBatch, next] = getTokensForProposalsPagination(
remainingTokens,
proposalPageSize
);
// Fetch vote & proposal summaries only if the proposal is a RFP.
// If it is a submission, just grab the records info.
onFetchProposalsBatch({
Expand Down
6 changes: 5 additions & 1 deletion src/containers/Proposal/Edit/Edit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ import { getMarkdownContent, isPublicProposal } from "../helpers";
import { formatUnixTimestampToObj } from "src/utils";
import { getAttachmentsFiles } from "src/helpers";
import { usdFormatter } from "src/utils";
import usePolicy from "src/hooks/api/usePolicy";

const EditProposal = ({ match }) => {
const tokenFromUrl = get("params.token", match);
const { proposal, loading } = useProposal(tokenFromUrl);
const {
policyTicketVote: { summariespagesize: proposalPageSize }
} = usePolicy();
const { proposal, loading } = useProposal(tokenFromUrl, proposalPageSize);
const isPublic = isPublicProposal(proposal);
const { onEditProposal, currentUser } = useEditProposal();
const { userid } = currentUser || {};
Expand Down
6 changes: 5 additions & 1 deletion src/containers/Proposal/User/Submitted.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import LazyList from "src/components/LazyList";
import LoadingPlaceholders from "src/components/LoadingPlaceholders";
import HelpMessage from "src/components/HelpMessage";
import usePolicy from "src/hooks/api/usePolicy";

const PAGE_SIZE = 20;

Expand All @@ -20,8 +21,11 @@ const Proposals = (props) => {
const [hasMoreToLoad, setHasMore] = useState(false);

const { userID } = props;
const {
policyTicketVote: { summariespagesize: proposalPageSize }
} = usePolicy();
const { proposals, loading, numOfUserProposals, onFetchMoreProposals } =
useUserProposals({ userID });
useUserProposals({ proposalPageSize, userID });

const amountOfProposalsFetched = proposals ? proposals.length : 0;

Expand Down
20 changes: 18 additions & 2 deletions src/containers/Proposal/Vetted/Vetted.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useCallback, useMemo } from "react";
import get from "lodash/fp/get";
import isEmpty from "lodash/fp/isEmpty";
import styles from "./VettedProposals.module.css";
import { tabValues, statusByTab, sortByTab } from "./helpers";
Expand All @@ -13,6 +14,7 @@ import { LIST_HEADER_VETTED, INELIGIBLE } from "src/constants";
import useQueryStringWithIndexValue from "src/hooks/utils/useQueryStringWithIndexValue";
import { PROPOSAL_STATUS_CENSORED } from "src/constants";
import useProposalsStatusChangeUser from "src/hooks/api/useProposalsStatusChangeUser";
import usePolicy from "src/hooks/api/usePolicy";

const renderProposal = (record) => (
<Proposal key={record.censorshiprecord.token} proposal={record} />
Expand All @@ -29,6 +31,16 @@ const VettedProposals = ({ TopBanner, PageDetails, Sidebar, Main }) => {
const [index, onSetIndex] = useQueryStringWithIndexValue("tab", 0, tabLabels);
const statuses = statusByTab[tabLabels[index]];
const sort = sortByTab[tabLabels[index]];
const policy = usePolicy();
const proposalPageSize = get(policy, [
"policyTicketVote",
"summariespagesize"
]);
const inventoryPageSize = get(policy, [
"policyTicketVote",
"inventorypagesize"
]);

const {
proposals: batchProposals,
proposalsTokens,
Expand All @@ -40,9 +52,12 @@ const VettedProposals = ({ TopBanner, PageDetails, Sidebar, Main }) => {
isProposalsBatchComplete
} = useProposalsBatch({
fetchRfpLinks: true,
fetchVoteSummaries: true,
statuses: statuses,
proposalPageSize: proposalPageSize,
inventoryPageSize: inventoryPageSize,
fetchVoteSummary: true,
fetchProposalSummary: true,
statuses: statuses
fetchProposalSummary: true
});

const { proposals, loading: mdLoading } = useProposalsStatusChangeUser(
Expand Down Expand Up @@ -130,6 +145,7 @@ const VettedProposals = ({ TopBanner, PageDetails, Sidebar, Main }) => {
dropdownTabsForMobile
hasMore={hasMoreProposals}
isLoading={loading || verifying || mdLoading}
pageSize={proposalPageSize}
sort={sort}>
{content}
</RecordsView>
Expand Down
15 changes: 6 additions & 9 deletions src/hooks/api/useProposalsBatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ const getUnfetchedTokens = (proposals, tokens) =>
keys(proposals).map((token) => shortRecordToken(token))
);

const getCurrentPage = (tokens) => {
return tokens ? Math.floor(+tokens.length / INVENTORY_PAGE_SIZE) : 0;
};

const cacheVoteStatus = {};

const updateCacheVoteStatusMap = (voteSummaries, isRefresh) => {
Expand Down Expand Up @@ -93,7 +89,8 @@ export default function useProposalsBatch({
unvetted = false,
proposalStatus,
statuses,
proposalPageSize = PROPOSAL_PAGE_SIZE
proposalPageSize = PROPOSAL_PAGE_SIZE,
inventoryPageSize = INVENTORY_PAGE_SIZE
}) {
const [remainingTokens, setRemainingTokens] = useState([]);
const [voteStatuses, setStatuses] = useState(statuses);
Expand Down Expand Up @@ -125,8 +122,8 @@ export default function useProposalsBatch({
[allByStatus, isByRecordStatus, currentStatus]
);
const page = useMemo(() => {
return getCurrentPage(tokens);
}, [tokens]);
return tokens ? Math.floor(+tokens.length / inventoryPageSize) : 0;
}, [tokens, inventoryPageSize]);
const errorSelector = useMemo(
() => or(sel.apiProposalsBatchError, sel.apiPropsVoteSummaryError),
[]
Expand Down Expand Up @@ -165,7 +162,7 @@ export default function useProposalsBatch({
// there are no tokens to be fetched from the next page
const scanNextStatus =
initializedInventory &&
(!(tokens.length % INVENTORY_PAGE_SIZE === 0 && tokens.length > 0) ||
(!(tokens.length % inventoryPageSize === 0 && tokens.length > 0) ||
remainingTokens.length === proposalPageSize);
if (scanNextStatus) {
const { index, tokens } = scanNextStatusTokens(
Expand Down Expand Up @@ -332,7 +329,7 @@ export default function useProposalsBatch({
!getUnfetchedTokens(proposals, tokens).length;

const isAnotherTokensScanningRequired =
tokens && tokens.length && tokens.length % INVENTORY_PAGE_SIZE === 0;
tokens && tokens.length && tokens.length % inventoryPageSize === 0;

const onFetchMoreProposals = useCallback(() => {
if (remainingTokens.length < proposalPageSize) return send(START);
Expand Down