All files / src/components/color/status StatusColor.tsx

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

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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112                                                                                                                                                                                                                               
import { FC, useState } from 'react';
import { Loading } from '../..';
import ReactModal from 'react-modal';
import { OkResponse } from '../../../models/okResponse';
import { ErrorResponse } from '../../../models/errorResponse';
import { Pending } from '../../../models/pending';
import { Status } from '../../../models/status';
import StatusColorButton from './StatusColorButton';
import StatusColorModalButton from './StatusColorModalButton';
import StatusColorResult from './StatusColorResult';
import styled from 'styled-components';
 
ReactModal.setAppElement('#root');
 
type Props = {
  id: number;
  hex_color_code: string;
  status: Status;
};
 
const StyledLabel = styled.p`
  font-size: 2.6rem;
  margin: 0 0 60px 0;
  padding: 0;
  text-align: center;
`;
 
const StyledCancelButton = styled.button`
  width: 120px;
  padding: 5px 20px;
  background-color: #caad63;
  border: none;
  font-size: 1.6rem;
  cursor: pointer;
`;
 
const StyledButtonWrapper = styled.div`
  display: flex;
  justify-content: center;
  gap: 40px;
`;
 
const StatusColor: FC<Props> = (props) => {
  // set modal state
  const [modalIsOpen, setIsOpen] = useState<boolean>(false);
  // set register result
  const [registerResult, setRegisterResult] = useState<OkResponse | ErrorResponse | Pending | null>(null);
  // handle modal open/close
  const handleOpen = (): void => {
    setIsOpen(true);
  };
  const handleClose = (): void => {
    setIsOpen(false);
    setRegisterResult(null);
  };
  const handleRedirect = (): void => {
    setIsOpen(false);
    setRegisterResult(null);
    window.location.reload();
  };
  return (
    <>
      <StatusColorModalButton setIsOpen={handleOpen} />
      <ReactModal
        isOpen={modalIsOpen}
        contentLabel="StatusColorModal"
        style={{
          overlay: {
            backgroundColor: 'rgba(0, 0, 0, 0.5)',
          },
          content: {
            top: '50%',
            left: '50%',
            transform: 'translate(-50%, -50%)',
            width: '90%',
            minWidth: '320px',
            maxWidth: '500px',
            height: '160px',
          },
        }}
      >
        {registerResult === null ? (
          // 初期表示
          <>
            <StyledLabel>本当に変更しますか?</StyledLabel>
            <StyledButtonWrapper>
              <StyledCancelButton onClick={handleClose}>変更しない</StyledCancelButton>
              <StatusColorButton
                id={props.id}
                hex_color_code={props.hex_color_code}
                status={props.status}
                setResult={setRegisterResult}
              />
            </StyledButtonWrapper>
          </>
        ) : registerResult === 'pending' ? (
          // 処理中
          <Loading />
        ) : (
          // fetch結果
          <>
            <button onClick={handleRedirect}>Close</button>
            <StatusColorResult result={registerResult} />
          </>
        )}
      </ReactModal>
    </>
  );
};
 
export default StatusColor;