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 | import { InboxOutlined } from '@ant-design/icons';
import { usePostFestivalNfcsMutation } from '@lib/api/mutations';
import {
ManageCreateOrUpdateNFCRegistryDto,
NFCRegistryInfoDto
} from '@uniquegood/realworld-adventure-interface';
import { Button, Modal, ModalProps, Table, UploadProps, message } from 'antd';
import { ColumnsType } from 'antd/es/table';
import Dragger from 'antd/es/upload/Dragger';
import Excel from 'exceljs';
import React from 'react';
interface ExcelModalProps {
modalData: ModalProps;
closeModal: () => unknown;
festivalId: string;
}
export default function ExcelModal({ modalData, closeModal, festivalId }: ExcelModalProps) {
const [excelData, setExcelData] = React.useState<ManageCreateOrUpdateNFCRegistryDto[]>();
const { mutateAsync } = usePostFestivalNfcsMutation(festivalId);
const handleRemoveClick = (e: React.MouseEvent, id: string) => {
e.stopPropagation();
setExcelData((prev) => prev?.filter((item) => item.nfcId !== id));
};
const columns: ColumnsType<Omit<NFCRegistryInfoDto, 'nfcRegistryId'>> = [
{
key: 'nfcId',
dataIndex: 'nfcId',
title: 'NFC 고유 ID'
},
{
key: 'latitude',
dataIndex: 'latitude',
title: '위도'
},
{
key: 'longitude',
dataIndex: 'longitude',
title: '경도'
},
{
key: 'description',
dataIndex: 'description',
title: '설명'
},
{
title: '동작',
render: (_, record) => (
<Button danger onClick={(e) => handleRemoveClick(e, record.nfcId)}>
삭제
</Button>
)
}
];
const props: UploadProps = {
name: 'file',
async beforeUpload(file) {
const wb = new Excel.Workbook();
const reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onload = () => {
const buffer = reader.result;
wb.xlsx.load(buffer as Buffer).then((workbook) => {
const data: ManageCreateOrUpdateNFCRegistryDto[] = [];
workbook.getWorksheet(1).eachRow((row, rowIndex) => {
if (rowIndex === 1) return;
if (!Array.isArray(row.values)) return;
row.values.some((value, index) => {
if (value) {
if (!Array.isArray(row.values)) return false;
const [nfcId, latitude, longitude, description] = row.values.slice(index);
const richDescription = description as unknown as object;
let modifiedDescription = description;
if (
richDescription &&
typeof richDescription !== 'string' &&
'richText' in richDescription
) {
modifiedDescription = (richDescription.richText as { text: string }[]).reduce(
(acc, cur) => {
if (cur.text && cur.text !== '') {
return acc + cur.text;
}
return acc;
},
''
);
}
data.push({
nfcId: nfcId as string,
latitude: latitude ? Number(latitude) : undefined,
longitude: longitude ? Number(longitude) : undefined,
description: modifiedDescription as string
});
return true;
}
return false;
});
});
setExcelData(data);
});
};
return false;
}
};
const handleSubmit = async () => {
try {
if (!excelData || excelData.length === 0) return;
const { success } = await mutateAsync(excelData.map((item) => item));
if (success) {
message.success('축제 NFC를 추가했습니다.');
closeModal();
} else {
throw new Error('failed to add questions');
}
} catch (e) {
message.error('축제 NFC를 추가하지 못했습니다. 형식을 확인해주세요.');
}
};
return (
<Modal
{...modalData}
title="엑셀 파일 추가"
width={1000}
afterClose={() => setExcelData([])}
onOk={handleSubmit}
okText="확인"
cancelText="닫기"
>
{excelData && excelData.length > 0 ? (
<Table
columns={columns}
dataSource={excelData}
rowKey="id"
pagination={{
pageSize: 10,
showSizeChanger: false
}}
/>
) : (
<Dragger
{...props}
accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
>
<p className="ant-upload-drag-icon">
<InboxOutlined />
</p>
<p className="ant-upload-text">
여기를 클릭하거나 드래그해서 엑셀 파일을 업로드 해보세요.
</p>
<p className="ant-upload-hint">.xlsx 파일 확장자를 지원합니다.</p>
</Dragger>
)}
</Modal>
);
}
|