Skip to content

initial version of activity tracker #8

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
43 changes: 40 additions & 3 deletions src/components/Dropzone/Dropzone.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PropTypes from 'prop-types';

import state from '../../core/state';
import core from '../../core/core';
import rpmEvent from '../../core/event';

class Dropzone extends Component {
constructor(props) {
Expand All @@ -24,6 +25,36 @@ class Dropzone extends Component {

componentWillMount() {
this._setInitialElements(this.props.initialElements);

if (this.props.id === 'root') {
rpmEvent.addEventListener('stateReset', (elements) => {
console.log('stateReset elements', elements);
const { id: dropzoneID, parentID } = this.props;
const updatedInitialItems = elements.map(e => ({
...e,
key: e.id,
dropzoneID,
parentID,
showBasicContent: false,
skipStateUpdate: true,
updateState: this._updateState,
removeElement: this._removeElement,
updateElement: this._updateElement,
flushDroppedElements: this._flushDroppedElements,
checkAndRemoveElement: this._checkAndRemoveElement
}));

this.setState({
initialElements: updatedInitialItems,
droppedElements: updatedInitialItems,
initDone: false
}, () => {
this.setState({
initDone: true
})
});
})
}
}

componentWillReceiveProps({ initialElements }) {
Expand Down Expand Up @@ -62,7 +93,7 @@ class Dropzone extends Component {
this.setState({
initialElements: elements,
droppedElements: elements
}, () => this._updateState(done));
}, () => this._updateState(done, false, false));
}

/**
Expand Down Expand Up @@ -178,18 +209,23 @@ class Dropzone extends Component {
* function will further call `updateState` from state API, which updates the application state
* @param cb {function} - callback function - optional
*/
_updateState = (cb = () => {}, dispatchElementRemove) => {
_updateState = (cb = () => {}, dispatchElementRemove, trackActivity = true) => {
const {
id: dropzoneID,
parentID
} = this.props;

if (!this.state.initDone && this.props.skipStateUpdate) {
return false;
}

state.updateState(
dropzoneID,
parentID,
this.state.droppedElements,
cb,
dispatchElementRemove
dispatchElementRemove,
trackActivity
);
}

Expand Down Expand Up @@ -443,6 +479,7 @@ Dropzone.propTypes = {
capacity: PropTypes.number,
onDrop: PropTypes.func,
onElementMove: PropTypes.func,
skipStateUpdate: PropTypes.bool,
allowHorizontal: PropTypes.bool,
initialElements: PropTypes.arrayOf(Object),
parentID: PropTypes.string.isRequired,
Expand Down
7 changes: 7 additions & 0 deletions src/core/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class RpmEvent {
this.event = {
change: [],
flush: [],
stateReset: [],
removeElement: [],
updateElement: []
};
Expand Down Expand Up @@ -42,6 +43,12 @@ class RpmEvent {
this.event.removeElement.forEach(e => e(element));
};

// private function to trigger all state reset CB
notifyStateReset = (newState) => {
// trigger all events
this.event.stateReset.forEach(e => e(newState));
};

/**
* function to add event
* @param {String} eventName
Expand Down
21 changes: 20 additions & 1 deletion src/core/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class State {
constructor() {
// all private varibale goes here
const state = {};
this.stack = [];
const shareableElementProps = ['id', 'type', 'name', 'payload', 'dropzoneID', 'parentID'];

// set base
Expand Down Expand Up @@ -256,11 +257,22 @@ class State {

// function to update the state
// once update is done then triggers CB and notifyStateChange
this.updateState = (dropzoneID, parentID, fields, cb = () => {}, dispatchElementRemove) => {
this.updateState = (
dropzoneID,
parentID,
fields,
cb = () => {},
dispatchElementRemove,
trackActivity
) => {
traverseAndUpdateTree(dropzoneID, parentID, fields);
cb(state.tree);
rpmEvent.notifyStateChange();

if (trackActivity) {
this.stack.push(JSON.stringify(this.getStorableState()));
}

// dispatch elementRemove event if necessary
if (dispatchElementRemove) {
rpmEvent.notifyElementRemove({
Expand All @@ -273,6 +285,13 @@ class State {
}
};

this.resetState = (level) => {
if (this.stack[level]) {
console.log('level', level, this.stack[level]);
rpmEvent.notifyStateReset(JSON.parse(this.stack[level]));
}
}

// function to return element parent
this.getElementParent = traverseAndReturnParent;

Expand Down