diff --git a/packages/component-faraday-ui/src/DateParser.js b/packages/component-faraday-ui/src/DateParser.js deleted file mode 100644 index cd35989c68f1947f7593e0176d9104dd4fa6ca5f..0000000000000000000000000000000000000000 --- a/packages/component-faraday-ui/src/DateParser.js +++ /dev/null @@ -1,12 +0,0 @@ -import React, { Fragment } from 'react' -import moment from 'moment' -import { Text } from 'pubsweet-component-faraday-ui' - -const DateParser = ({ label, date, format = 'DD.MM.YYYY' }) => ( - <Fragment> - {label && <Text>{label} </Text>} - {date && <Text mr={1}>{moment(date).format(format)}</Text>} - </Fragment> -) - -export default DateParser diff --git a/packages/component-faraday-ui/src/DateParser.md b/packages/component-faraday-ui/src/DateParser.md deleted file mode 100644 index a734dede59a3f900b073d89a555f6373e3c11ab9..0000000000000000000000000000000000000000 --- a/packages/component-faraday-ui/src/DateParser.md +++ /dev/null @@ -1,21 +0,0 @@ -Date parser without label - -```js -const date = 1534942072364 -;<DateParser date={date} /> -``` - -Date parser with label - -```js -const date = 1534942072364; -<DateParser date={date} label='Updated on: '/> -``` - -Date parser with format - -```js -const date = 1534942072364 -const format = 'DD.MM.YY' -;<DateParser date={date} format={format} /> -``` diff --git a/packages/component-faraday-ui/src/DownloadZipFiles.js b/packages/component-faraday-ui/src/DownloadZipFiles.js index 459f44b422ffc4a6a80e17089166f932bdd12a3e..3244bb90c623ccfecf7441f86da5a76a1733214d 100644 --- a/packages/component-faraday-ui/src/DownloadZipFiles.js +++ b/packages/component-faraday-ui/src/DownloadZipFiles.js @@ -1,107 +1,31 @@ -import React, { Fragment } from 'react' -import qs from 'querystring' -import PropTypes from 'prop-types' +import React from 'react' import { connect } from 'react-redux' import { Spinner } from '@pubsweet/ui' -import { compose, withHandlers, withState } from 'recompose' +import { compose, withState } from 'recompose' import { getUserToken, currentUserIsReviewer, } from 'pubsweet-component-faraday-selectors' +import { Item } from 'pubsweet-component-faraday-ui' -const ZipFiles = ({ disabled, fetching, children, downloadFiles }) => ( - <Fragment onClick={!disabled ? downloadFiles : null}> +import { withZipDownload } from './helpers' + +const DownloadZipFiles = ({ disabled, fetching, children, downloadFiles }) => ( + <Item + flex={0} + justify="flex-end" + mr={1} + onClick={!disabled ? downloadFiles : null} + > {fetching ? <Spinner /> : children} - </Fragment> + </Item> ) -const cache = {} - -const reviewerFiles = ['manuscripts', 'supplementary'] -const defaultFiles = [...reviewerFiles, 'coverLetter'] - -const DownloadZipFiles = compose( +export default compose( connect((state, { collectionId }) => ({ token: getUserToken(state), isReviewer: currentUserIsReviewer(state, collectionId), })), withState('fetching', 'setFetching', false), - withHandlers({ - createAnchorElement: () => (file, filename) => { - const url = URL.createObjectURL(file) - const a = document.createElement('a') - - a.href = url - a.download = filename - document.body.appendChild(a) - - return { - a, - url, - } - }, - removeAnchorElement: () => (a, url) => { - document.body.removeChild(a) - URL.revokeObjectURL(url) - }, - }), - withHandlers({ - downloadFiles: ({ - token, - isReviewer, - fragmentId, - setFetching, - archiveName, - createAnchorElement, - removeAnchorElement, - }) => () => { - const getUrl = `${ - window.location.origin - }/api/files/${fragmentId}?${qs.stringify({ - fileTypes: isReviewer ? reviewerFiles : defaultFiles, - })}` - if (cache[fragmentId]) { - const fileName = archiveName || `${fragmentId}-archive.zip` - - const { a, url } = createAnchorElement(cache[fragmentId], fileName) - a.click() - removeAnchorElement(a, url) - } else { - setFetching(fetching => true) - const xhr = new XMLHttpRequest() - xhr.onreadystatechange = function onXhrStateChange() { - if (this.readyState === 4) { - setFetching(fetching => false) - if (this.status >= 200 && this.status < 300) { - const fileName = archiveName || `${fragmentId}-archive.zip` - const f = new File([this.response], fileName, { - type: 'application/zip', - }) - cache[fragmentId] = f - - const { a, url } = createAnchorElement(f, fileName) - a.click() - removeAnchorElement(a, url) - } - } - } - xhr.open('GET', getUrl) - xhr.responseType = 'blob' - xhr.setRequestHeader('Authorization', `Bearer ${token}`) - xhr.send() - } - }, - }), -)(ZipFiles) - -DownloadZipFiles.propTypes = { - disabled: PropTypes.bool, - archiveName: PropTypes.string, - fragmentId: PropTypes.string.isRequired, -} - -DownloadZipFiles.defaultProps = { - disabled: false, -} - -export default DownloadZipFiles + withZipDownload, +)(DownloadZipFiles) diff --git a/packages/component-faraday-ui/src/helpers/index.js b/packages/component-faraday-ui/src/helpers/index.js index fdb100ed3c6ae307c079422034d3a5d61a359863..63fc161e32f2d37c749fec068c92dd09189b96a0 100644 --- a/packages/component-faraday-ui/src/helpers/index.js +++ b/packages/component-faraday-ui/src/helpers/index.js @@ -1,7 +1,7 @@ import * as validators from './formValidators' export { default as withFilePreview } from './withFilePreview' -export { default as withFileDownload } from './withFileDownload' +export * from './withFileDownload' export { default as withNativeFileDrop } from './withNativeFileDrop' export { default as withFileSectionDrop } from './withFileSectionDrop' diff --git a/packages/component-faraday-ui/src/helpers/withFileDownload.js b/packages/component-faraday-ui/src/helpers/withFileDownload.js index 132ad70c96698851eef1ab8f1836d926a9835d11..8ed0e5fac29b49e451b257208f9a20ab446a8747 100644 --- a/packages/component-faraday-ui/src/helpers/withFileDownload.js +++ b/packages/component-faraday-ui/src/helpers/withFileDownload.js @@ -1,6 +1,10 @@ import qs from 'querystring' import { withHandlers } from 'recompose' +const cache = {} +const reviewerFiles = ['manuscripts', 'supplementary'] +const defaultFiles = [...reviewerFiles, 'coverLetter'] + const createAnchorElement = (file, filename) => { const url = URL.createObjectURL(file) const a = document.createElement('a') @@ -20,7 +24,7 @@ const removeAnchorElement = (a, url) => { URL.revokeObjectURL(url) } -export default withHandlers({ +export const withFileDownload = withHandlers({ downloadFile: () => ({ fileId, token, fileName = 'file' }) => { if (!token) return @@ -48,3 +52,49 @@ export default withHandlers({ xhr.send() }, }) + +export const withZipDownload = withHandlers({ + downloadFiles: ({ + token, + isReviewer, + fragmentId, + setFetching, + archiveName, + }) => () => { + const getUrl = `${ + window.location.origin + }/api/files/${fragmentId}?${qs.stringify({ + fileTypes: isReviewer ? reviewerFiles : defaultFiles, + })}` + if (cache[fragmentId]) { + const fileName = archiveName || `${fragmentId}-archive.zip` + + const { a, url } = createAnchorElement(cache[fragmentId], fileName) + a.click() + removeAnchorElement(a, url) + } else { + setFetching(fetching => true) + const xhr = new XMLHttpRequest() + xhr.onreadystatechange = function onXhrStateChange() { + if (this.readyState === 4) { + setFetching(fetching => false) + if (this.status >= 200 && this.status < 300) { + const fileName = archiveName || `${fragmentId}-archive.zip` + const f = new File([this.response], fileName, { + type: 'application/zip', + }) + cache[fragmentId] = f + + const { a, url } = createAnchorElement(f, fileName) + a.click() + removeAnchorElement(a, url) + } + } + } + xhr.open('GET', getUrl) + xhr.responseType = 'blob' + xhr.setRequestHeader('Authorization', `Bearer ${token}`) + xhr.send() + } + }, +}) diff --git a/packages/component-faraday-ui/src/index.js b/packages/component-faraday-ui/src/index.js index 5265ed1692701c22ca6862324403566bf11ebf1b..118583283e1f10934ff3fb596c01f9e3d60837c3 100644 --- a/packages/component-faraday-ui/src/index.js +++ b/packages/component-faraday-ui/src/index.js @@ -21,7 +21,6 @@ export { default as Tag } from './Tag' export { default as Text } from './Text' export { default as WizardAuthors } from './WizardAuthors' export { default as WizardFiles } from './WizardFiles' -export { default as DateParser } from './DateParser' export { default as DownloadZipFiles } from './DownloadZipFiles' // Manuscript Details