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 | 66x | import { Button, Popover } from 'antd'; import React, { ReactElement, useEffect, useState } from 'react'; import { CSVDownload } from 'react-csv'; import { ExclamationCircleOutlined, DownloadOutlined, } from '@ant-design/icons'; import { useTranslation } from 'react-i18next'; import { CsvDownloadProps, RecordProps } from '@props/RecordProps'; import { isDynamicField, isEnumColumn, isObjectType } from "@utils/ColumnsUtils"; import { IdColumnKeyInCsv, UseNULLForNullValueTypes } from '@config/base'; import { quoteQuote } from '@utils/StringUtils'; import { getCsvHeader } from '@utils/CsvUtils'; import { isActionColumn } from '@utils/ColumnsUtils'; type DownloadRangeType = "all" | "selected" | undefined; const CsvDownloadComponent = (props: CsvDownloadProps): ReactElement => { const { columns, domainName, objectValues, enumValues, allData, zIndex, selectedData, visiblePopover, setVisiblePopoverCallback } = props; const { t } = useTranslation(); const [downloadMode, setDownloadMode] = useState<DownloadRangeType>(); //FIXME remove duplicate with CsvUploadComponent and extract custom hook useEffect(() => { window.addEventListener("click", (e: MouseEvent): void => { if (visiblePopover === 'csvDownload') { setVisiblePopoverCallback(undefined, e); } }); return () => window.removeEventListener("click", (e: MouseEvent): void => { setVisiblePopoverCallback(undefined, e); }); }, [setVisiblePopoverCallback, visiblePopover]); const noRecordSelected = (selectedData.length === 0); const prepareDownloadData = (originData: Array<RecordProps>): Array<RecordProps> => { const result = [] as Array<RecordProps>; const keys = columns.filter(c => !isActionColumn(c.key)).map(c => c.key); originData.forEach((row: RecordProps): void => { //This variable holds the value write to CSV file const currentRowData = {} as RecordProps; //This variable hols list of column does not present in this row //We will add it's value as "NULL" if it's type is string const remainingProperties = Object.assign([], keys); for (const property in row) { if (!Object.prototype.hasOwnProperty.call(row, property)) { continue; } const columnMeta = columns.find(c => (c.key === property)); if (columnMeta?.key == null || isActionColumn(columnMeta.key)) { continue; } const { type, key, title } = columnMeta; const index = remainingProperties.indexOf(key); remainingProperties.splice(index, 1); if (property === "id") { currentRowData[IdColumnKeyInCsv] = row.id; } else { const rowValue = row[property]; if (isEnumColumn(type)) { const enumValue = enumValues[property]; if (null != enumValue && enumValue.length > 0) { const matchObject = enumValue.find(v => v.value === rowValue); currentRowData[key] = quoteQuote(matchObject?.label); } } else if (isObjectType(type)) { const { labelField } = columnMeta; const columnKey = `${key}.${labelField}`; const objectValue = objectValues[type]; const id = (rowValue == null) ? null : rowValue.id; const matchObject = (id == null) ? null : objectValue?.find(v => v.value === id); currentRowData[columnKey] = quoteQuote(matchObject?.label); } else if (isDynamicField(key)) { //For dynamic field, we use it's title as header in export CSV file currentRowData[title] = quoteQuote(rowValue); } else if (type === 'string') { currentRowData[key] = (rowValue == null) ? 'NULL' : quoteQuote(rowValue?.toString()); } else if (type !== 'array') { currentRowData[key] = quoteQuote(rowValue?.toString()); } } } remainingProperties.forEach(k => { const meta = columns.find(c => c.key === k); if (meta != null && UseNULLForNullValueTypes.includes(meta.type)) { currentRowData[k] = "NULL"; } }); currentRowData["DELETE_FLAG"] = 'N'; result.push(currentRowData); }); return result; }; useEffect(() => { if (downloadMode !== undefined) { setDownloadMode(undefined); } }, [downloadMode]); const visible = (visiblePopover === 'csvDownload'); return ( <Popover placement="bottom" trigger="click" open={visible} overlayStyle={{ zIndex: zIndex + 1 }} content={ <div> {downloadMode === 'selected' && <CSVDownload data={prepareDownloadData(selectedData)} filename={`${new Date().toISOString().slice(0, 10)}-${domainName}-export.csv`} enclosingCharacter={`"`} headers={getCsvHeader(columns)} key={Math.random()} /> } <Button type="primary" size="middle" className="download-button" disabled={noRecordSelected} onClick={() => setDownloadMode('selected')} > {noRecordSelected ? (<ExclamationCircleOutlined />) : (<DownloadOutlined />)} {noRecordSelected ? t("Selected data(double click to select rows)") : t("Selected data (# rows selected)", { num: selectedData.length })} </Button> {downloadMode === 'all' && <CSVDownload data={prepareDownloadData(allData)} filename={`${new Date().toISOString().slice(0, 10)}-${domainName}-export.csv`} enclosingCharacter={`"`} headers={getCsvHeader(columns)} key={Math.random()} /> } <Button type="primary" size="middle" className="download-button" onClick={() => setDownloadMode('all')} > <DownloadOutlined /> {t('All data on this page')} </Button> </div > } > <a href="/#" title={t("Download data on current page as CSV file")} onClick={(e) => setVisiblePopoverCallback(visible ? undefined : 'csvDownload', e)}> <DownloadOutlined className="link-icon" /> </a> </Popover > ); }; export default CsvDownloadComponent; |