|
| 1 | +import * as React from 'react' |
| 2 | +import { Action } from 'redux' |
| 3 | +import { remote, ipcRenderer } from 'electron' |
| 4 | + |
| 5 | +import Modal from './Modal' |
| 6 | +import TextInput from '../form/TextInput' |
| 7 | +import Buttons from './Buttons' |
| 8 | +import moment from 'moment' |
| 9 | +import ButtonInput from '../form/ButtonInput' |
| 10 | + |
| 11 | +interface ExportVersionProps { |
| 12 | + onDismissed: () => void |
| 13 | + peername: string |
| 14 | + name: string |
| 15 | + path: string |
| 16 | + title: string |
| 17 | + exportPath: string |
| 18 | + timestamp: Date |
| 19 | + setExportPath: (path: string) => Action |
| 20 | +} |
| 21 | + |
| 22 | +const ExportVersion: React.FunctionComponent<ExportVersionProps> = (props) => { |
| 23 | + const { |
| 24 | + onDismissed, |
| 25 | + peername, |
| 26 | + name, |
| 27 | + path, |
| 28 | + title, |
| 29 | + timestamp, |
| 30 | + exportPath: persistedExportPath, |
| 31 | + setExportPath: saveExportPath |
| 32 | + } = props |
| 33 | + |
| 34 | + const pathUrl = path === '' ? '' : `/at/${path}` |
| 35 | + const exportUrl = `http://localhost:2503/export/${peername}/${name}${pathUrl}?download=true&all=true` |
| 36 | + const [exportPath, setExportPath] = React.useState(persistedExportPath) |
| 37 | + const [dismissable, setDismissable] = React.useState(true) |
| 38 | + const [buttonDisabled, setButtonDisabled] = React.useState(true) |
| 39 | + |
| 40 | + const handleSubmit = () => { |
| 41 | + ipcRenderer.send('export', { url: exportUrl, directory: exportPath }) |
| 42 | + onDismissed() |
| 43 | + } |
| 44 | + |
| 45 | + React.useEffect(() => { |
| 46 | + // persist the exportPath |
| 47 | + if (exportPath !== '') { |
| 48 | + saveExportPath(exportPath) |
| 49 | + if (buttonDisabled) setButtonDisabled(false) |
| 50 | + } else { |
| 51 | + setButtonDisabled(true) |
| 52 | + } |
| 53 | + }, [exportPath]) |
| 54 | + |
| 55 | + const handleChanges = (name: string, value: any) => { |
| 56 | + if (value[value.length - 1] === ' ') { |
| 57 | + |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + const handlePathPickerDialog = (showFunc: () => void) => { |
| 62 | + new Promise(resolve => { |
| 63 | + setDismissable(false) |
| 64 | + resolve() |
| 65 | + }) |
| 66 | + .then(() => showFunc()) |
| 67 | + .then(() => setDismissable(true)) |
| 68 | + } |
| 69 | + |
| 70 | + const showDirectoryPicker = () => { |
| 71 | + const window = remote.getCurrentWindow() |
| 72 | + const directory: string[] | undefined = remote.dialog.showOpenDialog(window, { |
| 73 | + properties: ['createDirectory', 'openDirectory'] |
| 74 | + }) |
| 75 | + |
| 76 | + if (!directory) { |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + const selectedPath = directory[0] |
| 81 | + |
| 82 | + setExportPath(selectedPath) |
| 83 | + } |
| 84 | + |
| 85 | + return ( |
| 86 | + <Modal |
| 87 | + id="export_modal" |
| 88 | + title={'Export a Version of this Dataset'} |
| 89 | + onDismissed={onDismissed} |
| 90 | + onSubmit={() => {}} |
| 91 | + dismissable={dismissable} |
| 92 | + setDismissable={setDismissable} |
| 93 | + > |
| 94 | + <div className='content-wrap'> |
| 95 | + <div> |
| 96 | + <div className='content'> |
| 97 | + <div className='margin-bottom'> |
| 98 | + <p><strong>{peername}/{name}</strong></p> |
| 99 | + <p>{title} - <i>{moment(timestamp).format('MMMM Do YYYY, h:mm:ss a')}</i></p> |
| 100 | + </div> |
| 101 | + <div className='flex-space-between'> |
| 102 | + <TextInput |
| 103 | + name='savePath' |
| 104 | + label='Export Folder' |
| 105 | + labelTooltip='Qri will export the dataset to this folder' |
| 106 | + tooltipFor='modal-tooltip' |
| 107 | + type='' |
| 108 | + value={exportPath} |
| 109 | + onChange={handleChanges} |
| 110 | + maxLength={600} |
| 111 | + /> |
| 112 | + <div className='margin-left'><ButtonInput id='chooseSavePath' onClick={() => handlePathPickerDialog(showDirectoryPicker)} >Choose...</ButtonInput></div> |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + <p className='submit-message'> |
| 117 | + {!buttonDisabled && ( |
| 118 | + <span>Qri will save a zip of this dataset version to {exportPath} </span> |
| 119 | + )} |
| 120 | + </p> |
| 121 | + </div> |
| 122 | + <Buttons |
| 123 | + cancelText='cancel' |
| 124 | + onCancel={onDismissed} |
| 125 | + submitText='Export Version' |
| 126 | + onSubmit={handleSubmit} |
| 127 | + disabled={buttonDisabled} |
| 128 | + loading={false} |
| 129 | + /> |
| 130 | + </Modal> |
| 131 | + ) |
| 132 | +} |
| 133 | + |
| 134 | +export default ExportVersion |
0 commit comments