Commit 0381c66c authored by René Jochum's avatar René Jochum

Add insert and active functionality

Signed-off-by: René Jochum's avatarRené Jochum <rene@jochums.at>
parent 20dbf139
......@@ -3,7 +3,7 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@minadmin/redux-tree-reducer": "file:../../",
"@minadmin/redux-tree-reducer": "^0.0.2",
"@testing-library/dom": "^6.11.0",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
......@@ -36,5 +36,8 @@
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@types/react": "^16.9.17"
}
}
......@@ -1066,18 +1066,20 @@
"@types/istanbul-reports" "^1.1.1"
"@types/yargs" "^13.0.0"
"@minadmin/redux-immutable-ts@^0.1.1":
version "0.1.1"
resolved "https://registry.yarnpkg.com/@minadmin/redux-immutable-ts/-/redux-immutable-ts-0.1.1.tgz#beab9fc81af05990bb53b41f3d33a54cc38bd70e"
integrity sha512-LsimR4dud7hAfIDBPxbH+14ggxvzQYNLK/kTO5b9PWVkEZgHRJs/5txWC7eV0cXJMNe5Zmanr3E5/2mYmYzXJw==
"@minadmin/redux-immutable-ts@^0.2.1":
version "0.2.1"
resolved "https://registry.yarnpkg.com/@minadmin/redux-immutable-ts/-/redux-immutable-ts-0.2.1.tgz#261e625e23640f022a9f3d5adebca14e110ac330"
integrity sha512-n6WD+qPHFkn2MQBGXXLlN4Skxnv7l104oUTPSY0h9s8hnFASMdoJlfTr37z1wNJARQ5ztDsNBSUE0wCrLWY32A==
dependencies:
"@babel/runtime" "^7.7.7"
"@minadmin/redux-tree-reducer@file:../..":
version "0.1.0"
"@minadmin/redux-tree-reducer@^0.0.1":
version "0.0.1"
resolved "https://registry.yarnpkg.com/@minadmin/redux-tree-reducer/-/redux-tree-reducer-0.0.1.tgz#b544a01de32fb2ee2472ca7ebd11af8a5f32af56"
integrity sha512-GB+ZHLfL6BiGUe8xm8KB+3ZxAM3YaCgArC3fhLmBsmOKBBBya2/Q1sw4ozEZjcjGzEU5+OYYwyzclgKbTbCwAg==
dependencies:
"@babel/runtime" "^7.7.7"
"@minadmin/redux-immutable-ts" "^0.1.1"
"@minadmin/redux-immutable-ts" "^0.2.1"
immutable "^4.0.0-rc.12"
redux "^4.0.5"
......@@ -1370,7 +1372,7 @@
hoist-non-react-statics "^3.3.0"
redux "^4.0.0"
"@types/react@*":
"@types/react@*", "@types/react@^16.9.17":
version "16.9.17"
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.9.17.tgz#58f0cc0e9ec2425d1441dd7b623421a867aa253e"
integrity sha512-UP27In4fp4sWF5JgyV6pwVPAQM83Fj76JOcg02X5BZcpSu5Wx+fP9RMqc2v0ssBoQIFvD5JdKY41gjJJKmw6Bg==
......
{
"name": "@minadmin/redux-tree-reducer",
"version": "0.0.1",
"version": "0.0.2",
"description": "Tree reducer for redux",
"author": "pcdummy",
"license": "MIT",
......
import { AnyAction } from "redux";
export const ADD_NODE = "ADD_NODE";
export const INSERT_NODE = "INSERT_NODE";
export const REMOVE_NODE = "REMOVE_NODE";
export const TOGGLE_NODE = "TOGGLE_NODE";
export const TOGGLE_ALL_NODES = "TOGGLE_ALL_NODES";
export const SET_ACTIVE_NODE = "SET_ACTIVE_NODE";
export const INSERT_BEFORE = -1;
export const INSERT_AFTER = 1;
export let nextId: number = 0;
export const setNextId = (newNextId: number) => (nextId = newNextId);
......@@ -14,6 +19,14 @@ export const addNode = <T>(obj: T, parentId = -1): AnyAction => ({
parentId
});
export const insertNode = <T>(obj: T, beforeId: number, where: number = INSERT_AFTER): AnyAction => ({
type: INSERT_NODE,
id: nextId++,
obj,
beforeId,
where
});
export const removeNode = (id: number): AnyAction => ({
type: REMOVE_NODE,
id
......@@ -38,3 +51,10 @@ export const toggleAllNodes = (
type: TOGGLE_ALL_NODES,
force
});
export const setActiveNode = (
id: number = -1
): AnyAction => ({
type: SET_ACTIVE_NODE,
id,
});
\ No newline at end of file
This diff is collapsed.
import {
ADD_NODE,
INSERT_NODE,
REMOVE_NODE,
TOGGLE_NODE,
TOGGLE_ALL_NODES
TOGGLE_ALL_NODES,
INSERT_BEFORE,
SET_ACTIVE_NODE
} from "../actions";
import { AnyAction } from "redux";
......@@ -60,6 +63,36 @@ function _addNode<T>(
);
}
function _insertNode<T>(
state: FlatTreeList<T>,
action: AnyAction
): FlatTreeList<T> {
const beforeIdx = state.findIndex((x) => x.get('id') === action.beforeId);
if (beforeIdx === -1) {
let newNode = new FlatTreeNode<T>({
id: action.id,
parentId: -1,
obj: action.obj
});
return state.insert(0, newNode);
}
const beforeNode = state.get(beforeIdx) as FlatTreeNode<T>;
let newNode = new FlatTreeNode<T>({
id: action.id,
parentId: beforeNode.parentId,
obj: action.obj
});
if (action.where === INSERT_BEFORE) {
return state.insert(beforeIdx, newNode);
}
return state.insert(beforeIdx+1, newNode);
}
function _removeNode<T>(
state: FlatTreeList<T>,
action: AnyAction
......@@ -164,6 +197,8 @@ function nodes<T>(
switch (action.type) {
case ADD_NODE:
return _addNode<T>(state, action);
case INSERT_NODE:
return _insertNode(state, action);
case REMOVE_NODE:
return _removeNode<T>(state, action);
case TOGGLE_NODE:
......@@ -181,10 +216,12 @@ function nodes<T>(
}
const activeNode = (
state: List<any> | undefined = List<any>(),
state: number | undefined = -1,
action: AnyAction
) => {
switch (action.type) {
case SET_ACTIVE_NODE:
return action.id;
default:
return state;
}
......
Markdown is supported
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