DescriptorPicker.tsx 8.08 KB
Newer Older
1 2 3 4 5 6 7
import * as React from 'react';
import Grid from '@material-ui/core/Grid';
import { withStyles } from '@material-ui/core/styles';
import * as classnames from 'classnames';
import Card, { CardHeader } from 'ui/common/Card';
import { translate } from 'react-i18next';

8
import { IPageRequest, SortDirection } from 'model/Page';
9 10 11
import Pagination from 'model/Pagination';
import DescriptorFilter from 'model/catalog/DescriptorFilter';
import Descriptor from 'model/catalog/Descriptor';
12
import FilteredPage from 'model/FilteredPage';
13 14 15 16 17 18

import Button from '@material-ui/core/Button';
import DescriptorCard from 'descriptors/ui/c/DescriptorCard';
import PaginationComponent from 'ui/common/pagination';
import DescriptorFilters from 'descriptors/ui/c/Filters';
import { log } from 'utilities/debug';
19
import PagedLoader from 'ui/common/PagedLoader';
20 21 22 23 24

interface IDescriptorPickerProps extends React.ClassAttributes<any> {
  classes: any;
  history: any;
  location: any;
25 26 27 28
  loadDescriptors: (page?: IPageRequest, filter?: DescriptorFilter | string) => void;
  loadMoreDescriptors: (paged?: FilteredPage<Descriptor>, filter?: string | DescriptorFilter) => void;
  clearDescriptors: () => void;
  matchingDescriptors: FilteredPage<Descriptor>; // results from loadDescriptors
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
  onAddDescriptor: (descriptor: Descriptor) => void; // event handler
  onRemoveDescriptor: (descriptor: Descriptor) => void; // event handler
  onAddAllDescriptors: any; // event handler
  onRemoveAllDescriptors: any; // event handler
  currentDescriptors: Descriptor[]; // currently selected descriptors
  pagination?: Pagination<DescriptorFilter>;
  loadCrops: () => any;
  t: any;
}

const styles = (theme) => ({
  leftPanel: theme.leftPanel.root,
  titleCardDisabled: {
    fontWeight: 'normal' as 'normal',
    fontStyle: 'italic' as 'italic',
    color: '#d4d4d2 ',
  },
  greenBackground: {
    backgroundColor: '#DCE8C8',
  },
  grayBackground: {
    position: 'relative' as 'relative',
    backgroundColor: '#ECEBE7',
  },
  emptyCardHeader: {
    padding: '16px !important',
  },
  paginationComponent: {
    position: 'absolute' as 'absolute',
    bottom: '0',
  },
  fake: {
    height: '30px',
  },
  btnBlue: theme.buttons.blue,
  btnDefault: theme.buttons.default,
  flexGrow: {
    flex: '1 1 auto',
  },
});

class DescriptorPicker extends React.Component<IDescriptorPickerProps, any> {

72 73
  protected onPaginationChange = (sortBy, dir, page, results) => {
    const { history, location, loadDescriptors } = this.props;
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    const params = new URLSearchParams(location.search);
    params.set('p', page);
    params.set('l', results);
    if (sortBy) {
      params.set('s', sortBy);
    } else {
      params.delete('s');
    }
    if (dir) {
      params.set('d', dir);
    } else {
      params.delete('d');
    }

    location.search = params.toString();
    history.push(location);
90
    loadDescriptors({ page: 0, size: 20, properties: sortBy, direction: dir as SortDirection }, null);
91
  }
92

93 94 95 96
  protected applyFilters = (newFilters) => {
    const { loadDescriptors, pagination } = this.props;

    log('Applying new filter', newFilters);
97
    loadDescriptors({ page: pagination.page, size: pagination.size, properties: pagination.sort as string[], direction: pagination.dir as SortDirection }, newFilters);
98
  }
99

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
  private onDescriptorSelected = (select) => (descriptor: Descriptor) => {
    const { onAddDescriptor, onRemoveDescriptor } = this.props;

    if (select) {
      onAddDescriptor(descriptor);
    } else {
      onRemoveDescriptor(descriptor);
    }
  }

  public componentWillMount() {
    const { matchingDescriptors, pagination, loadDescriptors, loadCrops } = this.props;

    if (!matchingDescriptors) {
      log('Loading descriptors');
      loadDescriptors({ page: pagination.page, size: pagination.size, properties: pagination.sort as string[], direction: pagination.dir as SortDirection }, pagination.filter);
    }

    loadCrops();
  }

121 122
  public componentWillUnmount() {
    this.props.clearDescriptors();
123 124 125 126
  }

