orb-ui-firebase
Version:
Firebase with redux
232 lines (184 loc) • 7.74 kB
JavaScript
exports.__esModule = true;
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _reduxForm = require('redux-form');
var _reactRedux = require('react-redux');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var FireForm = function (_Component) {
_inherits(FireForm, _Component);
function FireForm(props, context) {
_classCallCheck(this, FireForm);
var _this = _possibleConstructorReturn(this, _Component.call(this));
_this.getCreateValues = function (values) {
var handleCreateValues = _this.props.handleCreateValues;
if (handleCreateValues !== undefined && handleCreateValues instanceof Function) {
return handleCreateValues(values);
}
return values;
};
_this.getUpdateValues = function (values, dispatch, props) {
var handleUpdateValues = _this.props.handleUpdateValues;
if (handleUpdateValues !== undefined && handleUpdateValues instanceof Function) {
return handleUpdateValues(values);
}
return values;
};
_this.handleSubmit = function (values) {
var _this$props = _this.props,
path = _this$props.path,
uid = _this$props.uid,
firebaseApp = _this$props.firebaseApp,
useFirestore = _this$props.useFirestore;
if (uid) {
var updateValues = _this.getUpdateValues(_this.clean(values));
if (updateValues) {
if (useFirestore) {
firebaseApp.firestore().collection(path).doc(uid).update(updateValues);
} else {
firebaseApp.database().ref().child('' + path + uid).update(updateValues);
}
}
} else {
var createValues = _this.getCreateValues(_this.clean(values));
if (createValues) {
if (useFirestore) {
firebaseApp.firestore().collection(path).doc().set(createValues);
} else {
firebaseApp.database().ref().child('' + path).push(createValues);
}
}
}
};
_this.handleDelete = function () {
var _this$props2 = _this.props,
onDelete = _this$props2.onDelete,
path = _this$props2.path,
uid = _this$props2.uid,
firebaseApp = _this$props2.firebaseApp,
useFirestore = _this$props2.useFirestore;
if (uid) {
if (useFirestore) {
firebaseApp.firestore().collection(path).doc(uid).delete().then(function () {
if (onDelete && onDelete instanceof Function) {
onDelete();
}
});
} else {
firebaseApp.database().ref().child('' + path + uid).remove().then(function () {
if (onDelete && onDelete instanceof Function) {
onDelete();
}
});
}
}
};
_this.componentWillReceiveProps = function (nextProps) {
var _this$props3 = _this.props,
uid = _this$props3.uid,
name = _this$props3.name,
path = _this$props3.path,
firebaseApp = _this$props3.firebaseApp,
initialize = _this$props3.initialize,
useFirestore = _this$props3.useFirestore;
var nextUid = nextProps.uid;
if (uid && uid !== nextUid) {
if (useFirestore) {
if (_this.unsub) {
_this.unsub();
}
_this.unsub = firebaseApp.firestore().collection(path).doc(nextUid).onSnapshot(function (doc) {
if (doc.exists) {
_this.setState({ initialized: true }, function () {
initialize(name, doc.data(), true);
});
}
});
} else {
firebaseApp.database().ref('' + path + nextUid).on('value', function (snapshot) {
_this.setState({ initialized: true }, function () {
initialize(name, snapshot.val(), true);
});
});
}
}
};
_this.context = context;
_this.state = {
initialized: false
};
return _this;
}
FireForm.prototype.clean = function clean(obj) {
Object.keys(obj).forEach(function (key) {
return obj[key] === undefined && delete obj[key];
});
return obj;
};
FireForm.prototype.componentWillMount = function componentWillMount() {
var _this2 = this;
var _props = this.props,
path = _props.path,
uid = _props.uid,
name = _props.name,
firebaseApp = _props.firebaseApp,
initialize = _props.initialize,
useFirestore = _props.useFirestore;
if (uid) {
if (useFirestore) {
this.unsub = firebaseApp.firestore().collection(path).doc(uid).onSnapshot(function (doc) {
if (doc.exists) {
_this2.setState({ initialized: true }, function () {
initialize(name, doc.data(), true);
});
}
});
} else {
firebaseApp.database().ref('' + path + uid).on('value', function (snapshot) {
_this2.setState({ initialized: true }, function () {
initialize(name, snapshot.val(), true);
});
});
}
} else {
this.setState({ initialValues: {}, initialized: true });
}
};
FireForm.prototype.componentWillUnmount = function componentWillUnmount() {
var _props2 = this.props,
path = _props2.path,
uid = _props2.uid,
firebaseApp = _props2.firebaseApp;
firebaseApp.database().ref('' + path + uid).off();
if (this.unsub) {
this.unsub();
}
};
FireForm.prototype.render = function render() {
return _react2.default.Children.only(_react2.default.cloneElement(this.props.children, _extends({
onSubmit: this.handleSubmit
}, this.state, this.props)));
};
return FireForm;
}(_react.Component);
FireForm.propTypes = process.env.NODE_ENV !== "production" ? {
path: _propTypes2.default.string.isRequired,
name: _propTypes2.default.string.isRequired,
useFirestore: _propTypes2.default.bool,
firebaseApp: _propTypes2.default.any.isRequired,
uid: _propTypes2.default.string,
onDelete: _propTypes2.default.func,
handleCreateValues: _propTypes2.default.func,
handleUpdateValues: _propTypes2.default.func
} : {};
var mapStateToProps = function mapStateToProps(state) {
return {};
};
exports.default = (0, _reactRedux.connect)(mapStateToProps, { initialize: _reduxForm.initialize })(FireForm);
module.exports = exports['default'];
;