All files / web/modules/admin/article/list DeleteArticleButton.tsx

0% Statements 0/31
0% Branches 0/3
0% Functions 0/4
0% Lines 0/30

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 76 77 78 79 80 81 82 83                                                                                                                                                                     
import Button from 'antd/lib/button';
import message from 'antd/lib/message';
import * as React from 'react';
import { Mutation } from 'react-apollo';
import { connect } from 'react-redux';
import { IArticle } from '../../../../../shared';
import { ArticleStatuses } from '../../../../../shared/ArticleStatuses';
import { deleteArticlePermissions } from '../../../../../shared/permissions';
import { IState } from '../../../../types';
import {
  selectHasPermission,
  selectIsLoading,
  selectUserSub,
} from '../../../auth/reducer';
import Spin from '../../../components/Spin';
import { SETTINGS_ARTICLES } from '../../queries';
import { DELETE_ARTICLE } from '../mutations';
 
interface IProps extends IArticle {
  userSub: string;
  hasPermission: (groups: string[]) => boolean;
  isLoading: boolean;
}
 
const DeleteArticleButton = ({
  _id,
  title,
  userSub,
  user,
  hasPermission,
  isLoading,
  status,
}: IProps) => {
  if (isLoading) {
    return <Spin size={14} />;
  }
 
  if (
    !hasPermission(deleteArticlePermissions) ||
    user.sub !== userSub ||
    status === ArticleStatuses.published
  ) {
    return null;
  }
 
  const handleClick = (deleteArticle: any) => async () => {
    try {
      await deleteArticle({ variables: { _id } });
      message.success(`Článek "${title}" byl smazán`);
    } catch (e) {
      message.error(e.message);
    }
  };
 
  return (
    <Mutation
      mutation={DELETE_ARTICLE}
      refetchQueries={[{ query: SETTINGS_ARTICLES, variables: { userSub } }]}
    >
      {(deleteArticle, { loading }) => {
        return (
          <Button
            type="danger"
            shape="circle"
            icon="delete"
            loading={loading}
            onClick={handleClick(deleteArticle)}
          />
        );
      }}
    </Mutation>
  );
};
 
export default connect(
  (state: IState) => ({
    userSub: selectUserSub(state),
    hasPermission: selectHasPermission(state),
    isLoading: selectIsLoading(state),
  }),
  {},
)(DeleteArticleButton);