All files / src/components/delete_item DeleteItem.tsx

0% Statements 0/54
0% Branches 0/1
0% Functions 0/1
0% Lines 0/54

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 84 85 86 87 88                                                                                                                                                                               
import { FC, useState } from 'react';
import { Pending } from '../../models/pending';
import { ErrorResponse } from '../../models/errorResponse';
import { OkResponse } from '../../models/okResponse';
import { Loading } from '..';
import ReactModal from 'react-modal';
import DeleteItemButton from './DeleteItemButton';
import DeleteItemResult from './DeleteItemResult';
import styled from 'styled-components';
import { TfiClose } from 'react-icons/tfi';
import { Link } from 'react-router-dom';
 
type Props = {
  id: string;
  isHidden: boolean;
};
 
ReactModal.setAppElement('#root');
 
const StyledCloseButton = styled.button`
  height: 30px;
  width: 30px;
  padding: 0;
  margin: 0;
  background-color: rgba(0, 0, 0, 0);
  border: none;
  cursor: pointer;
`;
 
const StyledCloseButtonWrapper = styled.div`
  display: flex;
  justify-content: flex-end;
  width: 100%;
`;
 
const DeleteItem: FC<Props> = (props) => {
  // set modal state
  const [modalIsOpen, setIsOpen] = useState<boolean>(false);
  // set delete result
  const [result, setResult] = useState<OkResponse | ErrorResponse | Pending | null>(null);
  return (
    <>
      <DeleteItemButton id={props.id} isHidden={props.isHidden} setIsOpen={setIsOpen} setResult={setResult} />
      <ReactModal
        isOpen={modalIsOpen}
        contentLabel="DeleteItemModal"
        style={{
          overlay: {
            backgroundColor: 'rgba(0, 0, 0, 0.5)',
          },
          content: {
            top: '50%',
            left: '50%',
            right: 'auto',
            bottom: 'auto',
            transform: 'translate(-50%, -50%)',
            width: '90%',
            minWidth: '320px',
            maxWidth: '900px',
          },
        }}
      >
        {result === null ? (
          // 初期表示
          <></>
        ) : result === 'pending' ? (
          // 処理中
          <Loading />
        ) : (
          // fetch結果
          <>
            <StyledCloseButtonWrapper>
              <Link to={'/'}>
                <StyledCloseButton>
                  <TfiClose style={{ width: '30px', height: '30px' }} />
                </StyledCloseButton>
              </Link>
            </StyledCloseButtonWrapper>
            <DeleteItemResult result={result} />
          </>
        )}
      </ReactModal>
    </>
  );
};
 
export default DeleteItem;