import React, { Component } from 'react' import PropTypes from 'prop-types'; import FolderIcon from '@material-ui/icons/Folder' import RightIcon from "@material-ui/icons/KeyboardArrowRight" import UpIcon from "@material-ui/icons/ArrowUpward" import { connect } from 'react-redux' import classNames from 'classnames'; import { toggleSnackbar, } from "../../actions/index" import axios from 'axios' import { MenuList, MenuItem, IconButton, ListItemIcon, ListItemText, withStyles, ListItemSecondaryAction, } from '@material-ui/core'; import API from '../../middleware/Api' import { Api } from 'mdi-material-ui'; const mapStateToProps = state => { return { } } const mapDispatchToProps = dispatch => { return { toggleSnackbar:(vertical,horizontal,msg,color)=>{ dispatch(toggleSnackbar(vertical,horizontal,msg,color)) }, } } const styles = theme => ({ iconWhite:{ color: theme.palette.common.white, }, selected: { backgroundColor: theme.palette.primary.main+"!important", '& $primary, & $icon': { color: theme.palette.common.white, }, }, primary: {}, icon: {}, buttonIcon:{}, selector:{ minWidth: "300px", }, container:{ maxHeight: "330px", overflowY:" auto", } }) class PathSelectorCompoment extends Component { state = { presentPath:"/", dirList:[], selectedTarget:null, } componentDidMount= ()=>{ let toBeLoad = this.props.presentPath; this.enterFolder(toBeLoad); } back = ()=>{ let paths = this.state.presentPath.split("/"); paths.pop(); let toBeLoad = paths.join("/"); this.enterFolder(toBeLoad===""?"/":toBeLoad); } enterFolder = (toBeLoad)=>{ API.get('/directory'+toBeLoad,) .then( (response)=> { var dirList = response.data.filter( (x)=> { return (x.type === "dir" && (this.props.selected.findIndex((value)=>{ return (value.name === x.name )&&(value.path === x.path); }))===-1); }); if(toBeLoad ==="/"){ dirList.unshift({name:"/",path:""}) } this.setState({ presentPath:toBeLoad, dirList:dirList, selectedTarget:null, }) }) .catch((error) =>{ this.props.toggleSnackbar("top","right",error.message,"warning"); }); } handleSelect = (index) =>{ this.setState({selectedTarget:index}); this.props.onSelect(this.state.dirList[index]); } render() { const { classes} = this.props; return (
{this.state.presentPath!=="/"&& } {this.state.dirList.map((value,index)=>( this.handleSelect(index)}> {value.name!=="/"&& this.enterFolder(value.path === "/"?value.path+value.name:value.path+"/"+value.name)}> } ))}
); } } PathSelectorCompoment.propTypes = { classes: PropTypes.object.isRequired, presentPath:PropTypes.string.isRequired, selected:PropTypes.array.isRequired, }; export default connect( mapStateToProps, mapDispatchToProps )(withStyles(styles)(PathSelectorCompoment))