frontend-platform icon indicating copy to clipboard operation
frontend-platform copied to clipboard

Implement Gatsby-friendly initialization wrapper

Open davidjoy opened this issue 4 years ago • 0 comments

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.

davidjoy avatar Mar 26 '21 17:03 davidjoy