Home Reference Source

application/redux/actions/channel-actions.js

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

class ChannelService extends BaseService {

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

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

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

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

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

const service = new ChannelService;

/**
 * Action types
 */
export const CHANNELS_REQUEST = 'CHANNELS_REQUEST';
export const CHANNELS_RECEIVE = 'CHANNELS_RECEIVE';
export const CHANNEL_RECEIVE = 'CHANNEL_RECEIVE';
export const CHANNEL_SELECTED = 'CHANNEL_SELECTED';
export const CHANNELS_INVALIDATE = 'CHANNELS_INVALIDATE';

export const CHANNELS_RECEIVE_PAGINATION = 'CHANNELS_RECEIVE_PAGINATION';

/**
 * Action creators
 */
export function channelsReceivePagination(pagination) {
    return {
        type: CHANNELS_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 channelsRequest(params = {}) {
    return {
        type: CHANNELS_REQUEST,
        payload: {
            params: params
        }
    };
}

export function channelsReceive(items) {

    return {
        type: CHANNELS_RECEIVE,
        payload: {
            channels: items ? items : []
        }
    };
}

export function channelReceive(channel) {
    return {
        type: CHANNEL_RECEIVE,
        payload: {
            channel: channel ? channel : {}
        }
    };
}

export function channelSetSelected(channel) {
    return {
        type: CHANNEL_SELECTED,
        payload: {
            selected: channel ? channel : false
        }
    };
}

export function channelsInvalidate() {
    return {
        type: CHANNELS_INVALIDATE,
        payload: {
        }
    };
}

export function channelUpdate(id, data) {
    return function(dispatch, getState) {
        dispatch(channelsRequest({}));

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

export function channelCreate(data) {
    return function(dispatch, getState) {
        dispatch(channelsRequest({}));

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

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

    return channels.didInvalidate;
}

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

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

        if (!channels.items || !channels.items.size) {
            return dispatch(channelsFetch(params));
        }

        return Promise.resolve();
    }
};

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

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

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

export function channelsFetchOne(id) {
    return function (dispatch, getState) {
        dispatch(channelsRequest({}));

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

export function channelsRemove(uuid) {
    return function (dispatch, getState) {
        dispatch(channelsInvalidate());

        return service.remove(uuid);
    }
}