Skip to content

Instantly share code, notes, and snippets.

@andreychev
Created March 22, 2017 06:27
Show Gist options
  • Save andreychev/a38f9e9329129d41c6016dcd06b3512d to your computer and use it in GitHub Desktop.
Save andreychev/a38f9e9329129d41c6016dcd06b3512d to your computer and use it in GitHub Desktop.
import React, { PropTypes } from 'react';
import Constants from 'cx-api/constants';
import Loading from 'ux/flow/Loading';
function isPending(loadingComponent) {
function pendingHOC({ status, loading = {}, children }) {
if (status === Constants.STATE_PENDING) {
return (<loadingComponent { ...loading } />);
}
return children || null;
}
pendingHOC.propTypes = {
status: PropTypes.oneOf([
'',
Constants.STATE_PENDING,
Constants.STATE_RESOLVED,
Constants.STATE_REJECTED
]),
loading: PropTypes.object
};
return pendingHOC;
}
export { isPending };
export default isPending(Loading);
@andreychev
Copy link
Author

andreychev commented Mar 22, 2017

branch(
    props => props.status === Constants.STATE_PENDING,
    loadingHOC
)

The same↑

But I don't like: https://github.com/acdlite/recompose/blob/b8466a6f40151b35095e8e3f56779846ae1ae682/src/packages/recompose/branch.js#L17.

@andreychev
Copy link
Author

andreychev commented Mar 22, 2017

import React, { PropTypes } from 'react';

import isEmpty from 'lodash/isEmpty';
import isNil from 'lodash/isNil';

import Empty from 'ux/flow/Empty';

function defaultIsExist({ data }) {
    return !isNil(data) && !isEmpty(data);
}

function isExisting(emptyComponent) {
    function existingHOC(props, isExist = defaultIsExist) {
        const {
            empty = {},
            children
        } = props;

        if (isExist(props)) {
            return children || null;
        }

        return (<emptyComponent { ...empty } />);
    }

    existingHOC.propTypes = {
        empty: PropTypes.object
    };

    return existingHOC;
}

export { isExisting };

export default isExisting(Empty);

@andreychev
Copy link
Author

import React, { PropTypes } from 'react';

import Constants from 'cx-api/constants';

import Incident from 'ux/flow/Incident';

function isRejecting(rejectingComponent) {
    function rejectingHOC({ status, rejecting = {}, children }) {
        if (status === Constants.STATE_REJECTED) {
            return (<rejectingComponent { ...rejecting } />);
        }

        return children || null;
    }

    rejectingHOC.propTypes = {
        status: PropTypes.oneOf([
            '',
            Constants.STATE_PENDING,
            Constants.STATE_RESOLVED,
            Constants.STATE_REJECTED
        ]),
        rejecting: PropTypes.object
    };

    return rejectingHOC;
}

export { isRejecting };

export default isRejecting(Incident);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment