Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added action type: 'pushToCurrent' #389

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import assert from 'assert';
import Scene from './Scene';
export const JUMP_ACTION = 'jump';
export const PUSH_ACTION = 'push';
export const PUSH_TO_CURRENT_ACTION = 'pushToCurrent';
export const REPLACE_ACTION = 'replace';
export const POP_ACTION2 = 'back';
export const POP_ACTION = 'BackAction';
Expand Down Expand Up @@ -51,7 +52,7 @@ class Actions {
assert(root.props, "props should be defined for stack");
const key = root.key;
assert(key, "unique key should be defined ",root);
assert([POP_ACTION, POP_ACTION2, REFRESH_ACTION, REPLACE_ACTION, JUMP_ACTION, PUSH_ACTION, RESET_ACTION, 'create',
assert([POP_ACTION, POP_ACTION2, REFRESH_ACTION, REPLACE_ACTION, JUMP_ACTION, PUSH_ACTION, PUSH_TO_CURRENT_ACTION, RESET_ACTION, 'create',
'init','callback','iterate','current'].indexOf(key)==-1, key+" is not allowed as key name");
const {children, ...staticProps} = root.props;
let type = root.props.type || (parentProps.tabs ? JUMP_ACTION : PUSH_ACTION);
Expand Down
41 changes: 24 additions & 17 deletions src/Reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
*/

import {PUSH_ACTION, POP_ACTION2, FOCUS_ACTION, JUMP_ACTION, INIT_ACTION, REPLACE_ACTION, RESET_ACTION, POP_ACTION, REFRESH_ACTION} from './Actions';
import {PUSH_ACTION, PUSH_TO_CURRENT_ACTION, POP_ACTION2, FOCUS_ACTION, JUMP_ACTION, INIT_ACTION, REPLACE_ACTION, RESET_ACTION, POP_ACTION, REFRESH_ACTION} from './Actions';
import assert from 'assert';
import Immutable from 'immutable';
import {getInitialState} from './State';
Expand All @@ -16,13 +16,13 @@ function findElement(state, key) {
if (state.sceneKey != key){
if (state.children){
let result = undefined;
state.children.forEach(el=>{
for (let el of state.children) {
let res = findElement(el, key);
if (res){
result = res;
return res;
break;
}
});
}
return result;
} else {
return false;
Expand All @@ -34,27 +34,24 @@ function findElement(state, key) {

function getCurrent(state){
if (!state.children){
return state.key;
return state;
}
return getCurrent(state.children[state.index]);
}



function update(state,action){
// clone state, TODO: clone effectively?
if (!state.scenes[action.key] && action.key.indexOf('_')!=-1){
action.key = action.key.substring(action.key.indexOf('_')+1);
//console.log("Transform to key="+action.key);
if (!state.scenes[action.key]) {
console.log("No scene for key="+action.key);
return state;
}
const newProps = {...state.scenes[action.key], ...action};
let newProps = {...state.scenes[action.key], ...action};
let newState = Immutable.fromJS(state).toJS();

// change route property
//newState.scenes[action.key] = newProps;

// get parent
const parent = newProps.parent;
let parent = newProps.parent;
assert(parent, "No parent is defined for route="+action.key);

// find parent in the state
Expand All @@ -72,7 +69,8 @@ function update(state,action){
if (el.children.length > 1) {
el.children.pop();
el.index = el.children.length - 1;
newState.scenes.current = getCurrent(newState);
delete newState.scenes[newState.scenes.current];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleting looks not good - scenes should store all possible scenes, like library.

newState.scenes.current = getCurrent(newState).key;
return newState;
} else {
console.log("Cannot do pop");
Expand All @@ -86,10 +84,18 @@ function update(state,action){
el.children[ind] = getInitialState(newProps, newState.scenes, ind, action);
return newState;

case PUSH_TO_CURRENT_ACTION:
parent = getCurrent(newState).parent;
newProps.parent = parent;
el = findElement(newState, parent);
assert(el, "Cannot find element for parent="+parent+" within current state:"+JSON.stringify(newState));
// fall through to PUSH_ACTION

case PUSH_ACTION:
el.children.push(getInitialState(newProps, newState.scenes, el.children.length, action));
el.index = el.children.length - 1;
newState.scenes.current = getCurrent(newState);
newState.scenes.current = getCurrent(newState).key;
newState.scenes[newState.scenes.current] = newProps;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modification of scenes could bring some unpleasant issues - it is possible that we could have several scenes in stack with the same sceneKey (such issue was created already in the past), for example on Details of user1 and then Details of user2. And you are proposing to set 'scene' with user1 or user2?

return newState;

case JUMP_ACTION:
Expand All @@ -110,7 +116,7 @@ function update(state,action){
} else {
el.children = [getInitialState(newProps, newState.scenes, 0, action)];
}
newState.scenes.current = getCurrent(newState);
newState.scenes.current = getCurrent(newState).key;
return newState;

default:
Expand Down Expand Up @@ -146,6 +152,7 @@ function reducer({initialState, scenes}){
case POP_ACTION2:
case POP_ACTION:
case REFRESH_ACTION:
case PUSH_TO_CURRENT_ACTION:
case PUSH_ACTION:
case JUMP_ACTION:
case REPLACE_ACTION:
Expand All @@ -161,4 +168,4 @@ function reducer({initialState, scenes}){

}

export default reducer;
export default reducer;