application/redux/actions/entry-actions.js
import BaseService from './../../services/base-service';
class EntryService extends BaseService {
fetch(opts) {
return this.get('/v3/entry', opts);
}
fetchOne(id) {
return this.get('/v3/entry/' + id);
}
create(data) {
return this.post('/v3/entry', data);
}
update(id, data) {
return this.put('/v3/entry/' + id, data);
}
remove(uuid) {
return this.delete('/v3/entry/' + uuid);
}
}
const service = new EntryService;
/**
* Action types
*/
export const ENTRIES_REQUEST = 'ENTRIES_REQUEST';
export const ENTRIES_RECEIVE = 'ENTRIES_RECEIVE';
export const ENTRY_RECEIVE = 'ENTRY_RECEIVE';
export const ENTRY_SELECTED = 'ENTRY_SELECTED';
export const ENTRIES_INVALIDATE = 'ENTRIES_INVALIDATE';
export const ENTRIES_RECEIVE_PAGINATION = 'ENTRIES_RECEIVE_PAGINATION';
/**
* Action creators
*/
export function entriesReceivePagination(pagination) {
return {
type: ENTRIES_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 entriesRequest(params = {}) {
return {
type: ENTRIES_REQUEST,
payload: {
params: params
}
};
}
export function entriesReceive(items) {
return {
type: ENTRIES_RECEIVE,
payload: {
entries: items ? items : []
}
};
}
export function entryReceive(entry) {
return {
type: ENTRY_RECEIVE,
payload: {
entry: entry ? entry : {}
}
};
}
export function entrySetSelected(entry) {
return {
type: ENTRY_SELECTED,
payload: {
selected: entry ? entry : false
}
};
}
export function entriesInvalidate() {
return {
type: ENTRIES_INVALIDATE,
payload: {
}
};
}
export function entryUpdate(id, data) {
return function(dispatch, getState) {
dispatch(entriesRequest({}));
return service.update(id, data)
.then(response => {
return dispatch(entryReceive(response.first()));
});
}
}
export function entryCreate(data) {
return function(dispatch, getState) {
dispatch(entriesRequest({}));
return service.create(data)
.then(response => {
dispatch(entrySetSelected(response.first().get('uuid')));
return dispatch(entryReceive(response.first()));
});
}
}
export function entriesShouldFetch(state) {
const entries = state.entries;
if (!entries.items.length) {
return true;
} else if (entries.isFetching) {
return false;
}
return entries.didInvalidate;
}
export function entriesMaybeFetch(params = {}) {
return function(dispatch, getState) {
const entries = getState().entries.items;
if (getState().entries.isFetching) {
return Promise.resolve();
}
if (!entries.items || !entries.items.size) {
return dispatch(entriesFetch(params));
}
return Promise.resolve();
}
};
export function entriesFetch(params = {}) {
return function(dispatch, getState) {
dispatch(entriesRequest(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";
// default page
let page = 0;
if (params.page) {
// specific page is requested
page = params.page;
} else if (getState().entries.pagination.current) {
// existing pagination
page = getState().entries.pagination.current;
}
if (page) {
params.page = page;
}
return service.fetch(params)
.then(response => {
dispatch(entriesReceivePagination(response));
return dispatch(entriesReceive(response.get('items')));
});
};
}
export function entriesFetchNoSideEffect(params = {}) {
const baseParams = {
per_page: 20,
page: 1,
order: 'title',
dir: 'asc'
};
const myParams = Object.assign(baseParams, params);
return service.fetch(myParams)
.then(response => {
return response.get('items');
});
}
export function entriesFetchOne(id) {
return function (dispatch, getState) {
dispatch(entriesRequest({}));
return service.fetchOne(id)
.then(response => {
return dispatch(entryReceive(response.first()));
});
}
}
export function entriesRemove(uuid) {
return function (dispatch, getState) {
dispatch(entriesInvalidate());
return service.remove(uuid);
}
}