Skip to content

Commit b1191fa

Browse files
Bug fixes in add/removeYear and elsewhere, Final changes before demo
1 parent 7ee3829 commit b1191fa

File tree

1 file changed

+76
-20
lines changed

1 file changed

+76
-20
lines changed

app/javascript/packs/myjs.js

Lines changed: 76 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,11 @@ function checkMissingReqs() {
161161
}
162162

163163
window.addYears = function() {
164-
alert("Got into add year function");
165164
if (currPlan.years.length >= 12) {
166165
alert("You can only have a maximum of 12 years in a plan!");
167166
return;
168167
}
169168

170-
// let queryString = window.location.search;
171-
// let urlParams = new URLSearchParams(queryString);
172-
// let numYears = urlParams.get("numYears");
173-
174169
let numYears = parseInt($("#numYears").val());
175170

176171
if (numYears > 12 || currPlan.years.length + numYears >= 12) {
@@ -179,22 +174,19 @@ window.addYears = function() {
179174
}
180175

181176
let lastTermHeaderInPlan = $(".termHeader").get(-1);
182-
let termAndYear = lastTermHeaderInPlan.textContent.split(" ");
183-
let lastYearInPlan = parseInt(termAndYear[1]);
177+
let lastYearInPlan = 0;
178+
if (lastTermHeaderInPlan == "undefined") {
179+
lastYearInPlan = currPlan.currYear - 1;
180+
} else {
181+
let termAndYear = lastTermHeaderInPlan.textContent.split(" ");
182+
lastYearInPlan = parseInt(termAndYear[1]);
183+
}
184184

185185
for (let i = 1; i < numYears + 1; i++) {
186186
currPlan.years.push(new Year(lastYearInPlan + i));
187187
}
188188

189189
currPlan.generateHTML();
190-
191-
// $.post("/plan", {
192-
// plan: plan.plan_name,
193-
// user: plan.user.id,
194-
// designator: draggedCourse.designator,
195-
// term: event.target.children[0].children[0].innerText.split(" ")[0],
196-
// year: parseInt(event.target.children[0].children[0].innerText.split(" ")[1]),
197-
// });
198190
return;
199191
}
200192

@@ -212,6 +204,7 @@ window.deleteYear = function() {
212204
$("#hrsCurrent").html("Current Hours: " + currPlan.hrsCurrent);
213205
$("#hrsFuture").html("Remaining Hours: " + currPlan.hrsFuture);
214206
$("#hrsTotal").html("Total Hours Planned: " + currPlan.hrsTotal);
207+
checkMissingReqs();
215208
currPlan.generateHTML();
216209

217210
}
@@ -302,7 +295,7 @@ function deleteCoursesAndYear(year) {
302295

303296
for (designator in coursesToRemove) {
304297
$.get("/plan_courses", {
305-
designator: designator,
298+
designator: coursesToRemove[designator].id,
306299
user: plan.user.id,
307300
plan: plan.plan_name
308301
});
@@ -403,13 +396,35 @@ window.dropOnPlan = function(event){
403396
}
404397
// add to javascript plan object
405398
let destTerm = event.target.children[0].children[0].innerText.split(" ")[0];
406-
let destYear = parseInt(event.target.children[0].children[0].innerText.split(" ")[1]);
399+
let destYear = parseInt(event.target.children[0].children[0].innerText.split(" ")[1]);
400+
// if (destTerm == "Fall") {
401+
// destYear++;
402+
// }
407403
let newCourse = {
408404
"designator": draggedCourse.designator,
409405
"term": destTerm,
410406
"year": destYear
411-
};
412-
currPlan.courses[draggedCourse.designator] = newCourse;
407+
};
408+
currPlan.courses[draggedCourse.designator] = newCourse;
409+
// add full course to years array in plan
410+
let newYearCourse = new Course(draggedCourse.designator, destYear, destTerm);
411+
let year = null;
412+
for (let i = 0; i < currPlan.years.length; i++) {
413+
if (destYear == currPlan.years[i].name) {
414+
year = currPlan.years[i];
415+
break;
416+
}
417+
}
418+
if (destTerm == "Fall") {
419+
year.fall.push(newYearCourse);
420+
year.fallHrs += newYearCourse.hours;
421+
} else if (destTerm == "Spring") {
422+
year.spring.push(newYearCourse);
423+
year.springHrs += newYearCourse.hours;
424+
} else {
425+
year.summer.push(newYearCourse);
426+
year.summerHrs += newYearCourse.hours;
427+
}
413428
//update db
414429
$.post("/plan_courses", {
415430
plan: plan.plan_name,
@@ -459,7 +474,44 @@ window.dropInTrash = function(event){
459474
draggedPlanOrigin.parentElement.previousSibling.children[1].innerText = "Hours: " + (originHours - draggedCourse.credits);
460475
draggedPlanOrigin.remove();
461476
delete currPlan.courses[draggedCourse.designator];
462-
draggedPlanOrigin = null;
477+
draggedPlanOrigin = null;
478+
479+
for (i in currPlan.courses) {
480+
if (currPlan.courses[i].designator == draggedCourse.designator) {
481+
delete currPlan.courses[i];
482+
break;
483+
}
484+
}
485+
486+
let year = null;
487+
for (let i = 0; i < currPlan.years.length; i++) {
488+
if (this.draggedCourse.year == currPlan.years[i].name) {
489+
year = currPlan.years[i];
490+
break;
491+
}
492+
}
493+
if (this.draggedCourse.term == "Fall") {
494+
for (let i = 0; i < year.fall[i]; i++) {
495+
if (year.fall[i].id == draggedCourse.designator) {
496+
year.fall.splice(i, 1);
497+
}
498+
}
499+
year.fallHrs -= draggedCourse.hours;
500+
} else if (destTerm == "Spring") {
501+
for (let i = 0; i < year.spring[i]; i++) {
502+
if (year.spring[i].id == draggedCourse.designator) {
503+
year.spring.splice(i, 1);
504+
}
505+
}
506+
year.springHrs -= draggedCourse.hours;
507+
} else {
508+
for (let i = 0; i < year.summer[i]; i++) {
509+
if (year.summer[i].id == draggedCourse.designator) {
510+
year.summer.splice(i, 1);
511+
}
512+
}
513+
year.summerHrs += draggedCourse.hours;
514+
}
463515

464516
$.get("/plan_courses", {
465517
designator: draggedCourse.designator,
@@ -565,6 +617,10 @@ class Plan {
565617
}
566618

567619
generateHTML(){
620+
currPlan.hrsCompleted = 0;
621+
currPlan.hrsCurrent = 0;
622+
currPlan.hrsFuture = 0;
623+
currPlan.hrsTotal = 0;
568624
this.years.sort((a, b) => (a.name > b.name) ? 1 : -1);
569625
let urHTML = "<header class='panelHeader'>Course Schedule</header><div class='container'>";
570626
var beforeCurrent = true;

0 commit comments

Comments
 (0)