DisplayPage.tsx 6.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
import * as React from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { withStyles } from '@material-ui/core/styles';
import { translate } from 'react-i18next';

import { loadVocabulary, listVocabularyTerms } from 'vocabulary/actions/public';
import { deleteVocabulary } from 'vocabulary/actions/admin';
import Page from 'model/Page';

import confirm from 'utilities/confirmAlert';
import Loading from 'ui/common/Loading';
import Section from 'ui/common/layout/Section';
Matija Obreza's avatar
Matija Obreza committed
14
import Markdown from 'ui/common/markdown';
15 16 17 18 19 20 21 22 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
import ContentHeaderWithButton from 'ui/common/heading/ContentHeaderWithButton';
import { Table, TableRow, TableCell } from 'ui/common/tables';
import { VocabularyLink } from 'ui/catalog/Links';
import PaginationComponent from 'ui/common/pagination';
import BackButton from 'ui/common/buttons/BackButton';

import Grid from '@material-ui/core/Grid';
import Card, { CardHeader, CardContent, CardActions } from 'ui/common/Card';
import Button from '@material-ui/core/Button';
import PageLayout from 'ui/layout/PageLayout';
import VocabularyTerm from 'model/vocabulary/VocabularyTerm';
import Vocabulary from 'model/vocabulary/Vocabulary';
import TableInfiniteLoader from 'ui/common/TableInfiniteLoader';

interface IDisplayPageProps extends React.ClassAttributes<any> {
  classes: any;
  uuid?: string;
  t: any;

  vocabularyTerms: Page<VocabularyTerm>;
  loadVocabulary: (uuid: string) => void;
  listVocabularyTerms: any;
  deleteVocabulary: (vocabulary: Vocabulary) => Promise<Vocabulary>;
  vocabulary?: Vocabulary;
}

const styles = (theme) => ({
  pageLayout: {
    paddingTop: 'initial',
  },
  contentContainer: {
    backgroundColor: '#E8E5E0',
    padding: '1rem',
  },
  form: {
    padding: '20px',
    margin: '0 0 1rem 0',
  },
});

class DisplayPage extends React.Component<IDisplayPageProps, any> {

  protected static needs = [
    ({ params: { uuid } }) => loadVocabulary(uuid),
    ({ params: { uuid } }) => listVocabularyTerms(uuid, 0, 50),
  ];
  protected onPaginationChange = (page, results, sortBy) => {
    const { vocabulary, listVocabularyTerms } = this.props;

    listVocabularyTerms(vocabulary.uuid, page, results);
  }
  private onDelete = (e) => {
    const { vocabulary, deleteVocabulary, t } = this.props;

    confirm(<span>Delete <b>{ vocabulary.title }</b>?</span>, {
      description: t('vocabulary.admin.p.display.deleteDescription'),
      confirmLabel: t('common:action.delete'),
      abortLabel: t('common:action.cancel'),
    }).then(() => {
      deleteVocabulary(vocabulary);
    }).catch(() => {
      // don't delete
    });
  }

  public componentWillMount() {
    const { vocabulary, loadVocabulary, uuid, listVocabularyTerms } = this.props;
    if (uuid && (!vocabulary || vocabulary.uuid !== uuid)) {
      loadVocabulary(uuid);
      listVocabularyTerms(uuid, 0, 50);
    }
  }

  public render() {
    const { classes, uuid, vocabulary, listVocabularyTerms, vocabularyTerms, t } = this.props;

    const stillLoading: boolean = (!vocabulary || (uuid && vocabulary && vocabulary.uuid !== uuid));

    const renderVocabularyTerm = (term: VocabularyTerm) => (
      <TableRow key={ term.code }>
        <TableCell className="font-bold">{ term.code }</TableCell>
        <TableCell>{ term.title }</TableCell>
        <TableCell>{ term.description && <Markdown source={ term.description }/> }</TableCell>
      </TableRow>
    );

    return (
      <PageLayout classes={ {root: classes.pageLayout} }>
        <ContentHeaderWithButton title={ t('vocabulary.admin.p.display.title') }
                                 buttons={ <BackButton defaultTarget="/vocabulary"/> }/>
        { stillLoading ? <Loading/> :
          <Grid container className={ classes.contentContainer }>
            <Grid item xs={ 12 }>
              <Card className={ classes.card } square>
                <CardHeader className={ classes.cardHeader } title={ (
                  <span>{ vocabulary.title }
                    <small>{ vocabulary.versionTag }</small></span>
                ) }/>
                <CardContent className={ classes.cardContent }>
                  { vocabulary.description && <Markdown source={ vocabulary.description }/> }

                  <div>Record version: <b>{ vocabulary.version }</b> UUID: <b>{ vocabulary.uuid }</b>
                  </div>
                </CardContent>

                { (vocabulary._permissions.write || vocabulary._permissions.delete) && (
                  <CardActions>
                    { vocabulary._permissions.write &&
                    <VocabularyLink edit to={ vocabulary }>
                      <Button>{ t('common:action.edit') }</Button>
                    </VocabularyLink> }
                    { vocabulary._permissions.delete &&
                    <Button onClick={ this.onDelete } type="button">
                      { t('common:action.delete') }
                    </Button>
                    }
                  </CardActions>
                ) }
              </Card>
            </Grid>

            { vocabularyTerms && vocabularyTerms.content &&
            <Grid item xs={ 12 }>
              <Section title={ t('vocabulary.admin.p.display.acceptedTerms') }>
                { vocabularyTerms.totalElements > 50 &&
                <PaginationComponent pageObj={ vocabularyTerms }
                                     onChange={ this.onPaginationChange }
                                     infinite
                                     displayName="vocabulary.admin.p.display.terms"
                />
                }
                <Table widths={ ['10%', '30%', null] } headers={ (
                  <TableRow>
                    <TableCell>{ t('vocabulary.common.code') }</TableCell>
                    <TableCell>{ t('vocabulary.admin.p.display.term') }</TableCell>
                    <TableCell>{ t('common:label.description') }</TableCell>
                  </TableRow>
                ) }>
                  <TableInfiniteLoader
                    paged={ vocabularyTerms }
                    loadingIndicator={ <Loading/> }
                    loadMore={ listVocabularyTerms }
                    colSpan={ 3 }
                    itemRenderer={ renderVocabularyTerm }/>
                </Table>
              </Section>
            </Grid>
            }

          </Grid>
        }
      </PageLayout>
    );
  }
}

const mapStateToProps = (state, ownProps) => ({
  uuid: ownProps.match.params.uuid,
  vocabulary: state.vocabulary.public.currentVocabulary,
  vocabularyTerms: state.vocabulary.public.terms,
});

const mapDispatchToProps = (dispatch) => bindActionCreators({
  loadVocabulary,
  listVocabularyTerms,
  deleteVocabulary,
}, dispatch);

const styled = withStyles(styles)(DisplayPage);

export default translate()(connect(mapStateToProps, mapDispatchToProps)(styled));