Skip to content
46 changes: 23 additions & 23 deletions my-app/firebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,18 @@ export async function connectToFirebase(model) {
// setting missing
// also save filters to local storage
//
const options = JSON.parse(localStorage.getItem("filterOptions"));
if (options) {
model.setFilterOptions(options);
}
if (!model?.currentSearchText) {
const search = localStorage.getItem("search");
if (search) {
model.setCurrentSearchText(search);
}
// we dont restore for the expo...
//
// const options = JSON.parse(localStorage.getItem("filterOptions"));
// if (options) {
// model.setFilterOptions(options);
// }
const search = localStorage.getItem("search");
if (search) {
model.setCurrentSearchText(search);
}

// automaticaly save filter and search to local storage whenever they change
reaction(
() => ({
filterOptions: JSON.stringify(model.filterOptions),
search: model.currentSearchText,
}),
({ filterOptions, search }) => {
localStorage.setItem("filterOptions", filterOptions);
localStorage.setItem("search", search);
}
);
/**
* Hook to start synchronization when user is authenticated.
*/
Expand All @@ -91,6 +81,16 @@ export async function connectToFirebase(model) {
model.setReady();
}
});
reaction(
() => ({
filterOptions: JSON.stringify(model.filterOptions),
search: model.currentSearchText,
}),
({ filterOptions, search }) => {
localStorage.setItem("filterOptions", filterOptions);
localStorage.setItem("search", search);
}
);
}

// fetches all relevant information to create the model
Expand All @@ -104,8 +104,8 @@ async function firebaseToModel(model) {
await runTransaction(userRef, (currentData) => {
if (currentData) {
if (data?.favourites) model.setFavourite(data.favourites);
if (data?.currentSearchText)
model.setCurrentSearchText(data.currentSearchText);
// if (data?.currentSearchText)
// model.setCurrentSearchText(data.currentSearchText);
// Add other fields as needed
}
return currentData; // Return the current data to avoid overwriting
Expand All @@ -123,7 +123,7 @@ export function syncModelToFirebase(model) {
() => ({
userId: model?.user.uid,
favourites: toJS(model.favourites),
currentSearchText: toJS(model.currentSearchText),
//currentSearchText: toJS(model.currentSearchText),
}),
async ({ userId, favourites, currentSearchText }) => {
if (!userId) return;
Expand All @@ -134,7 +134,7 @@ export function syncModelToFirebase(model) {
return {
...currentData,
favourites,
currentSearchText,
//currentSearchText,
};
}).catch((error) => {
console.error("Error syncing model to Firebase:", error);
Expand Down
2 changes: 0 additions & 2 deletions my-app/src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import App from "./pages/App.jsx";
import "./styles.css";

import SharedView from "./pages/SharedView.jsx";
import { JsonToDatabase } from "./presenters/Tests/JsonToDatabase";
import { AllCoursesPresenter } from "./presenters/Tests/AllCoursesPresenter.jsx";


configure({ enforceActions: "never", reactionScheduler: (f) => setTimeout(f, 0),});
Expand Down
12 changes: 0 additions & 12 deletions my-app/src/pages/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,6 @@ import { slide as Menu } from "react-burger-menu";
function App({ model }) {
const [sidebarIsOpen, setSidebarIsOpen] = useState(model.sidebarIsOpen);

useEffect(() => {
const clearStorageOnUnload = () => {
localStorage.removeItem("filterOptions");
};

// window.addEventListener("unload", clearStorageOnUnload);

return () => {
window.removeEventListener("unload", clearStorageOnUnload);
};
}, []);

useState(() => {
if (window.innerWidth < 700) {
setSidebarIsOpen(false);
Expand Down