From: Alex-Laptop Date: Fri, 22 Mar 2019 17:40:22 +0000 (-0700) Subject: added save/import functionality X-Git-Tag: v1.0.0~84 X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=b836c524beb24e02ba336b0843ffb154c5a97525;p=workouts added save/import functionality --- diff --git a/.gitignore b/.gitignore index 466ca6a..0e6fab9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.nyc_output/** \ No newline at end of file +.nyc_output/** +workouts.json \ No newline at end of file diff --git a/src/classes/workout.js b/src/classes/workout.js index fdfef9b..4f04477 100644 --- a/src/classes/workout.js +++ b/src/classes/workout.js @@ -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 diff --git a/src/components/main/main.component.js b/src/components/main/main.component.js index e272c33..deeab19 100644 --- a/src/components/main/main.component.js +++ b/src/components/main/main.component.js @@ -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) ) ); } diff --git a/src/index.js b/src/index.js index 06a8a32..b40b47e 100644 --- a/src/index.js +++ b/src/index.js @@ -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 );