Skip to content

Commit c413e55

Browse files
authored
Merge pull request #46 from oslabs-beta/rydang/exportimport
export and import button functionality
2 parents 0262055 + b3eaf3e commit c413e55

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
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: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ let intervalId = null;
1414
class MainContainer extends Component {
1515
constructor(props) {
1616
super(props);
17+
1718
this.state = {
1819
snapshots: [],
1920
snapshotIndex: 0,
@@ -127,6 +128,40 @@ class MainContainer extends Component {
127128
port.postMessage({ action: 'jumpToSnap', payload: snapshots[snapshotIndex] });
128129
}
129130

131+
importSnapshots() {
132+
const { snapshots } = this.state;
133+
134+
// create invisible download anchor link
135+
const fileDownload = document.createElement('a');
136+
137+
// set file in anchor link
138+
fileDownload.href = URL.createObjectURL(
139+
new Blob([JSON.stringify(snapshots)], { type: 'application/json' }),
140+
);
141+
142+
// set anchor as file download and click it
143+
fileDownload.setAttribute('download', 'snapshot.json');
144+
fileDownload.click();
145+
146+
// remove file url
147+
URL.revokeObjectURL(fileDownload.href);
148+
}
149+
150+
exportSnapshots() {
151+
const fileUpload = document.createElement('input');
152+
fileUpload.setAttribute('type', 'file');
153+
154+
fileUpload.onchange = (event) => {
155+
const reader = new FileReader();
156+
reader.onload = () => {
157+
this.setState({ snapshots: JSON.parse(reader.result) });
158+
};
159+
reader.readAsText(event.target.files[0]);
160+
};
161+
162+
fileUpload.click();
163+
}
164+
130165
toggleMode(targetMode) {
131166
const { mode, mode: { locked, paused, persist }, port } = this.state;
132167
switch (targetMode) {
@@ -172,7 +207,12 @@ class MainContainer extends Component {
172207
playing={playing}
173208
pause={this.pause}
174209
/>
175-
<ButtonsContainer mode={mode} toggleMode={this.toggleMode} />
210+
<ButtonsContainer
211+
mode={mode}
212+
toggleMode={this.toggleMode}
213+
importSnapshots={this.importSnapshots}
214+
exportSnapshots={this.exportSnapshots}
215+
/>
176216
</div>
177217
</div>
178218
);

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)