application/services/container-service.js
import Immutable from 'immutable';
import alt from './../alt';
import Alt from 'alt';
import BaseService from './base-service';
import {ErrorService} from './error-service';
import {parseSrn} from './../util/strings';
import {info} from './../util/console';
/**
* Interact with container API service
*/
class ContainerService extends BaseService {
updateContainer(container) {
return container;
}
updateContainers(container) {
return container;
}
updatePagination(pagination) {
return pagination;
}
/**
* Remove (delete) container item
* @param {string} id
*/
remove(id) {
return (dispatch) => {
return this.delete('/v3/container/' + id)
.then(() => {
info(arguments);
})
.catch((e) => {
ErrorService.add('error', 'Unable to delete container, usually this means you don\'t have permission to remove items.');
});
};
}
/**
* Update existing container item
* @param {string} id UUID
* @param {object} data Hash of container data
* @param {Function} callback
*/
update(id, data, callback) {
return (dispatch) => {
return this.put('/v3/container/' + id, data)
.then((data) => {
info('Update returned', data);
this.updateContainer(data.first());
if (callback) {
callback(data.first());
}
})
.catch((e) => {
ErrorService.add('error', 'Unable to save container, usually this means you don\'t have it checked out.');
});
}
}
/**
* Create new container item
* @param {object} data Hash of content data
* @param {Function} callback
*/
create(data, callback) {
return (dispatch) => {
return this.post('/v3/container/', data)
.then((data) => {
info('Create returned', data);
this.updateContainer(data.first());
if (callback) {
callback(data.first());
}
})
.catch((e) => {
ErrorService.add('error', 'Unable to save container, usually this means you don\'t have it checked out.');
});
};
}
/**
* Attempt to obtain a container lock, then update the container store
* @param {string} srn valid SRN
*/
checkoutAndFetch(srn) {
return (dispatch) => {
return this.post('/v3/lock-request', {'srn': srn})
.then((data) => {
info('Checkout and Fetch returned', data);
const parsedSrn = parseSrn(srn);
this.fetchOne(parsedSrn.uuid, {}, false);
});
};
}
/**
* Attempt to release a container lock, then update container store
* @param {string} containerId container UUID
* @param {string} lockId Lock UUID
*/
checkinAndFetch(containerId, lockId) {
return (dispatch) => {
return this.delete('/v3/lock-request/' + lockId)
.then((data) => {
info('Checkin and fetch returned', data);
this.fetchOne(containerId, {}, false);
});
};
}
/**
* Fetch single container update
* @param {string} id UUID
* @param {object} opts Hash of query options
* @param {Boolean} forceUpdate If true (default) force update to container store, otherwise, don't
*/
fetchOne(id, opts, forceUpdate = true) {
if (forceUpdate === true) {
this.updateContainer([]);
}
return (dispatch) => {
return this.get('/v3/container/' + id, opts)
.then((data) => {
info('FetchOne returned', data);
this.updateContainer(data.first());
});
};
}
/**
* Fetch paginated container items
* @param {object} opts Hash of query options
*/
fetch(opts) {
return (dispatch) => {
return this.get('/v3/container', opts)
.then((data) => {
info('Fetch returned', data);
this.updatePagination(Immutable.fromJS({
'first': data.get('first'),
'before': data.get('before'),
'current': data.get('current'),
'last': data.get('last'),
'next': data.get('next'),
'total_pages': data.get('total_pages'),
'total_items': data.get('total_items'),
'limit': data.get('limit')
}));
if (data.get('items').size) {
this.updateContainers(data.get('items'))
} else {
this.updateContainers(Immutable.fromJS([]));
}
});
};
}
/**
* Run a query on the search endpoint. See search docs for query info
* @param {object} opts Query has
*/
search(opts) {
return (dispatch) => {
return this.get('/v3/search', opts)
.then((data) => {
info('Search returned', data);
this.updatePagination(Immutable.fromJS({
'first': data.get('first'),
'before': data.get('before'),
'current': data.get('current'),
'last': data.get('last'),
'next': data.get('next'),
'total_pages': data.get('total_pages'),
'total_items': data.get('total_items'),
'limit': data.get('limit')
}));
this.updateContents(data.get('items'))
});
};
}
}
const service = alt.createActions(ContainerService);
/**
* Container Flux store. Tracks
* <ul>
* <li>container</li>
* <li>containers</li>
* <li>pagination</li>
* </ul>
*/
class ContainersStore {
constructor() {
this.state = {
'container': Immutable.Map(),
'containers': Immutable.Map(),
'pagination': Immutable.Map()
};
this.bindListeners({
'handleUpdateContainer': service.UPDATE_CONTAINER,
'handleUpdateContainers': service.UPDATE_CONTAINERS,
'handleUpdatePagination': service.UPDATE_PAGINATION
});
}
handleUpdateContainer(container) {
this.setState({'container': container});
}
handleUpdateContainers(container) {
this.setState({'containers': container});
}
handleUpdatePagination(pagination) {
this.setState({'pagination': pagination});
}
}
const store = alt.createStore(ContainersStore, 'ContainersStore');
/**
* Instace wrapper if you don't or can't use the singleton action and classes.
*
* Actions and store are both under the 'containers' key.
*/
class ContainerFlux extends Alt {
constructor(config = {}) {
super(config);
this.addActions('containers', ContainerService);
this.addStore('containers', ContainersStore);
}
}
export {ContainerFlux as default, service as ContainerService, store as ContainersStore};