Skip to content

Commit

Permalink
finish normalizing proposals and proposalsVotes state; create short t…
Browse files Browse the repository at this point in the history
…oken helper for increased consistency
  • Loading branch information
thi4go committed May 7, 2021
1 parent 542bace commit 30772b8
Show file tree
Hide file tree
Showing 21 changed files with 75 additions and 67 deletions.
3 changes: 2 additions & 1 deletion src/components/ProposalForm/ProposalForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import DraftSaver from "./DraftSaver";
import { useProposalForm } from "./hooks";
import usePolicy from "src/hooks/api/usePolicy";
import {
shortRecordToken,
replaceBlobsByDigestsAndGetFiles,
replaceImgDigestByBlob
} from "src/helpers";
Expand Down Expand Up @@ -372,7 +373,7 @@ const ProposalFormWrapper = ({
: proposalState === PROPOSAL_STATE_VETTED
? ""
: "/unvetted"
}/${proposalToken.substring(0, 7)}`
}/${shortRecordToken(proposalToken)}`
);
resetForm();
} catch (e) {
Expand Down
3 changes: 2 additions & 1 deletion src/components/ProposalsList/ProposalsList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from "react";
import PropTypes from "prop-types";
import { Text, classNames, useTheme, getThemeProperty } from "pi-ui";
import styles from "./ProposalsList.module.css";
import { shortRecordToken } from "src/helpers";
import ProposalItem from "./ProposalItem";
import LoadingPlaceholders from "src/components/LoadingPlaceholders";
import ContentLoader from "react-content-loader";
Expand Down Expand Up @@ -41,7 +42,7 @@ const ProposalsList = ({ data: { proposals, voteSummaries } }) => {
proposal={proposal}
voteSummary={
voteSummaries[
proposals[index]?.censorshiprecord.token.substring(0, 7)
shortRecordToken(proposals[index]?.censorshiprecord.token)
]
}
/>
Expand Down
9 changes: 2 additions & 7 deletions src/components/RecordsView/RecordsView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import LazyList from "src/components/LazyList";
import { getRecordsByTabOption } from "./helpers";
import HelpMessage from "src/components/HelpMessage";
import { useConfig } from "src/containers/Config";
import { shortRecordToken } from "src/helpers";
import { NOJS_ROUTE_PREFIX, PROPOSAL_STATUS_CENSORED } from "src/constants";

const LoadingPlaceholders = ({ numberOfItems, placeholder }) => {
Expand All @@ -16,13 +17,7 @@ const LoadingPlaceholders = ({ numberOfItems, placeholder }) => {
};

const getFilteredRecordsAndToken = (records, tokens, tab, filterCensored) => {
console.log("getFilteredRecordsAndToken")
// const tokens = fullTokens.map((token) => token.substring(0, 7));
console.log(tokens)
console.log(records)
const filteredTokens = tokens[tab].map((token) => token.substring(0, 7));
console.log("filteredTokens")
console.log(filteredTokens)
const filteredTokens = tokens[tab].map((token) => shortRecordToken(token));
let filteredRecords =
(records &&
filteredTokens &&
Expand Down
3 changes: 2 additions & 1 deletion src/containers/Invoice/Detail/InvoiceDetails.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useState, memo } from "react";
import { Spinner, Text, H4, Table, Link as UiLink, classNames } from "pi-ui";
import Link from "src/components/Link";
import { shortRecordToken } from "src/helpers";
import { useInvoices } from "./hooks";
import { formatCentsToUSD } from "src/utils";
import useAdminInvoices from "src/hooks/api/useAdminInvoices";
Expand All @@ -16,7 +17,7 @@ const printInvoiceInfo = ({
Date: `${month}/${year}`,
Invoice: (
<Link to={`/invoices/${token}`} className={styles.invoiceLinkWrapper}>
<Text color="primary">{token.substring(0, 7)}</Text>
<Text color="primary">{shortRecordToken(token)}</Text>
</Link>
),
"Total (USD)": formatCentsToUSD(total)
Expand Down
9 changes: 5 additions & 4 deletions src/containers/Proposal/Detail/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getTokensForProposalsPagination
} from "../helpers";
import { getDetailsFile } from "./helpers";
import { shortRecordToken } from "src/helpers";
import { PROPOSAL_STATE_VETTED } from "src/constants";
import useFetchMachine from "src/hooks/utils/useFetchMachine";
import isEmpty from "lodash/fp/isEmpty";
Expand All @@ -23,11 +24,11 @@ const getUnfetchedVoteSummaries = (proposal, voteSummaries) => {
const rfpLinks = getProposalRfpLinksTokens(proposal);
const proposalToken = getProposalToken(proposal);
const tokens = concat(rfpLinks || [])(proposalToken);
// compare tokens by substring
// compare tokens by short form
return tokens.filter(
(t) =>
!keys(voteSummaries).some(
(vs) => vs.substring(0, 7) === t.substring(0, 7)
(vs) => shortRecordToken(vs) === shortRecordToken(t)
)
);
};
Expand All @@ -42,7 +43,7 @@ const getProposalRfpLinksTokens = (proposal) => {
};

export function useProposal(token, proposalState, threadParentID) {
const tokenShort = token.substring(0, 7);
const tokenShort = shortRecordToken(token);
const onFetchProposalDetails = useAction(act.onFetchProposalDetails);
const onFetchProposalsBatch = useAction(act.onFetchProposalsBatch);
const onFetchProposalsVoteSummary = useAction(
Expand All @@ -69,7 +70,7 @@ export function useProposal(token, proposalState, threadParentID) {
proposals: values(pick(proposals, rfpLinks)),
voteSummaries: pick(
voteSummaries,
rfpLinks.map((l) => l.substring(0, 7))
rfpLinks.map((l) => shortRecordToken(l))
)
};

Expand Down
3 changes: 0 additions & 3 deletions src/containers/Proposal/Vetted/Vetted.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ const VettedProposals = ({ TopBanner, PageDetails, Sidebar, Main }) => {
proposalPageSize: 4
});

console.log("proposals")
console.log(proposals)

// TODO: remove legacy
const { legacyProposals, legacyProposalsTokens } = useLegacyVettedProposals(
!hasMoreProposals,
Expand Down
6 changes: 3 additions & 3 deletions src/containers/Proposal/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
INELIGIBLE,
PROPOSAL_PAGE_SIZE
} from "../../constants";
import { getTextFromIndexMd } from "src/helpers";
import { getTextFromIndexMd, shortRecordToken } from "src/helpers";
import set from "lodash/fp/set";
import values from "lodash/fp/values";
import pick from "lodash/pick";
Expand Down Expand Up @@ -284,7 +284,7 @@ export const getProposalToken = (proposal) =>
export const getProposalUrl = (token, isJsEnabled, state) => {
const stateStr = state === PROPOSAL_STATE_VETTED ? "" : "/unvetted";
return isJsEnabled
? `/record${stateStr}/${token.substring(0, 7)}`
? `/record${stateStr}/${shortRecordToken(token)}`
: `${NOJS_ROUTE_PREFIX}/record${stateStr}/${token}`;
};

Expand All @@ -298,7 +298,7 @@ export const getProposalUrl = (token, isJsEnabled, state) => {
export const getCommentsUrl = (token, isJsEnabled, state) => {
const stateStr = state === PROPOSAL_STATE_VETTED ? "" : "/unvetted";
return isJsEnabled
? `/record${stateStr}/${token.substring(0, 7)}?scrollToComments=true`
? `/record${stateStr}/${shortRecordToken(token)}?scrollToComments=true`
: `${NOJS_ROUTE_PREFIX}/record${stateStr}/${token}?scrollToComments=true`;
};

Expand Down
10 changes: 5 additions & 5 deletions src/containers/Proposal/hooks/useProposalURLs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getProposalUrl, getCommentsUrl, getAuthorUrl } from "../helpers";
import { ARCHIVE_URL } from "src/constants";
import * as sel from "src/selectors";
import { useSelector } from "src/redux";
import { shortRecordToken } from "src/helpers";
import { PROPOSAL_STATE_VETTED } from "src/constants";

export default function useProposalURLs(
Expand All @@ -19,15 +20,14 @@ export default function useProposalURLs(
const isLegacy = legacyProposals.includes(proposalToken);
const proposalURL = !isLegacy
? getProposalUrl(proposalToken, javascriptEnabled, state, isLegacy)
: `${ARCHIVE_URL}proposals/${proposalToken.substring(0, 7)}`;
: `${ARCHIVE_URL}proposals/${shortRecordToken(proposalToken)}`;
const commentsURL = useMemo(
() =>
!isLegacy
? getCommentsUrl(proposalToken, javascriptEnabled, state)
: `${ARCHIVE_URL}proposals/${proposalToken.substring(
0,
7
)}?scrollToComments=true`,
: `${ARCHIVE_URL}proposals/${
shortRecordToken(proposalToken)
}?scrollToComments=true`,
[isLegacy, javascriptEnabled, proposalToken, state]
);
const authorURL = useMemo(() => getAuthorUrl(userid, javascriptEnabled), [
Expand Down
6 changes: 5 additions & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,16 @@ export const digest = (payload) => sha3_256(payload);
export const utoa = (str) => window.btoa(unescape(encodeURIComponent(str)));
export const atou = (str) => decodeURIComponent(escape(window.atob(str)));

// shortToken receives a full lengthed record token and parses to its short
// form
export const shortRecordToken = (token) => token.substring(0, 7);

// parseReceivedProposalsMap iterates over BE returned proposals map[token] => proposal, parses the
// metadata file & the proposal statuses
export const parseReceivedProposalsMap = (proposals) => {
const parsedProps = {};
for (const [token, prop] of Object.entries(proposals)) {
parsedProps[token.substring(0, 7)] = parseRawProposal(prop);
parsedProps[shortRecordToken(token)] = parseRawProposal(prop);
}
return parsedProps;
};
Expand Down
19 changes: 8 additions & 11 deletions src/hooks/api/useLegacyVettedProposals.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
PROPOSAL_VOTING_REJECTED,
PROPOSAL_VOTING_INELIGIBLE
} from "src/constants";
import { shortRecordToken } from "src/helpers";
import legacyProposalsInfo from "src/legacyproposals.json";
import tokenInventory from "src/legacytokeninventory.json";

Expand All @@ -30,38 +31,34 @@ export default function useLegacyVettedProposals(shouldReturn = false, status) {
const [legacyProposals, setLegacyProposals] = useState([]);
const [legacyProposalsTokens, setLegacyProposalsTokens] = useState({});
useEffect(() => {
console.log("----- useLegacyVettedProposals -------")
// shouldReturn is a boolean to control when the proposals are done fetching so we can return the legacy props.
if (shouldReturn) {
const proposalsTokensList =
const proposalsTokensList =
tokenInventory[mapStatusToString[status]] &&
tokenInventory[mapStatusToString[status]]
.map((token) => token.substring(0, 7));
console.log("proposalsTokensList")
console.log(proposalsTokensList)
.map((token) => shortRecordToken(token));
// filter propsals by tab and transform from Array to Object where the key is the proposal token and the value is the proposal info
const finalList = newLegacyProposalsInfo
.filter(
(p) =>
proposalsTokensList &&
proposalsTokensList.includes(
p.censorshiprecord.token.substring(0, 7)
shortRecordToken(p.censorshiprecord.token)
)
)
.reduce(
(acc, cur) => ({ ...acc, [cur.censorshiprecord.token.substring(0, 7)]: cur }),
(acc, cur) => ({
...acc,
[shortRecordToken(cur.censorshiprecord.token)]: cur
}),
{}
);
console.log("final list")
console.log(finalList)
setLegacyProposals(finalList);
setLegacyProposalsTokens(tokenInventory);
} else {
setLegacyProposals([]);
setLegacyProposalsTokens({});
}
console.log("----- ------------------------ -------")

}, [legacyProposals.proposals, shouldReturn, status]);
return { legacyProposals, legacyProposalsTokens };
}
4 changes: 2 additions & 2 deletions src/hooks/api/useProposalsBatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import flow from "lodash/fp/flow";
import isEmpty from "lodash/fp/isEmpty";
import keys from "lodash/fp/keys";
import difference from "lodash/fp/difference";
import { shortRecordToken } from "src/helpers";
import {
PROPOSAL_STATE_VETTED,
PROPOSAL_STATE_UNVETTED,
Expand Down Expand Up @@ -50,8 +51,7 @@ const getRfpSubmissions = (proposals) =>

const getUnfetchedTokens = (proposals, tokens) =>
difference(
tokens.map((token) => token.substring(0, 7))
// tokens
tokens.map((token) => shortRecordToken(token))
)(keys(proposals));

const getCurrentPage = (tokens) => {
Expand Down
13 changes: 7 additions & 6 deletions src/reducers/models/proposalVotes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
PROPOSAL_VOTING_NOT_AUTHORIZED,
PROPOSAL_VOTING_ACTIVE
} from "../../constants";
import { shortRecordToken } from "src/helpers";
import votesstatus from "src/legacyvotestatuses";

const DEFAULT_STATE = {
Expand All @@ -26,15 +27,15 @@ const DEFAULT_STATE = {
.reduce(
(acc, cur) => ({
...acc,
[cur.token]: cur
[shortRecordToken(cur.token)]: cur
}),
{}
),
bestBlock: null
};

const receiveVoteStatusChange = (state, token, newStatus) =>
update(["byToken", token.substring(0, 7)], (voteSummary) => ({
update(["byToken", shortRecordToken(token)], (voteSummary) => ({
...voteSummary,
status: newStatus
}))(state);
Expand All @@ -54,8 +55,8 @@ const proposalVotes = (state = DEFAULT_STATE, action) =>
const normalizedSummaries = keys.reduce(
(acc, key) => ({
...acc,
[key.substring(0, 7)]: {
...state.byToken[key.substring(0, 7)],
[shortRecordToken(key)]: {
...state.byToken[shortRecordToken(key)],
...action.payload.summaries[key]
}
}),
Expand All @@ -71,7 +72,7 @@ const proposalVotes = (state = DEFAULT_STATE, action) =>
},
[act.RECEIVE_VOTES_DETAILS]: () => {
return update(
["byToken", action.payload.token.substring(0, 7)],
["byToken", shortRecordToken(action.payload.token)],
(voteSummary) => ({
...voteSummary,
details: {
Expand All @@ -83,7 +84,7 @@ const proposalVotes = (state = DEFAULT_STATE, action) =>
},
[act.RECEIVE_PROPOSAL_VOTE_RESULTS]: () => {
return update(
["byToken", action.payload.token.substring(0, 7)],
["byToken", shortRecordToken(action.payload.token)],
(voteSummary) => ({
...voteSummary,
votes: action.payload.votes
Expand Down
13 changes: 8 additions & 5 deletions src/reducers/models/proposals.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
PROPOSAL_STATE_UNVETTED
} from "../../constants";
import {
shortRecordToken,
getIndexMdFromText,
parseReceivedProposalsMap,
parseRawProposal
Expand Down Expand Up @@ -52,7 +53,7 @@ const DEFAULT_STATE = {
numOfProposalsByUserId: {},
// TODO: remove legacy
legacyProposals: legacyProposals.proposals.map(
(p) => p.censorshiprecord.token
(p) => shortRecordToken(p.censorshiprecord.token)
),
newProposalToken: null
};
Expand All @@ -79,9 +80,6 @@ const mapReviewStatusToTokenInventoryStatus = (status, state) => {

const proposalToken = (proposal) => proposal.censorshiprecord.token;

const shortProposalToken = (proposal) =>
proposalToken(proposal).substring(0, 7);

const proposalIndexFile = (name = "", description = "") =>
getIndexMdFromText([name, description].join("\n\n"));

Expand Down Expand Up @@ -146,7 +144,12 @@ const proposals = (state = DEFAULT_STATE, action) =>
update("allByRecordStatus", updateInventory(action.payload))(state),
[act.RECEIVE_EDIT_PROPOSAL]: () =>
set(
["byToken", shortProposalToken(action.payload.proposal)],
[
"byToken",
shortRecordToken(
action.payload.proposal.censorshiprecord.token
)
],
parseRawProposal(action.payload.proposal)
)(state),
[act.RECEIVE_NEW_PROPOSAL]: () => {
Expand Down
3 changes: 2 additions & 1 deletion src/selectors/models/comments.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createSelector } from "reselect";
import get from "lodash/fp/get";
import { shortRecordToken } from "src/helpers";

export const commentsByToken = get(["comments", "comments", "byToken"]);
export const accessTimeByToken = get([
Expand All @@ -19,7 +20,7 @@ const getCommentsByToken = (token) => (commentsByToken) => {
const commentsTokens = Object.keys(commentsByToken);
// check if the provided token is prefix of original token
const matchedTokenByPrefix = commentsTokens.find(
(key) => key.substring(0, 7) === token
(key) => shortRecordToken(key) === token
);
return commentsByToken[matchedTokenByPrefix];
};
Expand Down
3 changes: 2 additions & 1 deletion src/selectors/models/proposalVotes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createSelector } from "reselect";
import get from "lodash/fp/get";
import keys from "lodash/keys";
import { shortRecordToken } from "src/helpers";

export const summaryByToken = get(["proposalVotes", "byToken"]);

Expand All @@ -9,7 +10,7 @@ export const bestBlock = get(["proposalVotes", "bestBlock"]);
export const makeGetProposalVoteSummary = (token) =>
createSelector(summaryByToken, (summary) => {
const tokenFromSummary = keys(summary).find(
(s) => s.substring(0, 7) === token.substring(0, 7)
(s) => shortRecordToken(s) === shortRecordToken(token)
);
return summary[tokenFromSummary];
});
Expand Down
Loading

0 comments on commit 30772b8

Please sign in to comment.