Skip to content

Commit ecf6010

Browse files
committed
starting working on import/export buttons
1 parent 52d8bda commit ecf6010

File tree

4 files changed

+52
-19
lines changed

4 files changed

+52
-19
lines changed

src/app/containers/ButtonsContainer.jsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
33

4-
const ButtonsContainer = ({ mode: { paused, locked, persist }, toggleMode }) => (
4+
const ButtonsContainer = ({ mode: { paused, locked, persist }, toggleMode, importSnapshots, exportSnapshots }) => (
55
<div className="buttons-container">
66
<div className="pause-button" onClick={() => toggleMode('paused')}>{(paused) ? 'Resume' : 'Pause'}</div>
77
<div className="lock-button" onClick={() => toggleMode('locked')}>{(locked) ? 'Unlock' : 'Lock'}</div>
88
<div className="persist-button" onClick={() => toggleMode('persist')}>{(persist) ? 'Unpersist' : 'Persist'}</div>
9+
<div className="import-button" onClick={importSnapshots}>Import</div>
10+
<div className="export-button" onClick={exportSnapshots}>Export</div>
911
</div>
1012
);
1113

1214
ButtonsContainer.propTypes = {
1315
toggleMode: PropTypes.func.isRequired,
16+
importSnapshots: PropTypes.func.isRequired,
17+
exportSnapshots: PropTypes.func.isRequired,
1418
mode: PropTypes.shape({
1519
paused: PropTypes.bool,
1620
locked: PropTypes.bool,

src/app/containers/MainContainer.jsx

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class MainContainer extends Component {
6969
if (snapshots.length > 0 && snapshotIndex > 0) {
7070
const newIndex = snapshotIndex - 1;
7171
// second callback parameter of setState to invoke handleJumpSnapshot
72-
this.setState({ snapshotIndex: newIndex }, this.handleJumpSnapshot(newIndex) );
72+
this.setState({ snapshotIndex: newIndex }, this.handleJumpSnapshot(newIndex));
7373
}
7474
}
7575

@@ -78,25 +78,25 @@ class MainContainer extends Component {
7878
this.pause();
7979
if (snapshotIndex < snapshots.length - 1) {
8080
const newIndex = snapshotIndex + 1;
81-
this.setState({ snapshotIndex: newIndex }, this.handleJumpSnapshot(newIndex) );
81+
this.setState({ snapshotIndex: newIndex }, this.handleJumpSnapshot(newIndex));
8282
}
8383
}
8484

8585
play() {
8686
globalPlaying = !globalPlaying
87-
this.setState({playing: globalPlaying}, () => {
88-
if(this.state.playing){
87+
this.setState({ playing: globalPlaying }, () => {
88+
if (this.state.playing) {
8989
intervalId = setInterval(() => {
9090
const { snapshots, snapshotIndex } = this.state;
91-
if (snapshotIndex < snapshots.length - 1) {
92-
const newIndex = snapshotIndex + 1;
93-
this.setState({ snapshotIndex: newIndex}, this.handleJumpSnapshot(newIndex) );
94-
} else {
95-
// clear interval when play reaches the end
96-
globalPlaying = false;
97-
clearInterval(intervalId);
98-
this.setState({ playing: false })
99-
}
91+
if (snapshotIndex < snapshots.length - 1) {
92+
const newIndex = snapshotIndex + 1;
93+
this.setState({ snapshotIndex: newIndex }, this.handleJumpSnapshot(newIndex));
94+
} else {
95+
// clear interval when play reaches the end
96+
globalPlaying = false;
97+
clearInterval(intervalId);
98+
this.setState({ playing: false })
99+
}
100100
}, 1000);
101101
} else {
102102
clearInterval(intervalId);
@@ -105,7 +105,7 @@ class MainContainer extends Component {
105105
}
106106

107107
pause() {
108-
this.setState({playing: false}, clearInterval(intervalId))
108+
this.setState({ playing: false }, clearInterval(intervalId))
109109
}
110110

111111
emptySnapshot() {
@@ -128,6 +128,28 @@ class MainContainer extends Component {
128128
port.postMessage({ action: 'jumpToSnap', payload: snapshots[snapshotIndex] });
129129
}
130130

131+
importSnapshots() {
132+
const { snapshots } = this.state;
133+
const fileDownload = document.createElement('a');
134+
fileDownload.style.display = 'none';
135+
document.body.appendChild(fileDownload);
136+
fileDownload.href = URL.createObjectURL(
137+
new Blob([JSON.stringify(snapshots)], { type: 'application/json' }),
138+
);
139+
fileDownload.setAttribute('download', 'snapshot.json');
140+
fileDownload.click();
141+
URL.revokeObjectURL(fileDownload.href);
142+
document.body.removeChild(fileDownload);
143+
}
144+
145+
exportSnapshots() {
146+
const fileUpload = document.createElement('input');
147+
fileUpload.setAttribute('type', 'file');
148+
149+
fileUpload.click();
150+
this;
151+
}
152+
131153
toggleMode(targetMode) {
132154
const { mode, mode: { locked, paused, persist }, port } = this.state;
133155
switch (targetMode) {
@@ -170,10 +192,15 @@ class MainContainer extends Component {
170192
moveBackward={this.moveBackward}
171193
moveForward={this.moveForward}
172194
play={this.play}
173-
playing = {playing}
174-
pause = {this.pause}
195+
playing={playing}
196+
pause={this.pause}
197+
/>
198+
<ButtonsContainer
199+
mode={mode}
200+
toggleMode={this.toggleMode}
201+
importSnapshots={this.importSnapshots}
202+
exportSnapshots={this.exportSnapshots}
175203
/>
176-
<ButtonsContainer mode={mode} toggleMode={this.toggleMode} />
177204
</div>
178205
</div>
179206
);

src/app/styles/components/_buttons.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
margin: 7px;
4646
}
4747

48+
.import-button,
49+
.export-button,
4850
.lock-button,
4951
.pause-button,
5052
.persist-button {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.buttons-container {
22
margin: 0 1% 0 1%;
33
display: grid;
4-
grid-template-columns: repeat(3, 1fr);
4+
grid-template-columns: repeat(5, 1fr);
55
grid-gap: 1%;
66
padding: 1% 0 1% 0;
77
}

0 commit comments

Comments
 (0)