application/redux/actions/container-actions.js
import BaseService from './../../services/base-service';
import {parseSrn} from './../../util/strings';
class ContainerService extends BaseService {
fetch(opts) {
return this.get('/v3/container', opts);
}
fetchOne(id) {
return this.get('/v3/container/' + id);
}
create(data) {
return this.post('/v3/container', data);
}
update(id, data) {
return this.put('/v3/container/' + id, data);
}
remove(id) {
return this.delete('/v3/container/' + id);
}
checkout(srn) {
return this.post('/v3/lock-request', {'srn': srn});
}
checkin(uuid, lockUuid) {
return this.delete('/v3/lock-request/' + lockUuid);
}
}
const service = new ContainerService;
/**
* Action types
*/
export const CONTAINERS_REQUEST = 'CONTAINERS_REQUEST';
export const CONTAINERS_RECEIVE = 'CONTAINERS_RECEIVE';
export const CONTAINER_RECEIVE = 'CONTAINER_RECEIVE';
export const CONTAINER_SELECTED = 'CONTAINER_SELECTED';
export const CONTAINERS_INVALIDATE = 'CONTAINERS_INVALIDATE';
export const CONTAINERS_RECEIVE_PAGINATION = 'CONTAINERS_RECEIVE_PAGINATION';
/**
* Action creators
*/
export function containersReceivePagination(pagination) {
return {
type: CONTAINERS_RECEIVE_PAGINATION,
payload: {
before: pagination.get('before'),
current: pagination.get('current'),
first: pagination.get('first'),
last: pagination.get('last'),
limit: pagination.get('limit'),
next: pagination.get('next'),
total_items: pagination.get('total_items'),
total_pages: pagination.get('total_pages'),
}
};
}
export function containersRequest(params = {}) {
return {
type: CONTAINERS_REQUEST,
payload: {
params: params
}
};
}
export function containersReceive(items) {
return {
type: CONTAINERS_RECEIVE,
payload: {
containers: items ? items : []
}
};
}
export function containerReceive(item) {
return {
type: CONTAINER_RECEIVE,
payload: {
container: item ? item : {}
}
};
}
export function containerSetSelected(container) {
return {
type: CONTAINER_SELECTED,
payload: {
selected: container ? container : false
}
};
}
export function containersInvalidate() {
return {
type: CONTAINERS_INVALIDATE,
payload: {
}
};
}
export function containersShouldFetch(state) {
const containers = state.containers;
if (!containers.items.length) {
return true;
} else if (containers.isFetching) {
return false;
} else if (containers.didInvalidate) {
return true;
}
return containers.didInvalidate;
}
export function containersMaybeFetch(params = {}) {
return function(dispatch, getState) {
const containers = getState().containers;
if (!containers.items || !containers.items.size || containers.didInvalidate) {
return dispatch(containersFetch(params));
}
return Promise.resolve();
}
};
export function containersFetch(params = {}) {
return function(dispatch, getState) {
dispatch(containersRequest(params));
params.per_page = params.per_page ? params.per_page : 20;
params.order = params.order ? params.order : "title";
params.dir = params.dir ? params.dir : "asc";
return service.fetch(params)
.then(response => {
dispatch(containersReceivePagination(response));
return dispatch(containersReceive(response.get('items')));
});
};
}
export function containerFetchOne(id) {
return function (dispatch, getState) {
dispatch(containersRequest({}));
return service.fetchOne(id)
.then(response => {
return dispatch(containerReceive(response.first()));
});
};
}
export function containerCreate(container = {}) {
return function(dispatch, getState) {
return service.create(container)
.then(response => {
dispatch(containerSetSelected(response.first().get('uuid')));
console.log(response.first().get('uuid'));
return dispatch(containerReceive(response.first()));
});
}
}
export function containerUpdate(id, container = {}) {
return function(dispatch, getState) {
return service.update(id, container)
.then(response => {
return dispatch(containerReceive(response.first()));
});
}
}
export function containersRemove(uuids) {
return function(dispatch, getState) {
dispatch(containersInvalidate());
return service.remove(uuids);
}
}
export function containersCheckOut(srn) {
return function(dispatch, getState) {
return service.checkout(srn)
.then((data) => {
return parseSrn(srn);
});
}
}
export function containersCheckIn(uuid, lockUuid) {
return function(dispatch, getState) {
return service.checkin(uuid, lockUuid);
}
}