index.tsx 5.68 KB
Newer Older
1 2 3 4 5 6 7 8
import * as React from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';

import { log } from 'utilities/debug';
import DescriptorList from 'model/catalog/DescriptorList';
import Descriptor from 'model/catalog/Descriptor';
import DescriptorFilter from 'model/catalog/DescriptorFilter';
9
import FilteredPage from 'model/FilteredPage';
10 11 12 13

import { loadCrops } from 'crop/actions/public';
import DescriptorPicker from 'descriptors/ui/c/DescriptorPicker';
import { importDescriptor } from 'descriptors/actions/editor';
14
import { listAccessibleDescriptors, moreAccessibleDescriptors, clearDescriptors } from 'descriptors/actions/dashboard';
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
15
import Page, { IPageRequest, SortDirection } from 'model/Page';
16 17 18 19 20 21 22 23 24 25 26 27
import Pagination from 'model/Pagination';
import {
  loadDescriptorList, saveDescriptorList, addDescriptorsToDescriptorList, removeDescriptorsFromDescriptorList,
  addDescriptorToDescriptorList, removeDescriptorFromDescriptorList,
} from 'descriptorlists/actions/editor';
import { parse } from 'query-string';
import StepperTemplate from 'ui/common/stepper/StepperTemplate';

interface IDescriptorListProps extends React.ClassAttributes<any> {
  history: any;
  pagination?: Pagination<DescriptorFilter>;
  matchingDescriptors: Page<Descriptor>;
28
  listAccessibleDescriptors: (page?: IPageRequest, filter?: DescriptorFilter) => void;
29 30
  moreAccessibleDescriptors: (paged?: FilteredPage<Descriptor>, filter?: string | DescriptorFilter) => void;
  clearDescriptors: () => void;
31 32 33 34 35
  addDescriptorToDescriptorList: any;
  addDescriptorsToDescriptorList: (descriptorList: DescriptorList, descriptorUuids: string[]) => void;
  removeDescriptorsFromDescriptorList: (descriptorList: DescriptorList, descriptorUuids: string[]) => void;
  removeDescriptorFromDescriptorList: any;
  loadCrops: () => any;
36 37 38 39 40
  filterCode: any;
  page: number;
  size: number;
  dir: string;
  sort: string;
41 42 43 44 45
}

class SelectDescriptorsStep extends StepperTemplate<IDescriptorListProps> {

  protected static needs = [
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
46
    ({ search }) => listAccessibleDescriptors({ page: +parse(search).p, size: +parse(search).l, properties: [...parse(search).s], direction: parse(search).d as SortDirection }),
47 48 49
  ];
  protected renderContent = () => (
    <DescriptorPicker
50
      loadDescriptors={ this.props.listAccessibleDescriptors }
51 52
      loadMoreDescriptors={ this.props.moreAccessibleDescriptors }
      clearDescriptors={ this.props.clearDescriptors }
53 54 55 56 57 58
      matchingDescriptors={ this.props.matchingDescriptors }
      currentDescriptors={ this.props.item && this.props.item.descriptors }
      onAddDescriptor={ this.addDescriptor }
      onRemoveAllDescriptors={ this.removeAllDescriptors }
      onRemoveDescriptor={ this.removeDescriptor }
      onAddAllDescriptors={ this.addAllDescriptors }
59 60 61 62 63 64
      pagination={ {
        page: this.props.page,
        size: this.props.size,
        sort: this.props.sort,
        dir: this.props.dir,
      } }
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
      loadCrops={ this.props.loadCrops }
      history={ this.props.history }
      location={ this.props.location }
      classes={ {} }
    />
  )

  protected addDescriptor = (descriptor: Descriptor) => {
    const { item: descriptorList, addDescriptorToDescriptorList } = this.props;
    log(`Adding descriptor ${descriptor.uuid}`);

    addDescriptorToDescriptorList(descriptorList, descriptor);
  }

  protected addAllDescriptors = () => {
    const { matchingDescriptors, item: descriptorList, addDescriptorsToDescriptorList } = this.props;
    const uuids = [];

    if (matchingDescriptors && matchingDescriptors.content) {
      matchingDescriptors.content.map((descriptor) => {
        const existed = descriptorList.descriptors.find((dl) => dl.uuid === descriptor.uuid);
        if (!existed) {
          uuids.push(descriptor.uuid);
        }
      });
    }
    if (uuids.length) {
      log(`Adding ${uuids.length} descriptor${uuids.length !== 1 ? 's' : '' }`);
      addDescriptorsToDescriptorList(descriptorList, uuids);
    }
  }

  protected removeAllDescriptors = () => {
    const { item: descriptorList, removeDescriptorsFromDescriptorList } = this.props;
    const uuids = [];

    if (descriptorList && descriptorList.descriptors && descriptorList.descriptors.length > 0) {
      descriptorList.descriptors.map((descriptor) => {
        uuids.push(descriptor.uuid);
      });
    }
    if (uuids.length) {
      log(`Removing ${uuids.length} descriptor${uuids.length !== 1 ? 's' : '' }`);
      removeDescriptorsFromDescriptorList(descriptorList, uuids);
    }
  }
  protected removeDescriptor = (descriptor: Descriptor) => {
    const { item: descriptorList, removeDescriptorFromDescriptorList } = this.props;

    log(`Removing descriptor ${descriptor.uuid}`);
    removeDescriptorFromDescriptorList(descriptorList, descriptor);
  }
}


const mapStateToProps = (state, ownProps) => ({
Viacheslav Pavlov's avatar
Viacheslav Pavlov committed
121 122
  descriptorList: state.descriptorList.dashboard.descriptorList ? state.descriptorList.dashboard.descriptorList.data : undefined,
  matchingDescriptors: state.descriptors.dashboard.paged ? state.descriptors.dashboard.paged.data : undefined,
123 124 125 126
  page: +parse(ownProps.location.search).p || 0, // current page
  size: +parse(ownProps.location.search).l || 20, // page size
  sort: parse(ownProps.location.search).s, // page sorts
  dir: parse(ownProps.location.search).d, // page sort directions
127 128 129 130 131 132 133 134
});

const mapDispatchToProps = (dispatch) => bindActionCreators({
  saveDescriptorList,
  loadDescriptorList,
  importDescriptor,
  removeDescriptorFromDescriptorList,
  addDescriptorToDescriptorList,
135
  listAccessibleDescriptors,
136
  moreAccessibleDescriptors,
137 138 139
  loadCrops,
  addDescriptorsToDescriptorList,
  removeDescriptorsFromDescriptorList,
140
  clearDescriptors,
141 142 143 144 145
}, dispatch);

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