ImageGalleryPage.tsx 4.62 KB
Newer Older
Matija Obreza's avatar
Matija Obreza committed
1
import * as React from 'react';
Viacheslav Pavlov's avatar
i18n    
Viacheslav Pavlov committed
2
import { translate } from 'react-i18next';
Matija Obreza's avatar
Matija Obreza committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { withStyles } from '@material-ui/core/styles';
import { normalize } from 'path';

import { getGallery, removeGallery, uploadFile } from 'repository/actions/admin';

import ImageGallery from 'model/repository/ImageGallery';
// import RepositoryFile from 'model/repository/RepositoryFile';

import Loading from 'ui/common/Loading';
import { PageContents } from 'ui/layout/PageLayout';
import ContentHeaderWithButton from 'ui/common/heading/ContentHeaderWithButton';
import Permissions from 'ui/common/permission/Permissions';

import Button from '@material-ui/core/Button';
import { navigateTo } from 'actions/navigation';
import confirmAlert from 'utilities/confirmAlert';
import ImageGalleryView from 'repository/ui/c/ImageGalleryView';
Viacheslav Pavlov's avatar
i18n    
Viacheslav Pavlov committed
22
import FolderCrumbs from 'repository/ui/c/FolderCrumbs';
Matija Obreza's avatar
Matija Obreza committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

// import Grid from '@material-ui/core/Grid';

interface IRepositoryBrowserProps extends React.ClassAttributes<any> {
  classes: any;
  t?: any;

  root: string;
  path: string;
  folderPath: string;
  gallery: ImageGallery;
  getGallery: (path: string) => any;
  removeGallery: (path: string) => any;
  uploadFile: (path: string, file: File) => any;
  navigateTo: (path: string, qs?: any) => any;
}

const styles = (theme) => ({

});

class ImageGalleryPage extends React.Component<IRepositoryBrowserProps, any> {

  protected static needs = [
    ({ root, path }) => getGallery(normalize(root + path).replace(/^(.+)\/$/, '$1')),
  ];

  public componentWillMount() {
51
52
    const { folderPath, gallery, getGallery } = this.props;
    // console.log(`RepositoryBrowser.componentWillMount...`, folderPath);
Matija Obreza's avatar
Matija Obreza committed
53
54

    if (!gallery || (gallery.folder !== folderPath)) {
55
      // console.log(`Loading gallery root=${root} path=${path}`);
Matija Obreza's avatar
Matija Obreza committed
56
57
58
59
60
      getGallery(folderPath);
    }
  }

  public componentWillReceiveProps(nextProps) {
61
    const { gallery, folderPath } = nextProps;
Matija Obreza's avatar
Matija Obreza committed
62
63
    const { getGallery } = this.props;

64
    // console.log(`Props root=${root} path=${path} folderPath=${folderPath}`);
Matija Obreza's avatar
Matija Obreza committed
65
66

    if (!gallery || (gallery.folder !== folderPath)) {
67
      // console.log(`Loading gallery root=${root} path=${path}`);
Matija Obreza's avatar
Matija Obreza committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
      getGallery(folderPath);
    }
  }

  protected upload = (e) => {
    const { uploadFile, folderPath, getGallery } = this.props;

    const file = e.target.files[0];
    uploadFile(folderPath, file).then(() => getGallery(folderPath));
    e.target.files = null;
    e.target.value = null;
  }

  protected deleteGallery = (e) => {
Viacheslav Pavlov's avatar
i18n    
Viacheslav Pavlov committed
82
    const { removeGallery, navigateTo, folderPath, t } = this.props;
Matija Obreza's avatar
Matija Obreza committed
83
84
85

    removeGallery(folderPath).then((result) => {
      if (result) {
86
        navigateTo(`/admin/repository/f${folderPath}/`);
Matija Obreza's avatar
Matija Obreza committed
87
      } else {
Viacheslav Pavlov's avatar
i18n    
Viacheslav Pavlov committed
88
        confirmAlert(<p>{ t('repository.admin.p.imageGallery.deleteGalleryAlert', {folderPath}) }</p>);
Matija Obreza's avatar
Matija Obreza committed
89
90
91
92
93
94
      }
    });
  }

  protected goToFolder = (e) => {
    const { navigateTo, folderPath } = this.props;
95
    navigateTo(`/admin/repository/f${folderPath}/`);
Matija Obreza's avatar
Matija Obreza committed
96
97
98
99
  }

  public render() {

Viacheslav Pavlov's avatar
i18n    
Viacheslav Pavlov committed
100
    const { gallery, root, path, t } = this.props;
Matija Obreza's avatar
Matija Obreza committed
101
102
103

    const stillLoading: boolean = !gallery;

104
    // console.log(gallery);
Matija Obreza's avatar
Matija Obreza committed
105
106
107
108
109
110

    return (
      stillLoading ? <Loading /> : (
        <div>
          <ContentHeaderWithButton title={ <FolderCrumbs disabled root={ root } path={ path } /> } buttons={
            <span>
111
              <Button key="viewf" variant="contained" onClick={ this.goToFolder }>{ t('repository.admin.p.imageGallery.toFolder') }</Button>
Viacheslav Pavlov's avatar
i18n    
Viacheslav Pavlov committed
112
              { gallery && gallery._permissions.delete && <Button onClick={ this.deleteGallery } key="deletef">{ t('repository.admin.p.imageGallery.deleteGallery') }</Button> }
Matija Obreza's avatar
Matija Obreza committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
              { gallery && gallery._permissions.manage && <Permissions clazz={ ImageGallery.clazz } id={ gallery.id } /> }
            </span>
           } />
          <PageContents>
            <ImageGalleryView imageGallery={ gallery } />
            <div>
              <input type="file" onChange={ this.upload } />
            </div>
          </PageContents>
        </div>
      )
    );
  }
}

const mapStateToProps = (state, ownProps) => ({
  root: ownProps.route.root || '/',
  path: ownProps.match.params.path || '/',
  folderPath: normalize((ownProps.route.root || '/') + (ownProps.match.params.path || '/')).replace(/^(.+)\/$/, '$1'),
  gallery: state.repository.admin.gallery,
  loading: state.repository.admin.loading,
});

const mapDispatchToProps = (dispatch) => bindActionCreators({
  getGallery,
  uploadFile,
  removeGallery,
  navigateTo,
}, dispatch);

Viacheslav Pavlov's avatar
i18n    
Viacheslav Pavlov committed
143
export default translate()(connect(mapStateToProps, mapDispatchToProps)(withStyles(styles)(ImageGalleryPage)));