Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #7

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ testem.log

#build script
build.sh

#api keys/environment
/config/environment.js
1 change: 1 addition & 0 deletions app/components/gym-sort.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Ember from 'ember';

//used to sort the gym records
export default Ember.Component.extend({
tagName: 'ul',
sortedGyms: Ember.computed.sort('gymLifts', 'sortDefinition'),
Expand Down
1 change: 1 addition & 0 deletions app/components/lift-sort.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Ember from 'ember';

//used to sort leaderboards
export default Ember.Component.extend({
tagName: 'ul',
sortedLifts: Ember.computed.sort('lifts', 'sortDefinition'),
Expand Down
43 changes: 32 additions & 11 deletions app/controllers/gyms.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import Ember from 'ember';
import config from '../config/environment';

export default Ember.Controller.extend({
ajax: Ember.inject.service(),
actions: {

//validate input and send ajax request to leaderboard
validateInput() {

let name = this.get('username');
let units = this.get('units');
let age = parseInt(this.get('age'));
Expand All @@ -16,12 +20,12 @@ export default Ember.Controller.extend({

let errors = false;

//check each field for errors
Ember.$('.error').css('visibility', 'hidden');
if(units == null) {
Ember.$('#unitsError').css('visibility', 'visible');
errors = true;
}
console.log(name);
if(name === undefined) {
Ember.$('#nameError').css('visibility', 'visible');
errors = true;
Expand Down Expand Up @@ -66,7 +70,6 @@ export default Ember.Controller.extend({
}

let addr = this.get('gymAddress');
let self = this;
let total = bench + squat + deadlift;

//store everything as pounds
Expand All @@ -78,13 +81,16 @@ export default Ember.Controller.extend({
height *= 0.393701;
}

let self = this;
//get the longitude and latitude from the address using google maps API
return this.get('ajax').request('/google-gym', {
method: 'GET',
data: {
address: addr
}
})
.then(function(res) {
//send another request to the leaderboard
return self.get('ajax').request('/gym-lifts', {
method: 'POST',
data: {
Expand All @@ -102,25 +108,30 @@ export default Ember.Controller.extend({
}
})
.then(function() {
//update view
self.set('newGymRecord', false);
self.send('findGym');
});
});
},
addGymRecord() {
//show the add record form
this.set('newGymRecord', true);
},
addGym() {
//adds a new gym to the database
let name = this.get('newGymName');
let addr = this.get('newGymAddress');
let self = this;
return this.get('ajax').request('/google-gym', {
//get the coordinates from google maps
method: 'GET',
data: {
address: addr
}
})
.then(function(res) {
//put the new gym in the database along with its coordinates
return self.get('ajax').request('/gym', {
method: 'POST',
data: {
Expand All @@ -131,24 +142,33 @@ export default Ember.Controller.extend({
}
})
.then(function() {
//add validation
//update the view to say we found the current gym
self.set('gymNotFound', false);
self.set('gymFound', false);
self.set('sucessfullyAdded', true);
});
});
},
addGymForm() {
//show the add gym form
this.set('addGymForm', true);
},
findGym() {
//find a gym from it's address
let self = this;
let addr = this.get('gymAddress');

//make sure the address form is completed
if(!addr) {
Ember.$('#addressError').css({'visibility': 'visible'});
return;
}

//if it was, remove the error message
Ember.$('#addressError').css({'visibility': 'hidden'});

return this.get('ajax').request('/google-gym', {
//get coordinates from google maps
method: 'GET',
data: {
address: addr
Expand All @@ -157,6 +177,7 @@ export default Ember.Controller.extend({
.then(function(res) {
self.latitude = res.latitude;
self.longitude = res.longitude;
//try to get a gym associated with this latitude and longitude
return self.get('ajax').request('/gym', {
method: 'GET',
data: {
Expand All @@ -166,24 +187,25 @@ export default Ember.Controller.extend({
})
.then(function(res) {
let gyms = res.gyms;
//if gyms exist, take the first one for simplicity
if(gyms.length > 0) {
self.set('centerLat', gyms[0].latitude);
self.set('centerLng', gyms[0].longitude);
const map = 'https://maps.googleapis.com/maps/api/staticmap?'+
'center=' + gyms[0].latitude +',' + gyms[0].longitude +'&zoom=12&size=400x400&' +
'maptype=roadmap&key=AIzaSyDizqIx-O56c809Cl9xK4NxqNuU_8SuXwU';
console.log(map);

//grab a map image
self.set('mapURL', 'https://maps.googleapis.com/maps/api/staticmap?'+
'center=' + gyms[0].latitude +',' + gyms[0].longitude +'&zoom=12&size=400x400&' +
'maptype=roadmap&key=AIzaSyDizqIx-O56c809Cl9xK4NxqNuU_8SuXwU');
'maptype=roadmap&key=' + config.KEYS.GOOGLE_MAPS);

//update view
self.set('gymNotFound', false);
self.set('addGymForm', false);
self.set('gymFound', true);
self.set('sucessfullyAdded', false);
self.set('foundGymName', gyms[0].name);
self.set('foundGymAddress', gyms[0].address);
console.log(gyms[0]);

//get the lifts for this gym
return self.get('ajax').request('/gym-lifts', {
method: 'GET',
data: {
Expand All @@ -192,13 +214,13 @@ export default Ember.Controller.extend({
}
})
.then(function(res) {
console.log(res.gymlift);
self.set('gymRecords', res.gymlift);
})
.catch(function(err) {
console.log(err);
});
}
//otherwise, add the gym!
else {
self.set('gymFound', false);
self.set('sucessfullyAdded', false);
Expand All @@ -213,7 +235,6 @@ export default Ember.Controller.extend({
.catch(function(err) {
console.log(err);
});

}
}
});
22 changes: 12 additions & 10 deletions app/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,8 @@ const wilksFemaleF = -0.00000009054;
export default Ember.Controller.extend({
ajax: Ember.inject.service(),
actions: {
validateInput() {


console.log('hello stats');
this.send('calculateLifts');
},
sendRequest() {
//get input fields
let age = parseInt(this.get('age'));
let weight = parseInt(this.get('weight'));
let height = parseInt(this.get('height'));
Expand All @@ -51,8 +46,12 @@ export default Ember.Controller.extend({
height *= 0.393701;
}

sex = (sex===true ? 'Male' : 'Female');
//convert radio button values into the user's sex
sex = (sex === true ? 'Male' : 'Female');

let total = bench + squat + deadlift;

//add data to the leaderboards
return this.get('ajax').request('/lifts', {
method: 'POST',
data: {
Expand All @@ -68,6 +67,7 @@ export default Ember.Controller.extend({
});
},
calculateLifts() {
//get input fields
let units = this.get('units');
let age = parseInt(this.get('age'));
let weight = parseInt(this.get('weight'));
Expand All @@ -79,6 +79,7 @@ export default Ember.Controller.extend({

let errors = false;

//validate forms
Ember.$('.error').css('visibility', 'hidden');
if(units == null) {
Ember.$('#unitsError').css('visibility', 'visible');
Expand Down Expand Up @@ -116,19 +117,23 @@ export default Ember.Controller.extend({
return;
}

//update view and start setting values
this.set('isCalculated', true);
this.set('liftTotal', bench+squat+deadlift);

//convert sex
sex = (sex===true ? 'Male' : 'Female');
this.set('sexResult', sex);

//convert everything to imperial units
if(!units) {
weight *= 2.2;
bench *= 2.2;
squat *= 2.2;
deadlift *= 2.2;
height *= 0.393701;
}

//used for color coding body diagram
let benchRate, squatRate, deadRate;
let benchColor, squatColor, deadColor;
Expand Down Expand Up @@ -314,10 +319,7 @@ export default Ember.Controller.extend({
data[i+1] = squatColor[1];
data[i+2] = squatColor[2];
}


}

ctx.putImageData(imgData, 0, 0);
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/templates/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
Deadlift: {{input value=deadMax enter="calculateLifts" class="form-control"}}
<div id="deadliftError" class="error"> Please enter a valid deadlift! </div>

<button {{action "validateInput"}} > Calculate </button>
<button {{action "sendRequest"}} > Calculate </button>
</form>


Expand Down
7 changes: 5 additions & 2 deletions config/environment.js → config/environment.js.example
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ module.exports = function(environment) {
},

APP: {
// Here you can pass flags/options to your application instance
// when it is created

},

KEYS: {
'GOOGLE_MAPS' : ''
},

contentSecurityPolicy: {
Expand Down