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

Implement Gatsby-friendly initialization wrapper #157

Open
davidjoy opened this issue Mar 26, 2021 · 0 comments
Open

Implement Gatsby-friendly initialization wrapper #157

davidjoy opened this issue Mar 26, 2021 · 0 comments

Comments

@davidjoy
Copy link
Contributor

import React, { useEffect, useState } from 'react';
import { Provider } from 'react-redux';
import 'regenerator-runtime/runtime';
import { AppProvider } from '@edx/frontend-platform/react'; 
import {
  configure as configureAuth,
  AxiosJwtAuthService,
  getAuthenticatedHttpClient,
  fetchAuthenticatedUser
} from '@edx/frontend-platform/auth';
import { getConfig } from '@edx/frontend-platform/config';
import {
  configure as configureLogging,
  getLoggingService,
  NewRelicLoggingService,
} from '@edx/frontend-platform/logging';
import {
  configure as configureAnalytics,
  SegmentAnalyticsService
} from '@edx/frontend-platform/analytics';
import {
  configure as configureI18n,
} from '@edx/frontend-platform/i18n'
import store from './src/store';
// eslint-disable-next-line react/display-name,react/prop-types
export default ({ children }) => {
  const [ready, setReady] = useState(false);
  useEffect(() => {
    configureLogging(NewRelicLoggingService, {
      config: getConfig(),
    });
    configureAuth(AxiosJwtAuthService, {
      loggingService: getLoggingService(),
      config: getConfig(),
    });
    // Analytics
    configureAnalytics(SegmentAnalyticsService, {
      config: getConfig(),
      loggingService: getLoggingService(),
      httpClient: getAuthenticatedHttpClient(),
    });
    // Internationalization
    configureI18n({
      messages: {},
      config: getConfig(),
      loggingService: getLoggingService(),
    });
    fetchAuthenticatedUser().then(() => {
      setReady(true);
    })
  }, []);
  if (!ready) {
    return null;
  }
  return (
    <AppProvider store={store}>{children}</AppProvider>
  );
}```

This class, when used in `wrapRootElement` allows for basic usage of frontend-platform services in a Gatsby-based project.  The initialize function can't be used because it's async.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant