Home Reference Source

application/redux/actions/site-actions.js

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

class SiteService extends BaseService {

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

    fetchRoutes(opts = {}) {
        return this.get('/v3/site/route', opts);
    }

    putRoute(uuid, data = {}) {
        let postData = data;
        postData['uuid'] = uuid;
        return this.put('/v3/site/route', postData);
    }

    postRoute(data = {}) {
        return this.post('/v3/site/route', data);
    }

    deleteRoute(uuid) {
        let postData = {
            uuid: uuid
        };
        return this.delete('/v3/site/route', postData);
    }
}

const service = new SiteService;

/**
 * Action types
 */
export const SITE_REQUEST = 'SITE_REQUEST';
export const SITE_RECEIVE = 'SITE_RECEIVE';
export const SITE_RECEIVE_ROUTES = 'SITE_RECEIVE_ROUTES';
export const SITE_RECEIVE_SSL = 'SITE_RECEIVE_SSL';
export const SITE_INVALIDATE = 'SITE_INVALIDATE';

/**
 * Action creators
 */
export function siteRequest(params = {}) {
    return {
        type: SITE_REQUEST,
        payload: {
            params: params
        }
    };
}

export function siteReceive(items) {

    return {
        type: SITE_RECEIVE,
        payload: {
            information: items ? items : []
        }
    };
}

export function siteReceiveRoutes(items) {

    return {
        type: SITE_RECEIVE_ROUTES,
        payload: {
            routes: items ? items : []
        }
    };
}

export function siteReceiveSsl(items) {

    return {
        type: SITE_RECEIVE_SSL,
        payload: {
            ssl: items ? items : []
        }
    };
}

export function siteInvalidate() {
    return {
        type: SITE_INVALIDATE,
        payload: {
        }
    };
}

export function siteShouldFetch(state) {
    const site = state.site;
    if (!site.information.length) {
        return true;
    } else if (site.isFetching) {
        return false;
    }

    return site.didInvalidate;
}

export function siteMaybeFetch(params = {}) {
    return function(dispatch, getState) {
        const site = getState().site;

        if (!site.information || !site.information.size) {
            return dispatch(siteFetch(params));
        }

        return Promise.resolve();
    }
};

export function siteFetch(params = {}) {
    return function(dispatch, getState) {
        dispatch(siteRequest(params));

        return service.fetch(params)
            .then(response => {
                return dispatch(siteReceive(response.get('site')));
            });
    };
};

export function siteFetchRoutes(params = {}) {
    return function(dispatch, getState) {
        dispatch(siteRequest(params));

        return service.fetchRoutes(params)
            .then(response => {
                return dispatch(siteReceiveRoutes(response.first()));
            });
    };
};

export function siteFetchSsl(params = {}) {
    return function(dispatch, getState) {
        dispatch(siteRequest(params));

        return service.get('/v3/site/ssl', params)
            .then(response => {
                return dispatch(siteReceiveSsl(response.first()));
            });
    };
};

export function siteCreateRoute(data = {}) {
    return function(dispatch, getState) {
        dispatch(siteRequest({}));

        return service.postRoute(data)
            .then(response => {
                return dispatch(siteReceiveRoutes(response.first()));
            });
    };
};

export function siteUpdateRoute(uuid, data = {}) {
    return function(dispatch, getState) {
        dispatch(siteRequest({}));

        return service.putRoute(uuid, data)
            .then(response => {
                return dispatch(siteReceiveRoutes(response.first()));
            });
    };
};

export function siteRemoveRoute(uuid) {
    return function(dispatch, getState) {
        dispatch(siteRequest({}));

        return service.deleteRoute(uuid);
    };
};