File size: 1,895 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// @flow
import React from 'react';
import compose from 'recompose/compose';
import type { Match } from 'react-router-dom';
import { connect } from 'react-redux';
import type { UserInfoType } from 'shared/graphql/fragments/user/userInfo';
import viewNetworkHandler, {
  type ViewNetworkHandlerType,
} from 'src/components/viewNetworkHandler';
import {
  getCommunityByMatch,
  type GetCommunityType,
} from 'shared/graphql/queries/community/getCommunity';
import { withCurrentUser } from 'src/components/withCurrentUser';
import Login from 'src/views/login';
import { CLIENT_URL } from 'src/api/constants';
import { ErrorView, LoadingView } from 'src/views/viewHelpers';
import { SignedIn } from './containers/signedIn';
import { PrivateCommunity } from './containers/privateCommunity';

type Props = {
  ...$Exact<ViewNetworkHandlerType>,
  currentUser: ?UserInfoType,
  match: Match,
  data: {
    community: GetCommunityType,
  },
};

const CommunityView = (props: Props) => {
  const { isLoading, queryVarIsChanging, hasError, currentUser } = props;

  if (isLoading || queryVarIsChanging) return <LoadingView />;

  const { community } = props.data;

  if (!community || hasError) return <ErrorView />;

  const { isPrivate, communityPermissions } = community;
  const { isMember, isBlocked } = communityPermissions;

  if (currentUser && !isBlocked && !isPrivate && !isMember)
    return <SignedIn community={community} />;

  if (isBlocked) return <ErrorView />;

  if (isPrivate && !currentUser) {
    const redirectPath = `${CLIENT_URL}/${community.slug}`;
    return <Login redirectPath={redirectPath} />;
  }

  if (isPrivate && currentUser && !isMember) {
    return <PrivateCommunity community={community} />;
  }

  return <SignedIn community={community} />;
};

export default compose(
  withCurrentUser,
  getCommunityByMatch,
  viewNetworkHandler,
  connect()
)(CommunityView);