  public render() {

127
    const { classes, matchingDescriptors, onAddAllDescriptors, onRemoveAllDescriptors, loadMoreDescriptors, t } = this.props;
128 129 130 131 132 133 134

    // handle empty current descriptors
    let { currentDescriptors } = this.props;
    if (!currentDescriptors) {
      currentDescriptors = [];
    }

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    const renderDescription = (descriptor: Descriptor) => (
      <DescriptorCard
        className="ml-20 mr-20 mb-20"
        key={ descriptor.uuid }
        descriptor={ descriptor }
        compact
        crop version
        textRows={ 3 }
        selectedProps={ {
          selectable: true,
          onSelect: this.onDescriptorSelected(true),
          disabled: currentDescriptors && currentDescriptors.map((d) => d.uuid).indexOf(descriptor.uuid) !== -1,
        } }
      />
    );

151 152
    return (
      <Grid container spacing={ 0 } className={ classes.container }>
Oleksii Savran's avatar
Oleksii Savran committed
153
        <Grid item xs={ 12 } lg={ 2 } className={ classes.leftPanel }>
154
          <DescriptorFilters initialValues={ matchingDescriptors && matchingDescriptors.filter || {} } onSubmit={ this.applyFilters }/>
155
        </Grid>
Oleksii Savran's avatar
Oleksii Savran committed
156
        <Grid item xs={ 12 } lg={ 5 } className={ classes.grayBackground }>
157 158 159 160
          { matchingDescriptors && matchingDescriptors.content && matchingDescriptors.content.length < 2 ?
            <div className={ classes.fake }/> :
            <PaginationComponent
              pageObj={ matchingDescriptors }
161
              onSortChange={ this.onPaginationChange }
162 163
              displayName={ t('descriptors.common.modelName_plural') }
              sortOptions={ Descriptor.SORT_OPTIONS }
164 165 166 167 168 169 170 171 172 173 174 175 176
              actions={
                <Button className={ classes.btnBlue } onClick={ onAddAllDescriptors }>
                  { t('descriptors.dashboard.c.picker.addAll') }
                </Button>
              }
            />
          }
          { matchingDescriptors && matchingDescriptors.content &&
            <PagedLoader
              paged={ matchingDescriptors }
              loadMore={ loadMoreDescriptors }
              roughItemHeight={ 200 }
              itemRenderer={ renderDescription }
177 178 179
            />
          }
        </Grid>
Oleksii Savran's avatar
Oleksii Savran committed
180
        <Grid item xs={ 12 } lg={ 5 } className={ `${ classnames({ [classes.greenBackground]: currentDescriptors.length > 0 }) } even-row` }>
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
          <Grid container item spacing={ 0 }>
            <h4 className="font-bold pt-20 pl-20">
              { currentDescriptors.length } { currentDescriptors.length > 1 ?
              t('descriptors.dashboard.c.picker.descriptor_plural') :
              t('descriptors.dashboard.c.picker.descriptor') } { t('descriptors.dashboard.c.picker.selected')
            }
            </h4>
            <div className={ classes.flexGrow }/>
            <div className="pt-15 pr-20 pb-20 pl-20">
              <Button className={ classes.btnDefault } onClick={ onRemoveAllDescriptors }>
                { t('descriptors.dashboard.c.picker.removeAll') }
              </Button>
            </div>
          </Grid>
          {
            currentDescriptors.length === 0 && (
              <Card square className="ml-20 mr-20 mb-20">
                <CardHeader
                  className={ classes.emptyCardHeader }
                  classes={ {
                    title: classes.titleCardDisabled,
                  } }
                  title={
                    (
                      <span>{ t('descriptors.dashboard.c.picker.zero') }</span>
                    )
                  }
                />
              </Card>
            )
          }
          { currentDescriptors.length > 0 && currentDescriptors.map((descriptor: Descriptor) => (
              <DescriptorCard
                key={ descriptor.uuid }
                className="ml-20 mr-20 mb-20"
                descriptor={ descriptor }
                compact
                crop version
                textRows={ 3 }
                onUnselect={ this.onDescriptorSelected(false) }
                selectedProps={ {
                  selectable: false,
                  onUnselect: this.onDescriptorSelected(false),
                } }
              />
            ),
          ) }
        </Grid>
      </Grid>
    );
  }
}

export default translate()(withStyles(styles)(DescriptorPicker));