Skip to content

Commit

Permalink
Clean up incentives card with new project selections
Browse files Browse the repository at this point in the history
  • Loading branch information
ayangster committed Oct 2, 2023
1 parent 004f968 commit b7d8a75
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/state-calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class RewiringAmericaStateCalculator extends LitElement {
selectedProjectTab: Project | undefined;

@property({ type: String })
selectedOtherTab: Project = 'battery';
selectedOtherTab: Project | undefined;

/**
* This is a hack to deal with a quirk of the UI.
Expand Down
28 changes: 23 additions & 5 deletions src/state-incentive-details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ export const stateIncentivesTemplate = (
selectedProjects: Project[],
onOtherTabSelected: (newOtherSelection: Project) => void,
onTabSelected: (newSelection: Project) => void,
selectedOtherTab: Project,
selectedOtherTab?: Project,
selectedProjectTab?: Project,
) => {
const allEligible = response.incentives.filter(i => i.eligible);
Expand All @@ -407,6 +407,11 @@ export const stateIncentivesTemplate = (
]),
) as Record<Project, Incentive[]>;

const nonSelectedProjects = Object.entries(PROJECTS)
.filter(([project, _]) => !selectedProjects.includes(project as Project))
.sort(([a], [b]) => shortLabel(a).localeCompare(shortLabel(b)))
.map(([project, _]) => project);

// Only offer "other" tabs if there are incentives for that project.
const otherTabs = (
Object.entries(incentivesByProject) as [Project, Incentive[]][]
Expand All @@ -418,8 +423,21 @@ export const stateIncentivesTemplate = (
.sort(([a], [b]) => shortLabel(a).localeCompare(shortLabel(b)))
.map(([project]) => project);

const projectTab = selectedProjectTab ?? selectedProjects[0];
const projectTab =
selectedProjectTab &&
selectedProjects.includes(selectedProjectTab as Project)
? selectedProjectTab
: selectedProjects[0];
const otherTab =
selectedOtherTab &&
nonSelectedProjects.includes(selectedOtherTab as Project)
? selectedOtherTab
: nonSelectedProjects[0];

const selectedIncentives = incentivesByProject[projectTab] ?? [];
const selectedOtherIncentives =
incentivesByProject[otherTab as Project] ?? [];

const otherIncentivesLabel =
selectedIncentives.length == 0
? 'Incentives available to you'
Expand All @@ -430,15 +448,15 @@ export const stateIncentivesTemplate = (
"Incentives you're interested in",
selectedIncentives,
selectedProjects,
selectedProjects.includes(projectTab) ? projectTab : selectedProjects[0],
projectTab,
onTabSelected,
)}
${gridTemplate(
otherIncentivesLabel,
incentivesByProject[selectedOtherTab] ?? [],
selectedOtherIncentives,
otherTabs,
// If a nonexistent tab is selected, pretend the first one is selected.
otherTabs.includes(selectedOtherTab) ? selectedOtherTab : otherTabs[0],
otherTab as Project,
onOtherTabSelected,
)}
${authorityLogosTemplate(response)}`;
Expand Down

0 comments on commit b7d8a75

Please sign in to comment.