Github user garrensmith commented on a diff in the pull request:
https://github.com/apache/couchdb-fauxton/pull/669#discussion_r72041835
--- Diff: app/addons/replication/components.react.jsx ---
@@ -0,0 +1,527 @@
+// Licensed under the Apache License, Version 2.0 (the "License"); you may
not
+// use this file except in compliance with the License. You may obtain a
copy of
+// the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations
under
+// the License.
+import app from '../../app';
+import FauxtonAPI from '../../core/api';
+import React from 'react';
+import Stores from './stores';
+import Actions from './actions';
+import Constants from './constants';
+import Components from '../components/react-components.react';
+import base64 from 'base-64';
+import AuthActions from '../auth/actions';
+import AuthComponents from '../auth/components.react';
+
+const store = Stores.replicationStore;
+const LoadLines = Components.LoadLines;
+const TypeaheadField = Components.TypeaheadField;
+const StyledSelect = Components.StyledSelect;
+const ConfirmButton = Components.ConfirmButton;
+const PasswordModal = AuthComponents.PasswordModal;
+
+
+class ReplicationController extends React.Component {
+ constructor (props) {
+ super(props);
+ this.state = this.getStoreState();
+ this.submit = this.submit.bind(this);
+ this.clear = this.clear.bind(this);
+ this.showPasswordModal = this.showPasswordModal.bind(this);
+ }
+
+ getStoreState () {
+ return {
+ loading: store.isLoading(),
+ databases: store.getDatabases(),
+ authenticated: store.isAuthenticated(),
+ password: store.getPassword(),
+
+ // source fields
+ replicationSource: store.getReplicationSource(),
+ sourceDatabase: store.getSourceDatabase(),
+ localSourceDatabaseKnown: store.isLocalSourceDatabaseKnown(),
+ remoteSource: store.getRemoteSource(),
+
+ // target fields
+ replicationTarget: store.getReplicationTarget(),
+ targetDatabase: store.getTargetDatabase(),
+ localTargetDatabaseKnown: store.isLocalTargetDatabaseKnown(),
+ remoteTarget: store.getRemoteTarget(),
+
+ // other
+ passwordModalVisible: store.isPasswordModalVisible(),
+ replicationType: store.getReplicationType(),
+ replicationDocName: store.getReplicationDocName()
+ };
+ }
+
+ componentDidMount () {
+ store.on('change', this.onChange, this);
+ }
+
+ componentWillUnmount () {
+ store.off('change', this.onChange);
+ }
+
+ onChange () {
+ this.setState(this.getStoreState());
+ }
+
+ // the four local replication targets all show slightly different fields
+ getReplicationTargetRow () {
+ const { replicationTarget, remoteTarget, databases, targetDatabase } =
this.state;
+ if (!replicationTarget) {
+ return null;
+ }
+ return (
+ <ReplicationTargetRow
+ remoteTarget={remoteTarget}
+ replicationTarget={replicationTarget}
+ databases={databases}
+ targetDatabase={targetDatabase}/>
+ );
+ }
+
+ clear (e) {
+ e.preventDefault();
+ Actions.clearReplicationForm();
+ }
+
+ showPasswordModal () {
+ const { replicationSource, replicationTarget } = this.state;
+
+ var hasLocalSourceOrTarget = (replicationSource ===
Constants.REPLICATION_SOURCE.LOCAL ||
+ replicationTarget ===
Constants.REPLICATION_TARGET.EXISTING_LOCAL_DATABASE ||
+ replicationTarget ===
Constants.REPLICATION_TARGET.NEW_LOCAL_DATABASE);
+
+ // if the user is authenticated, or if NEITHER the source nor target
are local, just submit. The password
+ // modal isn't necessary
+ if (!hasLocalSourceOrTarget || this.state.authenticated) {
+ this.submit();
+ return;
+ }
+ AuthActions.showPasswordModal();
+ }
+
+ getUsername () {
+ return app.session.get('userCtx').name;
+ }
+
+ getAuthHeaders () {
+ const username = this.getUsername();
+ return {
+ 'Authorization': 'Basic ' + base64.encode(username + ':' +
this.state.password)
+ };
+ }
+
+ submit () {
+ const { replicationSource, sourceDatabase, remoteSource, remoteTarget,
replicationTarget, targetDatabase, replicationType,
+ replicationDocName} = this.state;
+
+ const params = {};
+
+ // perform a little validating here
+ if (!this.validate()) {
+ return;
+ }
+
+ // source
+ if (replicationSource === Constants.REPLICATION_SOURCE.LOCAL) {
+ params.source = {
+ headers: this.getAuthHeaders(),
+ url: window.location.origin + '/' + sourceDatabase
+ };
+ } else {
+ params.source = remoteSource;
+ }
+
+ // target
+ if (replicationTarget ===
Constants.REPLICATION_TARGET.EXISTING_LOCAL_DATABASE) {
+ params.target = {
+ headers: this.getAuthHeaders(),
+ url: window.location.origin + '/' + targetDatabase
+ };
+ } else if (replicationTarget ===
Constants.REPLICATION_TARGET.EXISTING_REMOTE_DATABASE) {
+ params.target = remoteTarget;
+ } else if (replicationTarget ===
Constants.REPLICATION_TARGET.NEW_LOCAL_DATABASE) {
+
+ // check to see if we really need to send headers here or can just
do the ELSE clause in all scenarioe
+ if (replicationSource === Constants.REPLICATION_SOURCE.LOCAL) {
+ params.target = {
+ headers: this.getAuthHeaders(),
+ url: window.location.origin + '/' + targetDatabase
+ };
+ } else {
+ const port = window.location.port === '' ? '' : ':' +
window.location.port;
+ params.target = window.location.protocol + '//' +
this.getUsername() + ':' + this.state.password + '@'
+ + window.location.hostname + port + '/' + targetDatabase;
+ }
+ } else if (replicationTarget ===
Constants.REPLICATION_TARGET.NEW_REMOTE_DATABASE) {
+ params.target = remoteTarget;
+ }
+
+ if (_.contains([Constants.REPLICATION_TARGET.NEW_LOCAL_DATABASE,
Constants.REPLICATION_TARGET.NEW_REMOTE_DATABASE], replicationTarget)) {
+ params.create_target = true;
+ }
+ if (replicationType === Constants.REPLICATION_TYPE.CONTINUOUS) {
+ params.continuous = true;
+ }
+
+ if (replicationDocName) {
+ params._id = this.state.replicationDocName;
+ }
+
+ // POSTing to the _replicator DB requires authentication
+ const user = FauxtonAPI.session.user();
+ const userName = _.isNull(user) ? '' : FauxtonAPI.session.user().name;
+ params.user_ctx = {
+ name: userName,
+ roles: ['_admin', '_reader', '_writer']
+ };
+
+ Actions.replicate(params);
+ }
+
+ validate () {
+ const { replicationTarget, targetDatabase, databases } = this.state;
+
+ if (replicationTarget ===
Constants.REPLICATION_TARGET.NEW_LOCAL_DATABASE && _.contains(databases,
targetDatabase)) {
+ FauxtonAPI.addNotification({
+ msg: 'The <code>' + targetDatabase + '</code> database already
exists locally. Please enter another database name.',
+ type: 'error',
+ escape: false,
+ clear: true
+ });
+ return false;
+ }
+ if (replicationTarget ===
Constants.REPLICATION_TARGET.NEW_LOCAL_DATABASE ||
+ replicationTarget ===
Constants.REPLICATION_TARGET.NEW_REMOTE_DATABASE) {
+ let error = '';
+ if (/\s/.test(targetDatabase)) {
+ error = 'The target database may not contain any spaces.';
+ } else if (/^_/.test(targetDatabase)) {
+ error = 'The target database may not start with an underscore.';
+ }
+
+ if (error) {
+ FauxtonAPI.addNotification({
+ msg: error,
+ type: 'error',
+ escape: false,
+ clear: true
+ });
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ getReplicationSourceRow () {
+ const { replicationSource, databases, sourceDatabase, remoteSource } =
this.state;
--- End diff --
Can you break these replication source row into their own react components.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---