Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create next specific code #203

Open
kennylavender opened this issue Mar 10, 2019 · 1 comment
Open

Create next specific code #203

kennylavender opened this issue Mar 10, 2019 · 1 comment

Comments

@kennylavender
Copy link
Contributor

See #201 for working starting points

@kennylavender kennylavender self-assigned this Mar 10, 2019
@kennylavender kennylavender removed their assignment Mar 10, 2019
@ericelliott
Copy link
Contributor

Just copied this code into a third project:

import React from 'react';
import PropTypes from 'prop-types';
import { curry } from 'ramda';
import Error from 'next/error';
import {
  getCurrentActiveFeatureNames,
  isActiveFeatureName,
} from '@paralleldrive/feature-toggles';

const configureNextPageFeature = (
  initialFeatures,
  featureName,
  ComposedComponent
) => {
  const getComposedComponentsInitialProps = ctx =>
    ComposedComponent.getInitialProps
      ? ComposedComponent.getInitialProps(ctx)
      : {};

  const NextPageFeature = ({ statusCode, ...rest }) =>
    statusCode === 200 ? (
      <ComposedComponent {...rest} />
    ) : (
      <Error statusCode={statusCode} />
    );

  NextPageFeature.getInitialProps = async ctx => {
    const { res, req, err } = ctx;

    const activeFeaturesNames = getCurrentActiveFeatureNames({
      initialFeatures,
      req,
    });

    if (res && !isActiveFeatureName(featureName, activeFeaturesNames))
      res.statusCode = 404;

    const statusCode = res ? res.statusCode : err ? err.statusCode : null;

    return {
      ...(await getComposedComponentsInitialProps(ctx)),
      statusCode,
    };
  };

  NextPageFeature.propTypes = {
    statusCode: PropTypes.number.isRequired,
  };

  return NextPageFeature;
};

export default curry(configureNextPageFeature);

Do we have something like this in the library yet?

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

No branches or pull requests

2 participants