application/redux/actions/tag-actions.js
import BaseService from './../../services/base-service';
class TagService extends BaseService {
fetch(opts) {
return this.get('/v3/tag', opts);
}
fetchOne(id) {
return this.get('/v3/tag/' + id);
}
create(data) {
return this.post('/v3/tag', data);
}
update(id, data) {
return this.put('/v3/tag/' + id, data);
}
remove(uuid) {
return this.delete('/v3/tag/' + uuid);
}
}
const service = new TagService;
/**
* Action types
*/
export const TAGS_REQUEST = 'TAGS_REQUEST';
export const TAGS_RECEIVE = 'TAGS_RECEIVE';
export const TAG_RECEIVE = 'TAG_RECEIVE';
export const TAG_SELECTED = 'TAG_SELECTED';
export const TAGS_INVALIDATE = 'TAGS_INVALIDATE';
export const TAGS_RECEIVE_PAGINATION = 'TAGS_RECEIVE_PAGINATION';
/**
* Action creators
*/
export function tagsReceivePagination(pagination) {
return {
type: TAGS_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 tagsRequest(params = {}) {
return {
type: TAGS_REQUEST,
payload: {
params: params
}
};
}
export function tagsReceive(items) {
return {
type: TAGS_RECEIVE,
payload: {
tags: items ? items : []
}
};
}
export function tagReceive(tag) {
return {
type: TAG_RECEIVE,
payload: {
tag: tag ? tag : {}
}
};
}
export function tagSetSelected(tag) {
return {
type: TAG_SELECTED,
payload: {
selected: tag ? tag : false
}
};
}
export function tagsInvalidate() {
return {
type: TAGS_INVALIDATE,
payload: {
}
};
}
export function tagUpdate(id, data) {
return function(dispatch, getState) {
dispatch(tagsRequest({}));
return service.update(id, data)
.then(response => {
return dispatch(tagReceive(response.first()));
});
}
}
export function tagCreate(data) {
return function(dispatch, getState) {
dispatch(tagsRequest({}));
return service.create(data)
.then(response => {
dispatch(tagSetSelected(response.first().get('uuid')));
return dispatch(tagReceive(response.first()));
});
}
}
export function tagsShouldFetch(state) {
const tags = state.tags;
if (!tags.items.length) {
return true;
} else if (tags.isFetching) {
return false;
}
return tags.didInvalidate;
}
export function tagsMaybeFetch(params = {}) {
return function(dispatch, getState) {
const tags = getState().tags.items;
if (getState().tags.isFetching) {
return Promise.resolve();
}
if (!tags.items || !tags.items.size) {
return dispatch(tagsFetch(params));
}
return Promise.resolve();
}
};
export function tagsFetch(params = {}) {
return function(dispatch, getState) {
dispatch(tagsRequest(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";
// default page
let page = 0;
if (params.page) {
// specific page is requested
page = params.page;
} else if (getState().tags.pagination.current) {
// existing pagination
page = getState().tags.pagination.current;
}
if (page) {
params.page = page;
}
return service.fetch(params)
.then(response => {
dispatch(tagsReceivePagination(response));
return dispatch(tagsReceive(response.get('items')));
});
};
}
export function tagsFetchOne(id) {
return function (dispatch, getState) {
dispatch(tagsRequest({}));
return service.fetchOne(id)
.then(response => {
return dispatch(tagReceive(response.first()));
});
}
}
export function tagsRemove(uuid) {
return function (dispatch, getState) {
dispatch(tagsInvalidate());
return service.remove(uuid);
}
}