PathButtons.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import React from "react";
  2. import {Button, Input} from "reactstrap";
  3. import {fetchPathKeys, fetchPaths, savePathToDb} from "./helpers/dbHelpers.js";
  4. export default class PathButtons extends React.Component{
  5. constructor(props){
  6. super(props);
  7. this.state = {
  8. pathKey: "",
  9. pathKeys: [],
  10. savePathKey: "-"
  11. }
  12. this.savePathToDb = savePathToDb.bind(this)
  13. }
  14. componentDidMount(){
  15. this.getPathKeys()
  16. }
  17. getPathKeys = () => {
  18. fetchPathKeys().then(keys => this.setState({pathKeys: keys}))
  19. }
  20. getPath = () => {
  21. fetchPaths().then(paths => this.props.updateState("path", paths[this.state.pathKey]))
  22. }
  23. choosePath = (event) => {
  24. this.setState({pathKey: event.target.value})
  25. this.getPath()
  26. }
  27. savePath = () => {
  28. this.savePathToDb().then(res => {
  29. let pathKeysCopy = this.state.pathKeys.slice();
  30. pathKeysCopy.push(this.state.savePathKey);
  31. this.setState({pathKeys: pathKeysCopy});
  32. this.setState({savePathKey: ""});
  33. })
  34. }
  35. setSavePathKey = (event) => {
  36. this.setState({savePathKey: event.target.value})
  37. }
  38. render() {
  39. return (
  40. <div>
  41. <Input type="text" onChange={this.setSavePathKey} value={this.state.savePathKey}/>
  42. <Button onClick={this.savePath}>save path</Button>
  43. <Input type="select" name="select" id="savePath" onChange={this.choosePath}>
  44. {this.state.pathKeys.map((name, idx) =>
  45. <option value={name} key={idx}>{name}</option>
  46. )}
  47. </Input>
  48. </div>
  49. )
  50. }
  51. }