Skip to content

Commit bbdf6a5

Browse files
committed
TS-ify guide-index.ts
1 parent 7fca5d8 commit bbdf6a5

1 file changed

Lines changed: 38 additions & 51 deletions

File tree

pedagogy/static/bundled/pedagogy/guide-index.js renamed to pedagogy/static/bundled/pedagogy/guide-index.ts

Lines changed: 38 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import {
2-
getCurrentUrlParams,
3-
History,
4-
updateQueryString,
5-
} from "#core:utils/history.ts";
6-
import { ueFetchUeList } from "#openapi";
1+
import { getCurrentUrlParams, updateQueryString } from "#core:utils/history";
2+
import { type SimpleUeSchema, ueFetchUeList } from "#openapi";
73

84
const pageDefault = 1;
95
const pageSizeDefault = 100;
@@ -12,102 +8,93 @@ document.addEventListener("alpine:init", () => {
128
Alpine.data("ue_search", () => ({
139
ues: {
1410
count: 0,
15-
next: null,
16-
previous: null,
17-
results: [],
11+
next: null as string | null,
12+
previous: null as string | null,
13+
results: [] as SimpleUeSchema[],
1814
},
1915
loading: false,
2016
page: pageDefault,
2117
// biome-ignore lint/style/useNamingConvention: api is in snake_case
2218
page_size: pageSizeDefault,
2319
search: "",
24-
department: [],
20+
department: [] as string[],
2521
// biome-ignore lint/style/useNamingConvention: api is in snake_case
26-
credit_type: [],
27-
semester: [],
22+
credit_type: [] as string[],
23+
semester: [] as string[],
2824
// biome-ignore lint/style/useNamingConvention: api is in snake_case
29-
to_change: [],
30-
pushstate: History.Push,
25+
to_change: [] as { param: string; value: string }[],
3126

32-
update: undefined,
27+
// dummy implementation to make TS happy.
28+
// The real function is initialized in init
29+
update: () => {
30+
console.warn("Update not yet initialized");
31+
},
3332

3433
initializeArgs() {
3534
const url = getCurrentUrlParams();
36-
this.pushstate = History.Replace;
37-
38-
this.page = Number.parseInt(url.get("page"), 10) || pageDefault;
39-
this.page_size = Number.parseInt(url.get("page_size"), 10) || pageSizeDefault;
35+
this.page = Number.parseInt(url.get("page") || pageDefault.toString(), 10);
36+
this.page_size = Number.parseInt(
37+
url.get("page_size") || pageSizeDefault.toString(),
38+
10,
39+
);
4040
this.search = url.get("search") || "";
4141
this.department = url.getAll("department");
4242
this.credit_type = url.getAll("credit_type");
4343
/* The semester is easier to use on the backend as an enum (spring/autumn/both/none)
4444
and easier to use on the frontend as an array ([spring, autumn]).
4545
Thus there is some conversion involved when both communicate together */
46-
this.semester = url.has("semester") ? url.get("semester").split("_AND_") : [];
46+
this.semester = url.get("semester")?.split("_AND_") || [];
4747

4848
this.update();
4949
},
5050

5151
async init() {
5252
this.update = Alpine.debounce(async () => {
5353
/* Create the whole url before changing everything all at once */
54-
const first = this.to_change.shift();
55-
let url = updateQueryString(first.param, first.value, History.None);
56-
for (const value of this.to_change) {
57-
url = updateQueryString(value.param, value.value, History.None, url);
54+
for (const val of this.to_change) {
55+
updateQueryString(val.param, val.value);
5856
}
59-
updateQueryString(first.param, first.value, this.pushstate, url);
6057
await this.fetchData(); /* reload data on form change */
6158
this.to_change = [];
62-
this.pushstate = History.Push;
6359
}, 50);
6460

6561
const searchParams = ["search", "department", "credit_type", "semester"];
6662
const paginationParams = ["page", "page_size"];
6763

6864
for (const param of searchParams) {
6965
this.$watch(param, () => {
70-
if (this.pushstate !== History.Push) {
71-
/* This means that we are doing a mass param edit */
72-
return;
73-
}
7466
/* Reset pagination on search */
7567
this.page = pageDefault;
7668
this.page_size = pageSizeDefault;
7769
});
7870
}
7971
for (const param of searchParams.concat(paginationParams)) {
80-
this.$watch(param, (value) => {
72+
this.$watch(param, (value: string) => {
8173
this.to_change.push({ param: param, value: value });
8274
this.update();
8375
});
8476
}
85-
window.addEventListener("popstate", () => {
86-
this.initializeArgs();
87-
});
8877
this.initializeArgs();
8978
},
9079

9180
async fetchData() {
9281
this.loading = true;
93-
const args = {
94-
// biome-ignore lint/style/useNamingConvention: api is in snake_case
95-
page_size: this.page_size,
96-
};
97-
for (const [param, value] of new URL(
98-
window.location.href,
99-
).searchParams.entries()) {
100-
// Deal with array type params
101-
if (["credit_type", "department", "semester"].includes(param)) {
102-
if (args[param] === undefined) {
103-
args[param] = [];
104-
}
105-
args[param].push(value);
106-
} else {
107-
args[param] = value;
108-
}
82+
83+
const res = await ueFetchUeList({
84+
query: {
85+
// biome-ignore lint/style/useNamingConvention: api is in snake_case
86+
page_size: this.page_size,
87+
// biome-ignore lint/style/useNamingConvention: api is in snake_case
88+
credit_type: this.credit_type.length > 0 ? this.credit_type : undefined,
89+
semester: this.semester.length > 0 ? this.semester : undefined,
90+
91+
department: this.department.length > 0 ? this.department : undefined,
92+
search: this.search || undefined,
93+
},
94+
});
95+
if (res.data !== undefined) {
96+
this.ues = res.data;
10997
}
110-
this.ues = (await ueFetchUeList({ query: args })).data;
11198
this.loading = false;
11299
},
113100

0 commit comments

Comments
 (0)