All files / web/modules/score Score.tsx

44% Statements 11/25
0% Branches 0/2
0% Functions 0/3
50% Lines 11/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 66 67 68 69 70 71 72 73 74 75 761x 1x 1x 1x   1x           1x 1x 1x 1x                     1x                                                                                 1x                  
import * as React from 'react';
import { Query } from 'react-apollo';
import { connect } from 'react-redux';
import { scorePermission } from '../../../shared/permissions';
import { IState } from '../../types';
import {
  selectHasPermission,
  selectIsLoading,
  selectUsername,
  selectUserSub,
} from '../auth/reducer';
import Spin from '../components/Spin';
import CreateScore from './create/CreateScore';
import DeleteScore from './delete/DeleteScore';
import { SCORE } from './queries';
 
interface IProps {
  username: string;
  userSub: string;
  itemId: string;
  hasPermission: (groups: string[]) => boolean;
  isLoading: boolean;
  refetchQueries: any[];
}
 
const Score = (props: IProps) => {
  if (props.isLoading) {
    return <Spin size={15} />;
  }
 
  if (!props.hasPermission(scorePermission)) {
    return null;
  }
 
  const { itemId, userSub } = props;
 
  return (
    <Query query={SCORE} variables={{ itemId, userSub }}>
      {({ loading, error, data }) => {
        if (error) {
          console.log(error);
          return null;
        }
 
        if (loading) {
          return <Spin size={15} />;
        }
 
        const value = data.score && data.score.value;
 
        return (
          <div>
            <DeleteScore
              refetchQueries={props.refetchQueries}
              userSub={props.userSub}
              itemId={props.itemId}
              value={value}
            />
            <CreateScore {...props} value={value} />
          </div>
        );
      }}
    </Query>
  );
};
 
export default connect(
  (state: IState) => ({
    username: selectUsername(state),
    userSub: selectUserSub(state),
    hasPermission: selectHasPermission(state),
    isLoading: selectIsLoading(state),
  }),
  {},
)(Score);