Source

components/Music/MusicPerformanceHeader/MusicPerformanceHeader.js

  1. // NPM module imports
  2. import React, { Component } from "react";
  3. import PropTypes from "prop-types";
  4. import { withRouter } from "react-router-dom";
  5. // Component imports
  6. import RectangularButton from "../../Buttons/RectangularButton/RectangularButton";
  7. import ExerciseGenerator from "./ExerciseGenerator/ExerciseGenerator";
  8. // File imports
  9. import * as rectButtonColorOptions from "../../Buttons/RectangularButton/rectangularButtonColorOptions";
  10. import * as buttonTypes from "../../Buttons/buttonTypes";
  11. // Style imports
  12. import styles from "./MusicPerformanceHeader.module.scss";
  13. /**
  14. * Renders the MusicPerformanceHeader component.
  15. * This component is used when a user is viewing performance.
  16. * @extends {Component}
  17. * @category Music
  18. * @component
  19. */
  20. class MusicPerformanceHeader extends Component {
  21. /**
  22. * MusicPerformanceHeader component state
  23. * @property {boolean} shouldDisplayExerciseGenerator - Indicates of the exercise generator should be displayed
  24. */
  25. state = {
  26. shouldDisplayExerciseGenerator: false,
  27. };
  28. /**
  29. * Flips shouldDisplayExerciseGenerator property in state
  30. * @function
  31. */
  32. showOrHideExerciseGenerator = () => {
  33. this.setState((prevState) => {
  34. return {
  35. shouldDisplayExerciseGenerator: !prevState.shouldDisplayExerciseGenerator,
  36. };
  37. });
  38. };
  39. getExerciseGeneratorOrButton = () => {
  40. if (this.state.shouldDisplayExerciseGenerator) {
  41. // Returns the ExerciseGenerator component
  42. return (
  43. <ExerciseGenerator
  44. numberOfMeasures={this.props.numberOfMeasures}
  45. onGenerateExerciseClose={this.showOrHideExerciseGenerator}
  46. showExercise={this.props.switchToExercise}
  47. />
  48. );
  49. } else {
  50. // Returns a button that will open the ExerciseGenerator component
  51. return (
  52. <RectangularButton
  53. type={buttonTypes.BUTTON}
  54. value="create-exercise-generation"
  55. text="Create an Exercise"
  56. backgroundColor={rectButtonColorOptions.ORANGE}
  57. onClick={this.showOrHideExerciseGenerator}
  58. />
  59. );
  60. }
  61. };
  62. /**
  63. * Renders the MusicPerformanceHeader component
  64. */
  65. render() {
  66. return (
  67. <div className={styles.MusicPerformanceHeader}>
  68. {/* An exercise generator or a button */}
  69. {this.getExerciseGeneratorOrButton()}
  70. {/* A button to switch to the practice view */}
  71. <RectangularButton
  72. type={buttonTypes.BUTTON}
  73. value="practice"
  74. text="Practice Music"
  75. backgroundColor={rectButtonColorOptions.GREEN}
  76. onClick={this.props.switchToPractice}
  77. />
  78. </div>
  79. );
  80. }
  81. }
  82. // Prop types for the MusicPerformanceHeader component
  83. MusicPerformanceHeader.propTypes = {
  84. /**
  85. * The number of measures in the current piece of sheet music
  86. */
  87. numberOfMeasures: PropTypes.string.isRequired,
  88. /**
  89. * Switches to the practice view
  90. */
  91. switchToPractice: PropTypes.func.isRequired,
  92. /**
  93. * Switches to the exercise view
  94. */
  95. switchToExercise: PropTypes.func.isRequired,
  96. };
  97. export default withRouter(MusicPerformanceHeader);