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

External menu configurations #289

Merged
merged 20 commits into from
Jan 18, 2019
Merged

External menu configurations #289

merged 20 commits into from
Jan 18, 2019

Conversation

emmenko
Copy link
Member

@emmenko emmenko commented Jan 14, 2019

Closes #37

The menu configuration for each application has been moved outside of the AppShell and is available as a GraphQL endpoint on the MC domain (not the MC API).

Each internal application (managed by CT) can now define their own menu config, which is then uploaded to a storage service so that the GraphQL server can aggregate those configs are expose them.

Custom applications still need to manage their settings separately, however they don't need to update the app-kit dependencies every time we change the menu links. 🎉

For the "account" links, it works the same way.

screenshot 2019-01-14 at 18 17 49


TODOS:

  • for local development, allow to load the application menu config

};
redirectTo = targetUrl => window.location.replace(targetUrl);
render() {
if (this.props.externalLink) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This case was only used by the support link, which is now rendered separately. Hence the cleanup here

@@ -317,23 +294,19 @@ export class DataMenu extends React.PureComponent {
data: PropTypes.arrayOf(
PropTypes.shape({
key: PropTypes.string.isRequired,
labelKey: PropTypes.string,
Copy link
Member Author

Choose a reason for hiding this comment

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

All labels are now coming from labelAllLocales

size="scale"
theme={getIconTheme(
isActive || this.isMainMenuRouteActive(menu.uriPath),
menu.shouldRenderDivider
Copy link
Member Author

Choose a reason for hiding this comment

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

This determines the "theme" of the icon now

}
onMouseLeave={
this.state.isMenuOpen ? null : this.shouldCloseMenuFly
<MenuItem
Copy link
Member Author

Choose a reason for hiding this comment

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

Nothing much changed here, mostly indentation

</RestrictedMenuItem>
);
};

renderSupportLink = () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

This is now rendered separately

{this.props.data
.filter(menu => !this.bottomMenuItems.indexOf(menu.key))
.map((menu, index) => this.renderMenu(menu, 'fixed', index))}
<div className={styles['scrollable-menu']}>
Copy link
Member Author

Choose a reason for hiding this comment

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

No need for a react component here, keep it simple

<div className={styles['scrollable-menu']}>
{this.props.data.map((menu, index) => (
<React.Fragment key={index}>
{menu.shouldRenderDivider && <MenuItemDivider />}
Copy link
Member Author

Choose a reason for hiding this comment

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

The divider is now rendered based on the flag shouldRenderDivider

};

export const NavBarLayout = props => (
export const NavBarLayout = React.forwardRef((props, ref) => (
Copy link
Member Author

Choose a reason for hiding this comment

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

NOTE that we use React.forwardRef, as documented here: https://reactjs.org/docs/forwarding-refs.html

getNode = node => {
this.node = node;
};
ref = React.createRef();
Copy link
Member Author

Choose a reason for hiding this comment

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

🎉

: defaultNavigationItems
}
rootNode={this.ref.current}
data={navbarMenu.concat(customAppsMenu)}
Copy link
Member Author

Choose a reason for hiding this comment

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

Here we merge the internal apps with the custom apps config

LoadingNavBar.displayName = 'LoadingNavBar';
export class LoadingNavBar extends React.Component {
static displayName = 'LoadingNavBar';
ref = React.createRef();
Copy link
Member Author

Choose a reason for hiding this comment

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

Even though we don't use it here, the NavBarLayout expects a ref

import React from 'react';
import { shallow } from 'enzyme';
Copy link
Member Author

Choose a reason for hiding this comment

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

FYI: when we re-implement the navbar, we can drop these tests and write RTL tests.

email: PropTypes.string,
gravatarHash: PropTypes.string.isRequired,
};
const UserSettingsMenuBody = props => {
Copy link
Member Author

Choose a reason for hiding this comment

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

This component is basically rendered only when the user opens the menu, therefore we can connect to fetch the menu config on this component, hence the split of components

</div>
</Spacings.Inline>
</Spacings.Inset>
{menuLinks.map(menu => (
Copy link
Member Author

Choose a reason for hiding this comment

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

This is basically the only thing that changed

@emmenko emmenko added the 🚧 Status: WIP Work in progress label Jan 14, 2019
const noop = () => Component => Component;

const devonlyMenuLoader =
process.env.NODE_ENV !== 'development'
Copy link
Member Author

Choose a reason for hiding this comment

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

Dealing with loading the menu.json config for development was/is tricky to do, not much for the implementation but for the approach.

For instance, on production the application uses the https://mc.commercetools.com/api/graphql endpoint to load the configs through the Proxy server. In development though, there is no proxy running.
Furthermore, you want to load the menu.json of the application you're developing from.
In fact, you can't use the links to the other apps because they lead to 404 pages.

With that in mind, we need to disable the graphql queries to the proxy endpoint, since the URL is defined as ${window.location.origin}/api/graphql, which in development mode points to http://localhost:3001/api/graphql.
I thought about having a fake graphql endpoint in the webpack dev server, but there are some disadvantages with that:

  • it's tight to webpack (e.g. if someone wants to develop an app using something different, there is a problem)
  • we have duplication of the graphql schema in two places (app-kit + mc proxy)

I also tried using a custom apollo link to write to the cache with based on the menu.json, so that no network request is made since apollo will try to read from the cache. However, besides the implementation being a bit complex, it seems that apollo makes the request anyway, probably because the cache is not being filled in time.

This leaves us with the final approach I took, which is maybe not the ideal solution but it's good enough. One of the important things to keep in mind was that the setup should be as simple as possible for the user:

DEV_ONLY__getNavbarMenuConfig={() => import('../../../menu.json')}

I chose to explicitly mark the prop with DEV_ONLY_ to make it clear that this won't be used in production.

Little downside: I suppose webpack will create a chunk for the menu.json, since it finds import() split point. We could e.g. wrap the call with a process.env.NODE_ENV so that uglily will remove it, but I'm not sure if that makes things simpler.

Please let me know what do you think about this approach, about naming etc. I'm open to suggestions 😇

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah maybe wrapping the call would be a good idea? but another unused chunk in prod doesn't really make any difference 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

Could we also just have a HoC which reads the process.env to alternate the fetching behaviour? So instead of having a withDevOnlyMenuLoader and some graphql (Apollo) application we have a withMenuQuery and it alternates between fetching from the .json and through Apollo based on the env or a specific prop?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I think @montezume and I mean the same. Wrap the call in the HoC itself.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, I can try that

@emmenko emmenko requested review from tdeekens and montezume January 15, 2019 08:26
const noop = () => Component => Component;

const devonlyMenuLoader =
process.env.NODE_ENV !== 'development'
Copy link
Contributor

Choose a reason for hiding this comment

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

yeah maybe wrapping the call would be a good idea? but another unused chunk in prod doesn't really make any difference 🤔

@@ -40,6 +40,7 @@
"LoginForm.forgotPassword": "Forgot password?",
"LoginForm.forgotPasswordTitle": "We are redirecting you to the Forgot Password form in {countdown} seconds.",
"LoginForm.InvalidCredentials": "Sorry, but the login details entered below are not in our system. Please enter your correct email address and password below.",
"LoginForm.invalidEmail": "Please enter a valid Email address.",
Copy link
Contributor

Choose a reason for hiding this comment

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

one day we will have translations that don't have German capitalization rules in English 😆

const loadApplicationMessagesForLanguage = async lang => {
const messages = await import(`../../i18n/data/${lang}.json` /* webpackChunkName: "application-messages-[request]" */);
return messages.default;
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Unrelated refactoring, correct?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes

? noop
: (getLoaderFn, mapMenuToProps) => Component => {
class WrappedComponent extends React.Component {
static displayName = wrapDisplayName(
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: maybe not use recompose here as it's almost deprecated and we could aim to get rid of it in the app-shell maybe?

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn’t know that. What is the alternative then? We usually use it for compose

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, that came across wrong. I didn't mean to say "let's drop it" or that we decided. It was more of a suggestion: could the app-shell work without and if so what would be have to remove or custom build. Keep it for now. Should be a separate thing anyway. Sorry for the confusion.

Copy link
Member Author

Choose a reason for hiding this comment

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

No worries, I understand your point. I was just asking what should we use to compose. Should we just copy the implementation and have it as an internal function?

Copy link
Contributor

Choose a reason for hiding this comment

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

flowRight from lodash I suppose (the renamed compose to flowRight).

  1. I would have these HoCs as little utils, yes
  2. For the mc-fe it's almost unrealistic and not worthwhile getting rid of recompose

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah ok, good to know. We can do that as a follow up, I'll write it down on my list.

Copy link
Member Author

Choose a reason for hiding this comment

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

const noop = () => Component => Component;

const devonlyMenuLoader =
process.env.NODE_ENV !== 'development'
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we also just have a HoC which reads the process.env to alternate the fetching behaviour? So instead of having a withDevOnlyMenuLoader and some graphql (Apollo) application we have a withMenuQuery and it alternates between fetching from the .json and through Apollo based on the env or a specific prop?

@emmenko
Copy link
Member Author

emmenko commented Jan 15, 2019

Alright, I've refactored the HOC based on the feedback: bbe6565
Please have another look.

screenshot 2019-01-15 at 12 27 43

uri: `${environment.mcProxyApiUrl ||
defaultApiUrl}/api/graphql`,
}}
onError={props.handleActionError}
Copy link
Member Author

Choose a reason for hiding this comment

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

FYI: this is not necessary anymore when we use #292

@emmenko
Copy link
Member Author

emmenko commented Jan 15, 2019

I think this is good to go now 🚀

PS: I'm not sure if this is considered a breaking change or just a feature 🤔

response.setHeader('Content-Type', 'application/json');
const errorMessage = `This GraphQL endpoint is not available in ${
process.env.NODE_ENV
} mode as it's not necessary. The menu configuration is loaded from the file "menu.json" (more info at https://www.npmjs.com/package/@commercetools-frontend/application-shell). In case you do need to test things out, you can pass a "mcProxyApiUrl" to your "env.json" and point it to e.g. "https://mc.commercetools.com/api/graphql"`;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice.

…rs (#292)

* feat: add handleApolloErrors HOC to automatically dispatch query errors

* refactor(with-applications-menu): remove error handling, fix props spreading

* chore(handle-apollo-errors): add prop-types shape

* refactor(handle-apollo-errors): renaming

Co-Authored-By: emmenko <nicola.molinari@commercetools.de>

* chore: format
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🙏 Status: Dev Review Waiting for technical reviews 💅 Type: Enhancement Improves existing code ⛑ Type: Refactoring
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants