/src/components/pages/race/get-better-data/state-script.js

https://github.com/COVID19Tracking/website · JavaScript · 93 lines · 75 code · 10 blank · 8 comment · 5 complexity · b0f52427ee54c2df65116c141d3f281f MD5 · raw file

  1. /* eslint-disable react/forbid-prop-types */
  2. import React from 'react'
  3. import PropTypes from 'prop-types'
  4. import stateInfotyles from './state-script.module.scss'
  5. const template = require('mustache')
  6. const StateScript = ({ currentStateName, stateInfo }) => {
  7. if (currentStateName === '-- Select a state --') {
  8. return null
  9. }
  10. // find the current state
  11. const currentState = stateInfo.find(state => state.name === currentStateName)
  12. const numberToPercentage = number => {
  13. return number * 100 > 1 ? Math.round(number * 100) : '<1'
  14. }
  15. let script
  16. if (currentState.message) {
  17. // if there is a custom script for this state
  18. const scriptValues = {
  19. knownEthDeath: numberToPercentage(currentState.knownEthDeath), // separate
  20. knownEthPos: numberToPercentage(currentState.knownEthPos), // separate
  21. knownRaceDeath: numberToPercentage(currentState.knownRaceDeath), // separate
  22. knownRacePos: numberToPercentage(currentState.knownRacePos), // separate
  23. knownRaceEthDeath: numberToPercentage(currentState.knownRaceEthDeath), // combined
  24. knownRaceEthPos: numberToPercentage(currentState.knownRaceEthPos), // combined
  25. }
  26. // render Mustache template
  27. script = template.render(
  28. currentState.message.childMarkdownRemark.html,
  29. scriptValues,
  30. )
  31. }
  32. return (
  33. <div className={stateInfotyles.container}>
  34. <h5>Suggested script for {currentStateName}</h5>
  35. {script === undefined ? (
  36. <p>
  37. Im calling on {currentStateName} to release the latest race and
  38. ethnicity data for COVID-19.
  39. <br />
  40. <br />
  41. Nationwide, COVID-19 is disproportionately affecting Black,
  42. Indigenous, Latinx, and other people of color. Its important for our
  43. state to report accurate and up-to-date information about whats
  44. happening so that our health officials, business leaders, and the
  45. general public know how to respond to the pandemic.
  46. </p>
  47. ) : (
  48. <div
  49. dangerouslySetInnerHTML={{
  50. __html: script,
  51. }}
  52. />
  53. )}
  54. </div>
  55. )
  56. }
  57. StateScript.defaultProps = {
  58. currentStateName: '-- Select a state --',
  59. }
  60. StateScript.propTypes = {
  61. currentStateName: PropTypes.string,
  62. stateInfo: PropTypes.arrayOf(
  63. PropTypes.shape({
  64. name: PropTypes.string.isRequired,
  65. message: PropTypes.shape({
  66. childMarkdownRemark: PropTypes.shape({
  67. html: PropTypes.string,
  68. }),
  69. }),
  70. /*
  71. graphQL serves the following values as strings,
  72. yet they contain numerical values
  73. */
  74. knownEthDeath: PropTypes.any,
  75. knownEthPos: PropTypes.any,
  76. knownRaceDeath: PropTypes.any,
  77. knownRacePos: PropTypes.any,
  78. knownRaceEthDeath: PropTypes.any,
  79. knownRaceEthPos: PropTypes.any,
  80. }),
  81. ).isRequired,
  82. }
  83. export default StateScript