Skip to content

Commit

Permalink
Merge branch 'master' into refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
qjd2413 committed Sep 3, 2016
2 parents dedae20 + afd1f4f commit 8393b6c
Show file tree
Hide file tree
Showing 9 changed files with 421 additions and 65 deletions.
1 change: 0 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
"no-self-assign": "warn",
"no-self-compare": "warn",
"no-sequences": "error",
"no-throw-literal": "error",
"no-unmodified-loop-condition": "warn",
"no-unused-expressions": "warn",
"no-useless-call": "error",
Expand Down
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
],
"dependencies": {
"angular": "~1.3.15",
"angular-ui-router": "~0.2.13"
"angular-ui-router": "~0.2.13",
"angularjs-datepicker": "^2.1.6"
}
}
19 changes: 6 additions & 13 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ var gulp = require('gulp');
var autoprefixer = require('gulp-autoprefixer');
var argv = require('yargs').argv;
var bowerMain = require('bower-main');
var buddy = require('gulp-buddy.js');
var changed = require('gulp-changed');
var concat = require('gulp-concat');
var cssnano = require('gulp-cssnano');
Expand Down Expand Up @@ -33,8 +32,8 @@ var logger = require('./app/util/logger');
var jsFiles = [angularFiles, './config/config.js', './app/**/*.js'];

var movedFiles = [
'./config/*.js', 'index.js', './app/**/*.js',
'./node_modules/**/*.js', './models/**/*'
'./config/config.js', './app/**/*.js', './models/**/*', './index.js',
'./node_modules/**/*.js', './node_modules/**/*.json'
];
if(prod) {
logger.info('Prod Build');
Expand All @@ -50,10 +49,10 @@ var logger = require('./app/util/logger');
});

gulp.task('move', ['clean'], function() {
return gulp.src(movedFiles, { base: '.', read: false })
return gulp.src(movedFiles, { base: '.' })
.pipe(ignore(/gulp/))
.pipe(changed('dist'))
.pipe(gulp.dest('dist'));
.pipe(changed('./dist'))
.pipe(gulp.dest('./dist'));
});

