Skip to content

Conversation

@jkachel
Copy link
Contributor

@jkachel jkachel commented Oct 1, 2024

What are the relevant tickets?

Closes https://github.com/mitodl/hq/issues/5611

Description (What does it do?)

We're going to develop the frontend for Unified Ecommerce in Learn, and this adds some necessary support stuff to that:

  • Adds an EcommercePages folder so we can store the page components for the UE frontend
  • Adds an EcommerceFeature component so we can easily feature flag out the UE stuff (especially now, since we'll be doing things to begin with that don't have design yet)
  • Adds a CartPage placeholder page, just so there's something to go to
  • Adds routing for /cart/ to go to the UE code - this requires a logged-in session and an enabled feature flag to access

How can this be tested?

Automated tests should pass.

To manually test, you'll need to make sure your instance is hitting PostHog - so it needs a valid set of keys. You can either hit the "MIT Learn Test" project that's in the OL PostHog account, or you can spin up your own PostHog account, or you can spin up your own instance of PostHog locally; any of these will work. Then, you'll need to go into Feature Flags and create a flag that just releases a boolean value called enable-ecommerce.

Test by loading <site root>/cart/:

Flag On Flag Off
Authenticated Placeholder page shown ForbiddenError
Not Authenticated Redirect to login Redirect to login

Additional Context

This doesn't add the API client for UE - we'll do that in a separate PR because it'll probably require some tweaking in the UE code. So, we're just focusing on getting UE some space set up to move into.

- Added a "space" for this - EcommercePages
- Added a CartPage placeholder (it's the Privacy Policy for now, just needed _something_)
- Added a PostHogRoute component to allow us to feature flag the ecomm stuff (but may go a different route here, not sure)
- Added routes for the cart page

For this to work you'll need to make a feature flag called "enable-ecommerce" and turn it on in PostHog. (And you'll need to fix my code because this actually doesn't work right now.)
- Removed the PostHogRoute component as it didn't really work consistently
- Added a EcommerceFeature component to do the feature flag checking in a standardized way
- Removed a bunch of stuff from the "Cart" Page (since it was just the Privacy Policy anyway)

Not using the PostHogFeature component here - I was trying, but I kept running into issues where it'd throw to the fallback if the flag was enabled. It seems much more stable now with the useFeatureFlagEnabled but I wouldn't be opposed to doing it manually, since this doesn't trigger the Feature Flag View event (and then we can also just.. wait for the flag to come back as something).
@jkachel jkachel added Needs Review An open Pull Request that is ready for review product:mit-open Issues related to the MIT Open product labels Oct 1, 2024
import { useFeatureFlagEnabled } from "posthog-js/react"

jest.mock("posthog-js/react")
const mockedUseFatureFlagEnabled = jest.mocked(useFeatureFlagEnabled)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const mockedUseFatureFlagEnabled = jest.mocked(useFeatureFlagEnabled)
const mockedUseFeatureFlagEnabled = jest.mocked(useFeatureFlagEnabled)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor Suggestion: I wouldn't bother with most of the styling here:

  • A lot of it isn't doing anything, really. (e.g., the align-self: stetch and various flexboxes yield the same layout as default display: block here.
  • Some of this, like making the paragraph have component="h2" is undesirable (not a header) and might be accidentally left in later.

Clearly this is all placeholder, so it doesn't really matter, but

    <EcommerceFeature>
      <Container>
        <MetaTags title="Shopping Cart" />
        <Breadcrumbs
          variant="light"
          ancestors={[{ href: urls.HOME, label: "Home" }]}
          current="Shopping Cart"
        />
        <Typography component="h1" variant="h3">
          Shopping Cart
        </Typography>
        <Typography>
          The shopping cart layout should go here, if you're allowed to see
          this.
        </Typography>
      </Container>
    </EcommerceFeature>

looks fine, IMO

throw new ForbiddenError("Not enabled.")
}

return <>{ecommFlag ? children : null}</>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for a fragment here.

Suggested change
return <>{ecommFlag ? children : null}</>
return ecommFlag ? children : null

*/

const EcommerceFeature: React.FC<EcommerceFeatureProps> = ({ children }) => {
const ecommFlag = useFeatureFlagEnabled("enable-ecommerce")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth putting these as constants in @/common/feature_flags or something.

(Or a TS enum, though they are occasionally annoying.)

@ChristopherChudzicki
Copy link
Contributor

This looks good. Left some very minor requests. Mostly the typo + unnecessary fragment.

@ChristopherChudzicki ChristopherChudzicki added Waiting on author and removed Needs Review An open Pull Request that is ready for review labels Oct 3, 2024
- Added an enum for feature flags
- Updated places where I'd directly referenced the ecommerce flag to use the enum value instead
- Stripped out a bunch of styling components from CartPage - this will all go away at some point anyway so why not now?
@jkachel
Copy link
Contributor Author

jkachel commented Oct 3, 2024

3a02e22 should address the remaining stuff. Also - it's probably not a bad idea to make the EcommerceFeature component generic for any feature flag, so we can gate anything, but that's probably out of scope for this PR.

Copy link
Contributor

@ChristopherChudzicki ChristopherChudzicki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Also out of scope for this PR, but might want to make the posthog events an enum / constants, too.

@jkachel jkachel merged commit 598fac2 into main Oct 3, 2024
10 checks passed
@jkachel jkachel deleted the jkachel/add-ecommerce-frontend-base branch October 3, 2024 17:20
@odlbot odlbot mentioned this pull request Oct 4, 2024
5 tasks
Comment on lines +26 to +28
if (ecommFlag === false) {
throw new ForbiddenError("Not enabled.")
}
Copy link
Contributor

@ChristopherChudzicki ChristopherChudzicki Oct 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Post-merge comment

I noticed some odd behavior with PostHog and feature flags that I thought was worth mentioning. Essentially, I think there's no reliable way—with useFeatureFlagEnabled—to determine whether a flag is "off" or "not yet loaded". (Note: the useFeatureFlagEnabled is really simple. Here's its source.)

useFeatureFlagEnabled returns boolean | undefined. Its behavior is:

  1. useFeatureFlagEnabled("flag-A") returns undefined before any flags have loaded
  2. When any flags have loaded, useFeatureFlagEnabled("flag-A") returns true if the flag is on, and false if the flag is off or has not loaded yet.
    • "When any flags have loaded" includes:
      • bootstrapped flags
      • flags determined via API call to posthog site

So the following is possible:

Screenshot 2024-10-04 at 12 30 41 PM

In this scenario, we were attempting to:

  1. If flags not loaded yet, return null
  2. If flags loaded, either
    • redirect
    • or render the cart page

But this doesn't seem to work quite right if any flags are bootstrapped, as shown in the sreenshot.

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

Labels

product:mit-open Issues related to the MIT Open product Waiting on author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants