Skip to content

Commit

Permalink
react-app: Prevent throwing error when proposal type is unknown
Browse files Browse the repository at this point in the history
  • Loading branch information
hochiw committed Sep 13, 2022
1 parent d04f706 commit b9b584b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ const ProposalTypeAndProposer: React.FC<{ proposal: Proposal }> = ({
}) => {
const { type, proposerAddress, submitTime } = proposal;

const typeNameId = getProposalTypeMessage(type);

return (
<div
className={cn(
Expand All @@ -175,7 +177,7 @@ const ProposalTypeAndProposer: React.FC<{ proposal: Proposal }> = ({
<LocalizedText messageID="ProposalDetail.proposalType" />
</p>
<p className={cn("text-sm", "mb-4")}>
<LocalizedText messageID={getProposalTypeMessage(type)} />
{typeNameId !== null ? <LocalizedText messageID={typeNameId} /> : type}
</p>
<p className={cn("text-sm", "text-app-lightgreen", "mb-1")}>
<LocalizedText messageID="ProposalDetail.publishedBy" />
Expand Down
11 changes: 10 additions & 1 deletion react-app/src/components/proposals/ProposalCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ const ProposalCard: React.FC<ProposalCardProps> = (props) => {
});
}, [proposal]);

const proposalTypeNameId = useMemo(
() => getProposalTypeMessage(proposal.type),
[proposal]
);

return (
<div
className={cn(
Expand Down Expand Up @@ -86,7 +91,11 @@ const ProposalCard: React.FC<ProposalCardProps> = (props) => {
"text-app-darkgrey"
)}
>
<LocalizedText messageID={getProposalTypeMessage(proposal.type)} />
{proposalTypeNameId !== null ? (
<LocalizedText messageID={proposalTypeNameId} />
) : (
proposal.type
)}
</span>
<h1
className={cn(
Expand Down
4 changes: 2 additions & 2 deletions react-app/src/components/proposals/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function getProposalStatusBadgeConfig(
}
}

export function getProposalTypeMessage(type: ProposalType): MessageID {
export function getProposalTypeMessage(type: string): MessageID | null {
switch (type) {
case ProposalType.Text:
return "ProposalScreen.proposalType.text";
Expand All @@ -40,7 +40,7 @@ export function getProposalTypeMessage(type: ProposalType): MessageID {
case ProposalType.CancelSoftwareUpgrade:
return "ProposalScreen.proposalType.cancelSoftwareUpgrade";
default:
throw new Error("Unknown proposal type");
return null;
}
}

Expand Down

0 comments on commit b9b584b

Please sign in to comment.