DisplayPage.tsx 2.76 KB
Newer Older
Matija Obreza's avatar
Subsets  
Matija Obreza committed
1
2
3
4
import * as React from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
// Actions
5
6
import {loadSubset} from 'subsets/actions/public';
import {unpublishSubset} from 'subsets/actions/editor';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
7
import navigateTo from 'actions/navigation';
Matija Obreza's avatar
Subsets  
Matija Obreza committed
8
// Models
9
import Subset from 'model/subset/Subset';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
10
import {PublishState} from 'model/common.model';
Matija Obreza's avatar
Subsets  
Matija Obreza committed
11
12
13
// UI
import PageLayout, { PageContents } from 'ui/layout/PageLayout';
import Loading from 'ui/common/Loading';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
14
15
16
import ContentHeaderWithButton from 'ui/common/heading/ContentHeaderWithButton';
import ActionButton from 'ui/common/buttons/ActionButton';
import SubsetDisplay from './c/SubsetDisplay';
Matija Obreza's avatar
Subsets  
Matija Obreza committed
17
18
19
20

interface IBrowsePageProps extends React.ClassAttributes<any> {
  uuid: string;
  subset: Subset;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
21
  userRole: string[];
Matija Obreza's avatar
Subsets  
Matija Obreza committed
22
23
  error: any;
  loadSubset: any;
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
24
25
  navigateTo: any;
  unpublishSubset: (subset: Subset) => void;
Matija Obreza's avatar
Subsets  
Matija Obreza committed
26
27
28
29
30
31
}

class BrowsePage extends React.Component<IBrowsePageProps, any> {

  protected static needs = [
    ({ params: { uuid } }) => {
Matija Obreza's avatar
Matija Obreza committed
32
      return uuid ? loadSubset(uuid) : null;
Matija Obreza's avatar
Subsets  
Matija Obreza committed
33
34
35
    },
  ];

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
36
37
38
39
40
41
42
43
  private onUnpublish = () => {
    const {unpublishSubset, subset, navigateTo} = this.props;
    if (subset) {
      unpublishSubset(subset);
      navigateTo('/dashboard/subsets');
    }
  }

Matija Obreza's avatar
Subsets  
Matija Obreza committed
44
45
46
47
48
49
50
51
52
53
54
55
  constructor(props: IBrowsePageProps, context: any) {
      super(props, context);
  }

  public componentWillMount() {
    const { subset, uuid, loadSubset } = this.props;
    if (uuid && (! subset || uuid !== subset.uuid)) {
      loadSubset(uuid);
    }
  }

  public render() {
56
    const { error, subset, uuid, userRole } = this.props;
Matija Obreza's avatar
Subsets  
Matija Obreza committed
57
    const stillLoading: boolean = ! error && (! subset || (uuid && subset && subset.uuid !== uuid));
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
58
    const isActionsActive: boolean = userRole.findIndex((role) => role === 'ROLE_ADMINISTRATOR') !== -1 || (subset && subset.state === PublishState.REVIEWING);
Matija Obreza's avatar
Subsets  
Matija Obreza committed
59
60
61
62

    return (
      <PageLayout>

Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
63
        <ContentHeaderWithButton title={ `Subset ${subset && subset.title}` } buttons={ isActionsActive ? <ActionButton action={ this.onUnpublish } title="Reject"/> : '' } />
Matija Obreza's avatar
Subsets  
Matija Obreza committed
64
65

        <PageContents>
66
67
68
          { stillLoading ? <Loading /> :
            <div>
              { error && <div>{ JSON.stringify(error) }</div> }
69
              { subset && <SubsetDisplay isActionsActive={ isActionsActive } subset={ subset }/> }
70
71
            </div>
          }
Matija Obreza's avatar
Subsets  
Matija Obreza committed
72
73
74
75
76
77
78
        </PageContents>
      </PageLayout>
    );
  }
}

const mapStateToProps = (state, ownProps) => ({
79
  subset: state.subsets.public.subset,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
80
  userRole: state.login.authorities,
81
  error: state.subsets.public.subsetError,
Matija Obreza's avatar
Subsets  
Matija Obreza committed
82
83
84
85
86
  uuid: ownProps.match.params.uuid,
});

const mapDispatchToProps = (dispatch) => bindActionCreators({
  loadSubset,
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
87
88
  navigateTo,
  unpublishSubset,
Matija Obreza's avatar
Subsets  
Matija Obreza committed
89
90
91
92
}, dispatch);


export default connect(mapStateToProps, mapDispatchToProps)(BrowsePage);