Skip to content

Commit

Permalink
Store opened panes in the redux store (SuperblocksHQ#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown committed Dec 11, 2018
1 parent 258fa1d commit 17224cc
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 49 deletions.
2 changes: 2 additions & 0 deletions src/actions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './panes.actions';
export * from './projects.actions';
17 changes: 17 additions & 0 deletions src/actions/panes.actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const panesActions = {
ADD_PANE: 'ADD_PANE',
addPane(id, name) {
return {
type: panesActions.ADD_PANE,
data: { id, name }
}
},

REMOVE_PANE: 'REMOVE_PANE',
removePane(id) {
return {
type: panesActions.REMOVE_PANE,
data: { id }
}
}
};
6 changes: 6 additions & 0 deletions src/actions/projects.actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function selectProject(id, name) {
return {
type: 'SELECT_PROJECT',
data: { id, name }
};
}
8 changes: 0 additions & 8 deletions src/actions/projects.js

This file was deleted.

10 changes: 2 additions & 8 deletions src/actions/projects.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import * as actions from './projects';
import ProjectItem from '../components/projecteditor/control/item/projectItem';
jest.mock('../components/projecteditor/control/item/projectItem'); // ProjectItem is now a mock constructor

describe('actions', () => {
it('should create an action to select the projec to open when re-loading the app', () => {
const projectItem = new ProjectItem();

const expectedAction = {
type: 'SELECT_PROJECT',
data: {
project: projectItem
},
data: { id: 23, name: 'test' },
};
expect(actions.selectProject(projectItem)).toEqual(expectedAction);
expect(actions.selectProject(23, 'test')).toEqual(expectedAction);
});
});
1 change: 0 additions & 1 deletion src/actions/view.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export const closeAllPanels = () => ({ type: 'CLOSE_ALL_PANELS' });
export const toggleTransactionsHistoryPanel = () => ({ type: 'TOGGLE_TRANSACTIONS_HISTORY_PANEL' });
export const openTransactionsHistoryPanel = () => ({ type: 'OPEN_TRANSACTIONS_HISTORY_PANEL' });
export const closeTransactionsHistoryPanel = () => ({ type: 'CLOSE_TRANSACTIONS_HISTORY_PANEL' });
7 changes: 0 additions & 7 deletions src/actions/view.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import * as actions from './view';

describe('actions', () => {
it('should create an action to close all the panels', () => {
const expectedAction = {
type: 'CLOSE_ALL_PANELS'
};
expect(actions.closeAllPanels()).toEqual(expectedAction);
});

it('should create an action to toggle the transactions history panel', () => {
const expectedAction = {
type: 'TOGGLE_TRANSACTIONS_HISTORY_PANEL'
Expand Down
6 changes: 3 additions & 3 deletions src/components/projecteditor/control/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,15 @@ export default class Control extends Component {
* if all windows successfully closed then remove project from explorer.
*/
_closeProject = cb => {
const { router, closeAllPanels } = this.props;
const { router, closeTransactionsHistoryPanel } = this.props;
router.panes.closeAll(status => {
if (status == 0) {
this.setState({ activeProject: null });
}
if (cb) cb(status);
});

closeAllPanels();
closeTransactionsHistoryPanel();
};

/**
Expand Down Expand Up @@ -224,7 +224,7 @@ export default class Control extends Component {
*/
_setProjectActive = project => {
this.setState({ activeProject: project });
this.props.selectProject(project);
this.props.selectProject(project.getInode(), project.getName());
};

/**
Expand Down
12 changes: 6 additions & 6 deletions src/components/projecteditor/control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { connect } from 'react-redux';
import Control from './control';
import { getAppVersion } from '../../../selectors/app';
import { getSelectedProjectId } from '../../../selectors/projects';
import { selectProject } from '../../../actions/projects';
import { closeAllPanels } from '../../../actions/view';
import { selectProject } from '../../../actions';
import { closeTransactionsHistoryPanel } from '../../../actions/view';

const mapStateToProps = state => ({
appVersion: getAppVersion(state),
Expand All @@ -13,11 +13,11 @@ const mapStateToProps = state => ({

const mapDispatchToProps = dispatch => {
return {
selectProject: project => {
dispatch(selectProject(project));
selectProject: (id, name) => {
dispatch(selectProject(id, name));
},
closeAllPanels: () => {
dispatch(closeAllPanels())
closeTransactionsHistoryPanel: () => {
dispatch(closeTransactionsHistoryPanel())
}
};
};
Expand Down
2 changes: 1 addition & 1 deletion src/components/projecteditor/pane.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export class Pane {
} else {
this.props.parent.closePane(this.id);
}
this.props.parent.redraw();
// this.props.parent.redraw();
};

focusWindow = (winId, rePerform, cb) => {
Expand Down
43 changes: 35 additions & 8 deletions src/components/projecteditor/panes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
// along with Superblocks Lab. If not, see <http://www.gnu.org/licenses/>.

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { panesActions } from '../../actions';
import classnames from 'classnames';
import PropTypes from 'prop-types';
import style from './style.less';
import { Pane, PaneComponent } from './pane';
import { IconClose } from '../icons';
import { DropdownContainer } from '../dropdown';

export default class Panes extends Component {
class Panes extends Component {
constructor(props) {
super(props);
this.panes = [];
Expand All @@ -37,7 +39,7 @@ export default class Panes extends Component {
}

addWindow = (props, paneId) => {
var pane;
var pane, newPaneWasAdded = false;
if (paneId) {
pane = this.getPane(paneId).pane;
}
Expand All @@ -48,8 +50,12 @@ export default class Panes extends Component {
parent: this,
});
this.panes.unshift(pane);
newPaneWasAdded = true;
}
var winId = pane.addWindow(props);
if (newPaneWasAdded) {
this.props.addPane(pane.id, pane.getTitle()); // lifting some state to redux store
}
if (winId == null) return {};
return { pane, winId };
};
Expand All @@ -69,6 +75,7 @@ export default class Panes extends Component {
this.panes = this.panes.filter(pane => {
return pane.id != id;
});
this.props.removePane(id);
};

getPane = id => {
Expand Down Expand Up @@ -245,7 +252,9 @@ export default class Panes extends Component {
renderHeader = () => {
const tab = style.tab;
const selected = style.selected;
const html = this.panes.map((pane, index) => {
const html = this.props.panes.map((paneData, index) => {
const pane = this.panes[index];

const contextMenu = (
<div className={style.contextMenu}>
<div className={style.item} onClick={this.closeAllPanes}>
Expand All @@ -256,16 +265,17 @@ export default class Panes extends Component {
</div>
</div>
);
var isSelected = pane.id == this.activePaneId;

var isSelected = paneData.id == this.activePaneId;
const cls = {};
cls[tab] = true;
cls[selected] = isSelected;
return (
<div key={index}>
<div
className={classnames(cls)}
onMouseDown={e => this.tabClicked(e, pane.id)}
onContextMenu={e => this.tabRightClicked(e, pane.id)}
onMouseDown={e => this.tabClicked(e, paneData.id)}
onContextMenu={e => this.tabRightClicked(e, paneData.id)}
>
<DropdownContainer
dropdownContent={contextMenu}
Expand All @@ -277,13 +287,13 @@ export default class Panes extends Component {
{pane.getIcon()}
</div>
<div className={style.title2}>
{pane.getTitle()}
{paneData.name}
</div>
</div>
<div className={style.close}>
<button className="btnNoBg"
onClick={ e =>
this.tabClickedClose(e, pane.id)
this.tabClickedClose(e, paneData.id)
}
>
<IconClose />
Expand Down Expand Up @@ -374,3 +384,20 @@ Panes.propTypes = {
functions: PropTypes.object.isRequired,
isActionPanelShowing: PropTypes.bool.isRequired,
};

const mapStateToProps = state => ({
panes: state.panes.panes,
});

const mapDispatchToProps = (dispatch) => {
return {
addPane: (id, name) => {
dispatch(panesActions.addPane(id, name))
},
removePane: (id) => {
dispatch(panesActions.removePane(id))
}
}
}

export default connect(mapStateToProps, mapDispatchToProps)(Panes);
6 changes: 4 additions & 2 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import app from './app';
import projects from './projects';
import projects from './projects.reducer';
import settings from './settings';
import view from './view';
import panes from './panes.reducer';

const rehydrated = (state = false, action) => {
switch (action.type) {
Expand All @@ -17,5 +18,6 @@ export default {
app,
settings,
projects,
view
view,
panes
};
24 changes: 24 additions & 0 deletions src/reducers/panes.reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { panesActions } from '../actions'

export const initialState = {
panes: []
};

export default function panesReducer(state = initialState, action) {
switch (action.type) {
case panesActions.ADD_PANE: {
return {
...state,
panes: state.panes.concat([ { ... action.data } ])
};
}
case panesActions.REMOVE_PANE: {
return {
...state,
panes: state.panes.filter(p => p.id !== action.data.id)
}
}
default:
return state;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ export default function projectsReducer(state = initialState, action) {
case 'SELECT_PROJECT': {
return {
...state,
selectedProject: {
id: action.data.project.getInode(),
name: action.data.project.getName()
},
selectedProject: { ...action.data },
};
}
default:
Expand Down
2 changes: 1 addition & 1 deletion src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const config = {
key: 'root',
storage,
version: 2,
blacklist: ['app', 'view'],
blacklist: ['app', 'view', 'panes'],
migrate: createMigrate(migrations, { debug: true })
};

Expand Down

0 comments on commit 17224cc

Please sign in to comment.