@@ -34,15 +34,9 @@ function RegionSelector(props) {
3434 return { value : region . name , label : region . label } ;
3535 } ) ;
3636
37- // This is a temporary solution that will need to be superseded by
38- // an improved back end design; removing this value allows
39- // DatasetSelector to handle choosing between enhanced CPS data and
40- // standard US data
37+ // The below allows backward compatibility with a past design where enhanced_cps
38+ // was also a region value
4139 options = options . filter ( ( option ) => option . value !== "enhanced_us" ) ;
42-
43- // These lines are also a temporary solution; if user accesses the component
44- // with the enhanced_us region via URL, the component should instead display
45- // the US
4640 let inputRegion = searchParams . get ( "region" ) ;
4741 if ( inputRegion === "enhanced_us" ) {
4842 inputRegion = "us" ;
@@ -415,62 +409,65 @@ function FullLiteToggle() {
415409}
416410
417411/**
418- * A (hopefully temporary) component meant to abstract away the fact
419- * that the US enhanced CPS data is defined as a region within the US
420- * country package; this displays the enhanced CPS as a dataset on the
421- * right-hand policy panel
412+ * This displays the enhanced CPS as a dataset on the right-hand policy panel
422413 * @param {Object } props
423- * @param {String } presentRegion The region , taken from the searchParams
414+ * @param {String } presentDataset The dataset , taken from the searchParams
424415 * @param {Number|String } timePeriod The year the simulation should run over
425416 * @returns {import("react").ReactElement }
426417 */
427418function DatasetSelector ( props ) {
428- const { presentRegion, timePeriod } = props ;
419+ const { presentDataset, timePeriod } = props ;
420+ const [ isChecked , setIsChecked ] = useState ( confirmIsChecked ( presentDataset ) ) ;
429421 const [ searchParams , setSearchParams ] = useSearchParams ( ) ;
430422 const displayCategory = useDisplayCategory ( ) ;
431423
432- // Determine whether slider should be enabled or disabled
433- function shouldEnableSlider ( presentRegion , timePeriod ) {
434- // Define the regions the slider should be enabled
435- const showRegions = [ "enhanced_us" , "us" , null ] ;
424+ function confirmIsChecked ( presentDataset ) {
425+ // Define presentDataset value that activates check
426+ const checkValue = "enhanced_cps" ;
427+ if ( presentDataset === checkValue ) {
428+ return true ;
429+ }
430+ return false ;
431+ }
436432
437- // Define the times the slider should NOT be enabled
438- const dontShowTimes = [ "2021" ] ;
433+ // Determine whether slider should be enabled or disabled
434+ function shouldEnableSlider ( timePeriod ) {
435+ // Define earliest year slider should be shown for
436+ const sliderStartYear = 2024 ;
439437
440438 // Return whether or not slider should be enabled
441- if (
442- showRegions . includes ( presentRegion ) &&
443- ! dontShowTimes . includes ( String ( timePeriod ) )
444- ) {
439+ // Null timePeriod reflects no URL param setting yet -
440+ // this is actually default behavior
441+ if ( ! timePeriod || timePeriod >= sliderStartYear ) {
445442 return true ;
446443 }
447444
448445 return false ;
449446 }
450447
451- /**
452- * Switch change handler
453- * @param {Boolean } isChecked Whether or not the switch is "checked";
454- * note that the event is not passed to the handler by default
455- * @returns {undefined|null } Returns null as a safety check in cases where
456- * switch shouldn't be active in the first place
457- */
458- function handleChange ( isChecked ) {
459- // Define our desired states; item 0 corresponds to
460- // "true" and 1 to "false", since bools can't be used as keys
461- const outputStates = [ "enhanced_us" , "us" ] ;
462-
448+ function handleChange ( ) {
463449 // First, safety check - if the button isn't even
464450 // supposed to be shown, do nothing
465- if ( ! shouldEnableSlider ( presentRegion , timePeriod ) ) {
451+ if ( ! shouldEnableSlider ( timePeriod ) ) {
466452 return ;
467453 }
468454
469455 // Duplicate the existing search params
470456 let newSearch = copySearchParams ( searchParams ) ;
471457
472458 // Set params accordingly
473- newSearch . set ( "region" , isChecked ? outputStates [ 0 ] : outputStates [ 1 ] ) ;
459+ if ( isChecked ) {
460+ newSearch . delete ( "dataset" ) ;
461+ // Allows for backwards compatibility with a past design
462+ // where enhanced_cps was also a region value
463+ if ( searchParams . get ( "region" ) === "enhanced_us" ) {
464+ newSearch . set ( "region" , "us" ) ;
465+ }
466+ setIsChecked ( false ) ;
467+ } else {
468+ newSearch . set ( "dataset" , "enhanced_cps" ) ;
469+ setIsChecked ( true ) ;
470+ }
474471 setSearchParams ( newSearch ) ;
475472 }
476473
@@ -488,17 +485,15 @@ function DatasetSelector(props) {
488485 data-testid = "enhanced_cps_switch"
489486 size = { displayCategory !== "mobile" && "small" }
490487 onChange = { handleChange }
491- disabled = { ! shouldEnableSlider ( presentRegion , timePeriod ) }
492- checked = { presentRegion === "enhanced_us " ? true : false }
488+ disabled = { ! shouldEnableSlider ( timePeriod ) }
489+ checked = { presentDataset === "enhanced_cps " ? true : false }
493490 />
494491 < p
495492 style = { {
496493 margin : 0 ,
497494 fontSize : displayCategory !== "mobile" && "0.95em" ,
498- color :
499- ! shouldEnableSlider ( presentRegion , timePeriod ) && "rgba(0,0,0,0.5)" ,
500- cursor :
501- ! shouldEnableSlider ( presentRegion , timePeriod ) && "not-allowed" ,
495+ color : ! shouldEnableSlider ( timePeriod ) && "rgba(0,0,0,0.5)" ,
496+ cursor : ! shouldEnableSlider ( timePeriod ) && "not-allowed" ,
502497 } }
503498 >
504499 Use Enhanced CPS (beta)
@@ -804,6 +799,14 @@ export default function PolicyRightSidebar(props) {
804799 const focus = searchParams . get ( "focus" ) || "" ;
805800 const stateAbbreviation = focus . split ( "." ) [ 2 ] ;
806801 const hasHousehold = searchParams . get ( "household" ) !== null ;
802+
803+ let dataset = searchParams . get ( "dataset" ) ;
804+ // This allows backward compatibility with a past
805+ // design where enhanced_cps was also a region value
806+ if ( region === "enhanced_us" && ! dataset ) {
807+ dataset = "enhanced_cps" ;
808+ }
809+
807810 const options = metadata . economy_options . region . map ( ( stateAbbreviation ) => {
808811 return { value : stateAbbreviation . name , label : stateAbbreviation . label } ;
809812 } ) ;
@@ -1100,7 +1103,7 @@ export default function PolicyRightSidebar(props) {
11001103 </ div >
11011104 { metadata . countryId === "us" && (
11021105 < DatasetSelector
1103- presentRegion = { region }
1106+ presentDataset = { dataset }
11041107 timePeriod = { timePeriod }
11051108 />
11061109 ) }
0 commit comments