Skip to content

Commit 6bfe8c3

Browse files
authored
Merge pull request #70 from oslabs-beta/bryan/diff
Bryan/diff
2 parents 7bce3c3 + dfa5928 commit 6bfe8c3

File tree

11 files changed

+361
-44
lines changed

11 files changed

+361
-44
lines changed

package-lock.json

Lines changed: 28 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@
5252
},
5353
"dependencies": {
5454
"d3": "^3.5.17",
55+
"jsondiffpatch": "^0.3.11",
5556
"prop-types": "^15.7.2",
5657
"rc-slider": "^8.6.13",
5758
"rc-tooltip": "^3.7.3",
59+
"react-html-parser": "^2.0.2",
5860
"react": "^16.9.0",
5961
"react-dom": "^16.9.0",
6062
"react-json-tree": "^0.11.2",

src/app/components/Chart.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
/* eslint-disable react/no-string-refs */
55
import React, { Component } from 'react';
66
import PropTypes from 'prop-types';
7-
import '../styles/components/_d3Tree.scss';
87
import * as d3 from 'd3';
98

109
let root = {};

src/app/components/Diff.jsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import { diff, formatters } from 'jsondiffpatch';
4+
import ReactHtmlParser from 'react-html-parser';
5+
6+
import { useStoreContext } from '../store';
7+
8+
function Diff({ snapshot, show }) {
9+
const [mainState] = useStoreContext();
10+
const { currentTab, tabs } = mainState;
11+
const { snapshots, viewIndex, sliderIndex } = tabs[currentTab];
12+
let previous;
13+
14+
// previous follows viewIndex or sliderIndex
15+
if (viewIndex !== -1) {
16+
previous = snapshots[viewIndex - 1];
17+
} else {
18+
previous = snapshots[sliderIndex - 1];
19+
}
20+
21+
const delta = diff(previous, snapshot);
22+
// returns html in string
23+
const html = formatters.html.format(delta, previous);
24+
if (show) formatters.html.showUnchanged();
25+
else formatters.html.hideUnchanged();
26+
27+
if (previous === undefined) return <div> states are equal </div>;
28+
return (
29+
<div>
30+
{ ReactHtmlParser(html) }
31+
</div>
32+
);
33+
}
34+
35+
Diff.propTypes = {
36+
snapshot: PropTypes.shape({
37+
state: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
38+
children: PropTypes.arrayOf(PropTypes.object),
39+
}).isRequired,
40+
show: PropTypes.bool.isRequired,
41+
};
42+
43+
export default Diff;

src/app/components/DiffRoute.jsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import {
4+
MemoryRouter as Router, Route, NavLink, Switch,
5+
} from 'react-router-dom';
6+
7+
import Diff from './Diff';
8+
9+
const DiffRoute = ({ snapshot }) => (
10+
<Router>
11+
<div className="navbar">
12+
<NavLink className="router-link" activeClassName="is-active" exact to="/">
13+
Tree
14+
</NavLink>
15+
<NavLink className="router-link" activeClassName="is-active" to="/diffRaw">
16+
Raw
17+
</NavLink>
18+
</div>
19+
<Switch>
20+
<Route path="/diffRaw" render={() => <Diff snapshot={snapshot} show />} />
21+
<Route path="/" render={() => <Diff snapshot={snapshot} show={false} />} />
22+
</Switch>
23+
</Router>
24+
);
25+
26+
DiffRoute.propTypes = {
27+
snapshot: PropTypes.shape({
28+
state: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
29+
children: PropTypes.arrayOf(PropTypes.object),
30+
}).isRequired,
31+
};
32+
33+
export default DiffRoute;

src/app/components/StateRoute.jsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import {
4+
MemoryRouter as Router, Route, NavLink, Switch,
5+
} from 'react-router-dom';
6+
7+
import Chart from './Chart';
8+
import Tree from './Tree';
9+
10+
const StateRoute = ({ snapshot }) => (
11+
<Router>
12+
<div className="navbar">
13+
<NavLink className="router-link" activeClassName="is-active" exact to="/">
14+
Tree
15+
</NavLink>
16+
<NavLink className="router-link" activeClassName="is-active" to="/chart">
17+
Chart
18+
</NavLink>
19+
</div>
20+
<Switch>
21+
<Route path="/chart" render={() => <Chart snapshot={snapshot} />} />
22+
<Route path="/" render={() => <Tree snapshot={snapshot} />} />
23+
</Switch>
24+
</Router>
25+
);
26+
27+
StateRoute.propTypes = {
28+
snapshot: PropTypes.shape({
29+
state: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
30+
children: PropTypes.arrayOf(PropTypes.object),
31+
}).isRequired,
32+
};
33+
34+
export default StateRoute;

src/app/containers/StateContainer.jsx

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,38 @@
1-
import React from 'react';
1+
import React, { useState } from 'react';
22
import PropTypes from 'prop-types';
33
import {
44
MemoryRouter as Router, Route, NavLink, Switch,
55
} from 'react-router-dom';
6-
import Tree from '../components/Tree';
7-
import Chart from '../components/Chart';
6+
import StateRoute from '../components/StateRoute';
7+
import DiffRoute from '../components/DiffRoute';
88

9-
const StateContainer = ({ snapshot }) => (
10-
<Router>
11-
<div className="state-container">
12-
<div className="navbar">
13-
<NavLink className="router-link" activeClassName="is-active" exact to="/">
14-
Tree
15-
</NavLink>
16-
<NavLink className="router-link" activeClassName="is-active" to="/chart">
17-
Chart
18-
</NavLink>
9+
10+
const StateContainer = ({ snapshot }) => {
11+
const [Text, setText] = useState('State');
12+
return (
13+
<Router>
14+
<div className="state-container">
15+
<div className="main-navbar-container">
16+
<div className="main-navbar-text">
17+
{Text}
18+
</div>
19+
<div className="main-navbar">
20+
<NavLink className="main-router-link" activeClassName="is-active" exact to="/">
21+
State
22+
</NavLink>
23+
<NavLink className="main-router-link" activeClassName="is-active" to="/diff">
24+
Diff
25+
</NavLink>
26+
</div>
27+
</div>
28+
<Switch>
29+
<Route path="/diff" render={() => { setText('Diff'); return <DiffRoute snapshot={snapshot} />; }} />
30+
<Route path="/" render={() => { setText('State'); return <StateRoute snapshot={snapshot} />; }} />
31+
</Switch>
1932
</div>
20-
<Switch>
21-
<Route path="/chart" render={() => <Chart snapshot={snapshot} />} />
22-
<Route path="/" render={() => <Tree snapshot={snapshot} />} />
23-
</Switch>
24-
</div>
25-
</Router>
26-
);
33+
</Router>
34+
);
35+
};
2736

2837
StateContainer.propTypes = {
2938
snapshot: PropTypes.shape({

0 commit comments

Comments
 (0)