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

Add clickability to initial microsim outputs #2014

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 52 additions & 11 deletions src/pages/policy/output/PolicyBreakdown.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import { useSearchParams } from "react-router-dom";
import style from "../../../style";
import { motion } from "framer-motion";

export default function PolicyBreakdown(props) {
const { policyLabel, metadata, impact, timePeriod, region } = props;
const [searchParams, setSearchParams] = useSearchParams();

const regionObj = metadata.economy_options.region.find(
(elem) => elem.name === region,
);

const handleItemClick = (focusTarget) => {
let newSearch = new URLSearchParams(searchParams);

newSearch.set("focus", focusTarget);

setSearchParams(newSearch);
};
let regionLabel;
// This is a workaround for enhanced_us that should be changed
// if and when it is treated as something other than a "region"
Expand Down Expand Up @@ -42,13 +53,15 @@ export default function PolicyBreakdown(props) {
{
value: budgetaryImpact,
type: "budgetaryImpact",
focusTarget: "policyOutput.budgetaryImpact.overall",
formatOptions: {
currencyLabel: metadata.currency,
},
},
{
value: povertyRateChange,
type: "povertyRateChange",
focusTarget: "policyOutput.povertyImpact.regular.byAge",
formatOptions: {
percentage: true,
signFlip: true,
Expand All @@ -57,6 +70,7 @@ export default function PolicyBreakdown(props) {
{
value: decileOverview,
type: "winnersLosersPercent",
focusTarget: "policyOutput.winnersAndLosers.incomeDecile",
},
];

Expand All @@ -67,6 +81,7 @@ export default function PolicyBreakdown(props) {
data={listItems}
title={title}
bottomText={bottomText}
onItemClick={handleItemClick}
/>
</>
);
Expand All @@ -82,7 +97,7 @@ export default function PolicyBreakdown(props) {
* @returns {import("react-markdown/lib/react-markdown").ReactElement}
*/
function BreakdownTemplate(props) {
const { data, title, bottomText } = props;
const { data, title, bottomText, onItemClick } = props;

const COLORS = {
pos: style.colors.BLUE,
Expand All @@ -104,7 +119,7 @@ function BreakdownTemplate(props) {
gap: "20px",
}}
>
{formatWinnersLosers(item.value)}
{formatWinnersLosers(item, onItemClick)}
</div>
);
}
Expand All @@ -131,6 +146,7 @@ function BreakdownTemplate(props) {
}

const [descStart, descEnd] = formatDesc(item.value, item.type);
const focusTarget = item.focusTarget;
const formattedValue = formatValue(
item.value,
item.type,
Expand All @@ -148,10 +164,16 @@ function BreakdownTemplate(props) {
gap: "20px",
}}
>
<h2
<motion.h2
style={{
fontSize: 22,
cursor: "pointer",
}}
whileHover={{
backgroundColor: style.colors.BLUE_LIGHT,
}}
transition={{ duration: 0.001 }}
onClick={() => onItemClick(focusTarget)}
>
{descStart}
&nbsp;
Expand All @@ -164,7 +186,7 @@ function BreakdownTemplate(props) {
</span>
&nbsp;
{descEnd}
</h2>
</motion.h2>
</div>
);
});
Expand Down Expand Up @@ -345,7 +367,8 @@ function formatValue(value, type, options) {
* @returns {React.JSX} The formatted JSX, as this can contain anywhere
* between 0 and 2 values
*/
function formatWinnersLosers(decileOverview) {
function formatWinnersLosers(item, onItemClick) {
const { value: decileOverview, focusTarget } = item;
const winnersPercent =
decileOverview["Gain more than 5%"] + decileOverview["Gain less than 5%"];
const losersPercent =
Expand All @@ -354,13 +377,19 @@ function formatWinnersLosers(decileOverview) {
// If both values are 0...
if (!winnersPercent && !losersPercent) {
return (
<h2
<motion.h2
style={{
fontSize: 22,
cursor: "pointer",
}}
whileHover={{
backgroundColor: style.colors.BLUE_LIGHT,
}}
transition={{ duration: 0.001 }}
onClick={() => onItemClick(focusTarget)}
>
Does not affect anyone&apos;s net income
</h2>
</motion.h2>
);
}

Expand All @@ -383,10 +412,16 @@ function formatWinnersLosers(decileOverview) {
// If both aren't 0...
if (winnersPercent && losersPercent) {
return (
<h2
<motion.h2
style={{
fontSize: 22,
cursor: "pointer",
}}
whileHover={{
backgroundColor: style.colors.BLUE_LIGHT,
}}
transition={{ duration: 0.001 }}
onClick={() => onItemClick(focusTarget)}
>
Increases net income for&nbsp;
<span
Expand All @@ -405,16 +440,22 @@ function formatWinnersLosers(decileOverview) {
{losersValue}
</span>
&nbsp;of people
</h2>
</motion.h2>
);
}

// Otherwise, conditionally return whichever is correct
return (
<h2
<motion.h2
style={{
fontSize: 22,
cursor: "pointer",
}}
whileHover={{
backgroundColor: style.colors.BLUE_LIGHT,
}}
transition={{ duration: 0.001 }}
onClick={() => onItemClick(focusTarget)}
>
{`${winnersPercent ? "Raises" : "Lowers"} net income for`}
&nbsp;
Expand All @@ -426,7 +467,7 @@ function formatWinnersLosers(decileOverview) {
{winnersPercent ? winnersValue : losersValue}
</span>
&nbsp;of people
</h2>
</motion.h2>
);
}

Expand Down
Loading