Skip to content

Commit

Permalink
Merge pull request #657 from simonv3/fix-623-add-garden-bug
Browse files Browse the repository at this point in the history
Fix 623 add garden bug
  • Loading branch information
simonv3 committed Jul 7, 2015
2 parents 09652d6 + ca596de commit 9ef43ee
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ openFarmModule.directive('alerts', ['$timeout',
$scope.closeAlert = function(index) {
$scope.alerts.splice(index, 1);
};
$scope.$watch('alerts.length', function(){
$scope.$watch('alerts', function(){
console.log('alerts length changed');
if ($scope.alerts && $scope.alerts.length){
$timeout(function(){
$scope.alerts = $scope.alerts.filter(function(alert){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ openFarmModule.factory('gardenService', ['$http',
.success(function (response, object) {
alerts.push({
'type': 'success',
'msg': 'Success!'
'msg': 'Updated Your Garden!'
});
if (callback){
return callback(true, response, object);
Expand Down Expand Up @@ -56,7 +56,7 @@ openFarmModule.factory('gardenService', ['$http',
.success(function (object, status) {
alerts.push({
'type': 'success',
'msg': 'Success!'
'msg': 'Created Your Garden!'
});
if (callback){
return callback(true, object.garden, status);
Expand All @@ -82,7 +82,7 @@ openFarmModule.factory('gardenService', ['$http',
.success(function (response, object) {
alerts.push({
'type': 'success',
'msg': 'Success!'
'msg': 'Saved The Garden Crop!'
});
if (callback){
return callback(true, response, object);
Expand Down Expand Up @@ -110,7 +110,7 @@ openFarmModule.factory('gardenService', ['$http',
.success(function(response, object){
alerts.push({
'type': 'success',
'msg': 'Success!'
'msg': 'Added Crop to Garden!'
});
if (callback){
return callback(true, response, object);
Expand Down
60 changes: 0 additions & 60 deletions app/assets/javascripts/angular-libs/angular.openfarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,66 +18,6 @@ openFarmApp.config(['localStorageServiceProvider',
.setPrefix('openFarm');
}]);

openFarmModule.factory('userService', ['$http',
function userService($http) {
// get the guide specified.
var getUser = function(userId, alerts, callback){
$http({
url: '/api/users/' + userId,
method: 'GET'
}).success(function (response) {
return callback (true, response.user);
}).error(function (response, code) {
alerts.push({
msg: response,
type: 'warning'
});
return callback(false, response, code);
});
};

var setFavoriteCrop = function(userId, cropId, alerts, callback){
// wrapper function around put user
var params = {
'user': {},
'user_setting': {
'favorite_crop': cropId
}
}

$http.put('/api/users/' + userId + '/', params)
.success(function(response) {
return callback(true, response.user);
}).error(function (response, code) {
alerts.push({
msg: response,
type: 'warning'
});
return callback(false, response, code);
});
}

var updateUser = function(userId, params, alerts, callback) {
$http.put('/api/users/' + userId + '/', params)
.success(function(response) {
return callback(true, response.user);
}).error(function (response, code) {
alerts.push({
msg: response,
type: 'warning'
});
return callback(false, response, code);
});
}

return {
'getUser': getUser,
'updateUser': updateUser,
'setFavoriteCrop': setFavoriteCrop
};

}]);

openFarmModule.directive('markdown', ['$sanitize',
function markdown($sanitize) {
var converter = new Showdown.converter();
Expand Down
59 changes: 59 additions & 0 deletions app/assets/javascripts/angular-libs/angular.openfarm.users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
openFarmModule.factory('userService', ['$http',
function userService($http) {
// get the guide specified.
var getUser = function(userId, alerts, callback){
$http({
url: '/api/users/' + userId,
method: 'GET'
}).success(function (response) {
return callback (true, response.user);
}).error(function (response, code) {
alerts.push({
msg: response,
type: 'warning'
});
return callback(false, response, code);
});
};

var setFavoriteCrop = function(userId, cropId, alerts, callback){
// wrapper function around put user
var params = {
'user': {},
'user_setting': {
'favorite_crop': cropId
}
}

$http.put('/api/users/' + userId + '/', params)
.success(function(response) {
return callback(true, response.user);
}).error(function (response, code) {
alerts.push({
msg: response,
type: 'warning'
});
return callback(false, response, code);
});
}

var updateUser = function(userId, params, alerts, callback) {
$http.put('/api/users/' + userId + '/', params)
.success(function(response) {
return callback(true, response.user);
}).error(function (response, code) {
alerts.push({
msg: response,
type: 'warning'
});
return callback(false, response, code);
});
}

return {
'getUser': getUser,
'updateUser': updateUser,
'setFavoriteCrop': setFavoriteCrop
};

}]);
2 changes: 1 addition & 1 deletion app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
//= require ./libs/moment.min
//= require ./angular-libs/angular.openfarm
//= require ./angular-libs/angular.openfarm.directives
//= require ./angular-libs/angular.openfarm.users
//= require ./angular-libs/angular.openfarm.guides
//= require ./angular-libs/angular.openfarm.gardens
//= require ./angular-libs/angular.openfarm.stages
Expand Down Expand Up @@ -70,7 +71,6 @@ var getIDFromURL = function(key) {
};

var pushToAlerts = function (response, code, alerts){
console.log(response, code);
var msg = '';
angular.forEach(response, function(value){
msg += value;
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/users/gardens.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ openFarmApp.controller('gardenCtrl', ['$scope', '$http', 'userService',
var callback = function(success, garden){
if (success){
$scope.currentUser.gardens.push(garden);
$scope.profileUser.gardens.push(garden);
$scope.newGarden = {};
$scope.addingGarden = false;
}
};
gardenService.createGarden(newGarden, $scope.alerts, callback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@
}
}

.add-garden{
.add-garden-success {
margin: 1rem auto 4rem;
}

.add-garden {
margin: 1rem auto 4rem;

hr{
Expand Down
3 changes: 3 additions & 0 deletions app/views/users/show/_gardens.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<div class="gardens" ng-controller="gardenCtrl" ng-cloak>

<div alerts="alerts"></div>

<div class="section-header">
<h3 class="section-title">Gardens</h3>
<button ng-if="profileUser._id === currentUser._id" class="button right tiny" ng-click="toggleAddGarden()">Add Garden</button>
Expand Down

0 comments on commit 9ef43ee

Please sign in to comment.