fix useSearch and admin path registration

This commit is contained in:
dswbx
2025-06-09 07:21:32 +02:00
parent 0db052acca
commit 4729203d47
4 changed files with 34 additions and 28 deletions

View File

@@ -333,7 +333,7 @@ export class SchemaManager {
if (config.force) { if (config.force) {
try { try {
$console.info("[SchemaManager]", sql, parameters); $console.log("[SchemaManager]", sql);
await qb.execute(); await qb.execute();
} catch (e) { } catch (e) {
throw new Error(`Failed to execute query: ${sql}: ${(e as any).message}`); throw new Error(`Failed to execute query: ${sql}: ${(e as any).message}`);

View File

@@ -50,7 +50,7 @@ export class AdminController extends Controller {
} }
get basepath() { get basepath() {
return this.options.adminBasepath ?? "/"; return this.options.basepath ?? "/";
} }
private withBasePath(route: string = "") { private withBasePath(route: string = "") {
@@ -93,10 +93,13 @@ export class AdminController extends Controller {
path, path,
permission(SystemPermissions.accessAdmin, { permission(SystemPermissions.accessAdmin, {
onDenied: async (c) => { onDenied: async (c) => {
if (!path.startsWith("/auth")) {
addFlashMessage(c, "You are not authorized to access the Admin UI", "error"); addFlashMessage(c, "You are not authorized to access the Admin UI", "error");
$console.log("redirecting"); $console.log("redirecting", authRoutes.login);
return c.redirect(authRoutes.login); return c.redirect(authRoutes.login);
}
return;
}, },
}), }),
permission(SystemPermissions.schemaRead, { permission(SystemPermissions.schemaRead, {

View File

@@ -1,4 +1,4 @@
import { decodeSearch, encodeSearch, parseDecode } from "core/utils"; import { decodeSearch, encodeSearch, mergeObject, parseDecode } from "core/utils";
import { isEqual, transform } from "lodash-es"; import { isEqual, transform } from "lodash-es";
import { useLocation, useSearch as useWouterSearch } from "wouter"; import { useLocation, useSearch as useWouterSearch } from "wouter";
import { type s, parse } from "core/object/schema"; import { type s, parse } from "core/object/schema";
@@ -16,27 +16,31 @@ export function useSearch<Schema extends s.TAnySchema = s.TAnySchema>(
clone: true, clone: true,
}) as s.StaticCoerced<Schema>; }) as s.StaticCoerced<Schema>;
// @todo: add option to set multiple keys at once // @ts-ignore
function set<Key extends keyof s.StaticCoerced<Schema>>( const _defaults = mergeObject(schema.template({ withOptional: true }), defaultValue ?? {});
key: Key,
value: s.StaticCoerced<Schema>[Key], function set<Update extends Partial<s.StaticCoerced<Schema>>>(update: Update): void {
): void { // @ts-ignore
//console.log("set", key, value); if (schema.validate(update).valid) {
const update = parse(schema, { ...decodeSearch(searchString), [key]: value }); const search = getWithoutDefaults(mergeObject(value, update), _defaults);
const search = transform(
update as any,
(result, value, key) => {
if (defaultValue && isEqual(value, defaultValue[key])) return;
result[key] = value;
},
{} as s.StaticCoerced<Schema>,
);
const encoded = encodeSearch(search, { encode: false }); const encoded = encodeSearch(search, { encode: false });
navigate(location + (encoded.length > 0 ? "?" + encoded : "")); navigate(location + (encoded.length > 0 ? "?" + encoded : ""));
} }
}
return { return {
value: value as Required<s.StaticCoerced<Schema>>, value: value as Required<s.StaticCoerced<Schema>>,
set, set,
}; };
} }
function getWithoutDefaults(value: object, defaultValue: object) {
return transform(
value as any,
(result, value, key) => {
if (defaultValue && isEqual(value, defaultValue[key])) return;
result[key] = value;
},
{} as object,
);
}

View File

@@ -23,7 +23,7 @@ const searchSchema = s.partialObject({
perPage: s.number({ default: 10 }).optional(), perPage: s.number({ default: 10 }).optional(),
}); });
const PER_PAGE_OPTIONS = [5, 10, 25]; const PER_PAGE_OPTIONS = [5, 10, 25, 50, 100];
export function DataEntityList({ params }) { export function DataEntityList({ params }) {
const { $data } = useBkndData(); const { $data } = useBkndData();
@@ -61,19 +61,18 @@ export function DataEntityList({ params }) {
} }
function handleClickPage(page: number) { function handleClickPage(page: number) {
search.set("page", page); search.set({ page });
} }
function handleSortClick(name: string) { function handleSortClick(name: string) {
const sort = search.value.sort!; const sort = search.value.sort!;
const newSort = { by: name, dir: sort.by === name && sort.dir === "asc" ? "desc" : "asc" }; const newSort = { by: name, dir: sort.by === name && sort.dir === "asc" ? "desc" : "asc" };
search.set("sort", newSort as any); search.set({ sort: newSort as any });
} }
function handleClickPerPage(perPage: number) { function handleClickPerPage(perPage: number) {
// @todo: also reset page to 1 search.set({ perPage, page: 1 });
search.set("perPage", perPage);
} }
const isUpdating = $q.isLoading || $q.isValidating; const isUpdating = $q.isLoading || $q.isValidating;