Skip to content

Commit e61a820

Browse files
merge with currentDev branch
2 parents 45f69dc + a8f9892 commit e61a820

File tree

3 files changed

+85
-14
lines changed

3 files changed

+85
-14
lines changed

src/app/containers/ButtonsContainer.tsx

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ function importHandler(dispatch: (a: unknown) => void): void {
4747
// accepts data from user
4848
const fileUpload = document.createElement('input');
4949
// file is a type attribute on the input element, allows users to select a file
50+
console.log('fileUpload element:', fileUpload)
5051
fileUpload.setAttribute('type', 'file');
5152

5253
// onChange is when value of HTML element is changed
@@ -56,26 +57,39 @@ function importHandler(dispatch: (a: unknown) => void): void {
5657
// reads contents of local files in async
5758
// can use file or blob objects
5859
const reader = new FileReader();
60+
console.log('on change triggered')
61+
//console.log('reader :', reader);
62+
63+
const eventFiles = e.target as HTMLInputElement;
64+
// console.log('e.target:', e.target)
65+
// console.log('event files:', eventFiles.files[0]);
66+
67+
if (eventFiles) {
68+
reader.readAsText(eventFiles.files[0]);
69+
}
70+
5971
reader.onload = () => {
6072
// once the local file has been loaded, result property on FileReader object returns the file's contents
6173
// then take contents and convert to a string
74+
console.log('on load triggered:')
6275
const test = reader.result.toString();
6376
// dispatch sends the result of calling importSnapshots on the json parsed data from the file contents from the new FileReader object
6477
// importSnapshots defined in actions/actions.ts/line 71, it returns an action object with a type and payload, payload is newSnaps parameter
6578
// dispatch function is being called with that action object which gets sent to the reducer in reducers/mainReducer.js/line 203
6679
// this updates the current tab
6780
return dispatch(importSnapshots(JSON.parse(test)));
6881
};
69-
const eventFiles = e.target as HTMLInputElement;
70-
if (eventFiles?.hasOwnProperty('files')) {
71-
// const eventFiles = target as HTMLInputElement;
72-
if (eventFiles) {
73-
reader.readAsText(eventFiles.files[0]);
74-
}
75-
}
82+
// const eventFiles = e.target as HTMLInputElement;
83+
// if (eventFiles?.hasOwnProperty('files')) {
84+
// // const eventFiles = target as HTMLInputElement;
85+
// if (eventFiles) {
86+
// reader.readAsText(eventFiles.files[0]);
87+
// }
88+
// }
7689
};
7790

7891
fileUpload.click();
92+
//console.log('dispatch importSnapshots successful')
7993
}
8094

