-
Notifications
You must be signed in to change notification settings - Fork 70
Refactor GraphDropdown component from being a child of Graph to being a child of NavBar
#1637
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,12 +2,49 @@ import React from "react" | |
| import { FontAwesomeIcon } from "@fortawesome/react-fontawesome" | ||
| import { faDownload } from "@fortawesome/free-solid-svg-icons" | ||
| import { Tooltip } from "react-tooltip" | ||
| import GraphDropdown from "../graph/GraphDropdown" | ||
|
|
||
| /** | ||
| * NavBar component. | ||
| */ | ||
| export function NavBar({ selected_page, open_modal }) { | ||
| export function NavBar({ selected_page, open_modal, graphs = [], updateGraph }) { | ||
| const isActive = page => (page === selected_page ? "selected-page" : undefined) | ||
| const [showGraphDropdown, setShowGraphDropdown] = React.useState(false) | ||
| const [dropdownTimeouts, setDropdownTimeouts] = React.useState([]) | ||
|
|
||
| const clearDropdownTimeouts = () => { | ||
| dropdownTimeouts.forEach(timeout => clearTimeout(timeout)) | ||
| setDropdownTimeouts([]) | ||
| } | ||
|
|
||
| const handleShowGraphDropdown = () => { | ||
| clearDropdownTimeouts() | ||
| setShowGraphDropdown(true) | ||
| } | ||
|
|
||
| const handleHideGraphDropdown = () => { | ||
| const timeout = setTimeout(() => { | ||
| setShowGraphDropdown(false) | ||
| }, 500) | ||
| setDropdownTimeouts(dropdownTimeouts.concat(timeout)) | ||
| } | ||
|
|
||
| React.useEffect(() => { | ||
| const navGraph = document.querySelector("#nav-graph") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Part of your task is to remove DOM manipulation. This includes accessing DOM elements direction and manually adding callbacks ( |
||
|
|
||
| if (navGraph) { | ||
| navGraph.addEventListener("mouseenter", handleShowGraphDropdown) | ||
| navGraph.addEventListener("mouseleave", handleHideGraphDropdown) | ||
| } | ||
|
|
||
| return () => { | ||
| clearDropdownTimeouts() | ||
| if (navGraph) { | ||
| navGraph.removeEventListener("mouseenter", handleShowGraphDropdown) | ||
| navGraph.removeEventListener("mouseleave", handleHideGraphDropdown) | ||
| } | ||
| } | ||
| }, []) | ||
|
|
||
| return ( | ||
| <nav className="row header"> | ||
|
|
@@ -28,6 +65,13 @@ export function NavBar({ selected_page, open_modal }) { | |
| <ul id="nav-links"> | ||
| <li id="nav-graph" className={isActive("graph")}> | ||
| <a href="/graph">Graph</a> | ||
| <GraphDropdown | ||
| showGraphDropdown={showGraphDropdown} | ||
| onMouseMove={handleShowGraphDropdown} | ||
| onMouseLeave={handleHideGraphDropdown} | ||
| graphs={graphs} | ||
| updateGraph={updateGraph} | ||
| /> | ||
| </li> | ||
| <li id="nav-grid" className={isActive("grid")}> | ||
| <a href="/grid">Grid</a> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,13 +2,12 @@ import React from "react" | |
| import PropTypes from "prop-types" | ||
| import { CourseModal } from "../common/react_modal.js.jsx" | ||
| import { ExportModal } from "../common/export.js.jsx" | ||
| import { getProgram } from "../common/utils.js" | ||
| import { getPost } from "../common/utils.js" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is unrelated to this PR |
||
| import Bool from "./Bool" | ||
| import Edge from "./Edge" | ||
| import Node from "./Node" | ||
| import Button from "./Button" | ||
| import InfoBox from "./InfoBox" | ||
| import GraphDropdown from "./GraphDropdown" | ||
| import Sidebar from "./Sidebar" | ||
| import { parseAnd } from "../../util/util.js" | ||
| import { FontAwesomeIcon } from "@fortawesome/react-fontawesome" | ||
|
|
@@ -44,7 +43,6 @@ export class Graph extends React.Component { | |
| highlightedNodesFocus: [], | ||
| highlightedNodesDeps: [], | ||
| infoboxTimeouts: [], | ||
| dropdownTimeouts: [], | ||
| width: window.innerWidth, | ||
| height: window.innerHeight, | ||
| zoomFactor: 1, | ||
|
|
@@ -68,7 +66,6 @@ export class Graph extends React.Component { | |
| panStartX: 0, | ||
| panStartY: 0, | ||
| showCourseModal: false, | ||
| showGraphDropdown: false, | ||
| selectedNodes: new Set(), | ||
| } | ||
| this.exportModal = React.createRef() | ||
|
|
@@ -83,15 +80,6 @@ export class Graph extends React.Component { | |
| // can't detect keydown event when adding event listener to react-graph | ||
| document.body.addEventListener("keydown", this.onKeyDown) | ||
|
|
||
| if (document.querySelector("#nav-graph > a")) { | ||
| document | ||
| .querySelector("#nav-graph > a") | ||
| .addEventListener("mouseenter", this.setShowGraphDropdown) | ||
| document | ||
| .querySelector("#nav-graph > a") | ||
| .addEventListener("mouseleave", this.hideGraphDropdown) | ||
| } | ||
|
|
||
| if (document.querySelector(".sidebar")) { | ||
| document | ||
| .querySelector(".sidebar") | ||
|
|
@@ -107,23 +95,13 @@ export class Graph extends React.Component { | |
|
|
||
| componentWillUnmount() { | ||
| this.state.infoboxTimeouts.forEach(timeout => clearTimeout(timeout)) | ||
| this.state.dropdownTimeouts.forEach(timeout => clearTimeout(timeout)) | ||
| document.body.removeEventListener("keydown", this.onKeyDown) | ||
|
|
||
| if (document.getElementById("nav-export")) { | ||
| document | ||
| .getElementById("nav-export") | ||
| .removeEventListener("click", this.exportModal.current.openModal) | ||
| } | ||
|
|
||
| if (document.querySelector("#nav-graph > a")) { | ||
| document | ||
| .querySelector("#nav-graph > a") | ||
| .removeEventListener("mouseenter", this.setShowGraphDropdown) | ||
| document | ||
| .querySelector("#nav-graph > a") | ||
| .removeEventListener("mouseleave", this.hideGraphDropdown) | ||
| } | ||
| } | ||
|
|
||
| getGraph = () => { | ||
|
|
@@ -341,7 +319,7 @@ export class Graph extends React.Component { | |
| this.highlightFocuses([]) | ||
| } else if (this.props.currFocus !== prevProps.currFocus) { | ||
| const currFocusCourses = this.state.focusCourses[this.props.currFocus] | ||
| getProgram( | ||
| getPost( | ||
| this.props.currFocus, | ||
| currFocusCourses?.modifiedTime || new Date(0).toUTCString() | ||
| ).then(focusData => { | ||
|
|
@@ -367,10 +345,6 @@ export class Graph extends React.Component { | |
| this.state.infoboxTimeouts.forEach(timeout => clearTimeout(timeout)) | ||
| this.setState({ infoboxTimeouts: [] }) | ||
| break | ||
| case TIMEOUT_NAMES_ENUM.DROPDOWN: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's now possible that |
||
| this.state.dropdownTimeouts.forEach(timeout => clearTimeout(timeout)) | ||
| this.setState({ dropdownTimeouts: [] }) | ||
| break | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -693,20 +667,6 @@ export class Graph extends React.Component { | |
| }) | ||
| } | ||
|
|
||
| setShowGraphDropdown = () => { | ||
| this.clearAllTimeouts(TIMEOUT_NAMES_ENUM.DROPDOWN) | ||
| this.setState({ showGraphDropdown: true }) | ||
| } | ||
|
|
||
| hideGraphDropdown = () => { | ||
| const timeout = setTimeout(() => { | ||
| this.setState({ showGraphDropdown: false }) | ||
| }, 500) | ||
| this.setState({ | ||
| dropdownTimeouts: this.state.dropdownTimeouts.concat(timeout), | ||
| }) | ||
| } | ||
|
|
||
| onClose = () => { | ||
| this.setState({ showCourseModal: false }) | ||
| } | ||
|
|
@@ -1645,13 +1605,6 @@ export class Graph extends React.Component { | |
| onClose={this.onClose} | ||
| /> | ||
| <ExportModal context="graph" session="" ref={this.exportModal} /> | ||
| <GraphDropdown | ||
| showGraphDropdown={this.state.showGraphDropdown} | ||
| onMouseMove={this.setShowGraphDropdown} | ||
| onMouseLeave={this.hideGraphDropdown} | ||
| graphs={this.props.graphs} | ||
| updateGraph={this.props.updateGraph} | ||
| /> | ||
| {Object.keys(this.state.nodesJSON).length > 1 && ( | ||
| <div className="graph-button-group"> | ||
| <div className="button-group"> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,7 +24,8 @@ export default function GraphDropdown({showGraphDropdown, onMouseMove, onMouseLe | |
| onMouseMove={onMouseMove} | ||
| onMouseLeave={onMouseLeave} | ||
| data-testid={"test-graph-dropdown"} | ||
| style={{ left: graphTabLeft }} | ||
| style={{ left: graphTabLeft, top: "50px", zIndex: 1000 | ||
| }} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Put the Also, in this file there is still direct DOM manipulation, which you should remove by handling the logic in the parent |
||
| > | ||
| {graphs.map((graph, i) => { | ||
| return ( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The indentation at this line is incorrect