Skip to content

Commit

Permalink
fix double count add and double count delete
Browse files Browse the repository at this point in the history
  • Loading branch information
August Fu committed Mar 25, 2024
1 parent 56d06aa commit ad6f513
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 32 deletions.
2 changes: 1 addition & 1 deletion backend/degree/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def create(self, request, *args, **kwargs):
return self.partial_update(request, *args, **kwargs)
except Http404:
return super().create(request, *args, **kwargs)

@api_view(["GET"])
def courses_for_rule(request, rule_id: int):
"""
Expand Down
4 changes: 2 additions & 2 deletions frontend/degree-plan/components/FourYearPlan/CourseInPlan.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useDrag } from "react-dnd";
import { ItemTypes } from "../dnd/constants";
import { Course, DnDCourse } from "@/types";
import { Course, DnDCourse, Fulfillment } from "@/types";
import 'react-loading-skeleton/dist/skeleton.css'
import CourseComponent from "../Course/Course";

interface CoursePlannedProps {
course: DnDCourse;
course: Fulfillment;
removeCourse: (course: Course["id"]) => void;
semester: Course["semester"];
isDisabled: boolean;
Expand Down
8 changes: 1 addition & 7 deletions frontend/degree-plan/components/FourYearPlan/PlanPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,7 @@ const PlanPanel = ({
setModalObject(item)
},
create: () => {
/** When a semester is created,
* if there is no localStorage.getItem('PDP-start-grad-years'),
* the onboarding page will pop up which then sets PDP-start-grad-years
* in localStorage */
if (typeof window !== "undefined" && !!localStorage.getItem('PDP-start-grad-years'))
setModalKey("plan-create")
else setShowOnboardingModal(true);
setShowOnboardingModal(true);
}
}}
isLoading={isLoading}
Expand Down
19 changes: 14 additions & 5 deletions frontend/degree-plan/components/Requirements/CourseInReq.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { ReviewPanelTrigger } from "../Infobox/ReviewPanel";
import { Draggable } from "../common/DnD";
import CourseComponent, { PlannedCourseContainer } from "../Course/Course";
import { CourseXButton } from "../Course/Course";
import { useSWRCrud } from "@/hooks/swrcrud";
import { deleteFetcher, useSWRCrud } from "@/hooks/swrcrud";
import { mutate } from "swr";

interface CourseInReqProps {
course: DnDCourse;
Expand All @@ -22,14 +23,22 @@ interface CourseInReqProps {
const CourseInReq = (props : CourseInReqProps) => {
const { course, activeDegreePlanId, rule_id } = props;

const { remove } = useSWRCrud<Fulfillment>(
const { remove: removeFulfillment, createOrUpdate: updateFulfillment } = useSWRCrud<Fulfillment>(
`/api/degree/degreeplans/${activeDegreePlanId}/fulfillments`,
{ idKey: "full_code",
createDefaultOptimisticData: { semester: null, rules: [] }
// createDefaultOptimisticData: { semester: null, rules: [] }
});
const { createOrUpdate } = useSWRCrud<DockedCourse>(`/api/degree/docked`, { idKey: 'full_code' });
const handleRemoveCourse = (full_code: string) => {
remove(full_code);

const handleRemoveCourse = async (full_code: string) => {
const updated_rules = course.rules?.filter(rule => rule != rule_id);
/** If the current rule about to be removed is the only rule
* the course satisfied, then we delete the fulfillment */
if (updated_rules && updated_rules.length == 0) {
removeFulfillment(full_code);
} else {
updateFulfillment({rules: updated_rules}, full_code);
}
createOrUpdate({"full_code": full_code}, full_code);
}

Expand Down
8 changes: 4 additions & 4 deletions frontend/degree-plan/components/Requirements/QObject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ const QObject = ({ q, fulfillments, rule, satisfied, activeDegreePlanId }: QObje
// we've already used this course, so delete it
if (isChosen) fulfillmentsMap.delete(course.full_code);
// return <div>what </div>
return <CourseInReq course={course} isDisabled={satisfied && !isChosen} isUsed={isChosen} rule_id={rule.id} activeDegreePlanId={activeDegreePlanId}/>;
return <CourseInReq course={{...course, rules:fulfillment?.rules}} isDisabled={satisfied && !isChosen} isUsed={isChosen} rule_id={rule.id} activeDegreePlanId={activeDegreePlanId}/>;
});
const displayCoursesWithoutSemesters = courses.map(course => {
assert(typeof course.semester === "undefined")
Expand All @@ -284,7 +284,7 @@ const QObject = ({ q, fulfillments, rule, satisfied, activeDegreePlanId }: QObje

// we've already used this course, so delete it
if (isChosen) fulfillmentsMap.delete(course.full_code);
return <CourseInReq course={course} isDisabled={satisfied && !isChosen} isUsed={isChosen} rule_id={rule.id} activeDegreePlanId={activeDegreePlanId}/>;
return <CourseInReq course={{...course, rules:fulfillment?.rules}} isDisabled={satisfied && !isChosen} isUsed={isChosen} rule_id={rule.id} activeDegreePlanId={activeDegreePlanId}/>;
});

// transformations applied to parse tree should guarantee that searchConditions is a singleton
Expand All @@ -304,8 +304,8 @@ const QObject = ({ q, fulfillments, rule, satisfied, activeDegreePlanId }: QObje
case "SEARCH":
return <SearchCondition q={q.q} ruleIsSatisfied={satisfied} fulfillments={fulfillments} ruleId={rule.id} ruleQuery={rule.q} activeDegreeplanId={activeDegreePlanId}/>;
case "COURSE":
const isChosen = fulfillments.find(fulfillment => fulfillment.full_code == q.full_code && (!q.semester || q.semester === fulfillment.semester))
return <CourseInReq course={q} isDisabled={satisfied && !isChosen} isUsed={!!isChosen} rule_id={rule.id} activeDegreePlanId={activeDegreePlanId}/>;
const [fulfillment] = fulfillments.filter(fulfillment => fulfillment.full_code == q.full_code && (!q.semester || q.semester === fulfillment.semester))
return <CourseInReq course={{...q, rules: fulfillment ? fulfillment.rules : []}} isDisabled={satisfied && !fulfillment} isUsed={!!fulfillment} rule_id={rule.id} activeDegreePlanId={activeDegreePlanId}/>;
// onClick={() => { console.log("fired"); createOrUpdate({ rules: [rule.id] }, q.full_code)}}
}
}
Expand Down
3 changes: 1 addition & 2 deletions frontend/degree-plan/components/Requirements/Rule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ const RuleComponent = (ruleTree : RuleTree) => {
const [{ isOver, canDrop }, drop] = useDrop<DnDCourse, never, { isOver: boolean, canDrop: boolean }>({
accept: [ItemTypes.COURSE_IN_PLAN, ItemTypes.COURSE_IN_DOCK],
drop: (course: DnDCourse) => {
createOrUpdate({ rules: [rule.id] }, course.full_code)

createOrUpdate({ rules: course.rules !== undefined ? [...course.rules, rule.id] : [rule.id] }, course.full_code);
}, // TODO: this doesn't handle fulfillments that already have a rule
canDrop: () => { return !satisfied && !!rule.q },
collect: monitor => ({
Expand Down
10 changes: 0 additions & 10 deletions frontend/degree-plan/pages/OnboardingPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -272,17 +272,7 @@ const OnboardingPage = ({
`/api/degree/degreeplans/${res.id}/degrees`,
{ degree_ids: majors.map((m) => m.value.id) }
); // add degree
// mutate(`api/degree/degreeplans/${res.id}`, updated, { populateCache: true, revalidate: false }) // use updated degree plan returned
// mutate(key => key && key.startsWith(`/api/degree/degreeplans/${res.id}/fulfillments`)) // refetch the fulfillments
setActiveDegreeplan(res);
localStorage.setItem(
"PDP-start-grad-years",
JSON.stringify({
startingYear: startingYear?.value,
graduationYear: graduationYear?.value,
})
);
// TODO: update the backend on user's start/grad years
setShowOnboardingModal(false);
}
});
Expand Down
2 changes: 1 addition & 1 deletion frontend/degree-plan/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export interface Course {
// The interface we use with React DND
export interface DnDCourse {
full_code: string;
rule_id?: number;
rules?: number[];
}

export interface Fulfillment extends DBObject, DnDCourse {
Expand Down

0 comments on commit ad6f513

Please sign in to comment.