PathButtons.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 => {
  22. let path = paths[this.state.pathKey]
  23. console.log(paths)
  24. if ("switchLatLng" in this.props){
  25. path = path.map(pos => [pos[1], pos[0]])
  26. }
  27. this.props.updateState("path", path)
  28. })
  29. }
  30. choosePath = (event) => {
  31. this.setState({pathKey: event.target.value})
  32. this.getPath()
  33. }
  34. savePath = () => {
  35. this.savePathToDb().then(res => {
  36. let pathKeysCopy = this.state.pathKeys.slice();
  37. pathKeysCopy.push(this.state.savePathKey);
  38. this.setState({pathKeys: pathKeysCopy});
  39. this.setState({savePathKey: ""});
  40. })
  41. }
  42. setSavePathKey = (event) => {
  43. this.setState({savePathKey: event.target.value})
  44. }
  45. render() {
  46. return (
  47. <div>
  48. <Input type="text" onChange={this.setSavePathKey} value={this.state.savePathKey}/>
  49. <Button onClick={this.savePath}>save path</Button>
  50. <Input type="select" name="select" id="savePath" onChange={this.choosePath}>
  51. {this.state.pathKeys.map((name, idx) =>
  52. <option value={name} key={idx}>{name}</option>
  53. )}
  54. </Input>
  55. </div>
  56. )
  57. }
  58. }