Skip to content

Commit b3eaf3e

Browse files
committed
import and export button functionality done
1 parent 6c77ebd commit b3eaf3e

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/app/containers/MainContainer.jsx

Lines changed: 17 additions & 4 deletions
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,
@@ -129,24 +130,36 @@ class MainContainer extends Component {
129130

130131
importSnapshots() {
131132
const { snapshots } = this.state;
133+
134+
// create invisible download anchor link
132135
const fileDownload = document.createElement('a');
133-
fileDownload.style.display = 'none';
134-
document.body.appendChild(fileDownload);
136+
137+
// set file in anchor link
135138
fileDownload.href = URL.createObjectURL(
136139
new Blob([JSON.stringify(snapshots)], { type: 'application/json' }),
137140
);
141+
142+
// set anchor as file download and click it
138143
fileDownload.setAttribute('download', 'snapshot.json');
139144
fileDownload.click();
145+
146+
// remove file url
140147
URL.revokeObjectURL(fileDownload.href);
141-
document.body.removeChild(fileDownload);
142148
}
143149

144150
exportSnapshots() {
145151
const fileUpload = document.createElement('input');
146152
fileUpload.setAttribute('type', 'file');
147153

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+
148162
fileUpload.click();
149-
this;
150163
}
151164

152165
toggleMode(targetMode) {

0 commit comments

Comments
 (0)