mirror of
https://github.com/ae-utbm/sith.git
synced 2026-09-01 18:19:21 +00:00
TS-ify guide-index.ts
This commit is contained in:
+36
-49
@@ -1,9 +1,5 @@
|
|||||||
import {
|
import { getCurrentUrlParams, updateQueryString } from "#core:utils/history";
|
||||||
getCurrentUrlParams,
|
import { type SimpleUeSchema, ueFetchUeList } from "#openapi";
|
||||||
History,
|
|
||||||
updateQueryString,
|
|
||||||
} from "#core:utils/history.ts";
|
|
||||||
import { ueFetchUeList } from "#openapi";
|
|
||||||
|
|
||||||
const pageDefault = 1;
|
const pageDefault = 1;
|
||||||
const pageSizeDefault = 100;
|
const pageSizeDefault = 100;
|
||||||
@@ -12,38 +8,42 @@ document.addEventListener("alpine:init", () => {
|
|||||||
Alpine.data("ue_search", () => ({
|
Alpine.data("ue_search", () => ({
|
||||||
ues: {
|
ues: {
|
||||||
count: 0,
|
count: 0,
|
||||||
next: null,
|
next: null as string | null,
|
||||||
previous: null,
|
previous: null as string | null,
|
||||||
results: [],
|
results: [] as SimpleUeSchema[],
|
||||||
},
|
},
|
||||||
loading: false,
|
loading: false,
|
||||||
page: pageDefault,
|
page: pageDefault,
|
||||||
// biome-ignore lint/style/useNamingConvention: api is in snake_case
|
// biome-ignore lint/style/useNamingConvention: api is in snake_case
|
||||||
page_size: pageSizeDefault,
|
page_size: pageSizeDefault,
|
||||||
search: "",
|
search: "",
|
||||||
department: [],
|
department: [] as string[],
|
||||||
// biome-ignore lint/style/useNamingConvention: api is in snake_case
|
// biome-ignore lint/style/useNamingConvention: api is in snake_case
|
||||||
credit_type: [],
|
credit_type: [] as string[],
|
||||||
semester: [],
|
semester: [] as string[],
|
||||||
// biome-ignore lint/style/useNamingConvention: api is in snake_case
|
// biome-ignore lint/style/useNamingConvention: api is in snake_case
|
||||||
to_change: [],
|
to_change: [] as { param: string; value: string }[],
|
||||||
pushstate: History.Push,
|
|
||||||
|
|
||||||
update: undefined,
|
// dummy implementation to make TS happy.
|
||||||
|
// The real function is initialized in init
|
||||||
|
update: () => {
|
||||||
|
console.warn("Update not yet initialized");
|
||||||
|
},
|
||||||
|
|
||||||
initializeArgs() {
|
initializeArgs() {
|
||||||
const url = getCurrentUrlParams();
|
const url = getCurrentUrlParams();
|
||||||
this.pushstate = History.Replace;
|
this.page = Number.parseInt(url.get("page") || pageDefault.toString(), 10);
|
||||||
|
this.page_size = Number.parseInt(
|
||||||
this.page = Number.parseInt(url.get("page"), 10) || pageDefault;
|
url.get("page_size") || pageSizeDefault.toString(),
|
||||||
this.page_size = Number.parseInt(url.get("page_size"), 10) || pageSizeDefault;
|
10,
|
||||||
|
);
|
||||||
this.search = url.get("search") || "";
|
this.search = url.get("search") || "";
|
||||||
this.department = url.getAll("department");
|
this.department = url.getAll("department");
|
||||||
this.credit_type = url.getAll("credit_type");
|
this.credit_type = url.getAll("credit_type");
|
||||||
/* The semester is easier to use on the backend as an enum (spring/autumn/both/none)
|
/* The semester is easier to use on the backend as an enum (spring/autumn/both/none)
|
||||||
and easier to use on the frontend as an array ([spring, autumn]).
|
and easier to use on the frontend as an array ([spring, autumn]).
|
||||||
Thus there is some conversion involved when both communicate together */
|
Thus there is some conversion involved when both communicate together */
|
||||||
this.semester = url.has("semester") ? url.get("semester").split("_AND_") : [];
|
this.semester = url.get("semester")?.split("_AND_") || [];
|
||||||
|
|
||||||
this.update();
|
this.update();
|
||||||
},
|
},
|
||||||
@@ -51,15 +51,11 @@ document.addEventListener("alpine:init", () => {
|
|||||||
async init() {
|
async init() {
|
||||||
this.update = Alpine.debounce(async () => {
|
this.update = Alpine.debounce(async () => {
|
||||||
/* Create the whole url before changing everything all at once */
|
/* Create the whole url before changing everything all at once */
|
||||||
const first = this.to_change.shift();
|
for (const val of this.to_change) {
|
||||||
let url = updateQueryString(first.param, first.value, History.None);
|
updateQueryString(val.param, val.value);
|
||||||
for (const value of this.to_change) {
|
|
||||||
url = updateQueryString(value.param, value.value, History.None, url);
|
|
||||||
}
|
}
|
||||||
updateQueryString(first.param, first.value, this.pushstate, url);
|
|
||||||
await this.fetchData(); /* reload data on form change */
|
await this.fetchData(); /* reload data on form change */
|
||||||
this.to_change = [];
|
this.to_change = [];
|
||||||
this.pushstate = History.Push;
|
|
||||||
}, 50);
|
}, 50);
|
||||||
|
|
||||||
const searchParams = ["search", "department", "credit_type", "semester"];
|
const searchParams = ["search", "department", "credit_type", "semester"];
|
||||||
@@ -67,47 +63,38 @@ document.addEventListener("alpine:init", () => {
|
|||||||
|
|
||||||
for (const param of searchParams) {
|
for (const param of searchParams) {
|
||||||
this.$watch(param, () => {
|
this.$watch(param, () => {
|
||||||
if (this.pushstate !== History.Push) {
|
|
||||||
/* This means that we are doing a mass param edit */
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
/* Reset pagination on search */
|
/* Reset pagination on search */
|
||||||
this.page = pageDefault;
|
this.page = pageDefault;
|
||||||
this.page_size = pageSizeDefault;
|
this.page_size = pageSizeDefault;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
for (const param of searchParams.concat(paginationParams)) {
|
for (const param of searchParams.concat(paginationParams)) {
|
||||||
this.$watch(param, (value) => {
|
this.$watch(param, (value: string) => {
|
||||||
this.to_change.push({ param: param, value: value });
|
this.to_change.push({ param: param, value: value });
|
||||||
this.update();
|
this.update();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
window.addEventListener("popstate", () => {
|
|
||||||
this.initializeArgs();
|
|
||||||
});
|
|
||||||
this.initializeArgs();
|
this.initializeArgs();
|
||||||
},
|
},
|
||||||
|
|
||||||
async fetchData() {
|
async fetchData() {
|
||||||
this.loading = true;
|
this.loading = true;
|
||||||
const args = {
|
|
||||||
|
const res = await ueFetchUeList({
|
||||||
|
query: {
|
||||||
// biome-ignore lint/style/useNamingConvention: api is in snake_case
|
// biome-ignore lint/style/useNamingConvention: api is in snake_case
|
||||||
page_size: this.page_size,
|
page_size: this.page_size,
|
||||||
};
|
// biome-ignore lint/style/useNamingConvention: api is in snake_case
|
||||||
for (const [param, value] of new URL(
|
credit_type: this.credit_type.length > 0 ? this.credit_type : undefined,
|
||||||
window.location.href,
|
semester: this.semester.length > 0 ? this.semester : undefined,
|
||||||
).searchParams.entries()) {
|
|
||||||
// Deal with array type params
|
department: this.department.length > 0 ? this.department : undefined,
|
||||||
if (["credit_type", "department", "semester"].includes(param)) {
|
search: this.search || undefined,
|
||||||
if (args[param] === undefined) {
|
},
|
||||||
args[param] = [];
|
});
|
||||||
|
if (res.data !== undefined) {
|
||||||
|
this.ues = res.data;
|
||||||
}
|
}
|
||||||
args[param].push(value);
|
|
||||||
} else {
|
|
||||||
args[param] = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.ues = (await ueFetchUeList({ query: args })).data;
|
|
||||||
this.loading = false;
|
this.loading = false;
|
||||||
},
|
},
|
||||||
|
|
||||||
Reference in New Issue
Block a user