All files / lib/pages/QuestionGroup index.tsx

0% Statements 0/46
0% Branches 0/8
0% Functions 0/13
0% Lines 0/46

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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210                                                                                                                                                                                                                                                                                                                                                                                                                                   
'use client';
 
import {
  useDeleteQuestionGroupMutation,
  usePatchActivateQuestionGroupMutation
} from '@lib/api/mutations';
import { useGetQuestionGroupsQuery } from '@lib/api/queries';
import { HeaderContainer } from '@lib/components/styledComponents';
import useModalState from '@lib/hooks/useModalState';
import { QuestionGroupDetailResponseDto } from '@uniquegood/realworld-adventure-interface';
import { Button, Switch, Table, message } from 'antd';
import { ColumnsType } from 'antd/es/table';
import axios from 'axios';
import React from 'react';
import styled from 'styled-components';
import ConfirmDeleteModal from '@lib/components/ConfirmDeleteModal';
import CreateQuestionGroupModal from './Modal/CreateQuestionGroupModal';
import ModifyQuestionGroupModal from './Modal/ModifyQuestionGroupModal';
import ManageQuestionModal from './Modal/ManageQuestionModal';
 
const PAGE_SIZE = 20;
 
export default function QuestionGroupPage() {
  const [currentPage, setCurrentPage] = React.useState(0);
  const [currentQuestionGroupId, setCurrentQuestionGroupId] = React.useState('');
 
  const { data: questionGroups } = useGetQuestionGroupsQuery({
    page: currentPage,
    size: PAGE_SIZE
  });
  const { mutateAsync: patchQuestionGroupStatus } = usePatchActivateQuestionGroupMutation();
  const { mutateAsync: deleteQuestionGroup } = useDeleteQuestionGroupMutation();
 
  const {
    openModal: openCreateModal,
    closeModal: closeCreateModal,
    modal: createModal
  } = useModalState();
  const {
    openModal: openModifyModal,
    closeModal: closeModifyModal,
    modal: modifyModal
  } = useModalState();
  const {
    openModal: openConfirmModal,
    closeModal: closeConfirmModal,
    modal: confirmModal
  } = useModalState();
  const {
    openModal: openManageModal,
    closeModal: closeManageModal,
    modal: manageModal
  } = useModalState();
 
  const handleSwitchClick = async (questionGroupId: string) => {
    try {
      const data = await patchQuestionGroupStatus(questionGroupId);
 
      if (data.success) {
        message.success('랜덤 문제 그룹 활성화 상태가 변경되었습니다.');
      }
    } catch (e) {
      if (axios.isAxiosError(e)) {
        console.error(e);
 
        message.error(e.response?.data.message);
      }
    }
  };
 
  const handleButtonClick = () => {
    openCreateModal({});
  };
 
  const handleDeleteClick = async (questionGroupId: string) => {
    openConfirmModal({
      title: '정말 삭제하시겠어요?',
      onOk: async () => {
        try {
          const data = await deleteQuestionGroup(questionGroupId);
 
          if (data.success) {
            message.success('랜덤 문제 그룹이 삭제되었습니다.');
 
            closeConfirmModal();
          }
        } catch (e) {
          if (axios.isAxiosError(e)) {
            console.error(e);
 
            message.error(e.response?.data.message);
          }
        }
      }
    });
  };
 
  const columns: ColumnsType<Omit<QuestionGroupDetailResponseDto, 'questionList'>> = [
    {
      key: 'id',
      dataIndex: 'id',
      title: 'ID'
    },
    {
      key: 'title',
      dataIndex: 'title',
      title: '제목'
    },
    {
      key: 'description',
      dataIndex: 'description',
      title: '설명'
    },
    {
      key: 'isActive',
      dataIndex: 'isActive',
      title: '활성화 여부',
      render: (isActive, record) => (
        <Switch
          checked={isActive}
          onClick={(_, e) => {
            e.stopPropagation();
 
            handleSwitchClick(record.id);
          }}
        />
      )
    },
    {
      title: '동작',
      render: (record) => {
        return (
          <>
            <Button
              size="small"
              onClick={(e) => {
                e.stopPropagation();
 
                setCurrentQuestionGroupId(record.id);
 
                openManageModal({});
              }}
              style={{ marginRight: 4 }}
            >
              연결된 문제 관리
            </Button>
            <Button
              danger
              size="small"
              onClick={(e) => {
                e.stopPropagation();
 
                handleDeleteClick(record.id);
              }}
            >
              삭제
            </Button>
          </>
        );
      }
    }
  ];
 
  return (
    <>
      <HeaderContainer>
        <h1>랜덤 문제 그룹 관리</h1>
      </HeaderContainer>
      <ControlContainer>
        <Button onClick={handleButtonClick}>랜덤 문제 그룹 생성</Button>
      </ControlContainer>
      <div>
        <Table
          dataSource={questionGroups?.data?.content}
          columns={columns}
          pagination={{
            total: questionGroups?.data?.totalElements,
            onChange: (page) => setCurrentPage(page - 1),
            pageSize: PAGE_SIZE
          }}
          onRow={(record) => ({
            onClick: () => {
              setCurrentQuestionGroupId(record.id);
 
              openModifyModal({});
            }
          })}
        />
      </div>
      <CreateQuestionGroupModal modalData={createModal} closeModal={closeCreateModal} />
      <ModifyQuestionGroupModal
        modalData={modifyModal}
        closeModal={closeModifyModal}
        questionGroupId={currentQuestionGroupId}
      />
      <ConfirmDeleteModal modalData={confirmModal} />
      <ManageQuestionModal
        modalData={manageModal}
        closeModal={closeManageModal}
        questionGroupId={currentQuestionGroupId}
      />
    </>
  );
}
 
const ControlContainer = styled.div`
  margin-bottom: 16px;
  text-align: right;
`;