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 | 'use client';
import { useGetAchievementsQuery, useGetCampaignQuery } from '@lib/api/queries';
import useModalState from '@lib/hooks/useModalState';
import { Button, Tooltip, message, Switch } from 'antd';
import Table, { ColumnsType } from 'antd/es/table';
import styled from 'styled-components';
import React from 'react';
import { useActivationAchievementMutation, useDeleteAchievementMutation } from '@lib/api/mutations';
import { ManageAchievementDto } from '@uniquegood/realworld-adventure-interface';
import { useRouter } from 'next/navigation';
import ConfirmDeleteModal from '@lib/components/ConfirmDeleteModal';
import CreateModal from './Modal/CreateModal';
import ModifyModal from './Modal/ModifyModal';
interface AchievementPageProps {
campaignId: string;
questId: string;
}
export default function AchievementPage({ campaignId, questId }: AchievementPageProps) {
const [currentData, setCurrentData] = React.useState<ManageAchievementDto>();
const [page, setPage] = React.useState(0);
const [size] = React.useState(20);
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 { data: campaignData } = useGetCampaignQuery({ campaignId });
const { data } = useGetAchievementsQuery({ campaignId, questId, page, size });
const { mutateAsync } = useDeleteAchievementMutation();
const { mutateAsync: mutateActivateAsync, isLoading: isLoadingActivate } =
useActivationAchievementMutation();
const handleRemoveClick = (event: React.MouseEvent<HTMLElement>, achievementId: string) => {
event.stopPropagation();
openConfirmModal({
title: '정말로 삭제하시겠어요?',
onOk: async () => {
try {
const { success } = await mutateAsync({ campaignId, questId, achievementId });
if (success) {
closeConfirmModal();
message.success('업적을 삭제했습니다.');
}
} catch (e) {
console.error(e);
}
}
});
};
const handleChangeIsActive = async (record: ManageAchievementDto) => {
try {
const { success } = await mutateActivateAsync({
campaignId,
questId,
achievementId: record.achievementId
});
if (success) {
message.success('활성화 여부를 수정했습니다.');
}
} catch (e) {
console.error(e);
}
};
const columns: ColumnsType<ManageAchievementDto> = [
{
key: 'id',
dataIndex: 'achievementId',
title: 'ID',
width: 200,
ellipsis: true,
render: (value) => {
return (
<Tooltip title={value} placement="topLeft">
{value}
</Tooltip>
);
}
},
{
key: 'title',
dataIndex: 'title',
title: '이름'
},
{
key: 'description',
dataIndex: 'description',
title: '설명',
ellipsis: true
},
{
key: 'checkPoint',
dataIndex: 'checkPoint',
title: '체크포인트',
width: 125,
align: 'center'
},
{
key: 'reward',
title: '보상',
width: 150,
align: 'center',
render: (value, record) => {
return (
// eslint-disable-next-line jsx-a11y/click-events-have-key-events
<div
onClick={(e) => {
e.stopPropagation();
router.push(`/rewards/${record.reward.id}`);
}}
>
<Button type="link">{record.reward?.name}</Button>
</div>
);
}
},
{
key: 'isActive',
dataIndex: 'isActive',
title: '활성화',
width: 100,
render: (value, record) => {
return (
// eslint-disable-next-line jsx-a11y/click-events-have-key-events
<div onClick={(e) => e.stopPropagation()}>
<Switch
checked={value}
disabled={isLoadingActivate}
onChange={() => handleChangeIsActive(record)}
/>
</div>
);
}
},
{
title: '동작',
width: '100px',
render: (value, record) => {
return (
<Button danger size="small" onClick={(e) => handleRemoveClick(e, record.achievementId)}>
삭제
</Button>
);
}
}
];
return (
<>
<HeaderContainer>
<h1>{campaignData?.data?.title || '...'}의 업적 목록</h1>
<HeaderControllerContainer>
<Button onClick={() => openCreateModal({})}>업적 추가하기</Button>
</HeaderControllerContainer>
</HeaderContainer>
<Table
columns={columns}
dataSource={data?.data?.content || []}
rowKey="id"
loading={!data?.data?.content}
pagination={{
total: data?.data?.totalElements,
pageSize: 20,
onChange: (page) => setPage(page - 1)
}}
onRow={(record) => {
return {
onClick: () => {
setCurrentData(record);
openModifyModal({});
}
};
}}
/>
<CreateModal
campaignId={campaignId}
questId={questId}
modalData={createModal}
closeModal={closeCreateModal}
/>
{currentData && (
<ModifyModal
modalData={modifyModal}
closeModal={closeModifyModal}
campaignId={campaignId}
questId={questId}
achievementId={currentData.achievementId}
/>
)}
<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;
`;
|