All files / lib/pages/Campaign/Detail index.tsx

0% Statements 0/32
0% Branches 0/10
0% Functions 0/9
0% Lines 0/31

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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144                                                                                                                                                                                                                                                                                               
'use client';
 
import { useGetCampaignQuery, useGetCampaignQuestionGroupsQuery } from '@lib/api/queries';
import useModalState from '@lib/hooks/useModalState';
import { Button, Switch, Table, message } from 'antd';
import { QuestionGroupResponseDto } from '@uniquegood/realworld-adventure-interface';
import { ColumnsType } from 'antd/es/table';
import {
  useDeleteCampaignQuestionGroupMutation,
  usePatchActivateCampaignQuestionGroupMutation
} from '@lib/api/mutations';
import axios from 'axios';
import { HeaderContainer } from '@lib/components/styledComponents';
import { styled } from 'styled-components';
import ConfirmDeleteModal from '@lib/components/ConfirmDeleteModal';
import MappingQuestionModal from '../Modal/MappingQuestionModal';
 
type CampaignQuestionGroupPageProps = {
  campaignId: string;
};
 
export default function CampaignQuestionGroupPage({ campaignId }: CampaignQuestionGroupPageProps) {
  const { data: campaign } = useGetCampaignQuery({ campaignId });
  const { data: questionGroups, isLoading } = useGetCampaignQuestionGroupsQuery({ campaignId });
  const { mutateAsync: patchCampaignQuestionGroupStatus } =
    usePatchActivateCampaignQuestionGroupMutation({ campaignId });
  const { mutateAsync: deleteCampaignQuestionGroup } = useDeleteCampaignQuestionGroupMutation({
    campaignId
  });
 
  const { openModal, closeModal, modal } = useModalState();
  const {
    openModal: openConfirmModal,
    closeModal: closeConfirmModal,
    modal: confirmModal
  } = useModalState();
 
  const handleSwitchClick = async (questionGroupId: string) => {
    try {
      const data = await patchCampaignQuestionGroupStatus(questionGroupId);
 
      if (data.success) {
        message.success('랜덤 문제 그룹 활성화 상태가 변경되었습니다.');
      }
    } catch (e) {
      if (axios.isAxiosError(e)) {
        console.error(e);
 
        message.error(e.response?.data.message);
      }
    }
  };
 
  const handleDeleteClick = async (campaignQuestionGroupId: string) => {
    try {
      const data = await deleteCampaignQuestionGroup(campaignQuestionGroupId);
 
      if (data.success) {
        message.success('랜덤 문제 그룹 연결이 제거되었습니다.');
 
        closeConfirmModal();
      }
    } catch (e) {
      if (axios.isAxiosError(e)) {
        console.error(e);
 
        message.error(e.response?.data.message);
      }
    }
  };
 
  const columns: ColumnsType<QuestionGroupResponseDto> = [
    {
      key: 'id',
      dataIndex: 'id',
      title: 'ID'
    },
    {
      key: 'title',
      dataIndex: 'title',
      title: '이름'
    },
    {
      key: 'description',
      dataIndex: 'description',
      title: '설명'
    },
    {
      key: 'isActive',
      dataIndex: 'isActive',
      title: '활성화 여부',
      align: 'center',
      render: (isActive, record) => (
        <Switch checked={isActive} onClick={() => handleSwitchClick(record.id)} />
      )
    },
    {
      title: '동작',
      align: 'center',
      render: (record) => {
        return (
          <Button
            danger
            size="small"
            onClick={() => {
              openConfirmModal({
                title: '정말 삭제하시겠어요?',
                onOk: () => handleDeleteClick(record.id)
              });
            }}
          >
            삭제
          </Button>
        );
      }
    }
  ];
 
  return (
    <>
      <div>
        <HeaderContainer>
          <h1>[{campaign?.data?.title}] 랜덤 문제 그룹 관리</h1>
        </HeaderContainer>
        <ControllerContainer>
          <Button onClick={() => openModal({})}>랜덤 문제 그룹 연결하기</Button>
        </ControllerContainer>
        {questionGroups?.data?.length === 0 ? (
          <div>연결된 랜덤 문제 그룹이 없습니다.</div>
        ) : (
          <Table dataSource={questionGroups?.data} columns={columns} loading={isLoading} />
        )}
      </div>
      <MappingQuestionModal modalData={modal} closeModal={closeModal} campaignId={campaignId} />
      <ConfirmDeleteModal modalData={confirmModal} />
    </>
  );
}
 
const ControllerContainer = styled.div`
  margin-bottom: 16px;
  text-align: right;
`;