Config Vars - Get config vars from API

This commit is contained in:
2025-09-22 20:52:23 +02:00
parent 550beffba8
commit 3e77426d1d
4 changed files with 77 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
import { ConfigVar } from "@/model/ConfigVar";
import { fetcher, fetcherUnwraper } from "./backend";
import useSWR from "swr";
export function useAPIConfigVarList() {
const { data, error, isLoading } = useSWR<ConfigVar[]>(
"/api/config",
fetcherUnwraper,
);
return {
configVarList: data,
isLoading,
isError: error,
};
}
export function useAPIConfigVar(key: string) {
const url = `/api/config/var/${key}`;
const { data, error, isLoading } = useSWR<ConfigVar>(url, fetcher);
return {
configVar: data,
isLoading,
isError: error,
};
}
+20
View File
@@ -0,0 +1,20 @@
export type ConfigVar = {
key: string;
value: string;
};
export enum eConfigVarKeys {
varCompanyName = "CompanyName",
varCompanyWebsite = "CompanyWebsite",
varCompanyLogo = "CompanyLogo",
varCompanyLogoSize = "CompanyLogoSize",
varCompanyEmail = "CompanyEmail",
varCompanyPhone = "CompanyPhone",
varCompanyAddress = "CompanyAddress",
varNightAmountForContactDialog = "NightAmountForContactDialog",
varGuestNumberForContactDialog = "GuestNumberForContactDialog",
varContactDialogShowBehaviour = "ContactDialogShowBehaviour",
varShowNonAvailableRooms = "ShowNonAvailableRooms",
varSomniaAttributionNotice = "SomniaAttributionNotice",
varPhotoAspectRatio = "PhotoAspectRatio",
}
+17
View File
@@ -0,0 +1,17 @@
import { useAPIConfigVarList } from "@/api/api-configVars";
import { getConfigVarFromList } from "./utils";
import { eConfigVarKeys } from "@/model/ConfigVar";
export function useConfigVariable(key: eConfigVarKeys, defaultValue: string) {
const { configVarList, isLoading } = useAPIConfigVarList();
if (isLoading) {
return {
value: defaultValue,
isLoading: true,
};
}
const configVar = getConfigVarFromList(configVarList, key);
return { value: configVar?.value ?? defaultValue, isLoading: isLoading };
}
+12
View File
@@ -1,5 +1,6 @@
import { AvailabilityFilter, FareFilter } from "@/model/RoomSearch"; import { AvailabilityFilter, FareFilter } from "@/model/RoomSearch";
import { SearchFilter } from "@/model/InternalTypes"; import { SearchFilter } from "@/model/InternalTypes";
import { ConfigVar } from "@/model/ConfigVar";
export function makeDayUTCDate(date: Date | undefined): Date { export function makeDayUTCDate(date: Date | undefined): Date {
if (!date) { if (!date) {
@@ -50,3 +51,14 @@ export function makeFareFilterFromSearchFilter(
roomCategory: roomCategoryId, roomCategory: roomCategoryId,
}; };
} }
export function getConfigVarFromList(
varList: ConfigVar[] | undefined,
key: string,
): ConfigVar | undefined {
if (!varList) {
return undefined;
}
return varList.find((v) => v.key === key);
}