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

Wrapped Frontend Render Safety Checks #903

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
145 changes: 77 additions & 68 deletions src/components/includes/Wrapped.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,91 +199,100 @@ const Wrapped= (props: Props): JSX.Element => {
};

useEffect(() => {
// add usestate for totalstages calculate it in useEffect

const wrappedRef = firebase.firestore().collection('wrapped');
const fetchData = async () => {
if (!props.user?.userId) {
console.error("User ID is not available.");
return;
}

setLoading(true);

try {
const doc = await wrappedRef.doc(props.user?.userId).get();
const usersRef = firebase.firestore().collection('users');
if (doc.exists) {
const studentData = doc.data() as {
numVisits: number;
personalityType: string;
timeHelpingStudents: number;
totalMinutes: number;
favTaId: string;
favClass: string;
favDay: number;
favMonth: number;
numStudentsHelped: number;
}
setWrappedData(studentData);

const data = doc.data();
let numStages = 0;
if (data !== undefined) {
if (data.timeHelpingStudents === undefined || data.timeHelpingStudents === 0){
numStages = 6;
setRole(0);
} else if (data.favTaId === "" || data.favTaId === undefined){
numStages = 4;
setRole(2);
} else{
numStages = 7;
setRole(1);
}
}
const wrappedRef = firebase.firestore().collection("wrapped");
const doc = await wrappedRef.doc(props.user.userId).get();

if (!doc.exists) {
console.log("No document found for the user in 'wrapped' collection.");
return;
}

const studentData = doc.data() as {
numVisits: number;
personalityType: string;
timeHelpingStudents: number;
rgu0114 marked this conversation as resolved.
Show resolved Hide resolved
totalMinutes: number;
favTaId: string;
favClass: string;
favDay: number;
favMonth: number;
numStudentsHelped: number;
};

// Set initial wrapped data
setWrappedData(studentData);

let numStages = 0;

// Determine the user's role and adjust stages accordingly
if ((!studentData.timeHelpingStudents || studentData.timeHelpingStudents === 0)
&& studentData.favTaId) {
numStages = 6;
setRole(0);
} else if (!studentData.favTaId) {
numStages = 4;
setRole(2);
} else {
numStages = 7;
setRole(1);
}

// Favorites slide
if (studentData.favTaId && studentData.favClass) {
const usersRef = firebase.firestore().collection("users");
const userDoc = await usersRef.doc(studentData.favTaId).get();

const userDoc = await usersRef.doc(wrappedData.favTaId).get();
let taNameExists = false;

if (userDoc.exists) {
setTaName(userDoc.data() as {
firstName: string;
lastName: string;
});
const taData = userDoc.data() as { firstName: string; lastName: string };
setTaName(taData);
taNameExists = true;
} else {
// eslint-disable-next-line no-console
console.log('No such document!');
console.log("No such TA document found.");
}

if (studentData.favClass === "" ||
studentData.favDay === -1 ||
studentData.favTaId === "" ||
!taNameExists
) {
setDisplayFavs(false);
numStages--;
}
// Check if favorite fields are valid
const validFavs = studentData.favClass !== "" && studentData.favDay !== -1 && taNameExists;

setTotalStages(numStages);
setDisplayFavs(validFavs);

if (!validFavs) {
numStages--; // Adjust total stages if favorites can't be displayed
}

// Fetch favorite class details
const coursesRef = firebase.firestore().collection("courses");

const coursesDoc = await coursesRef.doc(wrappedData.favClass).get();
if (coursesDoc.exists){
setFavClass(coursesDoc.data() as {
code: string;
})
} else{
// eslint-disable-next-line no-console
console.log('No such document!');
}
} else {
// eslint-disable-next-line no-console
console.log('No such document!');
const coursesDoc = await coursesRef.doc(studentData.favClass).get();

if (coursesDoc.exists) {
const courseData = coursesDoc.data() as { code: string };
setFavClass(courseData);
} else {
console.log("No such course document found.");
}

}

// Update total stages
setTotalStages(numStages);
} catch (error) {
// eslint-disable-next-line no-console
console.error("Error fetching data: ", error);
}

console.error("Error fetching data:", error);
}
};

fetchData();
}, [props.user, wrappedData.favClass, wrappedData.favTaId]);
}, [props.user]);


useEffect(() => {
const timer = setTimeout(() => {
Expand Down
6 changes: 3 additions & 3 deletions src/components/includes/WrappedCountdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ const WrappedCountdown: React.FC<WrappedCountdownProps> = ({ setDisplayWrapped,
<>
<div>
<div className="countdownContainer_boxes">{prependZero(timeRemaining.days)}</div>
<p className="counter_sub">{timeRemaining.days <= 1 ? "DAY" : "DAYS"}</p>
<p className="counter_sub">{timeRemaining.days === 1 ? "DAY" : "DAYS"}</p>
</div>
<div>
<div className="countdownContainer_boxes">{prependZero(timeRemaining.hours)}</div>
<p className="counter_sub">{timeRemaining.hours <= 1 ? "HOUR" : "HOURS"}</p>
<p className="counter_sub">{timeRemaining.hours === 1 ? "HOUR" : "HOURS"}</p>
</div>
<div>
<div className="countdownContainer_boxes">{prependZero(timeRemaining.minutes)}</div>
<p className="counter_sub">{timeRemaining.minutes <= 1 ? "MINUTE" : "MINUTES"}</p>
<p className="counter_sub">{timeRemaining.minutes === 1 ? "MINUTE" : "MINUTES"}</p>
</div>
<div className="textContainer">
<p className="top">Queue Me In</p>
Expand Down
Loading