ImageList.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import PropTypes from 'prop-types';
  2. import React from 'react';
  3. import Gallery from 'react-grid-gallery';
  4. import LinearProgress from '@material-ui/core/LinearProgress';
  5. import ShareIcon from '@material-ui/icons/Share';
  6. import Button from '@material-ui/core/Button';
  7. import { makeStyles } from '@material-ui/core/styles';
  8. import Modal from '@material-ui/core/Modal';
  9. class imageList extends React.Component {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. images: this.props.images,
  14. currentImage: 0,
  15. error: null,
  16. isLoaded: false,
  17. items: [],
  18. };
  19. this.onCurrentImageChange = this.onCurrentImageChange.bind(this);
  20. this.deleteImage = this.deleteImage.bind(this);
  21. }
  22. onCurrentImageChange(index) {
  23. this.setState({ currentImage: index });
  24. }
  25. deleteImage() {
  26. if (window.confirm(`Are you sure you want to delete image number ${this.state.currentImage}?`)) {
  27. //"/system/file_system/share/new?path="
  28. console.log(this.state.images[this.state.currentImage].src);
  29. }
  30. }
  31. componentDidMount() {
  32. fetch("/backend_test/listFile", {})
  33. /*
  34. fetch("/system/ajgi/interface?script=/Photo/backend/listFile.js", {
  35. method: 'post',
  36. headers: {
  37. 'Accept': 'application/json',
  38. 'Content-Type': 'application/json'
  39. },
  40. body: JSON.stringify({ folder: this.props.path })
  41. })
  42. */
  43. .then(res => res.json())
  44. .then(
  45. (result) => {
  46. this.setState({
  47. isLoaded: true,
  48. images: result
  49. });
  50. },
  51. // Note: it's important to handle errors here
  52. // instead of a catch() block so that we don't swallow
  53. // exceptions from actual bugs in components.
  54. (error) => {
  55. this.setState({
  56. isLoaded: true,
  57. error: error
  58. });
  59. }
  60. )
  61. }
  62. setPath() {
  63. this.setState({ path: "user:/Photo/Photo/uploads/" });
  64. }
  65. render() {
  66. const { isLoaded, items, error } = this.state;
  67. const theme = {
  68. margin: 4,
  69. }
  70. if (error) {
  71. return <div>Error: {error.message} (imageList.js)</div>;
  72. }else if(!isLoaded) {
  73. return <div><LinearProgress /></div>;
  74. } else {
  75. return ( <Gallery images = { this.state.images }
  76. enableLightbox = { true }
  77. enableImageSelection={false}
  78. currentImageWillChange={this.onCurrentImageChange}
  79. customControls={[
  80. <Button
  81. variant="contained"
  82. color="primary"
  83. size="large"
  84. style={theme}
  85. startIcon={<ShareIcon />}
  86. onClick={this.deleteImage}
  87. >Share</Button>
  88. ]}
  89. />
  90. );
  91. }
  92. }
  93. }
  94. imageList.propTypes = {
  95. images: PropTypes.arrayOf(
  96. PropTypes.shape({
  97. src: PropTypes.string.isRequired,
  98. thumbnail: PropTypes.string.isRequired,
  99. srcset: PropTypes.array,
  100. caption: PropTypes.oneOfType([
  101. PropTypes.string,
  102. PropTypes.element
  103. ]),
  104. thumbnailWidth: PropTypes.number.isRequired,
  105. thumbnailHeight: PropTypes.number.isRequired
  106. })
  107. ).isRequired
  108. };
  109. imageList.defaultProps = {
  110. images: []
  111. };
  112. export default imageList;