Commit 76b5162c authored by Marco Sehrer's avatar Marco Sehrer
Browse files

Merge pull request #41 from ninjaconcept/ms-refactor-event-flow

More consistent event flow and routing
parents 8cd31405 7e217a83
......@@ -11,7 +11,7 @@ module Chemotion
end
get do
if(params[:collection_id])
current_user.collections.find(params[:collection_id]).samples
Collection.where("user_id = ? OR shared_by_id = ?", current_user.id, current_user.id).find(params[:collection_id]).samples
else
Sample.all
end
......
......@@ -9,7 +9,7 @@ import ElementFilter from './ElementFilter';
import SampleDetails from './SampleDetails';
import ShareModal from './managing_actions/ShareModal';
import ElementActions from './actions/ElementActions';
import UIActions from './actions/UIActions';
import Aviator from 'aviator'
Aviator.root = '/';
......@@ -18,28 +18,45 @@ Aviator.setRoutes({
'/': 'root',
target: {
root: function(e) {
ElementActions.unselectCurrentElement();
Aviator.navigate('/collections/all');
}
},
let modalDomNode = document.getElementById('modal');
if(modalDomNode) {
React.unmountComponentAtNode(modalDomNode);
'/collection': {
target: {
show: function(e) {
UIActions.selectCollection({id: e.params['id']});
if(!e.params['sampleID'])
{
UIActions.deselectAllElements('sample');
}
}
}
},
'/:id': 'show'
},
'/sample': {
target: {
show: function(e) {
let sampleId = e['params']['id'];
ElementActions.fetchSampleById(sampleId)
UIActions.selectElement({type: 'sample', id: e.params['sampleID']})
}
},
'/:id': 'show'
'/:sampleID': 'show'
},
'/sharing': {
'/*': 'showShareModal',
'/': 'show',
'/hide': 'hide',
target: {
showShareModal: function(e) {
show: function(e) {
React.render(<ShareModal/>, document.getElementById('modal'));
},
hide: function(e) {
let modalDomNode = document.getElementById('modal');
if(modalDomNode) {
React.unmountComponentAtNode(modalDomNode);
}
Aviator.navigate(Aviator.getCurrentURI().replace('/sharing/hide',''))
}
}
}
......
......@@ -11,11 +11,16 @@ import Aviator from 'aviator';
export default class CollectionSubtree extends React.Component {
constructor(props) {
super(props);
let uiState = UIStore.getState();
let selected = uiState.currentCollectionId == props.root.id;
let visible = props.root.descendant_ids.indexOf(parseInt(uiState.currentCollectionId)) > -1
this.state = {
label: props.root.label,
selected: false,
selected: selected,
root: props.root,
visible: false
visible: visible
}
}
......@@ -27,15 +32,8 @@ export default class CollectionSubtree extends React.Component {
UIStore.unlisten(this.onChange.bind(this));
}
componentWillUpdate(nextProps, nextState) {
if(nextState.selected) {
// TODO also for reactions and so on
ElementActions.fetchSamplesByCollectionId(this.state.root.id)
}
}
onChange(state) {
if(state.selectedCollectionIds[0] == this.state.root.id) {
if(state.currentCollectionId == this.state.root.id) {
this.setState({selected: true});
} else {
this.setState({selected: false});
......@@ -83,9 +81,7 @@ export default class CollectionSubtree extends React.Component {
}
handleClick() {
UIActions.deselectAllElements('collection');
UIActions.selectElement({type: 'collection', id: this.state.root.id});
Aviator.navigate('/');
Aviator.navigate('/collection/'+this.state.root.id);
}
toggleExpansion(e) {
......@@ -101,7 +97,7 @@ export default class CollectionSubtree extends React.Component {
}
return (
<div className="tree-view">
<div className="tree-view" key={this.state.root.id}>
<div className={"title " + this.selectedCssClass()} onClick={this.handleClick.bind(this)}>
{this.expandButton()}
{this.state.label}
......
......@@ -63,10 +63,12 @@ export default class CollectionTree extends React.Component {
}
render() {
//Fake All-Collection
let allCollection = {
label: 'All *',
label: 'All',
id: 'all',
children: []
children: [],
descendant_ids: []
};
return (
......
import React from 'react';
import {Label, Pagination, Table} from 'react-bootstrap';
import UIStore from './stores/UIStore';
import ElementStore from './stores/ElementStore';
import ElementAllCheckbox from './ElementAllCheckbox';
import ElementCheckbox from './ElementCheckbox';
......@@ -116,7 +117,8 @@ export default class ElementsTable extends React.Component {
}
showDetails(element) {
Aviator.navigate(`/${element.type}/${element.id}`);
let uiState = UIStore.getState();
Aviator.navigate(`/collection/${uiState.currentCollectionId}/${element.type}/${element.id}`);
}
handlePaginationSelect(event, selectedEvent) {
......
......@@ -4,6 +4,9 @@ import SVG from 'react-inlinesvg';
import ElementActions from './actions/ElementActions';
import ElementStore from './stores/ElementStore';
import UIStore from './stores/UIStore';
import NumeralInputWithUnits from './NumeralInputWithUnits'
import ElementCollectionLabels from './ElementCollectionLabels';
......@@ -39,7 +42,8 @@ export default class SampleDetails extends React.Component {
}
closeDetails() {
Aviator.navigate('/');
let uiState = UIStore.getState();
Aviator.navigate(`/collection/${uiState.currentCollectionId}`);
}
updateSample() {
......
......@@ -3,10 +3,6 @@ import SamplesFetcher from '../fetchers/SamplesFetcher';
class ElementActions {
unselectCurrentElement() {
this.dispatch();
}
fetchSampleById(id) {
SamplesFetcher.fetchById(id)
.then((result) => {
......
......@@ -2,6 +2,10 @@ import alt from '../alt';
// An element object has a type and an id, e.g., {type: 'sample', id: 1}
class UIActions {
selectCollection(collection) {
this.dispatch(collection)
}
checkAllElements(type) {
this.dispatch(type);
}
......@@ -19,7 +23,6 @@ class UIActions {
}
selectElement(element) {
console.log("selectElement:" + element.id + " type: " + element.type)
this.dispatch(element);
}
......
......@@ -8,7 +8,7 @@ export default class ShareButton extends React.Component {
}
showShareModal() {
Aviator.navigate('/sharing');
Aviator.navigate(Aviator.getCurrentURI()+'/sharing');
}
render() {
......
......@@ -34,7 +34,8 @@ export default class ShareModal extends React.Component {
}
hideModal() {
Aviator.navigate('/');
//window.history.back();
Aviator.navigate(Aviator.getCurrentURI()+'/hide');
}
handleSharing() {
......
import alt from '../alt';
import ElementActions from '../actions/ElementActions';
import UIActions from '../actions/UIActions';
import UIStore from './UIStore';
class ElementStore {
constructor() {
......@@ -12,7 +14,7 @@ class ElementStore {
handleFetchSampleById: ElementActions.fetchSampleById,
handleFetchSamplesByCollectionId: ElementActions.fetchSamplesByCollectionId,
handleUpdateSample: ElementActions.updateSample,
handleUnselectCurrentElement: ElementActions.unselectCurrentElement
handleUnselectCurrentElement: UIActions.deselectAllElements
})
}
......@@ -30,6 +32,7 @@ class ElementStore {
}
handleUnselectCurrentElement() {
//this.waitFor(UIStore.dispatchToken);
this.state.currentElement = null;
}
}
......
import alt from '../alt';
import UIActions from '../actions/UIActions';
import ElementActions from '../actions/ElementActions';
import ElementStore from './ElementStore';
import ArrayUtils from '../utils/ArrayUtils';
......@@ -9,10 +10,12 @@ class UIStore {
constructor() {
this.state = {
checkedSampleIds: Immutable.List(),
selectedCollectionIds: []
currentCollectionId: null,
currentSampleId: null
};
this.bindListeners({
handleSelectCollection: UIActions.selectCollection,
handleCheckAllElements: UIActions.checkAllElements,
handleCheckElement: UIActions.checkElement,
handleUncheckElement: UIActions.uncheckElement,
......@@ -59,19 +62,27 @@ class UIStore {
handleDeselectAllElements(type) {
switch(type) {
case 'collection':
this.state.selectedCollectionIds = [];
case 'sample':
this.state.currentSampleId = null;
break;
}
}
handleSelectElement(element) {
switch(element.type) {
case 'collection':
this.state.selectedCollectionIds.push(element.id);
case 'sample':
this.state.currentSampleId = element.id;
// TODO also for reactions and so on
ElementActions.fetchSampleById(element.id)
break;
}
}
handleSelectCollection(collection) {
this.state.currentCollectionId = collection.id;
// TODO also for reactions and so on
ElementActions.fetchSamplesByCollectionId(collection.id)
}
}
export default alt.createStore(UIStore, 'UIStore');
class CollectionSerializer < ActiveModel::Serializer
attributes :id, :label
attributes :id, :label, :descendant_ids
has_many :children
......@@ -7,4 +7,8 @@ class CollectionSerializer < ActiveModel::Serializer
object.children
end
def descendant_ids
object.descendant_ids
end
end
......@@ -4,7 +4,7 @@ describe Chemotion::CollectionAPI do
let(:json_options) {
{
only: [:id, :label],
include: :children
include: [:children, :descendant_ids]
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment