Skip to content

Commit

Permalink
v2.0.0-beta.10 (#291)
Browse files Browse the repository at this point in the history
* bugfix(auth): `auth.isLoaded` set to `true` on `AUTH_EMPTY_CHANGE` action dispatch - #290
* feat(storage): customizable file name with `uploadFile` and `uploadFiles` through `name` option (can be String or Function) - #285
* bugfix(query): `remove` dispatches `REMOVE` action type with associated reducer case - #257
* feat(query): `remove` accepts an options object as third argument
* feat(query): `remove` now has `dispatchAction` option for disabling dispatch of new `REMOVE` action type
* feat(query): `dispatchRemoveAction` config option add for global control of dispatching when calling `remove`
  • Loading branch information
prescottprue committed Oct 15, 2017
1 parent fb8cda6 commit 2fc118f
Show file tree
Hide file tree
Showing 16 changed files with 317 additions and 93 deletions.
16 changes: 12 additions & 4 deletions docs/api/constants.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Object containing all action types

- `START` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** `@@reactReduxFirebase/START`
- `SET` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** `@@reactReduxFirebase/SET`
- `REMOVE` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** `@@reactReduxFirebase/REMOVE`
- `MERGE` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** `@@reactReduxFirebase/MERGE`
- `SET_PROFILE` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** `@@reactReduxFirebase/SET_PROFILE`
- `LOGIN` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** `@@reactReduxFirebase/LOGIN`
Expand Down Expand Up @@ -88,15 +89,19 @@ Default configuration options
`'userSessions'`. If a function is passed, the arguments are: `(currentUser, firebase)`.
- `enableLogging` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** `false` Whether or not firebase
database logging is enabled.
- `preserveOnLougout` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)** `null` Data parameters to preserve when
- `preserveOnLogout` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)** `null` Data parameters to preserve when
logging out.
- `updateProfileOnLogin` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** `true` Whether or not to update
user profile when logging in.
- `resetBeforeLogin` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** `true` Whether or not to reset auth
and profile when logging in (see issue #254 for more details).
and profile when logging in (see issue
[#254](https://github.com/prescottprue/react-redux-firebase/issues/254)
for more details).
- `enableRedirectHandling` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** `true` Whether or not to enable
redirect handling. This must be disabled if environment is not http/https
such as with react-native.
- `onAuthStateChanged` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** `null` Function that runs when
auth state changes.
- `enableEmptyAuthChanges` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** `false` Whether or not to enable
empty auth changes. When set to true, `onAuthStateChanged` will be fired with,
empty auth changes such as `undefined` on initialization
Expand All @@ -110,13 +115,16 @@ Default configuration options
the data path. For example role parameter on profile populated from 'roles'
root. True will call SET_PROFILE as well as a SET action with the role that
is loaded (places it in data/roles).
- `dispatchOnUnsetListener` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** `false` Whether or not to
- `dispatchOnUnsetListener` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** `true` Whether or not to
dispatch UNSET_LISTENER when disabling listeners for a specific path. USE WITH CAUTION
Setting this to true allows an action to be called that removes data
from redux (which might not always be expected).
- `dispatchRemoveAction` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** `true` Whether or not to
dispatch REMOVE action when calling `remove`.
- `firebaseStateName` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** 'firebase' Assumed name of Firebase
state (name given when passing reducer to combineReducers). Used in
firebaseAuthIsReady promise (see #264).
firebaseAuthIsReady promise (see
[#264](https://github.com/prescottprue/react-redux-firebase/issues/264)).
- `attachAuthIsReady` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** `true` Whether or not to attach
firebaseAuthIsReady to store. authIsLoaded can be imported and used
directly instead based on preference.
2 changes: 2 additions & 0 deletions docs/api/enhancer.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ along side applyMiddleware.
profile when logging in. (default: `false`)
- `config.resetBeforeLogin` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Whether or not to empty profile
and auth state on login
- `config.perserveOnLogout` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)** Data parameters to perserve when
logging out. (default: `null`)
- `config.enableRedirectHandling` **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Whether or not to enable
auth redirect handling listener. (default: `true`)
- `config.onAuthStateChanged` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** Function run when auth state
Expand Down
5 changes: 5 additions & 0 deletions docs/api/firebaseInstance.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ Removes data from Firebase at a given path.

- `path` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Path to location on Firebase which to remove
- `onComplete` **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** Function to run on complete (`not required`)
- `options`

**Examples**

Expand Down Expand Up @@ -225,6 +226,8 @@ its metadata in Firebase Database
- `file` **File** File object to upload (usually first element from
array output of select-file or a drag/drop `onDrop`)
- `dbPath` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Database path to place uploaded file metadata
- `options` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** Options
- `options.name` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Name of the file

Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)** Containing the File object

Expand All @@ -239,6 +242,8 @@ to store their metadata in Firebase Database
- `files` **[Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)** Array of File objects to upload (usually from
a select-file or a drag/drop `onDrop`)
- `dbPath` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Database path to place uploaded files metadata.
- `options` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** Options
- `options.name` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Name of the file

Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)** Containing an array of File objects

Expand Down
107 changes: 61 additions & 46 deletions docs/storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,67 +8,74 @@ For Examples of how to use these methods, please visit the [recipes section](/do

Upload an array of files to a location on Firebase storage. This includes the option to also write meta data for the object to Firebase database.

Available on `this.props.firebase` and `getFirebase`.
Available on `props.firebase` if using `firebaseConnect` HOC or using `getFirebase`.

#### Parameters
- `path` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Path within Firebase Storage at which to upload File.
- `files` **[Blob](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Array of File Blobs to upload to Firebase Storage.
- `databasePath` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Path within Firebase Database at which to write files metadata.
- `path` [**String**][string-url] - Path within Firebase Storage at which to upload File.
- `files` [**Array**][array-url] - Array of File Blobs to upload to Firebase Storage.
- `databasePath` [**String**][string-url] - Path within Firebase Database at which to write files metadata.
- `options` [**Object**][object-url] - Options for upload
- `options.name` [**String**][string-url] | [**Function**][function-url] - Name of file or function that returns the name of the file. If a function is passed the argument syntax is `(file, internalFirebase, uploadConfig)` where `file` is the file object (`file.name` is used as default if no name option is passed).

##### Returns
[**Promise**][promise-url] Resolves with an array of [`uploadFile` promises results (described below)](#uploadFile).

### uploadFile

Upload a single file to a location.

Available on `this.props.firebase` and `getFirebase`.
Available on `props.firebase` if using `firebaseConnect` HOC or using `getFirebase`.

#### Parameters
- `path` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Path within Firebase Storage at which to upload File.
- `file` **[Blob](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** File Blob to upload to Firebase Storage.
- `databasePath` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Path within Firebase Database at which to write file metadata.
- `path` [**String**][string-url] - Path within Firebase Storage at which to upload File.
- `file` [**Blob**][blob-url] - File Blob to upload to Firebase Storage.
- `databasePath` [**String**][string-url] - Path within Firebase Database at which to write file metadata.
- `options` [**Object**][object-url] - Options for upload
- `options.name` [**String**][string-url] | [**Function**][function-url] - Name of file or function that returns the name of the file. If a function is passed the argument syntax is `(file, internalFirebase, uploadConfig)` where `file` is the file object (`file.name` is used as default if no name option is passed).

##### Returns
[**Promise**][promise-url] Resolves with an object containing `uploadTaskSnaphot` which is the [**firebase.storage.UploadTaskSnaphot**][upload-task-snapshot-url] returned from the `storageRef.put` call which happens internally. If `databasePath` is provided `snapshot`, `key`, `File`, and `metaDataSnapshot` parameters are also included.

#### Examples

##### [Dropzone File Upload/Delete](/docs/recipes/upload.html#file-dragdrop-upload-with-delete)

### deleteFile

Delete a file from Firebase storage.
Delete a file from Firebase storage with the option to remove metadata from real time database.

Available on `this.props.firebase` and `getFirebase`.
Available on `props.firebase` if using `firebaseConnect` HOC or using `getFirebase`.

#### Parameters
- `path` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Path within Firebase Storage of File to delete.
- `databasePath` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Path within Firebase Database from which to remove File metadata.
- `path` [**String**][string-url] - Path within Firebase Storage of File to delete.
- `databasePath` [**String**][string-url] - Path within Firebase Database from which to remove File metadata.

##### Returns
[**Promise**][promise-url]

#### Example

```javascript
import React, { Component } from 'react'
import React from 'react'
import PropTypes from 'prop-types'
import { firebaseConnect } from 'react-redux-firebase'

@firebaseConnect()
export default class Uploader extends Component {
static propTypes = {
firebase: PropTypes.shape({
deleteFile: PropTypes.func.isRequired
})
}

render() {
const { deleteFile } = this.props;

return (
<div>
<h1>Example File Delete</h1>
<span>Deletes `index.txt` from storage</span>
<button onClick={() => deleteFile('index.txt')}>
Delete
</button>
</div>
)
}
const Uploader = ({ deleteFile }) =>
<div>
<h1>Example File Delete</h1>
<span>Deletes `index.txt` from storage</span>
<button onClick={() => deleteFile('index.txt')}>
Delete
</button>
</div>

Uploader.propTypes = {
firebase: PropTypes.shape({ // comes from firebaseConnect
deleteFile: PropTypes.func.isRequired
})
}

export default firebaseConnect()(Uploader)
```

### Other Storage Methods
Expand All @@ -84,31 +91,39 @@ import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { firebaseConnect } from 'react-redux-firebase'

@firebaseConnect()
export default class Uploader extends Component {
class Uploader extends Component {
static propTypes = {
firebase: PropTypes.object
}

render() {
addTestFile = () => {
const { firebase: { storage } } = this.props;
const storageRef = storage().ref()
const fileRef = storageRef.child('test.txt')
return fileRef.putString('Some File Contents')
.then(snap => console.log('upload successful', snap))
.catch(err => console.error('error uploading file', err))
}

const addTestFile = () => {
const storageRef = storage().ref()
const fileRef = storageRef.child('test.txt')
fileRef.putString('Some File Contents')
.then(snap => console.log('upload successful', snap))
.catch(err => console.error('error uploading file', err))
}

render() {
return (
<div>
<h1>Example Upload</h1>
<button onClick={addTestFile}>
<button onClick={this.addTestFile}>
Upload Example File
</button>
</div>
)
}
}

export default firebaseConnect()(Uploader)
```

[promise-url]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
[string-url]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
[array-url]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
[object-url]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
[function-url]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function
[blob-url]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Blob
[upload-task-snapshot-url]:https://firebase.google.com/docs/reference/js/firebase.storage.UploadTaskSnapshot
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-redux-firebase",
"version": "2.0.0-beta.9",
"version": "2.0.0-beta.10",
"description": "Redux integration for Firebase. Comes with a Higher Order Component for use with React.",
"main": "lib/index.js",
"module": "es/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/actions/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ const setupPresence = (dispatch, firebase) => {
}
const ref = firebase.database().ref()
const { config: { presence, sessions }, authUid } = firebase._
let amOnline = ref.child('.info/connected')
let amOnline = firebase.database().ref('.info/connected')
let onlineRef = ref.child(isFunction(presence) ? presence(firebase.auth().currentUser, firebase) : presence).child(authUid)
let sessionsRef = isFunction(sessions) ? sessions(firebase.auth().currentUser, firebase) : sessions
if (sessionsRef) {
Expand Down
34 changes: 33 additions & 1 deletion src/actions/query.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isFunction } from 'lodash'
import { actionTypes } from '../constants'
import {
orderedFromSnapshot,
Expand Down Expand Up @@ -172,11 +173,42 @@ export const watchEvents = (firebase, dispatch, events) =>
/**
* @description Remove watchers from a list of events
* @param {Object} firebase - Internal firebase object
* @param {Function} dispatch - Action dispatch function
* @param {Array} events - List of events for which to remove watchers
*/
export const unWatchEvents = (firebase, dispatch, events) =>
events.forEach(event =>
unWatchEvent(firebase, dispatch, event)
)

export default { watchEvents, unWatchEvents }
/**
* @description Add watchers to a list of events
* @param {Object} firebase - Internal firebase object
* @param {Function} dispatch - Action dispatch function
* @param {String} path - Path of ref to be removed
* @param {Function} onComplete - Callback function that is called when removal is
* @param {Object} [options={}] - Configuration for removal
* @param {Boolean} [options.dispatchAction=true] - Whether or not to dispatch
* REMOVE action
* @return {Promise} Resolves with path
*/
export const remove = (firebase, dispatch, path, onComplete, options = {}) => {
const { dispatchAction = true } = options
const { dispatchRemoveAction } = firebase._.config
return firebase.database().ref(path).remove()
.then(() => {
if (dispatchRemoveAction && dispatchAction) {
dispatch({ type: actionTypes.REMOVE, path })
}
if (isFunction(onComplete)) {
onComplete()
}
return path
})
.catch((err) => {
dispatch({ type: actionTypes.ERROR, payload: err })
return Promise.reject(err)
})
}

export default { watchEvents, unWatchEvents, remove }
Loading

0 comments on commit 2fc118f

Please sign in to comment.