@darwino/darwino-react
Version:
A set of Javascript classes and utilities
289 lines (242 loc) • 8.19 kB
JavaScript
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
/*
* (c) Copyright Darwino Inc. 2014-2017.
*/
import React, { Component } from 'react';
import { get as _get, set as _set, isEqual as _isEqual } from 'lodash';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { Field } from 'redux-form';
import BaseReduxForm from "./BaseReduxForm";
import StoreActions from "./StoreActions";
var {
darwinoToStoreKey,
updateDocument,
createDocument,
loadDocument,
newDocument,
deleteDocument,
removeDocument
} = StoreActions;
/*
*/
class BaseDocumentForm extends BaseReduxForm {
// Default mapDispatchProps
constructor(props, context) {
super(props, context);
this.docEvents = {
initialize: (doc, newDoc) => {
this.setState({
mode: this.forceMode || (doc.readOnly ? BaseReduxForm.DISABLED : BaseReduxForm.EDITABLE),
docInitializing: true,
newDoc,
deleted: false,
unid: doc.unid,
doc
}, () => {
var initialized = false; // setState is asynchrounous, so we do that asynchronously
try {
this.fieldValues = doc.json; // Initialize the default values for a brand new document
if (newDoc) {
if (this.defaultValues) this.defaultValues(doc.json);
this._getRegisteredComponents().forEach(f => {
if (f.defaultValues) f.defaultValues(doc.json);
});
} // Calculate the initial values
this._onLoad(doc.json); // Prepare the document for display
// Computed on compose fields can also be calculated there
if (this.prepareForDisplay) this.prepareForDisplay(doc.json);
this._getRegisteredComponents().forEach(f => {
if (f.prepareForDisplay) f.prepareForDisplay(doc.json);
}); // Tell react-form to initialize the fields
this.props.initialize(_objectSpread({}, doc.json, {
__attachments: doc.attachments
})); // Mark the doc as initialized
initialized = true;
} catch (e) {
console.log(e);
} finally {
this.setState({
docInitializing: false,
docInitialized: initialized
});
}
});
},
prepareForSave: values => {
if (this.prepareForSave) this.prepareForSave(values);
this._getRegisteredComponents().forEach(f => {
if (f.prepareForSave) f.prepareForSave(values);
});
},
delete: doc => {
this.setState({
deleted: true
});
}
}; // We grab the database information from the properties
// Note that the unid can come from the URL as well
var databaseId = props.databaseId;
var storeId = props.storeId;
var unid = props.unid || props.match && props.match.params && props.match.params.unid || null; // The state carries information on the current document assigned to the form
this.state = _objectSpread({}, this.state, {
databaseId: databaseId,
storeId: storeId,
unid: unid,
newDoc: !unid,
deleted: false
});
}
componentDidMount() {
super.componentDidMount();
var {
loadDocument,
newDocument
} = this.props;
var {
databaseId,
storeId,
unid,
newDoc
} = this.state; // Load the initial values
// We could also call newDocument is we want to call the service to get the
// document initialized
if (databaseId && storeId) {
if (newDoc) {
newDocument(databaseId, storeId, unid, false, this.docEvents);
} else {
loadDocument(databaseId, storeId, unid, this.docEvents);
}
} else {
// Fake a new document...
this.docEvents.initialize({
json: {}
}, true);
}
}
componentWillUnmount() {
var {
databaseId,
storeId,
unid,
newDoc
} = this.state; // Remove the document from the redux store
if (!newDoc) {
removeDocument(databaseId, storeId, unid, this.docEvents);
}
super.componentWillUnmount();
}
getDocument() {
return this.state.doc;
}
setMode(mode) {
var doc = this.state.doc;
this.setState({
forceMode: mode,
mode: mode || (doc && !doc.readOnly ? BaseReduxForm.EDITABLE : BaseReduxForm.DISABLED)
});
}
handleUpdateDocument(state, dispatch) {
if (this.isSubmitting) {
return;
}
var {
doc,
newDoc
} = this.state;
var {
databaseId,
storeId,
unid
} = this.getDocState(state, newDoc);
var stateUnid = !newDoc ? state.unid : undefined;
;
if (!databaseId) {
this.postSave(false, "Database Id is empty");
return;
}
if (!storeId) {
this.postSave(false, "Store Id is empty");
return;
}
var {
createDocument,
updateDocument
} = this.props;
var {
__attachments
} = state,
fieldValues = _objectWithoutProperties(state, ["__attachments"]);
var properties = this.getUpdateProperties();
var promise;
if (this.querySave()) {
this.isSubmitting = true;
if (newDoc) {
promise = createDocument(databaseId, storeId, unid, _objectSpread({}, doc.json, {}, fieldValues), __attachments, properties, this.docEvents);
} else {
promise = updateDocument(databaseId, storeId, unid, _objectSpread({}, doc.json, {}, fieldValues), __attachments, properties, this.docEvents);
}
promise.then(() => {
if (newDoc) {
this.setState({
databaseId,
storeId,
unid
});
}
this.isSubmitting = false;
this.postSave(true);
}).catch(error => {
this.isSubmitting = false;
this.postSave(false, error);
});
}
} // This is for a very special case when the db, store or unid can be set dynamically
// Note that database/storeid/unid can be changed, which is also a very rare case
// If it happens, the existing document will be deleted and a new one created so the replication will work
getDocState(values, newDoc) {
var state = this.state;
return {
databaseId: state.databaseId,
storeId: state.storeId,
unid: state.unid
};
}
getUpdateProperties() {}
delete() {
if (!this.confirmDelete()) {
return;
}
var {
databaseId,
storeId,
unid
} = this.state;
if (databaseId && storeId) {
var {
deleteDocument: _deleteDocument
} = this.props;
_deleteDocument(databaseId, storeId, unid).then(() => {
this.postDelete(true);
}).catch(error => {
this.postDelete(false, error);
});
}
}
confirmDelete() {
return confirm("This will delete the document.\nDo you want to continue?");
}
}
_defineProperty(BaseDocumentForm, "mapDispatchToProps", {
updateDocument,
createDocument,
loadDocument,
newDocument,
deleteDocument,
removeDocument
});
export default BaseDocumentForm;
//# sourceMappingURL=BaseDocumentForm.js.map