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

Allow editing of extra information of Descriptor Lists

parent 87e8602a
......@@ -93,6 +93,10 @@ export interface IDescriptorFilter extends IAuditedVersionedModelFilter {
uom?: IStringFilter;
}
export enum DescriptorListExtra {
JSON_MAPPING = 'JSON_MAPPING',
}
export class DescriptorList extends UuidModel implements IUserPermissions {
public owner: Partner;
public crop: string;
......@@ -103,6 +107,7 @@ export class DescriptorList extends UuidModel implements IUserPermissions {
public publisher: string;
public bibliographicCitation: string;
public descriptors: Descriptor[];
public extras: Map<DescriptorListExtra, string>;
public _permissions: CurrentPermissions;
public static SORT_OPTIONS = {
......
import * as React from 'react';
// import {connect} from 'react-redux';
// import {bindActionCreators} from 'redux';
......
......@@ -23,6 +23,7 @@ import Grid from 'material-ui/Grid';
import Paper from 'material-ui/Paper';
import Button from 'material-ui/Button';
import DescriptorUpload from 'ui/catalog/descriptor/DescriptorUpload';
import DescriptorListExtras from './c/Extras';
interface IDescriptorListEditPageProps extends React.ClassAttributes<any> {
classes: any;
......@@ -60,7 +61,7 @@ class DescriptorListEditPage extends React.Component<IDescriptorListEditPageProp
public constructor(props: any) {
super(props);
this.state = { uploader: false, csvDescriptors: '', uploadedDescriptors: null };
this.state = { uploader: false, extras: false, csvDescriptors: '', uploadedDescriptors: null };
}
public componentWillMount() {
......@@ -111,7 +112,11 @@ class DescriptorListEditPage extends React.Component<IDescriptorListEditPageProp
public addDescriptors = () => {
log(`Openinig upload descriptors form`);
this.setState({ ...this.state, uploader: true });
this.setState({ ...this.state, extras: false, uploader: ! this.state.uploader });
}
public editExtras = () => {
this.setState({ ...this.state, uploader: false, extras: ! this.state.extras });
}
public importDescriptors = async (d: Descriptor[]) => {
......@@ -159,19 +164,27 @@ class DescriptorListEditPage extends React.Component<IDescriptorListEditPageProp
buttons={ (
<span>
<Link to="/dashboard/descriptorlists"><Button type="button">Back to dashboard</Button></Link>
{ descriptorList && descriptorList.uuid && <Button onClick={ this.addDescriptors }>Add descriptors from Excel</Button> }
{ descriptorList && descriptorList.uuid && <Button onClick={ this.addDescriptors }>{ this.state.uploader ? `No descriptors from Excel` : `Add descriptors from Excel` }</Button> }
{ descriptorList && descriptorList.uuid && <Button onClick={ this.editExtras }>{ this.state.extras ? `Cancel extras` : `Edit extras` }</Button> }
{ descriptorList && descriptorList.id > 0 && ! descriptorList.published && <Button type="button" onClick={ this.onPublish }>Approve and Publish</Button> }
</span>
) } />
</Paper>
</Grid>
{ this.state.uploader ? (
{ this.state.uploader && descriptorList.uuid && (
<Grid item xs={ 12 } className="mb-20">
<DescriptorUpload onImport={ this.importDescriptors } />
</Grid>
) }
{ this.state.extras && descriptorList.uuid && (
<Grid item xs={ 12 } className="mb-20">
<DescriptorListExtras descriptorList={ descriptorList } onSave={ this.onSave } />
</Grid>
) }
) : descriptorList.uuid && (
{ ! (this.state.uploader || this.state.extras) && descriptorList.uuid && (
<Grid item xs={ 12 } className="mb-20">
<DescriptorPicker
......
import * as React from 'react';
// import {connect} from 'react-redux';
// import {bindActionCreators} from 'redux';
import {log} from 'utilities/debug';
import { DescriptorListExtra, DescriptorList } from 'model/descriptor.model';
// import ContentHeaderWithButton from 'ui/common/heading/ContentHeaderWithButton';
// import Grid from 'material-ui/Grid';
import Paper from 'material-ui/Paper';
import Button from 'material-ui/Button';
import FormControl from 'material-ui/Form/FormControl';
import Input from 'material-ui/Input';
import InputLabel from 'material-ui/Input/InputLabel';
interface IExtras extends React.ClassAttributes<any> {
className?: any;
descriptorList: DescriptorList;
onSave: any;
}
// Page to edit descriptor list extras
class Extras extends React.Component<IExtras, any> {
public constructor(props: any) {
super(props);
this.state = { extras: this.props.descriptorList.extras };
}
private updateJson = (e) => {
const { extras } = this.state;
const { target: { value: jsonMapping } } = e;
extras[DescriptorListExtra.JSON_MAPPING] = e.target.value;
if (jsonMapping && jsonMapping !== '') {
try {
const parsedJson = JSON.parse(e.target.value);
log(`Nice JSON`, parsedJson);
this.setState({ ...this.state, extras, jsonError: null });
} catch (e) {
log(`Error parsing your JSON ${e}`);
this.setState({ ...this.state, extras, jsonError: `Invalid JSON ${e}` });
}
} else {
this.setState({ ...this.state, extras });
}
}
private getDefaults = (e) => {
const { extras } = this.state;
const { descriptorList } = this.props;
e.preventDefault();
const mapping: object[] = [];
descriptorList.descriptors.forEach((d) => {
mapping.push({ uuid: d.uuid, label: d.title, jsonPath: d.columnName, multiple: false });
});
extras[DescriptorListExtra.JSON_MAPPING] = JSON.stringify(mapping, null, 2);
this.setState({ ...this.state, extras });
}
private saveExtras = (e) => {
const { descriptorList, onSave } = this.props;
e.preventDefault();
log(`Saving extras ${descriptorList.uuid}`, this.state.extras, descriptorList.extras);
onSave({ ...descriptorList, extras: this.state.extras });
}
public render() {
return (
<div>
<Paper className={ `${this.props.className} p-20 mb-20` }>
<FormControl fullWidth>
<InputLabel error={ this.state.jsonError && true }>JSON mappings</InputLabel>
<Input multiline error={ this.state.jsonError && true } placeholder="" onChange={ this.updateJson } value={ this.state.extras[DescriptorListExtra.JSON_MAPPING] } />
</FormControl>
<Button raised type="button" disabled={ (this.state.jsonError) && true } onClick={ this.saveExtras }>Save extras</Button>
<Button type="button" onClick={ this.getDefaults }>Defaults</Button>
</Paper>
</div>
);
}
}
export default Extras;
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