gulp.task('html', ['clean'], function() {
Expand Down Expand Up @@ -109,21 +108,15 @@ var logger = require('./app/util/logger');
// run app files through jshint
gulp.task('hint', function() {
return gulp.src(jsFiles)
.pipe(buddy({ reporter: 'detailed' }))
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
gulp.watch(jsFiles, ['inject']);

// automatically restart the server if js files change
gulp.task('start', ['inject', 'move', 'img'], function() {
var script = 'index.js';
if(prod) {
script = './dist/index.js';
}
nodemon({
script: script,
script: './dist/index.js',
ext: 'js'
});
});
Expand Down
1 change: 1 addition & 0 deletions public/app/app.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,6 @@

angular.module('KDRPoints', [
'ui.router',
'720kb.datepicker'
]).config(KDRPoints);
}());
87 changes: 85 additions & 2 deletions public/app/ng-services/service.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,93 @@
(function() {
'use strict';

var toDateObj = function(date) {
var newDate = date.date + ' ' + date.hour + ':' +
date.min + ' ' + date.cycle;
newDate = new Date(Date.parse(newDate));
return newDate.toString() === 'Invalid Date' ? false : newDate;
};

var cleanHour = function(hour, type) {
var cleanedHour = {};
cleanedHour.description = hour.description;
if(!cleanedHour.description) {
throw 'Description is required.';
}
if(type === 'Hour') {
cleanedHour.startTime = toDateObj(hour.start);
cleanedHour.endTime = toDateObj(hour.end);
if(!cleanedHour.startTime || !cleanedHour.endTime) {
throw 'Invalid Date';
}
} else {
cleanedHour.amount = hour.amount;
if(cleanedHour.amount < 0) {
throw 'Amount must be greater than zero.';
}
}
return cleanedHour;
};

var submitInProgress = false;

var serviceService = function(httpService) {
return {
submit: function(hour) {
return httpService.post('/service/submit', hour);
calculateHourEquivalent: function(amount) {
var hour = 60;
var scale = 60 / 5;
var amountToMinutes = amount * scale;
if(amountToMinutes < 0) {
throw 'Amount must be greater than zero.';
}
var duration = {
hours: Math.floor(amountToMinutes / hour),
minutes: amountToMinutes % hour
};
if(duration.minutes < 10) {
duration.minutes = '0' + duration.minutes;
}
return duration;
},
calculateDuration: function(start, end) {
var startDate = toDateObj(start);
var endDate = toDateObj(end);

var diffMinutes = (endDate - startDate) / 60000;
if(diffMinutes < 0) {
throw 'Start date must be before end date.';
}
var hour = 60;
var duration = {
hours: Math.floor(diffMinutes / hour),
minutes: diffMinutes % hour
};
if(duration.minutes < 10) {
duration.minutes = '0' + duration.minutes;
}
return duration;
},
submit: function(hour, type) {
if(submitInProgress) {
return;
}
submitInProgress = true;
var cleanedHour = null;
try {
cleanedHour = cleanHour(hour, type);
} catch(e) {
submitInProgress = false;
throw e;
}
return httpService.post('/service/submit', cleanedHour)
.then(function(success) {
submitInProgress = false;
return success;
})
.catch(function() {
submitInProgress = false;
return false;
});
},
approvableHours: function() {
return httpService.get('/service')
Expand Down
92 changes: 75 additions & 17 deletions public/app/service/submit/ctrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,89 @@
(function() {
'use strict';

var months = ['January', 'February', 'March', 'April',
'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December'];
var hours = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'];
var minutes = ['00', '15', '30', '45'];

var parseDate = function(date) {
var month = months[date.getMonth()];
var day = date.getDate();
var year = date.getFullYear();
var min = date.getMinutes();
min = minutes[Math.floor(min / 15)];
var hour = date.getHours();
return {
date: month + ' ' + day + ', ' + year,
hour: (hour % 12).toString(),
min: min,
cycle: hour / 12 < 1 ? 'AM' : 'PM'
};
};

var ctrl = function($scope, serviceService) {
$scope.type = 'Hour';
$scope.hour = {};
$scope.notify = {};

$scope.hours = hours;
$scope.minutes = minutes;

var resetData = function() {
$scope.hour = {};
var endDate = new Date();
var startDate = new Date(endDate.getTime() - (15 * 60000));

$scope.hour.start = parseDate(startDate);
$scope.hour.end = parseDate(endDate);
$scope.hour.amount = 0;

$scope.calculateDuration();
$scope.calculateEquivalent();
};

$scope.submit = function() {
var cleanHour = {};
var hour = $scope.hour;
cleanHour.description = hour.description;
if($scope.type === 'Hour') {
cleanHour.startTime = hour.startTime;
cleanHour.endTime = hour.endTime;
var submission = serviceService.submit($scope.hour, $scope.type);
if(submission.then) {
submission.then(function(success) {
if(success) {
resetData();
} else {
$scope.notify.error = 'Submission failed.';
$scope.notify.success = null;
}
});
} else {
cleanHour.amount = hour.amount;
$scope.notify.error = submission;
}
serviceService.submit(cleanHour)
.then(function() {
$scope.hour = {};
$scope.success = true;
});
};

// remove success when new service hour is input
$scope.$watch('hour', function() {
$scope.success = false;
}, true);
$scope.calculateDuration = function() {
var start = $scope.hour.start;
var end = $scope.hour.end;
var duration = null;
try {
duration = serviceService.calculateDuration(start, end);
} catch(e) {
$scope.notify.error = e;
return;
}
$scope.hour.duration = duration;
};

$scope.calculateEquivalent = function() {
var amount = $scope.hour.amount;
var duration = null;
try {
duration = serviceService.calculateHourEquivalent(amount);
} catch(e) {
$scope.notify.error = e;
return;
}
$scope.hour.equivalent = duration;
};

resetData();
};

angular.module('KDRPoints')
Expand Down
56 changes: 43 additions & 13 deletions public/app/service/submit/template.html
Original file line number Diff line number Diff line change
@@ -1,23 +1,53 @@
<div id="submit-service">
<select ng-model="type">
<select ng-model="type" ng-change="calculateDuration(); calculateEquivalent()" >
<option>Hour</option>
<option>Donation</option>
</select>
<br />

Description:
<input type="text" ng-model="hour.description" />

<div ng-show="type === 'Hour'">
Start Time: <input type="text" ng-model="hour.startTime" /><br />
End Time: <input type="text" ng-model="hour.endTime" /><br />
</div>
<form name="hourForm">
Description:
<input type="text" ng-model="hour.description" />

<div ng-show="type === 'Donation'">
Amount: <input type="text" ng-model="hour.amount" />
</div>
<div ng-show="type === 'Hour'">
Start:
<datepicker date-format="MMMM d, yyyy" date-typer="true" >
<input ng-model="hour.start.date" type="text" ng-change="calculateDuration()" />
</datepicker><br />
<select ng-model="hour.start.hour" ng-options="hr for hr in hours"></select>
:
<select ng-model="hour.start.min" ng-options="min for min in minutes"></select>
<select ng-model="hour.start.cycle" ng-change="changeFunction()" >
<option value="AM">AM</option>
<option value="PM">PM</option>
</select><br />

End:
<datepicker date-format="MMMM d, yyyy" date-typer="true" >
<input ng-model="hour.end.date" type="text" ng-change="calculateDuration()" />
</datepicker><br />
<select ng-model="hour.end.hour" ng-options="hr for hr in hours"></select>
:
<select ng-model="hour.end.min" ng-options="min for min in minutes"></select>
<select ng-model="hour.end.cycle" ng-change="changeFunction()" >
<option value="AM">AM</option>
<option value="PM">PM</option>
</select><br />

<button ng-click="submit()">Submit</button>
<div class="duration">
Duration: {{hour.duration.hours}}:{{hour.duration.minutes}}
</div>
</div>

<div ng-show="success">Hour successfully submitted.</div>
<div ng-show="type === 'Donation'">
Amount: $<input type="number" ng-model="hour.amount" ng-change="calculateEquivalent()" /><br />
Hour Equivalent: {{hour.equivalent.hours}}:{{hour.equivalent.minutes}}
</div>

<button ng-click="submit()">Submit</button>

<div ng-show="notify.success">{{type}} successfully submitted.</div>
<div ng-show="notify.error">{{notify.error}}</div>

</form>
</div>
Loading

0 comments on commit 8393b6c

Please sign in to comment.