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 | 4x | // @flow import React from 'react'; import RefId from 'canner-ref-id'; import { Modal, Form, Button, Upload, Icon } from 'antd'; import { get, set } from 'lodash'; import { withApollo } from 'react-apollo'; import {FormattedMessage, injectIntl} from 'react-intl'; const Dragger = Upload.Dragger; type Props = { form: Object, fileName: string, triggerModal: Function, fields: Array<Object>, client: any, intl: Object, title: string, visible: boolean, query: Object, keyName: string } type State = { uploading: boolean } // $FlowFixMe @injectIntl @withApollo @Form.create() export default class ImportModal extends React.Component<Props, State> { state = { uploading: false }; download = (e: Event) => { e.preventDefault(); const { fileName, triggerModal, fields, } = this.props; const csv = genCSV([], fields); download(fileName, csv); triggerModal(); } handleCancel = () => { this.props.triggerModal(); } customRequest = ({ onSuccess, file }: Object) => { const reader = new FileReader(); reader.readAsText(file); reader.onload = (e) => { const csv = parseCSV(e.target.result, '\n', ','); this.request(csv); onSuccess('done'); }; } request(csv: Array<Array<string>>) { const {onChange, fields, keyName} = this.props; const refId = new RefId(keyName); csv.slice(1).forEach(values => { const payload = fields.reduce((result, field, index) => { let value = values[index]; if (field.type === 'number') { value = Number(value); } else if (field.type === 'boolean') { value = Boolean(value); } set(result, field.keyName, value); return result; }, {}); onChange(refId, 'create', payload); }); } uploadChange = ({file}: Object) => { if (file.status === 'done') { setTimeout(this.props.triggerModal, 700); } } render() { const {visible } = this.props; const {uploading} = this.state; return ( <Modal title={<FormattedMessage id="actions.export.modal.title" />} visible={visible} footer={null} > <React.Fragment> <div style={{marginBottom: 24}}> <span>get the template csv: </span> <Button onClick={this.download}> <Icon type="download" /> download </Button> </div> <Dragger name="file" customRequest={this.customRequest} accept="text/csv" onChange={this.uploadChange} > <Button> <Icon type="upload" /> Click to upload </Button> </Dragger> </React.Fragment> </Modal> ); } } function parseCSV(text, lineTerminator, cellTerminator) { const rows = []; //break the lines apart var lines = text.split(lineTerminator); for(var j = 0; j<lines.length; j++){ const values = []; if(lines[j] != ""){ //create a table row //split the rows at the cellTerminator character var information = lines[j].split(cellTerminator); for(var k = 0; k < information.length; k++){ //append the cell to the row values.push(information[k]); } } rows.push(values); } return rows; } function genCSV(data: Array<Object>, fields: Array<Object>) { const fieldNames = fields.map(field => field.title || field.keyName); const rows = [fieldNames]; data.forEach((datum) => { const values = []; fields.forEach((field) => { let value = get(datum, field.keyName); if (field.render) { value = field.render(value); } values.push(value); }); rows.push(values); }); let csvContent = 'data:text/csv;charset=utf-8,'; rows.forEach((rowArray) => { const row = rowArray.join(','); csvContent += `${row}\r\n`; }); return csvContent; } function download(fileName, csvContent) { const encodedUri = encodeURI(csvContent); const link = document.createElement('a'); link.setAttribute('href', encodedUri); link.setAttribute('download', `${fileName}.csv`); link.innerHTML = ''; document.body && document.body.appendChild(link); // Required for FF link.click(); } |