application/redux/actions/acl-actions.js
import BaseService from './../../services/base-service';
class AclService extends BaseService {
fetch(opts) {
return this.get('/v3/acl', opts);
}
fetchOne(id) {
return this.get('/v3/acl/' + id);
}
fetchResources() {
return this.get('/v3/acl/resources');
}
update(acl) {
return this.put('/v3/acl/' + acl.name, acl);
}
remove(names) {
return this.delete('/v3/acl/' + names);
}
}
const service = new AclService;
/**
* Action types
*/
export const ACLS_REQUEST = 'ACLS_REQUEST';
export const ACLS_RECEIVE = 'ACLS_RECEIVE';
export const ACL_RECEIVE = 'ACL_RECEIVE';
export const ACL_SELECTED = 'ACL_SELECTED';
export const ACLS_INVALIDATE = 'ACLS_INVALIDATE';
export const ACLS_RESOURCES_RECEIVE = 'ACLS_RESOURCES_RECEIVE';
export const ACLS_SET_PAGE = 'ACLS_SET_PAGE';
/**
* Action creators
*/
export function aclsSetPage(page = 0) {
return {
type: ACLS_SET_PAGE,
payload: {
page: page
}
};
}
export function aclsRequest(params = {}) {
return {
type: ACLS_REQUEST,
payload: {
params: params
}
};
}
export function aclsReceive(items) {
return {
type: ACLS_RECEIVE,
payload: {
acls: items ? items : []
}
};
}
export function aclsResourcesReceive(resources) {
return {
type: ACLS_RESOURCES_RECEIVE,
payload: {
resources: resources
}
}
}
export function aclReceive(acl) {
return {
type: ACL_RECEIVE,
payload: {
acl: acl ? acl : {}
}
};
}
export function aclSetSelected(acl) {
return {
type: ACL_SELECTED,
payload: {
selected: acl ? acl : false
}
};
}
export function aclsInvalidate() {
return {
type: ACLS_INVALIDATE,
payload: {
}
};
}
export function aclsShouldFetch(state) {
const acls = state.acls;
if (!acls.items.length) {
return true;
} else if (acls.isFetching) {
return false;
}
return acls.didInvalidate;
}
export function aclsMaybeFetch(params = {}) {
return function(dispatch, getState) {
const acls = getState().acls.items;
if (getState().acls.isFetching) {
return Promise.resolve();
}
if (!acls.items || !acls.items.size) {
return dispatch(aclsFetch(params));
}
return Promise.resolve();
}
};
export function aclsFetch(params = {}) {
return function(dispatch, getState) {
dispatch(aclsRequest(params));
params.per_page = params.per_page ? params.per_page : 20;
params.order = params.order ? params.order : "name";
params.dir = params.dir ? params.dir : "asc";
return service.fetch(params)
.then(response => {
return dispatch(aclsReceive(response));
});
};
}
export function aclsFetchOne(id) {
return function (dispatch, getState) {
dispatch(aclsRequest({}));
return service.fetchOne(id)
.then(response => {
return dispatch(aclReceive(response.first()));
});
};
}
export function aclsFetchResources() {
return function(dispatch, getState) {
return service.fetchResources()
.then(response => {
return dispatch(aclsResourcesReceive(response.toJS()));
});
}
}
export function aclUpdate(acl = {}) {
return function(dispatch, getState) {
return service.update(acl)
.then(response => {
return dispatch(aclReceive(response.first()));
});
}
}
export function aclsRemove(names) {
return function(dispatch, getState) {
dispatch(aclsInvalidate());
return service.remove(names);
}
}