UNPKG

refire-react

Version:

React components and helpers for Refire

84 lines 3.55 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); var React = require("react"); var react_1 = require("react"); var react_redux_1 = require("react-redux"); var refire_1 = require("refire"); var clearWriteErrors = refire_1.firebaseActions.clearWriteErrors, write = refire_1.firebaseActions.write; var validMethods = { push: true, set: true, transaction: true, update: true }; function default_1(options) { if (options === void 0) { options = {}; } var _a = options.path, path = _a === void 0 ? "" : _a, method = options.method; if (typeof path !== "function" && typeof path !== "string") { throw new Error("options.path must be a function or string"); } if (typeof method !== "string" || !validMethods[method]) { throw new Error("options.method must be one of: " + Object.keys(validMethods).join(", ")); } function mapStateToProps(state, ownProps) { var processing = state.firebase.writes.processing; var errors = state.firebase.writes.errors; var firebasePath = typeof path === "function" ? path(state, ownProps) : path; return { errors: errors[firebasePath], path: firebasePath, processing: processing[firebasePath] }; } return function (WrappedComponent) { var FirebaseWrite = /** @class */ (function (_super) { __extends(FirebaseWrite, _super); function FirebaseWrite() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.submit = function (value) { var dispatch = _this.props.dispatch; return dispatch(write({ method: method, path: path, value: value, ownProps: _this.props })); }; _this.clearErrors = function () { _this.props.dispatch(clearWriteErrors(_this.props.path)); }; return _this; } FirebaseWrite.prototype.render = function () { var processing = this.props.processing && !!this.props.processing.length; var errors = this.props.errors || []; var extraProps = { clearErrors: this.clearErrors, errors: errors, processing: processing, submit: this.submit }; return (React.createElement(WrappedComponent, __assign({}, this.props, extraProps))); }; return FirebaseWrite; }(react_1.Component)); return react_redux_1.connect(mapStateToProps)(FirebaseWrite); }; } exports.default = default_1; //# sourceMappingURL=FirebaseWrite.js.map