DisplayPage.tsx 6.11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
import * as React from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { translate } from 'react-i18next';

import { deletePartner } from 'partners/actions/editor';
import { loadPartner, editPartner } from 'partners/actions/dashboard';

import Partner from 'model/genesys/Partner';

import confirm from 'utilities/confirmAlert';
import Loading from 'ui/common/Loading';
Matija Obreza's avatar
Matija Obreza committed
13
import Markdown from 'ui/common/markdown';
14 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
import { ExternalLink, FaoWiewsLink } from 'ui/catalog/Links';
import { Properties, PropertiesItem } from 'ui/common/Properties';

import Grid from '@material-ui/core/Grid';
import Card, { CardHeader, CardContent, CardActions } from 'ui/common/Card';
import Button from '@material-ui/core/Button';
import Permissions from 'ui/common/permission/Permissions';
import PageLayout from 'ui/layout/PageLayout';

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

  uuid?: string;
  partner?: Partner;
  loading?: any;
  loadPartner: any;
  editPartner: any;
  deletePartner: any;
}

// Page to edit a descriptor list
class PartnerPage extends React.Component<IPartnerPageProps, any> {

  protected static needs = [
    ({ params: { uuid } }) => loadPartner(uuid),
  ];
  private onEditPartner = (e) => {
    const { partner, editPartner } = this.props;
    editPartner(partner.uuid);
  }
  private onDelete = (e) => {
    const { t, partner, deletePartner } = this.props;

    confirm(<span>Delete <b>{ partner.name }</b>?</span>, {
      description: t('common:message.confirmDelete', { what: t('partners.common.modelName') }),
      confirmLabel: t('common:action.delete'),
      abortLabel: t('common:action.cancel'),
    }).then(() => {
      deletePartner(partner);
    }).catch(() => {
      // don't delete
    });
  }

  public componentWillMount() {
    const { uuid, partner, loadPartner } = this.props;

    if (uuid && (!partner || partner.uuid !== uuid)) {
      console.log(`cwm: Loading partner ${uuid}`, partner);
      loadPartner(uuid);
    }
  }

  public componentWillReceiveProps(nextProps) {
    const { uuid, partner, loading, loadPartner } = nextProps;

    if (uuid && (!partner || partner.uuid !== uuid)) {
      if (!loading || loading.uuid !== uuid) {
        console.log(`cwrp: Loading partner ${uuid}`, partner, loading);
        loadPartner(uuid);
      }
    }
  }

  public render() {
    const { t, uuid, partner } = this.props;

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

    return (
      <PageLayout>
        { stillLoading ? <Loading/> :
          <Grid container spacing={ 0 } className="back-gray p-20">
            <Grid item xs={ 12 }>
              <Card square>
                <CardHeader title={ (
                  <span>
                        { partner.name }
                    { partner.shortName && <small> #{ partner.shortName } </small> }
                      </span>
                ) }/>
                <CardContent>
                  { partner.description && <Markdown className="mb-20" source={ partner.description }/> }

                  <Properties>
                    { partner.urls.length > 0 && (
                      <PropertiesItem title={ t('partners.public.p.display.urls') }>
                        { partner.urls.map((url) => (
                          <div key={ url }><ExternalLink link={ url }>{ url }</ExternalLink></div>
                        )) }
                      </PropertiesItem>
                    ) }
                    { partner.wiewsCodes && partner.wiewsCodes.length > 0 && (
                      <PropertiesItem title={ t('partners.common.wiewsCodes') }>
                        { partner.wiewsCodes.map((wiewsCode) => (
                          <div key={ wiewsCode }><FaoWiewsLink wiewsCode={ wiewsCode }>{ wiewsCode }</FaoWiewsLink></div>
                        )) }
                      </PropertiesItem>
                    ) }
                    { partner.countryCodes.length > 0 && (
                      <PropertiesItem title={ t('partners.common.countries') }>
                        { partner.countryCodes.map((countryCode) => (
                          <div key={ countryCode }>{ countryCode }</div>
                        )) }
                      </PropertiesItem>
                    ) }
                    { partner.email && (
                      <PropertiesItem title={ t('user.common.email') }>
                        <div><a href={ `mailto:${partner.email}` }>{ partner.email }</a></div>
                      </PropertiesItem>
                    ) }
                    { partner.phone && (
                      <PropertiesItem title={ t('user.common.phone') }>{ partner.phone }</PropertiesItem>
                    ) }
                    { partner.address && (
                      <PropertiesItem title={ t('geo.common.address') }>{ partner.address }</PropertiesItem>
                    ) }
                    <PropertiesItem title={ t('partners.public.p.display.metadata') }><code>{ partner.uuid }, v{ partner.version }</code></PropertiesItem>
                  </Properties>
                </CardContent>

                { (partner._permissions.write || partner._permissions.delete) && (
                  <CardActions>
                    { partner._permissions.write && <Button onClick={ this.onEditPartner } type="button">{ t('common:action.edit') }</Button> }
                    { partner._permissions.delete && <Button onClick={ this.onDelete } type="button">{ t('common:action.delete') }</Button> }
                    { partner._permissions.manage && <Permissions clazz={ Partner.clazz } id={ partner.id }/> }
                  </CardActions>
                ) }
              </Card>
            </Grid>
          </Grid>
        }
      </PageLayout>
    );
  }
}

const mapStateToProps = (state, ownProps) => ({
  uuid: ownProps.match.params.uuid,
  partner: state.partner.dashboard.currentPartner,
  loading: state.partner.dashboard.loading,
});

const mapDispatchToProps = (dispatch) => bindActionCreators({
  loadPartner,
  editPartner,
  deletePartner,
}, dispatch);

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