Skip to content

Commit 7e5a5c5

Browse files
daDevBoatkexanaDinoxhLSKprSailet03
authored
Some URL stuff (#121)
* sidebar component add * tailwind workin, beggining page building * structure * fixed app * Main design layout * initial sidebar * courseView init * the course page initial tests * Make it run on my comuter * Tree without database integration * fixes * npm update * add to page * Biiiiig Refactoring * Broken version of prereq tree * Fixed not rendering anything. Still broken * Working v1 * handling #prereqs * Test * removed commented out code * More info expands now * More info expands now * More info expands now * Start coding on line 135 in PrereqTreePresenter Mr. PO * Prereqs colored and expands more info * added css styling * Fixed copy bug * Crash fix in PrerequisitePresenter.jsx * merge fix * Prereq tree compression, error handling, debuging, database update * Added null support (hopefully) * Bug fixes * Desplays periods * Bug finally fixed (hopefully) * Check if course exists before doing pop up * Set up for url handling * URL in progress --------- Co-authored-by: kexana <deotsts@gmail.com> Co-authored-by: Sami Al Saati <sami.alsaati@hotmail.com> Co-authored-by: Kacper Lisik <lisik@kth.se> Co-authored-by: Elias Tosteberg <elias.tosteberg@gmail.com> Co-authored-by: Sailet03 <52610280+Sailet03@users.noreply.github.com>
1 parent 0fc3662 commit 7e5a5c5

4 files changed

Lines changed: 96 additions & 15 deletions

File tree

my-app/src/index.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { configure, makeAutoObservable } from "mobx";
2-
import { createBrowserRouter, RouterProvider } from "react-router-dom";
2+
import { createHashRouter, RouterProvider } from "react-router-dom";
33
import { createRoot } from "react-dom/client";
44
import { connectToFirebase } from "../firebase";
55
import { model } from "./model";
@@ -17,7 +17,7 @@ const reactiveModel = makeAutoObservable(model);
1717
connectToFirebase(reactiveModel);
1818

1919
export function makeRouter(reactiveModel) {
20-
return createBrowserRouter([
20+
return createHashRouter([
2121
{
2222
path: "/",
2323
element: <App model={reactiveModel} />,

my-app/src/model.js

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ export const model = {
4949
isPopupOpen: false,
5050
selectedCourse: null,
5151

52+
_coursesListeners: [], // internal list of listeners
53+
54+
onCoursesSet(callback) {
55+
this._coursesListeners.push(callback);
56+
},
57+
5258
setUser(user) {
5359
if (!this.user)
5460
this.user = user;
@@ -68,6 +74,7 @@ export const model = {
6874

6975
setCourses(courses){
7076
this.courses = courses;
77+
this._coursesListeners.forEach(cb => cb(courses));
7178
},
7279

7380
async addCourse(course) {
@@ -252,14 +259,19 @@ export const model = {
252259
},
253260

254261
setPopupOpen(isOpen) {
255-
console.log("OPENING/CLOSING A POPUP");
256-
// if (isOpen && this.selectedCourse) {
257-
// // this.addHistoryItem(this.selectedCourse.code);
258-
// console.log("user path history:")
259-
// for (let index = 0; index < this.searchHistory.length; index++) {
260-
// console.log(this.searchHistory[index]);
261-
// }
262-
// }
262+
if (isOpen) {
263+
window.history.pushState({}, '', '/' + this.selectedCourse.code);
264+
}
265+
console.log("POPOPOOPOPOOOOOOP")
266+
if (!isOpen) {
267+
let current_url = window.location.href;
268+
console.log(current_url);
269+
let end_idx = indexOfNth(current_url, '/', 3);
270+
if (end_idx >= 0 && end_idx < current_url.length - 1 && current_url.indexOf("#") == -1) {
271+
window.history.back();
272+
}
273+
}
274+
263275
this.isPopupOpen = isOpen;
264276
},
265277

@@ -276,5 +288,42 @@ export const model = {
276288

277289
toggleSidebarIsOpen() {
278290
this.sidebarIsOpen = !this.sidebarIsOpen;
291+
},
292+
293+
handleUrlChange() {
294+
let current_url = window.location.href;
295+
let start_idx = indexOfNth(current_url, '/', 3) + 1;
296+
console.log(current_url)
297+
298+
if (start_idx > 0 && start_idx < current_url.length && current_url.indexOf("#") == -1) {
299+
let course_code = current_url.slice(start_idx);
300+
let course = this.getCourse(course_code);
301+
console.log(course_code)
302+
if (course) {
303+
console.log("ACTIVE")
304+
this.setSelectedCourse(course);
305+
this.setPopupOpen(true);
306+
}
307+
console.log("Forward");
308+
} else {
309+
console.log("Back")
310+
this.setPopupOpen(false);
311+
}
312+
//console.log("back");
279313
}
314+
280315
};
316+
317+
318+
function indexOfNth(string, char, n) {
319+
let count = 0;
320+
for (let i = 0; i < string.length; i++) {
321+
if (string[i] == char) {
322+
count++;
323+
}
324+
if (count == n) {
325+
return i;
326+
}
327+
}
328+
return -1;
329+
}

my-app/src/presenters/ListViewPresenter.jsx

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,39 @@ const ListViewPresenter = observer(({ model }) => {
103103
setSortedCourses(sorted);
104104
}, [model.currentSearch, sortBy, sortDirection]);
105105

106+
107+
window.addEventListener('popstate', () => {
108+
model.handleUrlChange();
109+
});
110+
111+
function indexOfNth(string, char, n) {
112+
let count = 0;
113+
for (let i = 0; i < string.length; i++) {
114+
if (string[i] == char) {
115+
count++;
116+
}
117+
if (count == n) {
118+
return i;
119+
}
120+
}
121+
return -1;
122+
}
123+
124+
model.onCoursesSet((courses) => {
125+
let current_url = window.location.href;
126+
if (indexOfNth(current_url, '/', 3) != current_url.length - 1) {
127+
window.history.replaceState({}, '', '/');
128+
/*
129+
setTimeout(() => {
130+
const newPath = '/' + current_url.slice(indexOfNth(current_url, '/', 3) + 1);
131+
window.history.pushState({}, '', newPath);
132+
}, 50);
133+
}
134+
*/
135+
model.handleUrlChange();
136+
}
137+
})
138+
106139
const preP = <PrerequisitePresenter
107140
model={model}
108141
isPopupOpen={model.isPopupOpen}
@@ -130,7 +163,7 @@ const ListViewPresenter = observer(({ model }) => {
130163
handleFavouriteClick={handleFavouriteClick}
131164

132165
isPopupOpen={model.isPopupOpen}
133-
handlePopupOpen={(isOpen) => model.setPopupOpen(isOpen)}
166+
setPopupOpen={(isOpen) => model.setPopupOpen(isOpen)}
134167
setSelectedCourse={(course) => model.setSelectedCourse(course)}
135168
popup={popup}
136169

@@ -148,4 +181,4 @@ const ListViewPresenter = observer(({ model }) => {
148181
/>;
149182
});
150183

151-
export { ListViewPresenter };
184+
export { ListViewPresenter };

my-app/src/views/ListView.jsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,11 @@ function ListView(props) {
158158
initialScrollY={0}
159159
>
160160
{displayedCourses.map(course => (
161-
<Link to={'/?' + course.code}>
162161
<div
163162
onClick={() => {
164163
props.setSelectedCourse(course);
165-
props.handlePopupOpen(true);
164+
props.setPopupOpen(true);
165+
//window.history.pushState({}, '', '/' + course.code);
166166
}}
167167
key={course.code}
168168
className="p-5 mb-3 hover:bg-blue-100 flex items-center border border-b-black border-solid w-full rounded-lg cursor-pointer"
@@ -245,7 +245,6 @@ function ListView(props) {
245245
</div>
246246
</div>
247247
</div>
248-
</Link>
249248
))}
250249
</InfiniteScroll>
251250
</div>

0 commit comments

Comments
 (0)