Skip to content
Snippets Groups Projects
Commit edae851d authored by Alexandru Munteanu's avatar Alexandru Munteanu
Browse files

docs(faraday-ui): add documentation for faraday helpers

parent 38c4a0c4
No related branches found
No related tags found
3 merge requests!222Sprint #26,!217Sprint #26,!191Hin 1116 documentation
## Hindawi utility HOCs.
## Hindawi Helpers
_Utility HOCs_
* [withCountries](#withCountries)
* [withFetching](#withFetching)
......@@ -8,6 +10,17 @@
* [withRoles](#withRoles)
* [withZipDownload](#withZipDownload)
_HOCs used for files drag and drop_
* [withFileSectionDrop](#withFileSectionDrop)
* [withNativeFileDrop](#withNativeFileDrop)
_Utility functions_
* [handleError](#handleError)
# Utility HOCs
## withCountries
Injects `countries` and `countryLabel` as props.
......@@ -28,6 +41,8 @@ const Wrapped = ({ countries, countryLabel }) => (
<span>Selected country: {countryLabel('RO')}</span>
</div>
)
export default withCountries(Wrapped)
```
## withFetching
......@@ -166,3 +181,98 @@ This HOCs assumes the following props are present on the wrapped component:
### withZipDownload props
* `downloadFiles: () => any`: download all the fragment's file as a zip
# Files drag and drop
## withFileSectionDrop
HOC used to provide drop functionality to the `FileSection` component. It's main purpose is to change a file from one list to another. This is usually done in a callback called `changeList` that should be provided to the wrapped component.
This HOC assumes the wrapped component has the following props:
* `files: [{id: string, ...}]`: the list of files passed to the wrapped component
* `setError: (error: string) => any`: error setting callback
* `listId: string`: the current list id
* `allowedFileExtensions: [string]`: the allowed files
* `maxFiles: number`: the maximum number of files allowed
* `changeList: (fromListId: string, toListId: string: fileId: string)`: callback called if all the conditions are met (allowed files, number of files, etc)
```js
import { compose, withHandler, withProps } from 'recompose'
import { FileSection, withFileSectionDrop } from 'pubsweet-component-faraday-ui'
const Wrapped = compose(
withProps({
files: [...],
listId: 'CoverLetter',
maxFiles: 3,
allowedFileExtensions: ['pdf'],
}),
withHandlers({
changeList: () => (fromListId, toListId, fileId) => {
// do the actual change here
}
}),
withFileSectionDrop,
)(FileSection)
export default Wrapped
```
## withNativeFileDrop
HOC used to provide native file drop functionality to the `FileSection` component. It's purpose is to do something when dragging files from the computer's hard drive into the app. _This HOC allows only single items! Dragging multiple items into the wrapped component will only handle the first item!_
This HOC assumes the wrapped component has the following props:
* `files: [{id: string, ...}]`: the list of files passed to the wrapped component
* `setError: (error: string) => any`: error setting callback
* `allowedFileExtensions: [string]`: the allowed files
* `maxFiles: number`: the maximum number of files allowed
* `onFileDrop: (file: File)`: callback called when a valid file is dropped
```js
import { compose, withHandler, withProps } from 'recompose'
import { FileSection, withNativeFileDrop } from 'pubsweet-component-faraday-ui'
const Wrapped = compose(
withProps({
files: [...],
listId: 'CoverLetter',
maxFiles: 3,
allowedFileExtensions: ['pdf'],
}),
withHandlers({
onFileDrop: () => file => {
// do something with the dropped file
}
}),
withNativeFileDrop,
)(FileSection)
export default Wrapped
```
# Utility functions
## handleError
Function that parses the server error. Calls the passed function with the parsed error.
Has the following signature:
`(callbackFn: (parsedError) => any) => (e: Error) => any`
```js
const customErrorLogger = parsedError =>
console.error(`This is very handled: ${parsedError}`)
// point free notation
anAsyncOperation().catch(handleError(customErrorLogger))
// can be used besides other function calls
anAsyncOperation().catch(err => {
setFetching(false)
handleError(customErrorLogger)(err)
})
```
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment