Home Reference Source

application/redux/actions/ssts-actions.js

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

class SstsService extends BaseService {

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

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

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

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

    updateMultiple(data) {
        return this.put('/v3/ssts', data);
    }

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

const service = new SstsService;

/**
 * Action types
 */
export const SSTSS_REQUEST = 'SSTSS_REQUEST';
export const SSTSS_RECEIVE = 'SSTSS_RECEIVE';
export const SSTS_RECEIVE = 'SSTS_RECEIVE';
export const SSTS_SELECTED = 'SSTS_SELECTED';
export const SSTSS_INVALIDATE = 'SSTSS_INVALIDATE';

export const SSTSS_RECEIVE_PAGINATION = 'SSTSS_RECEIVE_PAGINATION';

/**
 * Action creators
 */
export function sstssReceivePagination(pagination) {
    return {
        type: SSTSS_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 sstssRequest(params = {}) {
    return {
        type: SSTSS_REQUEST,
        payload: {
            params: params
        }
    };
}

export function sstssReceive(items) {

    return {
        type: SSTSS_RECEIVE,
        payload: {
            sstss: items ? items : []
        }
    };
}

export function sstsReceive(ssts) {
    return {
        type: SSTS_RECEIVE,
        payload: {
            ssts: ssts ? ssts : {}
        }
    };
}

export function sstsSetSelected(ssts) {
    return {
        type: SSTS_SELECTED,
        payload: {
            selected: ssts ? ssts : false
        }
    };
}

export function sstssInvalidate() {
    return {
        type: SSTSS_INVALIDATE,
        payload: {
        }
    };
}

export function sstsUpdate(id, data) {
    return function(dispatch, getState) {
        dispatch(sstssRequest({}));

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

export function sstsUpdateMultiple(data) {
    return function(dispatch, getState) {
        dispatch(sstssRequest({}));

        return service.updateMultiple(data)
            .then(response => {
                dispatch(sstssReceivePagination(response));
                return dispatch(sstssReceive(response.get('items')));
            });
   }
}

export function sstsCreate(data) {
    return function(dispatch, getState) {
        dispatch(sstssRequest({}));

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

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

    return sstss.didInvalidate;
}

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

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

        if (!sstss.items || !sstss.items.size) {
            return dispatch(sstssFetch(params));
        }

        return Promise.resolve();
    }
};

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

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

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

export function sstssFetchOne(id) {
    return function (dispatch, getState) {
        dispatch(sstssRequest({}));

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

export function sstssRemove(uuid) {
    return function (dispatch, getState) {
        dispatch(sstssInvalidate());

        return service.remove(uuid);
    }
}