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

Add in progress computation #125

Merged
merged 19 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Logic of "compte_in_progress" is working
  • Loading branch information
liadaram1 committed Mar 26, 2022
commit 96daa96ce8ea61b671fb34a04bb2e6c2d8ab48f5
8 changes: 5 additions & 3 deletions packages/server/src/api/students.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
resources::{
catalog::DisplayCatalog,
course,
user::{Settings, User, UserDetails},
user::{User, UserDetails, UserSettings},
},
};

Expand Down Expand Up @@ -160,6 +160,8 @@ pub async fn compute_degree_status(
course_list = user_details.degree_status.set_in_progress_to_complete();
}

println!("{:#?}", course_list);

user_details
.degree_status
.compute(catalog, course::vec_to_map(vec_courses), malag_courses);
Expand All @@ -173,7 +175,7 @@ pub async fn compute_degree_status(
Ok(HttpResponse::Ok().json(user))
}

// here "modified" becomes true
// here "modified" is true
#[put("/students/details")]
pub async fn update_details(
mut user: User,
Expand All @@ -190,7 +192,7 @@ pub async fn update_details(
#[put("/students/settings")]
pub async fn update_settings(
mut user: User,
settings: Json<Settings>,
settings: Json<UserSettings>,
client: Data<mongodb::Client>,
) -> Result<HttpResponse, Error> {
let user_id = user.sub.clone();
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/core/bank_rule/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl<'a> BankRuleHandler<'a> {
for course_status in self.degree_status.course_statuses.iter() {
if course_status.r#type == Some(self.bank_name.clone()) {
*sum_credit_requirement += course_status.course.credit;
if !course_status.passed() {
if !course_status.completed() {
*completed = false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/core/bank_rule/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl<'a> BankRuleHandler<'a> {
for course_id in chain {
if let Some(course_id) = credit_info.handled_courses.get(course_id) {
if let Some(course_status) = self.degree_status.get_course_status(course_id) {
if course_status.passed() {
if course_status.completed() {
chain_done.push(course_status.course.name.clone());
} else {
completed_chain = false;
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/core/bank_rule/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl<'a> BankRuleHandler<'a> {
sum_credit: &mut f32,
) -> bool {
course_status.set_type(bank_name);
if course_status.passed() {
if course_status.completed() {
*sum_credit += course_status.course.credit;
true
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl<'a> BankRuleHandler<'a> {
.degree_status
.get_course_status(&course_id_done_by_user)
{
if course_status.passed() {
if course_status.completed() {
completed_courses.push(course_id_in_list);
}
}
Expand Down
7 changes: 4 additions & 3 deletions packages/server/src/core/degree_status/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl DegreeStatus {
course_status.course.id.clone(),
course_status.semester.clone(),
));
course_status.state = Some(CourseState::InProgress);
course_status.state = Some(CourseState::Complete);
}
}
changed_course_statuses
Expand All @@ -65,8 +65,9 @@ impl DegreeStatus {
if course_list.contains(&(
course_status.course.id.clone(),
course_status.semester.clone(),
)) {
course_status.state = Some(CourseState::Complete);
)) && course_status.state == Some(CourseState::Complete)
{
course_status.state = Some(CourseState::InProgress);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/core/degree_status/preprocessing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl DegreeStatus {
if let Some(r#type) = &course_status.r#type {
course_status.semester.is_some()
|| course_status.modified
|| course_status.passed()
|| course_status.completed()
|| !bank_names.contains(r#type)
} else {
true
Expand Down
1 change: 1 addition & 0 deletions packages/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ async fn main() -> std::io::Result<()> {
.service(api::students::add_courses)
.service(api::students::compute_degree_status)
.service(api::students::update_details)
.service(api::students::update_settings)
.service(api::bo::get_all_courses)
.service(api::bo::get_course_by_id)
.service(api::bo::create_or_update_course)
Expand Down
4 changes: 4 additions & 0 deletions packages/server/src/resources/course.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ impl CourseStatus {
}
}

pub fn completed(&self) -> bool {
return self.state == Some(CourseState::Complete);
}

pub fn extract_semester(&self) -> f32 {
match self.semester.clone() {
Some(semester) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/server/src/resources/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct UserDetails {
}

#[derive(Default, Clone, Debug, Deserialize, Serialize)]
pub struct Settings {
pub struct UserSettings {
pub modified: bool,
pub compute_in_progress: bool,
}
Expand All @@ -29,7 +29,7 @@ pub struct User {
#[serde(rename(serialize = "_id", deserialize = "_id"))]
pub sub: String,
pub details: Option<UserDetails>,
pub settings: Settings,
pub settings: UserSettings,
}

impl User {
Expand Down
4 changes: 3 additions & 1 deletion packages/sogrim-app/src/components/App/UserApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const UserAppComp: React.FC = () => {
const { userAuthToken } = useAuth();
const { data, isLoading, isError, error } = useUserState(userAuthToken);
const {
dataStore: { updateStoreUserDetails },
dataStore: { updateStoreUserDetails, updateStoreUserSettings },
uiStore: { computeUserRegistrationState, userRegistrationState },
} = useStore();

Expand All @@ -29,10 +29,12 @@ const UserAppComp: React.FC = () => {
}
if (!isLoading && data) {
updateStoreUserDetails(data.details);
updateStoreUserSettings(data.settings);
}
}, [
data,
updateStoreUserDetails,
updateStoreUserSettings,
isLoading,
userRegistrationState,
computeUserRegistrationState,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import { Button, Card, CardContent, Typography } from "@mui/material";
import { Button, Card, CardContent, Typography, Switch } from "@mui/material";
import { DegreeStatusBar } from "./DegreeStatusBar";
import { useState, useEffect } from "react";
import { useState, useEffect, useCallback } from "react";
import { useStore } from "../../../hooks/useStore";
import { observer } from "mobx-react-lite";
import { UserSettings } from "../../../types/data-types";
import useUpdateUserSettings from "../../../hooks/apiHooks/useUpdateUserSettings";
import { useAuth } from "../../../hooks/useAuth";

const DegreeMainStatusComp: React.FC = () => {
const {
dataStore: { userDetails },
dataStore: { userDetails, userSettings },
} = useStore();

const { userAuthToken } = useAuth();
const { mutate, isError, error } = useUpdateUserSettings(userAuthToken);

const [totalCredit, setTotalCredit] = useState<number>(0);
const [pointsDone, setPointsDone] = useState<number>(0);
const [catalogName, setCatalogName] = useState<string>("");

const [showMainStatus, setShowMainStatus] = useState<boolean>(false);
const [computeInProgress, setComputeInProgress] = useState<boolean>(
userSettings.compute_in_progress
);

// TODO: loading? or loading to all the banner!
useEffect(() => {
Expand All @@ -28,17 +38,39 @@ const DegreeMainStatusComp: React.FC = () => {
setCatalogName(catalogName);
setShowMainStatus(totalCredit * pointsDone > 0 && catalogName !== "");
}
}, [pointsDone, userDetails, userDetails?.degree_status?.course_statuses]);
if (userSettings) {
setComputeInProgress(userSettings.compute_in_progress);
}
}, [
pointsDone,
userDetails,
userDetails?.degree_status?.course_statuses,
userSettings,
]);

const progress =
pointsDone / totalCredit >= 1 ? 100 : (pointsDone / totalCredit) * 100;

const handleChange = useCallback(
(_: React.SyntheticEvent, computeInProgress: boolean) => {
setComputeInProgress(computeInProgress);
let newUserSettings = {
...userSettings,
compute_in_progress: computeInProgress,
} as UserSettings;
mutate(newUserSettings);
},
[mutate]
);

return showMainStatus ? (
<Card sx={{ minWidth: 275, maxHeight: 150 }}>
<CardContent>
<Typography sx={{ fontSize: 18 }} color="text.secondary" gutterBottom>
סטאטוס תואר
</Typography>
<Switch onChange={handleChange} checked={computeInProgress} />
{/* TODO: work on design */}
<DegreeStatusBar progress={progress} />
<Typography sx={{ fontSize: 22 }} color="text.primary">
{`השלמת ${pointsDone} מתוך ${totalCredit} נקודות`}
Expand Down
11 changes: 11 additions & 0 deletions packages/sogrim-app/src/hooks/apiHooks/useUpdateUserSettings.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useMutation } from "react-query";
import { putUserSettings } from "../../services/api";
import { UserSettings } from "../../types/data-types";

export default function useUpdateUserSettings(authToken: any) {
return useMutation(
// "userState", // The caching key TODO: check if need this
(updatedUserSettings: UserSettings) =>
putUserSettings(authToken, updatedUserSettings)
);
}
28 changes: 27 additions & 1 deletion packages/sogrim-app/src/services/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import axios from "axios";
import { UserDetails, UserState, Catalog, Course } from "../types/data-types";
import {
UserDetails,
UserState,
Catalog,
Course,
UserSettings,
} from "../types/data-types";
import { API_URL } from "./api-url";

export const getCatalogs = async (authToken: any): Promise<Catalog[]> => {
Expand Down Expand Up @@ -135,3 +141,23 @@ export const getComputeEndGame = async (authToken: any): Promise<UserState> => {
}
return res;
};

export const putUserSettings = async (
authToken: any,
updatedUserSettings: UserSettings
): Promise<UserSettings> => {
const fallback: UserSettings = {} as UserSettings;
let res: UserSettings;
try {
res =
(await axios.put(`${API_URL}/students/settings`, updatedUserSettings, {
headers: {
authorization: `${authToken}`,
},
})) || fallback;
} catch (e) {
res = fallback;
throw e;
}
return res;
};
12 changes: 11 additions & 1 deletion packages/sogrim-app/src/stores/DataStore.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { makeAutoObservable } from "mobx";
import { createData } from "../components/Pages/SemestersPage/SemesterTable/SemesterTableUtils";
import { RowData } from "../components/Pages/SemestersPage/SemesterTabsConsts";
import { CourseStatus, UserDetails, CourseState } from "../types/data-types";
import {
CourseStatus,
UserDetails,
CourseState,
UserSettings,
} from "../types/data-types";
import { RootStore } from "./RootStore";

export class DataStore {
public userDetails: UserDetails = {} as UserDetails;
public userSettings: UserSettings = {} as UserSettings;
public userBankNames: string[] = [];

constructor(public readonly rootStore: RootStore) {
Expand All @@ -16,6 +22,10 @@ export class DataStore {
this.userDetails = newUserDitails;
};

updateStoreUserSettings = (newUserSettings: UserSettings) => {
this.userSettings = newUserSettings;
};

get modifiedStatus() {
return (
this.userDetails?.degree_status?.course_statuses?.length > 0 &&
Expand Down
4 changes: 2 additions & 2 deletions packages/sogrim-app/src/types/data-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export type DegreeStatus = {
total_credit: number;
};

export type Settings = {
export type UserSettings = {
modified: boolean;
compute_in_progress: boolean;
};
Expand All @@ -67,5 +67,5 @@ export type UserDetails = {
export type UserState = {
_id: string;
details: UserDetails;
settings: Settings;
settings: UserSettings;
};