Home Reference Source

application/redux/actions/author-actions.js

import BaseService from './../../services/base-service';

class AuthorService extends BaseService {

    fetch(opts) {
        return this.get('/v3/author', opts);
    }

    fetchOne(id) {
        return this.get('/v3/author/' + id);
    }

    create(data) {
        return this.post('/v3/author', data);
    }

    update(id, data) {
        return this.put('/v3/author/' + id, data);
    }

    remove(uuid) {
        return this.delete('/v3/author/' + uuid);
    }
}

const service = new AuthorService;

/**
 * Action types
 */
export const AUTHORS_REQUEST = 'AUTHORS_REQUEST';
export const AUTHORS_RECEIVE = 'AUTHORS_RECEIVE';
export const AUTHOR_RECEIVE = 'AUTHOR_RECEIVE';
export const AUTHOR_SELECTED = 'AUTHOR_SELECTED';
export const AUTHORS_INVALIDATE = 'AUTHORS_INVALIDATE';

export const AUTHORS_RECEIVE_PAGINATION = 'AUTHORS_RECEIVE_PAGINATION';

/**
 * Action creators
 */
export function authorsReceivePagination(pagination) {
    return {
        type: AUTHORS_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 authorsRequest(params = {}) {
    return {
        type: AUTHORS_REQUEST,
        payload: {
            params: params
        }
    };
}

export function authorsReceive(items) {

    return {
        type: AUTHORS_RECEIVE,
        payload: {
            authors: items ? items : []
        }
    };
}

export function authorReceive(author) {
    return {
        type: AUTHOR_RECEIVE,
        payload: {
            author: author ? author : {}
        }
    };
}

export function authorSetSelected(author) {
    return {
        type: AUTHOR_SELECTED,
        payload: {
            selected: author ? author : false
        }
    };
}

export function authorsInvalidate() {
    return {
        type: AUTHORS_INVALIDATE,
        payload: {
        }
    };
}

export function authorUpdate(id, data) {
    return function(dispatch, getState) {
        dispatch(authorsRequest({}));

        return service.update(id, data)
            .then(response => {
                return dispatch(authorReceive(response.first()));
            });
   }
}

export function authorCreate(data) {
    return function(dispatch, getState) {
        dispatch(authorsRequest({}));

        return service.create(data)
            .then(response => {
                dispatch(authorSetSelected(response.first().get('uuid')));
                return dispatch(authorReceive(response.first()));
            });
   }
}

export function authorsShouldFetch(state) {
    const authors = state.authors;
    if (!authors.items.length) {
        return true;
    } else if (authors.isFetching) {
        return false;
    }

    return authors.didInvalidate;
}

export function authorsMaybeFetch(params = {}) {
    return function(dispatch, getState) {
        const authors = getState().authors.items;

        if (getState().authors.isFetching) {
            return Promise.resolve();
        }

        if (!authors.items || !authors.items.size) {
            return dispatch(authorsFetch(params));
        }

        return Promise.resolve();
    }
};

export function authorsFetch(params = {}) {
    return function(dispatch, getState) {
        dispatch(authorsRequest(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().authors.pagination.current) {
            // existing pagination
            page = getState().authors.pagination.current;
        }

        if (page) {
            params.page = page;
        }

        return service.fetch(params)
            .then(response => {
                dispatch(authorsReceivePagination(response));
                return dispatch(authorsReceive(response.get('items')));
            });
    };
}

export function authorsFetchOne(id) {
    return function (dispatch, getState) {
        dispatch(authorsRequest({}));

        return service.fetchOne(id)
            .then(response => {
                return dispatch(authorReceive(response.first()));
            });
    }
}

export function authorsRemove(uuid) {
    return function (dispatch, getState) {
        dispatch(authorsInvalidate());

        return service.remove(uuid);
    }
}