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

Add/Remove descriptors from a Descriptor List

- Reusing existing components
parent 8504dc73
Pipeline #3801 passed with stages
in 3 minutes and 19 seconds
......@@ -3,7 +3,7 @@ import { SubmissionError } from 'redux-form';
import { dereferenceReferences } from 'utilities';
import { DescriptorListService } from 'service/DescriptorListService';
import { DescriptorList } from 'model/descriptors.model';
import { DescriptorList, Descriptor } from 'model/descriptors.model';
import { IDescriptorListFilter } from 'model/filter.model';
import { Partner } from 'model/partner.model';
import { Page } from 'model/common.model';
......@@ -118,3 +118,21 @@ export const publishDescriptorList = (descriptorList: DescriptorList, published?
dispatch(push(`/descriptorlist/${descriptorList.uuid}`));
});
};
// Add a descriptor to the descriptor list
export const addDescriptorToDescriptorList = (descriptorList: DescriptorList, descriptor: Descriptor) => (dispatch, getState) => {
return DescriptorListService.addDescriptor(getState().login.access_token, descriptorList, [ descriptor.uuid ])
.then((descriptorList) => {
// receive updates
dispatch(receiveDescriptorList(descriptorList));
});
};
// Remove a descriptor to the descriptor list
export const removeDescriptorFromDescriptorList = (descriptorList: DescriptorList, descriptor: Descriptor) => (dispatch, getState) => {
return DescriptorListService.removeDescriptor(getState().login.access_token, descriptorList, [ descriptor.uuid ])
.then((descriptorList) => {
// receive updates
dispatch(receiveDescriptorList(descriptorList));
});
};
......@@ -22,6 +22,8 @@ export const REMOVE_DESCRIPTORLIST_URL = API_BASE_URL + '/descriptorlist';
export const CREATE_DESCRIPTORLIST_URL = API_BASE_URL + '/descriptorlist/create';
export const UPDATE_DESCRIPTORLIST_URL = API_BASE_URL + '/descriptorlist/update';
export const PUBLISH_DESCRIPTORLIST_URL = API_BASE_URL + '/descriptorlist/publish';
export const ADD_DESCRIPTORLIST_DESCRIPTOR_URL = API_BASE_URL + '/descriptorlist/add-descriptors';
export const REMOVE_DESCRIPTORLIST_DESCRIPTOR_URL = API_BASE_URL + '/descriptorlist/remove-descriptors';
// Descriptors API
export const GET_DESCRIPTORS_URL = API_BASE_URL + '/descriptor/descriptor/list';
......
import { DescriptorList, Descriptor } from 'model/descriptors.model';
import { Page } from 'model/common.model';
import { MY_LIST_DESCRIPTORSLISTS_URL, GET_DESCRIPTORLIST_URL, REMOVE_DESCRIPTORLIST_URL, CREATE_DESCRIPTORLIST_URL, UPDATE_DESCRIPTORLIST_URL, LIST_DESCRIPTORLISTS_URL, PUBLISH_DESCRIPTORLIST_URL } from 'constants/apiURLS';
import { MY_LIST_DESCRIPTORSLISTS_URL, GET_DESCRIPTORLIST_URL, REMOVE_DESCRIPTORLIST_URL,
CREATE_DESCRIPTORLIST_URL, UPDATE_DESCRIPTORLIST_URL, LIST_DESCRIPTORLISTS_URL, PUBLISH_DESCRIPTORLIST_URL,
ADD_DESCRIPTORLIST_DESCRIPTOR_URL, REMOVE_DESCRIPTORLIST_DESCRIPTOR_URL } from 'constants/apiURLS';
import authenticatedRequest from 'utilities/requestUtils';
import { IDescriptorListFilter } from 'model/filter.model';
......@@ -86,7 +88,24 @@ export class DescriptorListService {
}).then(({ data }) => new DescriptorList(data));
}
// TODO
// public static addDescriptorToList
// public static removeDescriptorFromList
public static addDescriptor(token: string, descriptorList: DescriptorList, descriptorUuids: string[]): Promise<DescriptorList> {
console.log('Adding descriptor to descriptor list', descriptorUuids);
return authenticatedRequest(token, {
url: `${ADD_DESCRIPTORLIST_DESCRIPTOR_URL}/${descriptorList.uuid},${descriptorList.version}`,
method: 'POST',
data: [ ...descriptorUuids ],
}).then(({ data }) => new DescriptorList(data));
}
public static removeDescriptor(token: string, descriptorList: DescriptorList, descriptorUuids: string[]): Promise<DescriptorList> {
console.log('Removing descriptor to descriptor list', descriptorUuids);
return authenticatedRequest(token, {
url: `${REMOVE_DESCRIPTORLIST_DESCRIPTOR_URL}/${descriptorList.uuid},${descriptorList.version}`,
method: 'POST',
data: [ ...descriptorUuids ],
}).then(({ data }) => new DescriptorList(data));
}
}
......@@ -89,7 +89,13 @@ class DescriptorPicker extends React.Component<IDescriptorPickerProps, any> {
public render() {
const {classes, loadDescriptors, matchingDescriptors, currentDescriptors} = this.props;
const {classes, loadDescriptors, matchingDescriptors} = this.props;
// handle empty current descriptors
let {currentDescriptors} = this.props;
if (! currentDescriptors) {
currentDescriptors = [];
}
return (
<Grid container spacing={ 0 } className={ classes.container }>
......
......@@ -124,10 +124,11 @@ const mapStateToProps = (state, ownProps) => ({
});
const mapDispatchToProps = (dispatch) => bindActionCreators({
createDatasetRequest,
createDescriptorList,
listDatasets: listMyDatasets,
listDescriptors: listMyDescriptors,
listDescriptorLists: listMyDescriptorLists,
createDescriptorList,
}, dispatch);
export default connect(mapStateToProps, mapDispatchToProps)(withStyles(styles)(AdministrationDashboard));
......@@ -47,6 +47,10 @@ export default function MyDataTable({
onPaginationChange,
}: IDatasetLinkProps) {
if (! paged) {
return null;
}
let sortOptions = defaultSortOptions;
switch (tab) {
......
......@@ -141,8 +141,8 @@ class DescriptorListPage extends React.Component<IDescriptorListPageProps, any>
</Grid>
{ descriptorList.descriptors.map((descriptor, i) => (
<Grid item xs={ 12 }>
<DescriptorCard className={ classes.card } key={ `${descriptor.uuid}` } descriptor={ descriptor } />
<Grid item xs={ 12 } key={ `${descriptor.uuid}` }>
<DescriptorCard className={ classes.card } descriptor={ descriptor } />
</Grid>
),
) }
......
......@@ -3,28 +3,53 @@ import * as React from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import {loadDescriptorList, saveDescriptorList, publishDescriptorList} from 'actions/descriptorList';
import {loadDescriptors} from 'actions/descriptors';
import {loadDescriptorList, saveDescriptorList, publishDescriptorList, addDescriptorToDescriptorList, removeDescriptorFromDescriptorList} from 'actions/descriptorList';
import {DescriptorList, Descriptor} from 'model/descriptors.model';
import {Page} from 'model/common.model';
import DescriptorListForm from './c/DescriptorListForm';
import DescriptorPicker from 'ui/catalog/descriptor/DescriptorPicker';
import ContentHeaderWithButton from 'ui/common/heading/ContentHeaderWithButton';
import { withStyles } from 'material-ui/styles';
import Grid from 'material-ui/Grid';
import Paper from 'material-ui/Paper';
interface IDescriptorListEditPageProps extends React.ClassAttributes<any> {
classes: any;
uuid?: string;
descriptorList?: DescriptorList;
loadDescriptorList: any;
saveDescriptorList: any;
publishDescriptorList: any;
addDescriptorToDescriptorList: any;
removeDescriptorFromDescriptorList: any;
loadDescriptors: any;
matchingDescriptors: Page<Descriptor>;
}
const styles = (theme) => ({
contentContainer: {
backgroundColor: '#E8E5E0',
padding: '16px',
},
form: {
padding: '20px',
},
});
// Page to edit a descriptor list
class DescriptorListEditPage extends React.Component<IDescriptorListEditPageProps, any> {
protected static needs = [
({ params: { uuid } } ) => loadDescriptorList(uuid),
loadDescriptors,
];
public componentWillMount() {
const {uuid, descriptorList, loadDescriptorList} = this.props;
const {uuid, descriptorList, loadDescriptorList, loadDescriptors, matchingDescriptors} = this.props;
/*console.log(`componentWillMount... ${tab}`, this.props);*/
if (uuid && ! descriptorList) {
......@@ -46,15 +71,54 @@ class DescriptorListEditPage extends React.Component<IDescriptorListEditPageProp
publishDescriptorList(descriptorList);
}
public addDescriptor = (descriptor: Descriptor) => {
const {descriptorList, addDescriptorToDescriptorList} = this.props;
console.log(`Adding descriptor ${descriptor.uuid}`);
addDescriptorToDescriptorList(descriptorList, descriptor);
}
public removeDescriptor = (descriptor: Descriptor) => {
const {descriptorList, removeDescriptorFromDescriptorList} = this.props;
console.log(`Removing descriptor ${descriptor.uuid}`);
removeDescriptorFromDescriptorList(descriptorList, descriptor);
}
public render() {
const {descriptorList} = this.props;
const {classes, loadDescriptors, matchingDescriptors} = this.props;
let { descriptorList } = this.props;
// console.log('Editing descriptor list', descriptorList);
if (! descriptorList) {
descriptorList = new DescriptorList();
}
return (
<div>
<DescriptorListForm initialValues={ descriptorList } onPublish={ this.onPublish } onSubmit={ this.onSave }/>
</div>
<div>
<ContentHeaderWithButton title="Edit descriptor list information" buttonName="BACK TO DASHBOARD" buttonUrl="/dashboard/descriptorlists" />
<Grid container className={ classes.contentContainer }>
<Grid item xs={ 12 }>
<Paper className={ classes.form }>
<DescriptorListForm initialValues={ descriptorList } onPublish={ this.onPublish } onSubmit={ this.onSave } />
</Paper>
</Grid>
{ descriptorList.uuid && (
<Grid item xs={ 12 }>
<DescriptorPicker
loadDescriptors={ loadDescriptors }
matchingDescriptors={ matchingDescriptors }
currentDescriptors={ descriptorList.descriptors }
onAddDescriptor={ this.addDescriptor }
onRemoveDescriptor={ this.removeDescriptor }
classes={ {} }
/>
</Grid>
) }
</Grid>
</div>
);
}
}
......@@ -62,10 +126,12 @@ class DescriptorListEditPage extends React.Component<IDescriptorListEditPageProp
const mapStateToProps = (state, ownProps) => ({
uuid: ownProps.params.uuid,
descriptorList: state.descriptorList.currentDescriptorList,
matchingDescriptors: state.pagination.descriptorPage,
});
const mapDispatchToProps = (dispatch) => bindActionCreators({
loadDescriptorList, saveDescriptorList, publishDescriptorList,
loadDescriptors, addDescriptorToDescriptorList, removeDescriptorFromDescriptorList,
}, dispatch);
export default connect(mapStateToProps, mapDispatchToProps)(DescriptorListEditPage);
export default connect(mapStateToProps, mapDispatchToProps)(withStyles(styles)(DescriptorListEditPage));
......@@ -5,6 +5,8 @@ import Radio, { RadioGroup} from 'material-ui/Radio';
import {FormLabel, FormControl, FormControlLabel} from 'material-ui/Form';
import { DESCRIPTORLIST_FORM } from 'constants/descriptors';
import { Link } from 'react-router';
import { TextField } from 'ui/common/text-field';
import { MarkdownField } from 'ui/common/markdown';
import Heading from 'ui/common/heading';
......@@ -24,6 +26,7 @@ const DescriptorListForm = ({error, handleSubmit, initialValues, onPublish}) =>
<div>{ error && <strong>{ error }</strong> }</div>
<Button raised type="submit" >Save changes</Button>
<Link to="/dashboard/descriptorlists"><Button type="button">Back to dashboard</Button></Link>
{ initialValues && initialValues.id > 0 && ! initialValues.published && <Button type="button" onClick={ onPublish }>Approve and Publish</Button> }
</form>
);
......
Markdown is supported
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