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

`Descriptors` cleanup

parent 7dfde9b2
......@@ -3,37 +3,35 @@ import {SubmissionError} from 'redux-form';
import {dereferenceReferences} from 'utilities';
import {DescriptorService} from 'service/DescriptorService';
import {RECEIVE_DESCRIPTOR, RECEIVE_DESCRIPTOR_PAGE } from 'constants/descriptors';
import {CREATE_DESCRIPTOR, RECEIVE_DESCRIPTOR, RECEIVE_DESCRIPTOR_PAGE } from 'constants/descriptors';
import {Descriptor} from 'model/descriptors.model';
import {Partner} from 'model/partner.model';
import {Page} from 'model/common.model';
export function receiveDescriptor(descriptor: Descriptor) {
return {
type: RECEIVE_DESCRIPTOR,
descriptor,
};
return {
type: RECEIVE_DESCRIPTOR,
payload: descriptor,
};
}
function receiveDescriptorPage(page: Page<Descriptor>) {
return {
type: RECEIVE_DESCRIPTOR_PAGE,
payload: {
page,
},
};
return {
type: RECEIVE_DESCRIPTOR_PAGE,
payload: page,
};
}
export function showDescriptors() {
return (dispatch) => {
dispatch(push(`/descriptors`));
};
return (dispatch) => {
dispatch(push(`/descriptors`));
};
}
export function showDescriptorsDashboard() {
return (dispatch) => {
dispatch(push(`/dashboard`));
};
return (dispatch) => {
dispatch(push(`/dashboard`));
};
}
export function loadDescriptor(uuid: string) {
......@@ -51,37 +49,27 @@ export function loadDescriptor(uuid: string) {
};
}
export function createDescriptor(descriptor: Descriptor) {
console.log('Creating descriptor', descriptor);
return (dispatch, getState) => {
const token = getState().login.access_token;
return DescriptorService.createDescriptor(token, descriptor)
.then((saved) => {
dispatch(receiveDescriptor(saved));
dispatch(showDescriptors());
}).catch((error) => {
console.log('Save error', error);
throw new SubmissionError({ title: 'Title already used', _error: error.error });
});
};
}
export function updateDescriptor(descriptor: Descriptor) {
console.log('Updating descriptor', descriptor);
return (dispatch, getState) => {
const token = getState().login.access_token;
return DescriptorService.updateDescriptor(token, descriptor)
.then((saved) => {
dispatch(receiveDescriptor(saved));
saved.published ? dispatch(showDescriptors()) : dispatch(showDescriptorsDashboard());
}).catch((error) => {
console.log('Save error', error);
throw new SubmissionError({ title: 'Title already used', _error: error.error });
});
};
}
// Create a new descriptor
export const createDescriptor = () => (dispatch) => {
dispatch({ type: CREATE_DESCRIPTOR });
return dispatch(push(`/descriptor/edit`));
};
export const saveDescriptor = (descriptor: Descriptor) => (dispatch, getState) => {
console.log('Saving descriptor', descriptor);
const createOrSave = descriptor.version && descriptor.uuid ? DescriptorService.updateDescriptor : DescriptorService.createDescriptor;
return createOrSave(getState().login.access_token, descriptor)
.then((descriptor) => {
// receive the updated descriptor
dispatch(receiveDescriptor(descriptor));
// and redirect to proper edit page
return dispatch(push(`/descriptor/${descriptor.uuid}/edit`));
}).catch((error) => {
console.log('Save error', error);
throw new SubmissionError({ title: 'Could not save descriptor', _error: error.error });
});
};
export function listMyDescriptors(page?, results?, sortBy?, filter?, order?) {
return (dispatch, getState) => {
......@@ -94,7 +82,6 @@ export function listMyDescriptors(page?, results?, sortBy?, filter?, order?) {
.then((paged) => {
dereferenceReferences(paged.content, 'owner', (o) => new Partner(o));
dispatch(receiveDescriptorPage(paged));
paged.content.forEach((d) => dispatch(receiveDescriptor(d)));
})
.catch((error) => {
console.log('Error', error);
......@@ -113,7 +100,6 @@ export function loadDescriptors(page?, results?, sortBy?, filter?, order?) {
.then((paged) => {
dereferenceReferences(paged.content, 'owner', (o) => new Partner(o));
dispatch(receiveDescriptorPage(paged));
paged.content.forEach((d) => dispatch(receiveDescriptor(d)));
})
.catch((error) => {
console.log('Error', error);
......
......@@ -16,25 +16,25 @@ export const MY_DESCRIPTORS_LIST_URL = `${ME_API}/descriptors`;
export const MY_LIST_DESCRIPTORSLISTS_URL = `${ME_API}/descriptorlists`;
// Descriptor List API
export const LIST_DESCRIPTORLISTS_URL = API_BASE_URL + '/descriptorlist/list';
export const GET_DESCRIPTORLIST_URL = API_BASE_URL + '/descriptorlist';
const DESCRIPTORLIST_API = `${API_BASE_URL}/descriptorlist`;
export const GET_DESCRIPTORLIST_URL = DESCRIPTORLIST_API;
export const LIST_DESCRIPTORLISTS_URL = `${DESCRIPTORLIST_API}/list`;
// -- these require proper permissions
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';
export const REMOVE_DESCRIPTORLIST_URL = DESCRIPTORLIST_API;
export const CREATE_DESCRIPTORLIST_URL = `${DESCRIPTORLIST_API}/create`;
export const UPDATE_DESCRIPTORLIST_URL = `${DESCRIPTORLIST_API}/update`;
export const PUBLISH_DESCRIPTORLIST_URL = `${DESCRIPTORLIST_API}/publish`;
export const ADD_DESCRIPTORLIST_DESCRIPTOR_URL = `${DESCRIPTORLIST_API}/add-descriptors`;
export const REMOVE_DESCRIPTORLIST_DESCRIPTOR_URL = `${DESCRIPTORLIST_API}/remove-descriptors`;
// Descriptors API
export const GET_DESCRIPTORS_URL = API_BASE_URL + '/descriptor/descriptor/list';
export const GET_DESCRIPTOR_CATEGORIES_URL = API_BASE_URL + '/descriptor/categories';
export const GET_DESCRIPTORS_WITH_FILTER_URL = API_BASE_URL + '/descriptor/descriptor/list';
export const CREATE_DESCRIPTOR_URL = API_BASE_URL + '/descriptor/descriptor/create';
// POST /api/v0/descriptor/descriptor/update/{UUID},{version}
export const UPDATE_DESCRIPTOR_URL = API_BASE_URL + '/descriptor/descriptor/update';
export const GET_DESCRIPTOR_URL = API_BASE_URL + '/descriptor/descriptor';
export const PUBLISH_DESCRIPTOR_URL = API_BASE_URL + '/descriptor/descriptor/publish';
const DESCRIPTOR_API = `${API_BASE_URL}/descriptor`;
export const GET_DESCRIPTOR_URL = DESCRIPTOR_API;
export const LIST_DESCRIPTORS_URL = `${DESCRIPTOR_API}/list`;
export const GET_DESCRIPTOR_CATEGORIES_URL = `${DESCRIPTOR_API}/categories`;
export const CREATE_DESCRIPTOR_URL = `${DESCRIPTOR_API}/create`;
export const UPDATE_DESCRIPTOR_URL = `${DESCRIPTOR_API}/update`;
export const PUBLISH_DESCRIPTOR_URL = `${DESCRIPTOR_API}/publish`;
// Partner API
const PARTNER_API = `${API_BASE_URL}/partner`;
......
// Descriptors
export const ADD_DESCRIPTOR = 'App/ADD_DESCRIPTOR';
export const CREATE_DESCRIPTOR = 'App/Descriptor/CREATE_DESCRIPTOR';
export const PUBLISH_DESCRIPTOR = 'App/PUBLISH_DESCRIPTOR';
export const RECEIVE_DESCRIPTOR_LIST_PAGE = 'App/RECEIVE_DESCRIPTOR_LIST_PAGE';
export const RECEIVE_DESCRIPTOR = 'App/RECEIVE_DESCRIPTOR';
......
import * as update from 'immutability-helper';
import { IReducerAction } from 'model/common.model';
import { Descriptor } from 'model/descriptors.model';
import {
RECEIVE_DESCRIPTOR,
CREATE_DESCRIPTOR, RECEIVE_DESCRIPTOR,
} from 'constants/descriptors';
import * as update from 'immutability-helper';
import {DescriptorList, Descriptor} from 'model/descriptors.model';
const INITIAL_STATE = {
items: [],
byId: {},
isEditing: false,
currentDescriptor: null,
};
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
return self.indexOf(value) === index;
}
function descriptors(state = INITIAL_STATE, action: { type?: string, descriptor?: Descriptor } = {type: ''}) {
function descriptors(state = INITIAL_STATE, action: IReducerAction = { type: '' }) {
const dl = action.descriptor;
const arr: { [key: string]: Descriptor; } = {};
let items;
switch (action.type) {
switch (action.type) {
case CREATE_DESCRIPTOR: {
return update(state, {
currentDescriptor: { $set: new Descriptor() },
});
}
case RECEIVE_DESCRIPTOR: {
arr[dl.uuid] = dl;
items = state.items.slice();
items.push(dl.uuid);
return update(state, {
byId: {$merge: arr},
items: {$set: items.filter(onlyUnique)},
});
}
default:
return state;
case RECEIVE_DESCRIPTOR: {
console.log(RECEIVE_DESCRIPTOR, action);
return update(state, {
currentDescriptor: { $set: action.payload },
});
}
default:
return state;
}
}
export default descriptors;
import {Descriptor} from 'model/descriptors.model';
import {Page} from 'model/common.model';
import {MY_DESCRIPTORS_LIST_URL, GET_DESCRIPTOR_URL, CREATE_DESCRIPTOR_URL, UPDATE_DESCRIPTOR_URL, PUBLISH_DESCRIPTOR_URL, GET_DESCRIPTORS_WITH_FILTER_URL, GET_DESCRIPTOR_CATEGORIES_URL} from 'constants/apiURLS';
import {MY_DESCRIPTORS_LIST_URL, LIST_DESCRIPTORS_URL, GET_DESCRIPTOR_URL, CREATE_DESCRIPTOR_URL, UPDATE_DESCRIPTOR_URL, PUBLISH_DESCRIPTOR_URL, GET_DESCRIPTOR_CATEGORIES_URL} from 'constants/apiURLS';
import authenticatedRequest from 'utilities/requestUtils';
import {IDescriptorFilter} from 'model/filter.model';
......@@ -35,7 +35,7 @@ export class DescriptorService {
const sortParam = sortBy ? `&s=${sortBy}` : '';
return authenticatedRequest(token, {
url: `${GET_DESCRIPTORS_WITH_FILTER_URL}?p=${page}&l=${results}&d=${order}${sortParam}`,
url: `${LIST_DESCRIPTORS_URL}?p=${page}&l=${results}&d=${order}${sortParam}`,
method: 'POST',
data: {
...filter,
......@@ -67,8 +67,13 @@ export class DescriptorService {
public static updateDescriptor(token: string, descriptor: Descriptor): Promise<Descriptor> {
console.log('Update descriptor', descriptor);
if (descriptor.owner) {
console.log('WARNING: Descriptor.owner is removed for updates');
delete descriptor.owner;
}
return authenticatedRequest(token, {
url: `${UPDATE_DESCRIPTOR_URL}/${descriptor.uuid},${descriptor.version}`,
url: `${UPDATE_DESCRIPTOR_URL}`,
method: 'POST',
data: {
...descriptor,
......
......@@ -51,13 +51,13 @@ function DescriptorLink({
}: IDescriptorLinkProps) {
if (edit) {
return (
<Link to={ `/descriptors/${descriptor.uuid}/edit` }>
<Link to={ `/descriptor/${descriptor.uuid}/edit` }>
{ children }
</Link>
);
} else {
return (
<Link to={ `/descriptors/${descriptor.uuid}` }>
<Link to={ `/descriptor/${descriptor.uuid}` }>
{ children }
</Link>
);
......
......@@ -5,7 +5,7 @@ import Grid from 'material-ui/Grid';
import {bindActionCreators} from 'redux';
import {createDatasetRequest, listMyDatasets} from 'actions/dataset';
import {listMyDescriptors} from 'actions/descriptors';
import {listMyDescriptors, createDescriptor} from 'actions/descriptors';
import {listMyDescriptorLists, createDescriptorList} from 'actions/descriptorList';
import {IDatasetFilter, IDescriptorFilter} from 'model/filter.model';
import {Page} from 'model/common.model';
......@@ -47,7 +47,7 @@ class AdministrationDashboard extends BaseMyDataPage {
}
public goToNewDescriptorForm = () => {
this.props.router.push('/descriptors/create');
this.props.router.push('/descriptor/edit');
}
public goToDataInPreparation = () => {
......@@ -64,7 +64,7 @@ class AdministrationDashboard extends BaseMyDataPage {
public render() {
const {classes, tab, datasets, descriptors, descriptorLists, createDescriptorList} = this.props;
const {classes, tab, datasets, descriptors, descriptorLists, createDescriptor, createDescriptorList} = this.props;
let paged = datasets;
......@@ -84,7 +84,7 @@ class AdministrationDashboard extends BaseMyDataPage {
<DashboardButton title="CREATE DATASET" onClickFunction={ this.goToNewDataset }/>
</Grid>
<Grid item xs={ 6 } lg={ 3 }>
<DashboardButton title="CREATE DESCRIPTOR" onClickFunction={ this.goToNewDescriptorForm }/>
<DashboardButton title="CREATE DESCRIPTOR" onClickFunction={ createDescriptor }/>
</Grid>
<Grid item xs={ 6 } lg={ 3 }>
<DashboardButton title="CREATE DESCRIPTOR LIST" onClickFunction={ createDescriptorList }/>
......@@ -125,6 +125,7 @@ const mapStateToProps = (state, ownProps) => ({
const mapDispatchToProps = (dispatch) => bindActionCreators({
createDatasetRequest,
createDescriptor,
createDescriptorList,
listDatasets: listMyDatasets,
listDescriptors: listMyDescriptors,
......
import * as React from 'react';
const connect = require('react-redux').connect;
import {withStyles} from 'material-ui/styles';
import {createDescriptor} from 'actions/descriptors';
import {loadPartners} from 'actions/partner';
import DescriptorForm from './DescriptorForm';
import {Descriptor} from 'model/descriptors.model';
import {Partner} from 'model/partner.model';
import {IPartnerFilter} from 'model/filter.model';
interface ICreateDescriptorPageProps extends React.ClassAttributes<any> {
classes: any;
createDescriptor: (descriptor: Descriptor) => void;
loadPartners: (page?: number, results?: number, sortBy?: string, filter?: IPartnerFilter) => void;
partners: Partner[];
}
const styles = (theme) => ({
root: {
width: '100%',
},
});
class CreateDescriptorPage extends React.Component<ICreateDescriptorPageProps, any> {
public componentDidMount() {
if (this.props.partners.length === 0) {
console.log('Fetching partners');
this.props.loadPartners(0, 50, null, {});
}
}
public onSave = (e) => {
// todo add partner select to page
if ( this.props.partners.length > 0 ) {
const partnerCreated = { ...e };
partnerCreated['owner'] = {id: this.props.partners[0].id};// tslint:disable-line
console.log('descriptorCreated', partnerCreated);
this.props.createDescriptor(partnerCreated);
} else {
alert('Please create partner');
}
}
public render() {
const {classes} = this.props;
return (
<div>
<DescriptorForm onSubmit={ this.onSave } />
</div>
);
};
}
function mapStateToProps(state, ownProps) {
return {
partners: state.partners.items.map((uuid) => state.partners.byId[uuid]),
};
}
function mapDispatchToProps(dispatch) {
return {
createDescriptor: (descriptor: Descriptor): void => dispatch(createDescriptor(descriptor)),
loadPartners: (page?: number, results?: number, sortBy?: string, filter?: IPartnerFilter): void => dispatch(loadPartners(page, results, sortBy, filter)),
};
}
export default connect(
mapStateToProps,
mapDispatchToProps,
)((withStyles as any)(styles)(CreateDescriptorPage));
import * as React from 'react';
import {connect} from 'react-redux';
import {withStyles} from 'material-ui/styles';
import {loadDescriptor, createDescriptor, updateDescriptor, publishDescriptor} from 'actions/descriptors';
import {loadPartners} from 'actions/partner';
import DescriptorForm from './DescriptorForm';
import {Descriptor} from 'model/descriptors.model';
import {Partner} from 'model/partner.model';
import {IPartnerFilter} from 'model/filter.model';
interface IDescriptorPageProps extends React.ClassAttributes<any> {
classes: any;
params: any;
appMounted: boolean;
loadDescriptor: (uuid: string) => void;
createDescriptor: (descriptor: Descriptor) => void;
updateDescriptor: (descriptor: Descriptor) => void;
publishDescriptor: (descriptor: Descriptor) => void;
loadPartners: (page?: number, results?: number, sortBy?: string, filter?: IPartnerFilter) => void;
partners: Partner[];
descriptor: Descriptor;
}
const styles = (theme) => ({
root: {
width: '100%',
},
});
class DescriptorPage extends React.Component<IDescriptorPageProps, any> {
public componentDidMount() {
const {descriptor, partners, appMounted, loadDescriptor, params: {uuid}} = this.props;
if (!descriptor && uuid) {
loadDescriptor(uuid);
}
if (!partners || partners.length === 0) {
this.props.loadPartners(0, 50, null, {});
}
/*if (this.props.descriptor === undefined || this.props.descriptor === null) {
this.props.descriptor = new Descriptor();
}*/
}
public onSave = (e) => {
const {partners, createDescriptor, updateDescriptor} = this.props;
// todo add partner select to page
if ( partners.length > 0 ) {
const updatedDescriptor = { ...e };
updatedDescriptor['owner'] = {id: partners[0].id};// tslint:disable-line
// console.log('descriptorCreated', updatedDescriptor);
updatedDescriptor.id > 0 ? updateDescriptor(updatedDescriptor) : createDescriptor(updatedDescriptor);
} else {
alert('Please create a partner');
}
}
public onPublish = (e) => {
const {descriptor, publishDescriptor} = this.props;
console.log('Publishing descriptor', descriptor);
publishDescriptor(descriptor);
}
public render() {
const {classes, descriptor} = this.props;
return (
<div>
<DescriptorForm initialValues={ descriptor } onPublish={ this.onPublish } onSubmit={ this.onSave }/>
</div>
);
};
}
function mapStateToProps(state, ownProps) {
return {
partners: state.partners.items.map((uuid) => state.partners.byId[uuid]),
descriptor: state.descriptors.byId[ownProps.params.uuid],
appMounted: state.appMounted,
};
}
function mapDispatchToProps(dispatch) {
return {
loadDescriptor: (uuid: string): void => dispatch(loadDescriptor(uuid)),
updateDescriptor: (descriptor: Descriptor): void => dispatch(updateDescriptor(descriptor)),
createDescriptor: (descriptor: Descriptor): void => dispatch(createDescriptor(descriptor)),
publishDescriptor: (descriptor: Descriptor): void => dispatch(publishDescriptor(descriptor)),
loadPartners: (page?: number, results?: number, sortBy?: string, filter?: IPartnerFilter): void => dispatch(loadPartners(page, results, sortBy, filter)),
};
}
export default connect(
mapStateToProps,
mapDispatchToProps,
)((withStyles as any)(styles)(DescriptorPage));
import * as React from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import {withStyles} from 'material-ui/styles';
import {loadDescriptor, saveDescriptor, publishDescriptor} from 'actions/descriptors';
import {loadMyPartners} from 'actions/partner';
import {Descriptor} from 'model/descriptors.model';
import {Partner} from 'model/partner.model';
import DescriptorForm from './c/DescriptorForm';
interface IDescriptorEditPageProps extends React.ClassAttributes<any> {
classes: any;
uuid?: string;
loadDescriptor: (uuid: string) => void;
saveDescriptor: (descriptor: Descriptor) => void;
publishDescriptor: (descriptor: Descriptor) => void;
loadMyPartners: () => void;
myPartners: Partner[];
descriptor: Descriptor;
}
const styles = (theme) => ({
root: {
width: '100%',
},
});
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();
}
if (!descriptor && uuid) {
loadDescriptor(uuid);
}
}
public onSave = (updatedDescriptor: Descriptor) => {
const {saveDescriptor, myPartners} = this.props;
console.log('onSave', updatedDescriptor);
if (updatedDescriptor.uuid && updatedDescriptor.version) {
delete updatedDescriptor.owner;
}
// console.log('Saving descriptor', updatedDescriptor);
saveDescriptor(updatedDescriptor);
}
public onPublish = (e) => {
const {descriptor, publishDescriptor} = this.props;
console.log('Publishing descriptor', descriptor);
publishDescriptor(descriptor);
}
public render() {
const {classes, uuid, myPartners} = this.props;
let {descriptor} = this.props;
if (! descriptor && ! uuid) {