8195
function ButtonsContainer(): JSX.Element {
@@ -85,13 +99,15 @@ function ButtonsContainer(): JSX.Element {
8599
mode: { paused },
86100
} = tabs[currentTab];
87101

102+
console.log('----state after any change----', tabs[currentTab])
103+
88104
return (
89105
<div className='buttons-container'>
90106
<button className='pause-button' type='button' onClick={() => dispatch(toggleMode('paused'))}>
91107
{paused ? <FontAwesomeIcon icon={faLock} /> : <FontAwesomeIcon icon={faUnlock} />}
92108
{paused ? 'Locked' : 'Unlocked'}
93109
</button>
94-
<button className='export-button' type='button' onClick={() => exportHandler(snapshots)}>
110+
<button className='export-button' type='button' onClick={() => exportHandler(tabs[currentTab])}>
95111
<FontAwesomeIcon icon={faDownload} />
96112
Download
97113
</button>

src/app/containers/MainContainer.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,13 @@ function MainContainer(): JSX.Element {
5454

5555
// listen for a message containing snapshots from the /extension/build/background.js service worker
5656
currentPort.onMessage.addListener(
57-
(message: { // parameter message is an object with following type script properties
57+
// parameter message is an object with following type script properties
58+
(message: {
5859
action: string;
5960
payload: Record<string, unknown>;
6061
sourceTab: number
6162
}) => {
62-
const { action, payload, sourceTab } = message; // we destructure message into action, payload, sourceTab
63+
const { action, payload, sourceTab } = message;
6364
let maxTab: number;
6465

6566
if (!sourceTab) { // if the sourceTab doesn't exist or is 0

src/app/reducers/mainReducer.js

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
import { produce } from 'immer';
22
import _, { values } from 'lodash';
33
import * as types from '../constants/actionTypes.ts';
4+
// import { current } from 'immer';
45

56
export default (state, action) =>
67
produce(state, (draft) => {
78
const { port, currentTab, tabs } = draft;
89
const { hierarchy, snapshots, mode, intervalId, viewIndex, sliderIndex } =
910
tabs[currentTab] || {};
1011

12+
// console.log('----consoles before reducer funcs!-----')
13+
// console.log('state:', state)
14+
//console.log(tabs[currentTab]);
15+
//console.log('properties of tabs[currentTab]:', hierarchy, snapshots, mode, intervalId, viewIndex, sliderIndex)
16+
17+
//console.log('reducer file!', 'hierarchy:', hierarchy, 'tabs:', tabs)
18+
1119
// eslint-disable-next-line max-len
1220
// function that finds the index in the hierarchy and extracts the name of the equivalent index to add to the post message
1321
// eslint-disable-next-line consistent-return
@@ -36,6 +44,7 @@ export default (state, action) =>
3644
switch (action.type) {
3745
// This saves the series user wants to save to chrome local storage
3846
case types.SAVE: {
47+
console.log('save action reducer!', 'payload:', action.payload);
3948
const { newSeries, newSeriesName } = action.payload;
4049
if (!tabs[currentTab].seriesSavedStatus) {
4150
tabs[currentTab] = { ...tabs[currentTab], seriesSavedStatus: 'inputBoxOpen' };
@@ -176,18 +185,28 @@ export default (state, action) =>
176185
break;
177186
}
178187
case types.EMPTY: {
188+
console.log('-----clear snapshots reducer----');
189+
console.log('state before:', state.tabs[currentTab]);
190+
179191
// send msg to background script
180-
port.postMessage({ action: 'emptySnap', tabId: currentTab });
192+
port.postMessage({ action: 'emptySnap', tabId: currentTab }); //communicate with background.js
193+
194+
// properties associated with timetravel + seek bar
181195
tabs[currentTab].sliderIndex = 0;
182196
tabs[currentTab].viewIndex = 0;
183197
tabs[currentTab].playing = false;
184-
const lastSnapshot = tabs[currentTab].snapshots[tabs[currentTab].snapshots.length - 1];
198+
199+
const lastSnapshot = tabs[currentTab].snapshots[tabs[currentTab].snapshots.length - 1]; // first snapshot?
200+
185201
// resets hierarchy to page last state recorded
186202
tabs[currentTab].hierarchy.stateSnapshot = { ...lastSnapshot };
203+
187204
// resets hierarchy
188205
tabs[currentTab].hierarchy.children = [];
206+
189207
// resets snapshots to page last state recorded
190208
tabs[currentTab].snapshots = [lastSnapshot];
209+
191210
// resets currLocation to page last state recorded
192211
tabs[currentTab].currLocation = tabs[currentTab].hierarchy;
193212
tabs[currentTab].index = 1;
@@ -201,12 +220,47 @@ export default (state, action) =>
201220
break;
202221
}
203222
case types.IMPORT: {
223+
// Log the value of tabs[currentTab].snapshots before the update
224+
console.log('-----import snapshots reducer----');
225+
console.log('state before:', state.tabs[currentTab]);
226+
console.log('action payload:', action.payload);
227+
204228
port.postMessage({
205229
action: 'import',
206-
payload: action.payload,
230+
payload: action.payload, //.snapshots,
207231
tabId: currentTab,
208232
});
209-
tabs[currentTab].snapshots = action.payload;
233+
234+
//============
235+
const savedSnapshot = action.payload;
236+
237+
tabs[currentTab].sliderIndex = savedSnapshot.sliderIndex;
238+
tabs[currentTab].viewIndex = savedSnapshot.viewIndex;
239+
tabs[currentTab].playing = false;
240+
241+
// resets hierarchy to page last state recorded
242+
tabs[currentTab].hierarchy.stateSnapshot = savedSnapshot.hierarchy.stateSnapshot;
243+
244+
// resets hierarchy
245+
tabs[currentTab].hierarchy.children = savedSnapshot.hierarchy.children;
246+
247+
// resets snapshots to page last state recorded
248+
tabs[currentTab].snapshots = savedSnapshot.snapshots;
249+
250+
// resets currLocation to page last state recorded
251+
tabs[currentTab].currLocation = tabs[currentTab].hierarchy;
252+
tabs[currentTab].index = savedSnapshot.index;
253+
tabs[currentTab].currParent = savedSnapshot.currParent;
254+
tabs[currentTab].currBranch = savedSnapshot.Branch;
255+
tabs[currentTab].seriesSavedStatus = false;
256+
257+
//============
258+
//tabs[currentTab].snapshots = action.payload.snapshots;
259+
260+
// console.log('New snapshots:', action.payload);
261+
// console.log('updated tabs[CurrentTab].snapshots:', tabs[currentTab].snapshots)
262+
//console.log('state after:', state)
263+
210264
break;
211265
}
212266
case types.TOGGLE_MODE: {

0 commit comments

Comments
 (0)