Commit dabcd93b authored by Matija Obreza's avatar Matija Obreza
Browse files

Partner filter

parent 637a033b
......@@ -11,6 +11,7 @@ import CropFilter from 'ui/catalog/crop/CropFilter';
import {DESCRIPTOR_FILTER_FORM} from 'constants/filter';
import {Descriptor} from 'model/descriptor.model';
import DescriptorListPicker from './DescriptorListPicker';
import PartnerFilter from 'ui/catalog/partner/PartnerFilter';
// <CollapsibleComponentSearch title="Descriptions">
// <StringFilter name="title" searchType="contains" label="Descriptor title" placeholder="Title"/>
......@@ -25,6 +26,7 @@ import DescriptorListPicker from './DescriptorListPicker';
const DescriptorFilters = ({handleSubmit, initialize, ...other}) => (
<FiltersBlock title="Descriptors" handleSubmit={ handleSubmit } initialize={ initialize } { ...other }>
<TextFilter name="_text" label="Keyword search" placeholder="mardi rice" className="p-20" />
<PartnerFilter name="owner" label="Partner" className="p-20" />
<DescriptorListPicker name="list" label="Select descriptor list" className="p-20" />
<CollapsibleComponentSearch title="Crop">
<CropFilter />
......
import * as React from 'react';
import { Field } from 'redux-form';
import SelectPartner from './SelectPartner';
interface IPartnerFilter extends React.ClassAttributes<any> {
name: string;
label: string;
className?: string;
}
class PartnerFilter extends React.Component<IPartnerFilter, any> {
public render() {
const { name , label, className, ...other } = this.props;
return (
<div className={ className }>
<Field
name={ `${name}.uuid` }
component={ SelectPartner }
label={ label }
useUuid
allowNull
multiple
{ ...other }
/>
</div>
);
}
}
export default PartnerFilter;
......@@ -5,9 +5,8 @@ import { bindActionCreators } from 'redux';
import {log} from 'utilities/debug';
import {Page} from 'model/common.model';
import {Partner} from 'model/partner.model';
import {promiseLoadPartners} from 'actions/partner';
import {loadPartners, loadMyPartners} from 'actions/partner';
import Input from 'material-ui/Input';
import FormControl from 'ui/common/forms/FormControl';
......@@ -30,61 +29,73 @@ interface ISelectPartnerProps extends React.ClassAttributes<any> {
editable?: boolean;
required?: boolean;
meta?: any;
promiseLoadPartners: any;
loadPartners: any;
loadMyPartners: any;
partners: Partner[];
onlyMine?: boolean; // list only my partners
useUuid?: boolean; // toggle this if input contains the UUID instead of Partner object
allowNull?: boolean; // add blank option (null) to selection
multiple?: boolean; // allow multiple
}
class SelectPartner extends React.Component<ISelectPartnerProps, any> {
public constructor(props: any) {
super(props);
this.state = {
partners: props.partners || [],
};
}
public componentWillMount() {
const { promiseLoadPartners } = this.props;
promiseLoadPartners(0, 50).then((data) => new Page<Partner>(data, (dl) => new Partner(dl)))
.then((paged) => {
this.setState({
partners: paged.content,
});
});
const { onlyMine, loadPartners, loadMyPartners, partners } = this.props;
// console.log(`SelectPartner`, onlyMine, partners);
if (! partners || partners.length === 0) {
onlyMine ? loadMyPartners() : loadPartners();
}
}
public handleChange = ({target: {value}}) => {
const { editable, input } = this.props;
const { partners } = this.state;
log(`Partner selection editable=${editable}`, value);
const p = partners.find((p) => p.uuid === value);
if (editable === undefined || editable) {
input.onChange(p);
const { partners, editable, input, useUuid, multiple } = this.props;
value = value === '' ? null : value;
log(`Partner selection editable=${editable} value=${value}`, value);
if (useUuid) {
if (multiple) {
input.onChange(value ? [ value ] : null);
} else {
input.onChange(value);
}
} else {
const p = partners.find((p) => p.uuid === value);
if (editable === undefined || editable) {
if (multiple) {
input.onChange(p ? [ p ] : null);
} else {
input.onChange(p);
}
}
}
}
public render() {
const {classes, input, label, editable, required, meta } = this.props;
const { partners } = this.state;
const { partners, classes, input, label, editable, required, meta, useUuid, allowNull, multiple } = this.props;
const allowsEdit = editable === undefined || editable;
const val = input && input.value ?
(multiple ? input.value[0] : input.value) : null;
if (! allowsEdit) {
return (
<FormControl fullWidth label={ label }>
<Input value={ input.value && input.value.name } className={ classes.input } disabled />
<Input value={ val && val.name } className={ classes.input } disabled />
</FormControl>
);
}
// console.log(`Partner req=${required}`, meta);
// <MenuItem value="" />
return (
<FormControl fullWidth required={ required } meta={ meta } label={ label }>
<Select error={ meta.touched && meta.error } className={ classes.select } value={ input.value && input.value.uuid } onChange={ this.handleChange }
<Select error={ meta.touched && meta.error } className={ classes.select } value={ useUuid ? val || '' : val && val.uuid || '' } onChange={ this.handleChange }
input={ <Input /> }
>
{ allowNull && <MenuItem key="noPartner" value=""><em>Select partner</em></MenuItem> }
{ partners && partners.map((p) => <MenuItem key={ p.uuid } value={ p.uuid }>{ p.name }</MenuItem>) }
</Select>
</FormControl>
......@@ -93,12 +104,15 @@ class SelectPartner extends React.Component<ISelectPartnerProps, any> {
}
const mapStateToProps = (state) => ({
partners: state.partner.myPartners,
const mapStateToProps = (state, ownProps) => ({
partners: ownProps.onlyMine ?
state.partner.myPartners || null
: state.partner.paged && state.partner.paged.content || null,
});
const mapDispatchToProps = (dispatch) => bindActionCreators({
promiseLoadPartners,
loadPartners,
loadMyPartners,
}, dispatch);
export default connect(mapStateToProps, mapDispatchToProps)(withStyles(styles)(SelectPartner));
......@@ -9,12 +9,14 @@ import StringFilter from 'ui/common/filter/StringFilter';
import StringArrFilter from 'ui/common/filter/StringArrFilter';
import TextFilter from 'ui/common/filter/TextFilter';
import CropFilter from 'ui/catalog/crop/CropFilter';
import PartnerFilter from 'ui/catalog/partner/PartnerFilter';
// <StringArrFilter name="language" label="Language" placeholder="Language"/>
const DatasetFilters = ({handleSubmit, initialize, ...other}) => (
<FiltersBlock title="Datasets" handleSubmit={ handleSubmit } initialize={ initialize } { ...other }>
<TextFilter name="_text" label="Keyword search" placeholder="mardi rice" className="p-20" />
<PartnerFilter name="owner" label="Partner" className="p-20" />
<CollapsibleComponentSearch title="Crop">
<CropFilter />
</CollapsibleComponentSearch>
......
......@@ -33,6 +33,7 @@ class BasicInfoStep extends React.Component<ILoginContainerProps, any> {
<Field required
name="owner"
component={ SelectPartner }
onlyMine
label="Select Partner"
placeholder="Partner"
editable={ ! (initialValues.uuid && initialValues.version) }
......
......@@ -5,10 +5,8 @@ import {bindActionCreators} from 'redux';
import {log} from 'utilities/debug';
import {loadDescriptor, saveDescriptor, publishDescriptor} from 'actions/descriptors';
import {loadMyPartners} from 'actions/partner';
import { Descriptor } from 'model/descriptor.model';
import { Partner } from 'model/partner.model';
import DescriptorForm from './c/DescriptorForm';
import VocabularyCard from 'ui/pages/vocabulary/c/VocabularyCard';
......@@ -20,9 +18,6 @@ interface IDescriptorEditPageProps extends React.ClassAttributes<any> {
classes: any;
uuid?: string;
loadMyPartners: () => void;
myPartners: Partner[];
loadDescriptor: (uuid: string) => void;
saveDescriptor: (descriptor: Descriptor) => void;
publishDescriptor: (descriptor: Descriptor) => void;
......@@ -33,15 +28,10 @@ class DescriptorEditPage extends React.Component<IDescriptorEditPageProps, any>
protected static needs = [
({ params: { uuid } }) => loadDescriptor(uuid),
loadMyPartners,
];
public componentDidMount() {
const {descriptor, myPartners, loadMyPartners, loadDescriptor, uuid} = this.props;
if (!myPartners || myPartners.length === 0) {
loadMyPartners();
}
const {descriptor, loadDescriptor, uuid} = this.props;
if (uuid && (!descriptor || descriptor.uuid !== uuid)) {
loadDescriptor(uuid);
......@@ -68,14 +58,11 @@ class DescriptorEditPage extends React.Component<IDescriptorEditPageProps, any>
}
public render() {
const {uuid, myPartners} = this.props;
const {uuid} = this.props;
let {descriptor} = this.props;
if (! descriptor && ! uuid) {
descriptor = new Descriptor();
if (myPartners && myPartners.length > 0) {
descriptor.owner = myPartners.length[0];
}
}
if (! descriptor) {
......@@ -87,7 +74,7 @@ class DescriptorEditPage extends React.Component<IDescriptorEditPageProps, any>
<Grid container spacing={ 0 } className="p-20 back-gray">
<Grid item xs={ 12 }>
<Paper className="p-20">
<DescriptorForm partners={ myPartners } initialValues={ descriptor } pageTitle="Edit descriptor" onPublish={ this.onPublish } onSubmit={ this.onSave }/>
<DescriptorForm initialValues={ descriptor } pageTitle="Edit descriptor" onPublish={ this.onPublish } onSubmit={ this.onSave }/>
</Paper>
{ descriptor.vocabulary && (
<VocabularyCard className="mt-20" vocabulary={ descriptor.vocabulary } />
......@@ -100,12 +87,10 @@ class DescriptorEditPage extends React.Component<IDescriptorEditPageProps, any>
const mapStateToProps = (state, { match }) => ({
uuid: match.params.uuid,
myPartners: state.partner.myPartners,
descriptor: state.descriptors.currentDescriptor,
});
const mapDispatchToProps = (dispatch) => bindActionCreators({
loadMyPartners,
loadDescriptor,
saveDescriptor,
publishDescriptor,
......
......@@ -113,7 +113,7 @@ class DescriptorForm extends React.Component<any, any> {
}
public render() {
const {error, handleSubmit, initialValues, pageTitle, onPublish, partners, invalid, submitting, anyTouched, currentOwner, currentVocabulary} = this.props;
const {error, handleSubmit, initialValues, pageTitle, onPublish, invalid, submitting, anyTouched, currentOwner, currentVocabulary} = this.props;
if (! initialValues) {
return null;
......@@ -136,7 +136,7 @@ class DescriptorForm extends React.Component<any, any> {
<Field required name="owner"
editable={ ! (initialValues.uuid && initialValues.version) }
label="Owner of the descriptor" placeholder="Partner"
partners={ partners }
onlyMine
component={ SelectPartner }
validate={ [ Validators.required ] }
/>
......
......@@ -9,11 +9,13 @@ import StringFilter from 'ui/common/filter/StringFilter';
import StringArrFilter from 'ui/common/filter/StringArrFilter';
import CropFilter from 'ui/catalog/crop/CropFilter';
import TextFilter from 'ui/common/filter/TextFilter';
import PartnerFilter from 'ui/catalog/partner/PartnerFilter';
const DescriptorListFilters = ({handleSubmit, initialize, ...other}) => (
<FiltersBlock title="Crop descriptors" handleSubmit={ handleSubmit } initialize={ initialize } { ...other }>
<TextFilter name="_text" label="Keyword search" placeholder="mardi rice" className="p-20" />
<PartnerFilter name="owner" label="Partner" className="p-20" />
<CollapsibleComponentSearch title="Crop">
<CropFilter />
</CollapsibleComponentSearch>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment