Commit 597d737f authored by Matija Obreza's avatar Matija Obreza
Browse files

Defined actions and reducers for Descriptor Lists

- store.descriptorList is the root for Descriptor Lists
parent 47e2ba55
import { push } from 'react-router-redux';
import { SubmissionError } from 'redux-form';
import { dereferenceReferences } from 'utilities';
import { DescriptorListService } from 'service/DescriptorListService';
import { DescriptorList } from 'model/descriptors.model';
import { Partner } from 'model/partner.model';
import { Page } from 'model/common.model';
import {
GET_DESCRIPTORLIST, RECEIVE_DESCRIPTORLIST,
CREATE_DESCRIPTORLIST, EDIT_DESCRIPTORLIST,
SAVE_DESCRIPTORLIST, DELETE_DESCRIPTORLIST, PUBLISH_DESCRIPTORLIST,
LIST_DESCRIPTORLISTS, RECEIVE_DESCRIPTORLISTS,
} from 'constants/descriptors';
const receiveDescriptorList = (descriptorList: DescriptorList) => ({
type: RECEIVE_DESCRIPTORLIST,
descriptorList,
});
const receiveDescriptorLists = (paged: Page<DescriptorList>) => ({
type: RECEIVE_DESCRIPTORLISTS,
paged,
});
// Just load a descriptor list
export const loadDescriptorList = (uuid: string) => (dispatch, getState) => {
console.log('Loading descriptor list', uuid);
const token = getState().login.access_token;
return DescriptorListService.loadDescriptorList(token, uuid)
// receive the current descriptor list
.then((descriptorList) => {
return dispatch(receiveDescriptorList(descriptorList));
}).catch((error) => {
console.log(`No descriptor list with uuid ${uuid}`, error);
});
};
// List current user's descriptor lists
export const listMyDescriptorLists = (page?, results?, sortBy?, filter?, order?) => (dispatch, getState) => {
console.log('Loading my descriptor lists');
const token = getState().login.access_token;
return DescriptorListService.listMyDescriptorLists(token, page, results, sortBy, filter, order)
// receive the current descriptor list
.then((descriptorLists) => {
return dispatch(receiveDescriptorLists(descriptorLists));
}).catch((error) => {
console.log(`Error loading my descriptor lists`, error);
});
};
// List published descriptor lists
export const listDescriptorLists = (page?, results?, sortBy?, filter?, order?) => (dispatch, getState) => {
console.log('Loading published descriptor lists');
const token = getState().login.access_token;
return DescriptorListService.listDescriptorLists(token, page, results, sortBy, filter, order)
// receive the current descriptor list
.then((descriptorLists) => {
return dispatch(receiveDescriptorLists(descriptorLists));
}).catch((error) => {
console.log(`Error loading published descriptor lists`, error);
});
};
// Create a new descriptor list
export const createDescriptorList = () => (dispatch) => {
dispatch({ type: CREATE_DESCRIPTORLIST });
return dispatch(push(`/descriptorlist/edit`));
};
// Edit an existing descriptor list
export const editDescriptorList = (uuid: string) => (dispatch, getState) => {
return dispatch(loadDescriptorList(uuid))
.then((descriptorList) => dispatch(push(`/descriptorlist/${descriptorList.uuid}/edit`)));
};
// Save the descriptor list
export const saveDescriptorList = (descriptorList: DescriptorList) => (dispatch, getState) => {
console.log('Saving descriptor list', descriptorList);
const createOrSave = descriptorList.version ? DescriptorListService.updateDescriptorList : DescriptorListService.createDescriptorList;
return createOrSave(getState().login.access_token, descriptorList)
// when response is back
.then((descriptorList) => {
// receive the updated descriptor list
dispatch(receiveDescriptorList(descriptorList));
// and redirect to proper edit page
return dispatch(push(`/descriptorlist/${descriptorList.uuid}/edit`));
}).catch((error) => {
console.log(`Error saving descriptor list`, error, descriptorList);
});
};
// Publish the descriptor list
export const publishDescriptorList = (descriptorList: DescriptorList, published?: boolean) => (dispatch, getState) => {
return DescriptorListService.publishDescriptorList(getState().login.access_token, descriptorList, published)
.then((descriptorList) => {
// receive updates
dispatch(receiveDescriptorList(descriptorList));
// go to the published descriptor list page
dispatch(push(`/descriptorlist/${descriptorList.uuid}`));
});
};
export const CREATE_DESCRIPTOR_LIST = 'App/CREATE_DESCRIPTOR_LIST_URL';
export const EDIT_DESCRIPTOR_LIST = 'App/EDIT_DESCRIPTOR_LIST_URL';
export const FINISH_EDIT_DESCRIPTOR_LIST = 'App/FINISH_EDIT_DESCRIPTOR_LIST_URL';
export const DELETE_DESCRIPTOR_LIST = 'App/DELETE_DESCRIPTOR_LIST_URL';
export const RECEIVE_DESCRIPTOR_LIST = 'App/RECEIVE_DESCRIPTOR_LIST';
export const LIST_DESCRIPTOR_LIST = 'App/LIST_DESCRIPTOR_LIST';
// Descriptors
export const ADD_DESCRIPTOR = 'App/ADD_DESCRIPTOR';
export const PUBLISH_DESCRIPTOR = 'App/PUBLISH_DESCRIPTOR';
export const DESCRIPTOR_LIST_FORM = 'Form/DESCRIPTOR_LIST_FORM';
export const RECEIVE_DESCRIPTOR_LIST_PAGE = 'App/RECEIVE_DESCRIPTOR_LIST_PAGE';
export const RECEIVE_DESCRIPTOR = 'App/RECEIVE_DESCRIPTOR';
export const RECEIVE_DESCRIPTOR_PAGE = 'App/RECEIVE_DESCRIPTOR_PAGE';
export const DESCRIPTOR_FORM = 'Form/DESCRIPTOR_FORM';
export const RECEIVE_DESCRIPTOR_PAGE = 'App/RECEIVE_DESCRIPTOR_PAGE';
// Descriptor lists
export const GET_DESCRIPTORLIST = 'App/GET_DESCRIPTORLIST';
export const RECEIVE_DESCRIPTORLIST = 'App/RECEIVE_DESCRIPTORLIST';
export const CREATE_DESCRIPTORLIST = 'App/CREATE_DESCRIPTORLIST';
export const EDIT_DESCRIPTORLIST = 'App/EDIT_DESCRIPTORLIST';
export const DESCRIPTORLIST_FORM = 'Form/DESCRIPTORLIST';
export const SAVE_DESCRIPTORLIST = 'App/SAVE_DESCRIPTORLIST';
export const DELETE_DESCRIPTORLIST = 'App/DELETE_DESCRIPTORLIST';
export const PUBLISH_DESCRIPTORLIST = 'App/DELETE_DESCRIPTORLIST';
export const LIST_DESCRIPTORLISTS = 'App/LIST_DESCRIPTORLISTS';
export const RECEIVE_DESCRIPTORLISTS = 'App/RECEIVE_DESCRIPTORLISTS';
/*
// Descriptor list actions
import {
GET_DESCRIPTORLIST, RECEIVE_DESCRIPTORLIST,
CREATE_DESCRIPTORLIST, EDIT_DESCRIPTORLIST,
SAVE_DESCRIPTORLIST, DELETE_DESCRIPTORLIST, PUBLISH_DESCRIPTORLIST,
LIST_DESCRIPTORLISTS, RECEIVE_DESCRIPTORLISTS,
} from 'constants/descriptors';
*/
......@@ -58,4 +58,12 @@ export interface IDescriptorFilter extends IAuditedVersionedModelFilter {
description?: IStringFilter;
integerOnly?: boolean;
uom?: IStringFilter;
}
\ No newline at end of file
}
export interface IDescriptorListFilter extends IAuditedVersionedModelFilter {
published?: boolean;
title?: IStringFilter;
description?: IStringFilter;
crop?: string[];
bibliographicCitation?: IStringFilter;
}
import * as update from 'immutability-helper';
import { DescriptorList, Descriptor } from 'model/descriptors.model';
import { Page } from 'model/common.model';
import {
GET_DESCRIPTORLIST, RECEIVE_DESCRIPTORLIST,
CREATE_DESCRIPTORLIST, EDIT_DESCRIPTORLIST,
SAVE_DESCRIPTORLIST, DELETE_DESCRIPTORLIST, PUBLISH_DESCRIPTORLIST,
LIST_DESCRIPTORLISTS, RECEIVE_DESCRIPTORLISTS,
} from 'constants/descriptors';
const INITIAL_STATE = {
currentDescriptorList: null,
paged: null,
};
function descriptorList(state = INITIAL_STATE, action: { type?: string, descriptorList?: DescriptorList, paged?: Page<DescriptorList> } = { type: '' }) {
switch (action.type) {
// set the currentDescriptorList to whatever came in
case RECEIVE_DESCRIPTORLIST: {
return update(state, {
currentDescriptorList: { $set: action.descriptorList },
});
}
// set the currentDescriptorList to a blank new object
case CREATE_DESCRIPTORLIST: {
return update(state, {
currentDescriptorList: { $set: new DescriptorList() },
});
}
// set the paged to whatever came in
case RECEIVE_DESCRIPTORLISTS: {
return update(state, {
paged: { $set: action.paged },
});
}
default:
return state;
}
}
export default descriptorList;
import {
RECEIVE_DESCRIPTOR_LIST,
DELETE_DESCRIPTOR_LIST, EDIT_DESCRIPTOR_LIST, FINISH_EDIT_DESCRIPTOR_LIST, RECEIVE_DESCRIPTOR,
RECEIVE_DESCRIPTOR,
} from 'constants/descriptors';
import * as update from 'immutability-helper';
import {DescriptorList, Descriptor} from 'model/descriptors.model';
......
......@@ -2,6 +2,7 @@ import {combineReducers} from 'redux';
import { reducer as formReducer } from 'redux-form';
import { routerReducer } from 'react-router-redux';
import descriptors from './descriptors';
import descriptorList from './descriptorList';
import creators from './creator';
import partners from './partners';
import login from './login';
......@@ -15,6 +16,7 @@ import appMounted from './appMounted';
const rootReducer = combineReducers({
descriptors,
descriptorList,
partners,
login,
serverInfo,
......
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