withTeams.tsx 1012 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import * as React from 'react';
  2. import TeamStore from 'app/stores/teamStore';
  3. import {Team} from 'app/types';
  4. import getDisplayName from 'app/utils/getDisplayName';
  5. type InjectedTeamsProps = {
  6. teams: Team[];
  7. };
  8. type State = {
  9. teams: Team[];
  10. };
  11. /**
  12. * Higher order component that uses TeamStore and provides a list of teams
  13. */
  14. function withTeams<P extends InjectedTeamsProps>(
  15. WrappedComponent: React.ComponentType<P>
  16. ) {
  17. class WithTeams extends React.Component<Omit<P, keyof InjectedTeamsProps>, State> {
  18. static displayName = `withTeams(${getDisplayName(WrappedComponent)})`;
  19. state = {
  20. teams: TeamStore.getAll(),
  21. };
  22. componentWillUnmount() {
  23. this.unsubscribe();
  24. }
  25. unsubscribe = TeamStore.listen(
  26. () => this.setState({teams: TeamStore.getAll()}),
  27. undefined
  28. );
  29. render() {
  30. return (
  31. <WrappedComponent {...(this.props as P)} teams={this.state.teams as Team[]} />
  32. );
  33. }
  34. }
  35. return WithTeams;
  36. }
  37. export default withTeams;