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 | 'use client';
import { useGetTreasureTypesQuery } from '@lib/api/queries';
import { TreasureTypeResponseDto } from '@uniquegood/realworld-adventure-interface';
import { Button, Table, Tooltip, message, Image, Input, Switch } from 'antd';
import { ColumnsType } from 'antd/es/table';
import useModalState from '@lib/hooks/useModalState';
import React from 'react';
import styled from 'styled-components';
import {
useDeleteTreasureTypeMutation,
usePatchTreasureTypeForChangeStatusMutation
} from '@lib/api/mutations';
import { useRouter } from 'next/navigation';
import ConfirmDeleteModal from '@lib/components/ConfirmDeleteModal';
import CreateModal from './Modal/TreasureType/CreateModal';
export default function TreasurePage() {
const [keyword, setKeyword] = React.useState('');
const { data, isLoading } = useGetTreasureTypesQuery({ keyword });
const { mutateAsync: patchTreasureTypeForChangeStatus } =
usePatchTreasureTypeForChangeStatusMutation();
const { mutateAsync } = useDeleteTreasureTypeMutation();
const router = useRouter();
const {
openModal: openCreateModal,
closeModal: closeCreateModal,
modal: createModal
} = useModalState();
const {
openModal: openConfirmModal,
closeModal: closeConfirmModal,
modal: confirmModal
} = useModalState();
const handleToggleClick = async (value: boolean, record: TreasureTypeResponseDto) => {
try {
const data = await patchTreasureTypeForChangeStatus({
...record,
treasureTypeId: record.id,
isActive: value
});
if (data.success) {
message.success('보물 활성화 상태를 변경했습니다.');
} else {
message.error('보물 활성화 상태 변경에 실패했습니다.');
}
} catch (e) {
console.error(e);
message.error('보물 활성화 상태 변경에 실패했습니다.');
}
};
const handleDeleteClick = (e: React.MouseEvent, record: TreasureTypeResponseDto) => {
e.stopPropagation();
openConfirmModal({
title: '정말 삭제하시겠어요?',
onOk: async () => {
const { success } = await mutateAsync(record.id);
if (success) {
message.success('보물 타입이 삭제되었습니다.');
closeConfirmModal();
} else {
message.success('보물 타입 삭제에 실패하였습니다.');
}
},
okButtonProps: {
danger: true
},
okText: '삭제',
cancelText: '닫기'
});
};
const handleSearch = (value: string) => {
setKeyword(value);
};
const columns: ColumnsType<TreasureTypeResponseDto> = [
{
key: 'id',
dataIndex: 'id',
title: 'ID',
width: 200,
ellipsis: true,
render: (value) => {
return <Tooltip title={value}>{value}</Tooltip>;
}
},
{
key: 'name',
dataIndex: 'name',
title: '이름'
},
{
key: 'point',
dataIndex: 'point',
title: '포인트'
},
{
key: 'treasureImageUrl',
dataIndex: 'treasureImageUrl',
title: '보물 이미지',
width: 100,
align: 'center',
render: (value) => {
return (
// eslint-disable-next-line jsx-a11y/click-events-have-key-events
<div
onClick={(e) => {
e.stopPropagation();
}}
>
<Image width={50} height={50} style={{ objectFit: 'contain' }} src={value} />
</div>
);
}
},
{
key: 'isActive',
dataIndex: 'isActive',
title: '활성화 여부',
render: (value, record) => (
// eslint-disable-next-line jsx-a11y/click-events-have-key-events
<div onClick={(e) => e.stopPropagation()}>
<Switch checked={value} onClick={(value) => handleToggleClick(value, record)} />
</div>
)
},
{
title: '동작',
render: (record) => {
return (
<Button danger size="small" onClick={(e) => handleDeleteClick(e, record)}>
삭제
</Button>
);
}
}
];
return (
<>
<HeaderContainer>
<h1>보물 관리</h1>
<HeaderControllerContainer>
<Input.Search
placeholder="검색어를 입력해주세요."
onSearch={handleSearch}
enterButton
style={{ width: '300px' }}
/>
<Button onClick={() => openCreateModal({})}>보물 타입 생성</Button>
</HeaderControllerContainer>
</HeaderContainer>
<Table
dataSource={data?.data}
columns={columns}
loading={isLoading}
rowKey="id"
onRow={(record) => {
return {
onClick: () => {
router.push(`/treasures/${record.id}`);
}
};
}}
pagination={false}
/>
<CreateModal modalData={createModal} closeModal={closeCreateModal} />
<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;
`;
|