application/redux/actions/issue-actions.js
import BaseService from './../../services/base-service';
class IssueService extends BaseService {
fetch(opts) {
return this.get('/v3/issue', opts);
}
fetchOne(id) {
return this.get('/v3/issue/' + id);
}
create(data) {
return this.post('/v3/issue', data);
}
update(id, data) {
return this.put('/v3/issue/' + id, data);
}
remove(id) {
return this.delete('/v3/issue/' + id);
}
}
const service = new IssueService;
/**
* Action types
*/
export const ISSUES_REQUEST = 'ISSUES_REQUEST';
export const ISSUES_RECEIVE = 'ISSUES_RECEIVE';
export const ISSUE_RECEIVE = 'ISSUE_RECEIVE';
export const ISSUE_SELECTED = 'ISSUE_SELECTED';
export const ISSUES_INVALIDATE = 'ISSUES_INVALIDATE';
export const ISSUES_FILTER_OPEN = 'ISSUES_FILTER_OPEN';
export const ISSUES_RECEIVE_PAGINATION = 'ISSUES_RECEIVE_PAGINATION';
/**
* Action creators
*/
export function issuesReceivePagination(pagination) {
return {
type: ISSUES_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 issuesRequest(params = {}) {
return {
type: ISSUES_REQUEST,
payload: {
params: params
}
};
}
export function issuesFilterOpen() {
return {
type: ISSUES_FILTER_OPEN,
payload: {}
}
}
export function issuesReceive(items) {
return {
type: ISSUES_RECEIVE,
payload: {
issues: items ? items : []
}
};
}
export function issueReceive(item) {
return {
type: ISSUE_RECEIVE,
payload: {
issue: item ? item : {}
}
};
}
export function issueSetSelected(issue) {
return {
type: ISSUE_SELECTED,
payload: {
selected: issue ? issue : false
}
};
}
export function issuesInvalidate() {
return {
type: ISSUES_INVALIDATE,
payload: {
}
};
}
export function issuesFilterWebOnly() {
return {
type: ISSUES_FILTER_WEBONLY,
payload: {
}
}
}
export function issuesFilterPrintOnly() {
return {
type: ISSUES_FILTER_PRINTONLY,
payload: {
}
}
}
export function issuesShouldFetch(state) {
const issues = state.issues;
if (!issues.items.length) {
return true;
} else if (issues.isFetching) {
return false;
}
return issues.didInvalidate;
}
export function issuesMaybeFetch(params = {}) {
return function(dispatch, getState) {
const issues = getState().issues;
if (!issues.items || !issues.items.size) {
return dispatch(issuesFetch(params));
}
return Promise.resolve();
}
};
export function issuesFetch(params = {}) {
return function(dispatch, getState) {
dispatch(issuesRequest(params));
params.per_page = params.per_page ? params.per_page : 100;
params.order = params.order ? params.order : "published_at";
params.dir = params.dir ? params.dir : "desc";
params.include_archived = params.include_archived ? params.include_archived : 0;
return service.fetch(params)
.then(response => {
dispatch(issuesReceivePagination(response));
return dispatch(issuesReceive(response.get('items')));
});
};
}
export function issuesFetchOne(id) {
return function (dispatch, getState) {
dispatch(issuesRequest({}));
return service.fetchOne(id)
.then(response => {
return dispatch(issueReceive(response.first()));
});
};
}
export function issueCreate(issue = {}) {
return function(dispatch, getState) {
return service.create(issue)
.then(response => {
return dispatch(issueReceive(response.first()));
});
}
}
export function issueUpdate(id, issue = {}) {
return function(dispatch, getState) {
return service.update(id, issue)
.then(response => {
return dispatch(issueReceive(response.first()));
});
}
}
export function issuesRemove(uuids) {
return function(dispatch, getState) {
dispatch(issuesInvalidate());
return service.remove(uuids);
}
}