Skip to content

Commit caf1d61

Browse files
committed
declerative state changed
1 parent 21af97d commit caf1d61

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/app/components/Action.jsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ const Action = props => {
1414
role="presentation"
1515
style={index > sliderIndex ? { color: '#5f6369' } : {}}
1616
>
17-
<div className="action-component-text">{index}</div>
17+
<div className="action-component-text">
18+
{`${index
19+
}: ${delta}`}
20+
21+
</div>
1822
<button
1923
className="jump-button"
2024
onClick={e => {

src/app/containers/ActionContainer.jsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* eslint-disable react/no-array-index-key */
2+
/* eslint-disable no-inner-declarations */
23
import React from 'react';
4+
import { diff, formatters } from 'jsondiffpatch';
35
import Action from '../components/Action';
46

57
import { emptySnapshots } from '../actions/actions';
@@ -17,15 +19,56 @@ function ActionContainer() {
1719
let actionsArr = [];
1820
// build actions array
1921
if (snapshots.length > 0) {
22+
// breadth first function - take in an delta obj
23+
function breadthFirst(delta) {
24+
// aux array = current and one called next
25+
let current = Object.values(delta.children); // Object.keys(delta.children);
26+
let next = [];
27+
let result = '';
28+
// return a string
29+
while (current.length > 0) {
30+
const shifted = current.shift();
31+
if (shifted.state) {
32+
// eslint-disable-next-line no-loop-func
33+
Object.keys(shifted.state).forEach(key => {
34+
result = result.concat(key, ', ');
35+
});
36+
}
37+
if (shifted.children) {
38+
Object.keys(shifted.children).forEach(el => {
39+
next.push(shifted.children[el]);
40+
});
41+
}
42+
if (current.length === 0) {
43+
current = next;
44+
next = [];
45+
}
46+
}
47+
return result;
48+
}
49+
50+
function truncate(newDiff, num = 15) {
51+
if (newDiff.length <= num) return newDiff.replace(',', '');
52+
const thisdiff = newDiff.slice(0, num);
53+
return thisdiff.concat('...');
54+
}
2055
actionsArr = snapshots.map((snapshot, index) => {
2156
const selected = index === viewIndex;
57+
let newDiff = '';
58+
// if index is greater than 0
59+
if (index > 0) {
60+
// calculate the diff
61+
const delta = diff(snapshots[index - 1], snapshot);
62+
newDiff = truncate(breadthFirst(delta));
63+
}
2264
return (
2365
<Action
2466
key={`action${index}`}
2567
index={index}
2668
selected={selected}
2769
dispatch={dispatch}
2870
sliderIndex={sliderIndex}
71+
delta={newDiff}
2972
/>
3073
);
3174
});

0 commit comments

Comments
 (0)