routes.js 3.5 KB
/* eslint-disable max-len */

import React                                from 'react';
import { render }                           from 'react-dom';
import { Router, Route,
  IndexRoute, browserHistory }              from 'react-router';
import { Meteor }                           from 'meteor/meteor';

/**
 * General Components
 */
import Index                                from '/imports/client/views/app/module/Index';

/**
 * Org Components
 */

import { App }                              from '/imports/client/layouts/OrgApp';
import { AppModule }                        from '/imports/client/views/org/app/module/Index';
import { Orgs }                             from '/imports/collections/orgs/index';
import NotFound                             from '/imports/client/views/org/NotFound';

/**
 * NonOrg Components
 */
import Signup                               from '/imports/client/views/nonOrg/enter/SignupView';

/**
 * Invalid Org Components
 */

const authenticate = (nextState, replace) => {
  if (!Meteor.loggingIn() && !Meteor.userId()) {
    replace({
      pathname: '/login',
      state: { nextPathname: nextState.location.pathname },
    });
  }
};


const detectOrg = () => {
  orgSlug = "";
  var hostnameArray = document.location.hostname.split( "." );
  if(hostnameArray[1]=='localhost'){
    orgSlug = hostnameArray[0];
  }
  if(orgSlug!=""){
    Meteor.call('checkExistingOrg', {slug:orgSlug}, function(err, res) {
      if(res){
        Session.set('orgId', res._id._str);
        Session.set('orgSlug', orgSlug);
        render(getOrgRoutes(),document.getElementById('app'));
      }else{
        render(getInvalidOrgRoute(),document.getElementById('app'));
      }
    });
  }else{
    render(getNonOrgRoutes(),document.getElementById('app'));
  }
}
const checkSlug = (nextState, replace) => {
  orgId = Session.get('orgId');
}

/**
There are three types of routes
1)getOrgRoutes: all the routes that should be present for a registered org
2)getInvalidOrgRoute: all the routes where someone tries to enter a subdomain which hasn't been registered yet (404 mostly :D)
3)getNonOrgRoutes: all routes linked to normal site, ie signing up a new org. CHeking out demo and everything internal
**/
const getOrgRoutes = () => (
  <Router history={ browserHistory }>
    <Route path="/"                                                           component={ App }>
      <IndexRoute name="index"                                                component={ AppModule } />
      <Route path="*"                                                         component={ NotFound } />
    </Route>
  </Router>
)


const getInvalidOrgRoute = () => (
  <Router history={ browserHistory }>
    <Route path="/"                                                           component={ App }>
      <IndexRoute name="index"                                                component={ NotFound } />
      <Route path="*"                                                         component={ NotFound } />
    </Route>
  </Router>
)

const getNonOrgRoutes = () => (
  <Router history={ browserHistory }>
    <Route path="/"                                                           component={ App }>
      <IndexRoute name="index"                                                component={ Index } />
      <Route name="signup"            path="/signup"                          component={ Signup } />
      <Route path="*"                                                         component={ NotFound } />
    </Route>
  </Router>
)


Meteor.startup(() => {
  detectOrg();
});