All files / lib/pages/Festival index.tsx

0% Statements 0/47
0% Branches 0/4
0% Functions 0/23
0% Lines 0/43

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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
'use client';
 
import useModalState from '@lib/hooks/useModalState';
import { Button, Image, Switch, Table, Tag, message } from 'antd';
import styled from 'styled-components';
import {
  AdventureFestivalDto,
  AdventureFestivalDtoStatusEnum
} from '@uniquegood/realworld-adventure-interface';
import { ColumnsType } from 'antd/es/table';
import { useGetFestivalsQuery } from '@lib/api/queries';
import React from 'react';
import dayjs from 'dayjs';
import { festivalStatusToColor, festivalStatusToLabel } from '@lib/constants/festival';
import { useActivateFestivalMutation, useDeleteFestivalMutation } from '@lib/api/mutations';
import { useRouter } from 'next/navigation';
import { Swiper, SwiperSlide } from 'swiper/react';
import { Pagination } from 'swiper/modules';
import ConfirmDeleteModal from '@lib/components/ConfirmDeleteModal';
import CreateFestivalModal from './Modal/CreateFestivalModal';
import ModifyFestivalModal from './Modal/ModifyFestivalModal';
import 'swiper/css';
import 'swiper/css/pagination';
 
export default function FestivalsPage() {
  const router = useRouter();
 
  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 [currentPage, setCurrentPage] = React.useState(0);
  const [currentFestivalId, setCurrentFestivalId] = React.useState('');
 
  const { data: festivals } = useGetFestivalsQuery({ page: currentPage, size: 10 });
  const { mutateAsync: deleteFestival } = useDeleteFestivalMutation();
  const { mutateAsync: activateFestival } = useActivateFestivalMutation();
 
  const handleActivateChange = async (festivalId: string) => {
    const { success } = await activateFestival(festivalId);
 
    if (success) {
      message.success('축제 활성화 상태를 변경했습니다.');
    } else {
      message.success('축제 활성화 상태 변경에 실패했습니다.');
    }
  };
 
  const columns: ColumnsType<AdventureFestivalDto> = [
    {
      key: 'title',
      dataIndex: 'title',
      title: '제목'
    },
    {
      key: 'subTitle',
      dataIndex: 'subTitle',
      title: '부제목'
    },
    {
      key: 'status',
      dataIndex: 'status',
      title: '상태',
      render: (value: AdventureFestivalDtoStatusEnum) => (
        <Tag color={festivalStatusToColor[value]}>{festivalStatusToLabel[value]}</Tag>
      )
    },
    {
      key: 'startAt',
      dataIndex: 'startAt',
      title: '시작일',
      render: (value) => dayjs(value).format('YYYY-MM-DD HH:mm')
    },
    {
      key: 'endAt',
      dataIndex: 'endAt',
      title: '종료일',
      render: (value) => dayjs(value).format('YYYY-MM-DD HH:mm')
    },
    {
      key: 'imageUrls',
      dataIndex: 'imageUrls',
      title: '이미지',
      align: 'center',
      render: (value) => (
        // eslint-disable-next-line jsx-a11y/click-events-have-key-events
        <div onClick={(e) => e.stopPropagation()}>
          <Swiper
            modules={[Pagination]}
            centeredSlides
            slidesPerView={1}
            pagination
            style={{ width: '200px' }}
          >
            {value.map((url: string) => (
              <SwiperSlide>
                <Image src={url} width={100} height={100} />
              </SwiperSlide>
            ))}
          </Swiper>
        </div>
      )
    },
    {
      key: 'detailNotice',
      dataIndex: 'detailNotice',
      title: '공지'
    },
    {
      key: 'iconImageUrl',
      dataIndex: 'iconImageUrl',
      title: '지도 아이콘 이미지',
      render: (value) => (
        // eslint-disable-next-line jsx-a11y/click-events-have-key-events
        <span onClick={(e) => e.stopPropagation()}>
          <Image src={value} width={100} height={100} />
        </span>
      )
    },
    {
      key: 'prizeImageUrl',
      dataIndex: 'prizeImageUrl',
      title: '랭킹 이미지',
      render: (value) => (
        // eslint-disable-next-line jsx-a11y/click-events-have-key-events
        <span onClick={(e) => e.stopPropagation()}>
          <Image src={value} width={100} height={100} />
        </span>
      )
    },
    {
      key: 'address',
      dataIndex: 'address',
      title: '주소'
    },
    {
      key: 'limitDrawCount',
      dataIndex: 'limitDrawCount',
      title: '뽑기 최대 횟수'
    },
    {
      key: 'requiredNfcLogCount',
      dataIndex: 'requiredNfcLogCount',
      title: '뽑기에 필요한 NFC 개수'
    },
    {
      key: 'isActive',
      dataIndex: 'isActive',
      title: '활성화 여부',
      align: 'center',
      render: (value, record) => (
        // eslint-disable-next-line jsx-a11y/click-events-have-key-events
        <div onClick={(e) => e.stopPropagation()}>
          <Switch checked={value} onChange={() => handleActivateChange(record.id)} />
        </div>
      )
    },
    {
      title: '동작',
      render: (record) => (
        <ControlContainer>
          <Button
            onClick={(e) => {
              e.stopPropagation();
 
              router.push(`/festivals/${record.id}/nfc`);
            }}
          >
            NFC 관리
          </Button>
          <Button
            danger
            onClick={(e) => {
              e.stopPropagation();
 
              openConfirmModal({
                onOk: async () => {
                  const { success } = await deleteFestival(record.id);
 
                  if (success) {
                    closeConfirmModal();
                    message.success('축제를 삭제했습니다.');
                  } else {
                    message.error('축제 삭제에 실패했습니다.');
                  }
                },
                title: '정말 삭제하시겠어요?'
              });
            }}
          >
            삭제
          </Button>
        </ControlContainer>
      )
    }
  ];
 
  return (
    <>
      <HeaderContainer>
        <h1>축제 목록</h1>
        <HeaderControllerContainer>
          <Button onClick={() => openCreateModal({})}>축제 추가하기</Button>
        </HeaderControllerContainer>
      </HeaderContainer>
      <Table
        columns={columns}
        dataSource={festivals?.data?.content}
        pagination={{
          showSizeChanger: false,
          onChange: (page) => setCurrentPage(page - 1),
          total: festivals?.data?.totalElements
        }}
        scroll={{ x: 2400 }}
        onRow={(record) => ({
          onClick: () => {
            openModifyModal({});
            setCurrentFestivalId(record.id);
          }
        })}
        rowKey="id"
      />
      <CreateFestivalModal modalData={createModal} closeModal={closeCreateModal} />
      <ModifyFestivalModal
        modalData={modifyModal}
        closeModal={closeModifyModal}
        festivalId={currentFestivalId}
      />
      <ConfirmDeleteModal modalData={confirmModal} />
    </>
  );
}
 
const HeaderContainer = styled.div`
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
`;
 
const HeaderControllerContainer = styled.div`
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: min-content;
  gap: 8px;
`;
 
const ControlContainer = styled.div`
  display: grid;
  grid-auto-flow: column;
  gap: 8px;
`;