@@ -13,31 +13,57 @@ import { useStoreContext } from '../store';
13
13
14
14
import Tutorial from '../components/Tutorial' ;
15
15
16
+ // function exportHandler takes in a parameter snapshots which is typed as an array
17
+ // the function does not return anything so the type is void
16
18
function exportHandler ( snapshots : [ ] ) : void {
17
19
// create invisible download anchor link
20
+ // fileDownload is typed as HTMLAnchorElement
21
+ // HTML anchor element has the <a></a> tag
18
22
const fileDownload : HTMLAnchorElement = document . createElement ( 'a' ) ;
19
23
20
24
// set file in anchor link
25
+ // href is the reference to the URL object created from the Blob
21
26
fileDownload . href = URL . createObjectURL (
27
+ // Blob obj is raw data, json is raw, stringify the snapshots as json so the Blob can access the raw data
22
28
new Blob ( [ JSON . stringify ( snapshots ) ] , { type : 'application/json' } ) ,
23
29
) ;
24
30
25
31
// set anchor as file download and click it
32
+ // anchor element has a download attribute
33
+ // snapshot.json is what the file name will be once the file is downloaded locally
26
34
fileDownload . setAttribute ( 'download' , 'snapshot.json' ) ;
35
+ // click is a method on all HTML elements
36
+ // simulates a mouse click, triggering the element's click event
27
37
fileDownload . click ( ) ;
28
38
29
39
// remove file url
40
+ // after file is downloaded, remove the href
30
41
URL . revokeObjectURL ( fileDownload . href ) ;
31
42
}
32
43
33
44
function importHandler ( dispatch : ( a : unknown ) => void ) : void {
45
+ // create input element
46
+ // fileUpload is HTMLInputElement, which is an interface for HTML input elements
47
+ // accepts data from user
34
48
const fileUpload = document . createElement ( 'input' ) ;
49
+ // file is a type attribute on the input element, allows users to select a file
35
50
fileUpload . setAttribute ( 'type' , 'file' ) ;
36
51
52
+ // onChange is when value of HTML element is changed
53
+ // parameter for onChange is an event
37
54
fileUpload . onchange = ( e : Event ) => {
55
+ // FileReader is an object
56
+ // reads contents of local files in async
57
+ // can use file or blob objects
38
58
const reader = new FileReader ( ) ;
39
59
reader . onload = ( ) => {
60
+ // once the local file has been loaded, result property on FileReader object returns the file's contents
61
+ // then take contents and convert to a string
40
62
const test = reader . result . toString ( ) ;
63
+ // dispatch sends the result of calling importSnapshots on the json parsed data from the file contents from the new FileReader object
64
+ // importSnapshots defined in actions/actions.ts/line 71, it returns an action object with a type and payload, payload is newSnaps parameter
65
+ // dispatch function is being called with that action object which gets sent to the reducer in reducers/mainReducer.js/line 203
66
+ // this updates the current tab
41
67
return dispatch ( importSnapshots ( JSON . parse ( test ) ) ) ;
42
68
} ;
43
69
const eventFiles = e . target as HTMLInputElement ;
0 commit comments