added save/import functionality
authorAlex-Laptop <[email protected]>
Fri, 22 Mar 2019 17:40:22 +0000 (10:40 -0700)
committerAlex-Laptop <[email protected]>
Fri, 22 Mar 2019 17:40:22 +0000 (10:40 -0700)
.gitignore
src/classes/workout.js
src/components/main/main.component.js
src/index.js

index 466ca6a1eba418f3f083ad3730fd2a00fdc5adbb..0e6fab9fc608a2a50e9b4d6eee31044526b78e83 100644 (file)
@@ -1 +1,2 @@
-.nyc_output/**
\ No newline at end of file
+.nyc_output/**
+workouts.json
\ No newline at end of file
index fdfef9b6237b2f8786d31ae8fee77da314f273dc..4f0447764babe930fbb5fb7d8b2fb2703ff16f76 100644 (file)
@@ -13,6 +13,40 @@ export default class workout {
                        this.datesDone = [];
                        this.name = "New Workout";
                        this.description = "";
+                       if(typeof toClone == "object") {
+                               if(toClone.attributes !== void(0)) {
+                                       for(let i in toClone.attributes) {
+                                               if(toClone.attributes[i] === true) {
+                                                       try {
+                                                               this.toggleAttribute(i);
+                                                       } catch(err) {
+
+                                                       }
+                                               }
+                                       }
+                               }
+                               if(toClone.datesDone !== void(0)) {
+                                       try {
+                                               this.add(toClone.datesDone);
+                                       } catch(err) {
+
+                                       }
+                               }
+                               if(toClone.name !== void(0)) {
+                                       try {
+                                               this.setName(toClone.name);
+                                       } catch(err) {
+
+                                       }
+                               }
+                               if(toClone.description !== void(0)) {
+                                       try {
+                                               this.changeDescription(toClone.description);
+                                       } catch(err) {
+
+                                       }
+                               }
+                       }
                }
        }
        add(dates) { // add new workout to datesDone
index e272c33fb72241be44a6db769566e6e8ec0bb789..deeab19fcde21aac0d41612eea7f6ec72bd88f40 100644 (file)
@@ -8,14 +8,34 @@ import {recent} from '../recent/recent.js';
 export default class Main extends React.Component {
        constructor(props) {
                super(props);
+               this.save = this.save.bind(this);
                this.switchView = this.switchView.bind(this);
+               this.handleKeyPress = this.handleKeyPress.bind(this);
+       }
+       componentDidMount() {
+               document.addEventListener('keydown',this.handleKeyPress);
+       }
+       componentWillUnmount() {
+               document.removeEventListener('keydown',this.handleKeyPress);
+       }
+       handleKeyPress(event) {
+               if(event.ctrlKey) {
+                       if(event.key=="s") {
+                               this.save();
+                               event.preventDefault();
+                       }
+               }
+       }
+       save() {
+               const {workouts,save} = this.props;
+               save(workouts);
        }
        switchView(type) {
                this.props.switchView(type);
        }
        render() {
-               const props = this.props;
-               const otherView = (props.view=="manage")?"recent":"manage";
+               const {view} = this.props;
+               const otherView = (view=="manage")?"recent":"manage";
                return createElement("div",{className:style.container},
                        createElement("div",{className:style.headerContainer},
                                createElement(header,null)
@@ -23,7 +43,8 @@ export default class Main extends React.Component {
                        createElement("div",{className:style.viewContainer},
                                createElement(daysAgo,null),
                                createElement("input",{type:"button",onClick:this.switchView.bind(this,otherView),value:otherView}),
-                               (props.view=="manage")?createElement(manage,null):createElement(recent,null)
+                               createElement("input",{type:"button",onClick:this.save,value:"Save Workouts"}),
+                               (view=="manage")?createElement(manage,null):createElement(recent,null)
                        )
                );
        }
index 06a8a32db42a799d784a6c312403072db45db1ee..b40b47ebf52de071880c336d420c920f7b5fb199 100644 (file)
@@ -8,14 +8,41 @@ import reducers from 'reducers/combined.js';
 
 import {main} from 'components/main/main.js';
 
-window.workoutsInit = (anchor) => {
+import workout from 'classes/workout.js';
+
+import data from '../workouts.json';
+
+const defaultSaveFunction = (data) => {
+       const content = JSON.stringify(data);
+       var a = document.createElement('a');
+    var blob = new Blob([content], {'type':'application/octet-stream'});
+    a.href = window.URL.createObjectURL(blob);
+    a.download = 'workouts.json';
+    document.body.appendChild(a);
+    a.click();
+    setTimeout(() => {
+       document.body.removeChild(a);
+       window.URL.revokeObjectURL(blob);
+    }, 0);
+};
+
+window.workoutsInit = (anchor,saveCb) => {
        if(!(anchor instanceof HTMLElement)) {
                throw new Error("Invalid anchor");
        }
-       const store = createStore(reducers,{});
+       // Import data into store
+       if((data !== void(0))&&(typeof data =="object")) {
+               for(let i in data) {
+                       data[i] = new workout(data[i]);
+               }
+       }
+       const store = createStore(reducers,{workouts:data});
+       if(typeof saveCb != "function") {
+               saveCb = defaultSaveFunction;
+       }
        render(
                createElement(Provider,{store},
-                       createElement(main,null)
+                       createElement(main,{save:saveCb})
                ),
                anchor
        );