Skip to content
Merged
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
42 changes: 42 additions & 0 deletions my-app/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,49 @@ googleProvider.addScope("email");

// fetches all relevant information to create the model
async function firebaseToModel(model) {
// Load metadata from localStorage
const cachedMetadata = JSON.parse(localStorage.getItem("coursesMetadata"));
const firebaseTimestamp = await fetchLastUpdatedTimestamp();
// check if up to date
if (cachedMetadata && cachedMetadata.timestamp === firebaseTimestamp) {
console.log("Using cached courses...");
let mergedCourses = [];
for (let i = 0; i < cachedMetadata.parts; i++) {
const part = JSON.parse(localStorage.getItem(`coursesPart${i}`));
if (part)
mergedCourses = mergedCourses.concat(part);
}
model.setCourses(mergedCourses);
return;
}

// Fetch if outdated or missing
console.log("Fetching courses from Firebase...");
const courses = await fetchAllCourses();
model.setCourses(courses);
saveCoursesInChunks(courses, firebaseTimestamp);
}

function saveCoursesInChunks(courses, timestamp) {
const parts = 3; // Adjust this based on course size
const chunkSize = Math.ceil(courses.length / parts);

for (let i = 0; i < parts; i++) {
const chunk = courses.slice(i * chunkSize, (i + 1) * chunkSize);
localStorage.setItem(`coursesPart${i}`, JSON.stringify(chunk));
}
localStorage.setItem("coursesMetadata", JSON.stringify({ parts, timestamp }));
}

async function updateLastUpdatedTimestamp() {
const timestampRef = ref(db, "metadata/lastUpdated");
await set(timestampRef, Date.now());
}

async function fetchLastUpdatedTimestamp() {
const timestampRef = ref(db, "metadata/lastUpdated");
const snapshot = await get(timestampRef);
return snapshot.exists() ? snapshot.val() : 0;
}

export function connectToFirebase(model) {
Expand All @@ -40,6 +81,7 @@ export async function addCourse(course){
return;
const myRef = ref(db, `courses/${course.code}`);
await set(myRef, course);
updateLastUpdatedTimestamp();
}

export async function fetchAllCourses() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default function UploadField(props) {
<p className='text-sm opacity-50'> - filter description</p>
</div>
</div>
<div class="flex flex-col sm:inline-flex sm:flex-row rounded-lg shadow-2xs w-full
<div className="flex flex-col sm:inline-flex sm:flex-row rounded-lg shadow-2xs w-full
items-center font-medium text-white bg-[#aba8e0] border border-gray-200 p-3">
<div className="flex-1 justify-between pl-2">
<span
Expand Down
14 changes: 7 additions & 7 deletions my-app/src/views/Components/SideBarComponents/ToggleField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ export default function ToggleField({field1, field2 }) {
<p className='text-sm opacity-50'> - filter description</p>
</div>
</div>
<div class="flex flex-col sm:flex-row md:flex-row rounded-lg shadow-2xs w-full items-center
<div className="flex flex-col sm:flex-row md:flex-row rounded-lg shadow-2xs w-full items-center
font-medium text-white bg-[#aba8e0] ">
<label class="flex-auto py-3 px-4 inline-flex gap-x-2 -mt-px -ms-px
<label className="flex-auto py-3 px-4 inline-flex gap-x-2 -mt-px -ms-px
first:rounded-t-md last:rounded-b-md sm:first:rounded-s-md sm:mt-0 sm:first:ms-0 s
m:first:rounded-se-none sm:last:rounded-es-none sm:last:rounded-e-md text-sm font-medium
focus:z-10 border border-gray-200 shadow-2xs cursor-pointer">
<input type="checkbox" value="" class="sr-only peer" />
<div class="relative w-11 h-6 bg-gray-200 peer-focus:outline-none
<input type="checkbox" value="" className="sr-only peer" />
<div className="relative w-11 h-6 bg-gray-200 peer-focus:outline-none
peer-focus:ring-4 peer-focus:ring-blue-300
rounded-full peer peer-checked:after:translate-x-full
rtl:peer-checked:after:-translate-x-full peer-checked:after:border-white after:content-['']
Expand All @@ -28,12 +28,12 @@ export default function ToggleField({field1, field2 }) {
peer-checked:bg-violet-500 "></div>
<span>{field1}</span>
</label>
<label class="flex-auto py-3 px-4 inline-flex gap-x-2 -mt-px -ms-px
<label className="flex-auto py-3 px-4 inline-flex gap-x-2 -mt-px -ms-px
first:rounded-t-md last:rounded-b-md sm:first:rounded-s-md sm:mt-0 sm:first:ms-0 s
m:first:rounded-se-none sm:last:rounded-es-none sm:last:rounded-e-md text-sm font-medium
focus:z-10 border border-gray-200 shadow-2xs cursor-pointer">
<input type="checkbox" value="" class="sr-only peer" />
<div class="relative w-11 h-6 bg-gray-200 peer-focus:outline-none peer-focus:ring-4
<input type="checkbox" value="" className="sr-only peer" />
<div className="relative w-11 h-6 bg-gray-200 peer-focus:outline-none peer-focus:ring-4
peer-focus:ring-blue-300 rounded-full peer
peer-checked:after:translate-x-full rtl:peer-checked:after:-translate-x-full
peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px]
Expand Down
4 changes: 2 additions & 2 deletions my-app/src/views/Components/SideBarComponents/UploadField.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ export default function UploadField(props) {
return (
<div className='pb-5 px-8 '>
<div className="flex items-center justify-center ">
<label for="PDF-Scraper-Input" className="flex flex-col items-center justify-center w-full h-50 border-2
<label htmlFor="PDF-Scraper-Input" className="flex flex-col items-center justify-center w-full h-50 border-2
border-gray-300 border-dashed rounded-lg cursor-pointer bg-[#aba8e0] hover:bg-gray-400">
<div className="flex flex-col items-center justify-center pt-5 pb-6">
<svg className="w-8 h-8 mb-4 text-white" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 20 16">
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 13h3a3 3 0 0 0 0-6h-.025A5.56 5.56 0 0 0 16 6.5 5.5 5.5 0 0 0 5.207 5.021C5.137 5.017 5.071 5 5 5a4 4 0 0 0 0 8h2.167M10 15V6m0 0L8 8m2-2 2 2" />
<path stroke="currentColor" strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M13 13h3a3 3 0 0 0 0-6h-.025A5.56 5.56 0 0 0 16 6.5 5.5 5.5 0 0 0 5.207 5.021C5.137 5.017 5.071 5 5 5a4 4 0 0 0 0 8h2.167M10 15V6m0 0L8 8m2-2 2 2" />
</svg>
<p className="mb-2 text-sm "><span className="font-semibold">Click to upload</span> or drag and drop</p>
<p className="text-xs">KTH trnascript of records in PDF format</p>
Expand Down
4 changes: 2 additions & 2 deletions my-app/src/views/SidebarView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ function SidebarView(props) {
[&::-webkit-scrollbar]:opacity-2
[&::-webkit-scrollbar-track]:bg-gray-100
[&::-webkit-scrollbar-thumb]:bg-gray-300'>
<div class="z-10 w-100% rounded-lg justify-center" >
<h6 class="m-2 text-lg font-medium text-white text-center">
<div className="z-10 w-100% rounded-lg justify-center" >
<h6 className="m-2 text-lg font-medium text-white text-center">
Filters
</h6>
<ToggleField
Expand Down