feat: add system configuration API endpoint and frontend hook for dynamic settings
This commit is contained in:
15
k-notes-frontend/src/hooks/useConfig.ts
Normal file
15
k-notes-frontend/src/hooks/useConfig.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { api } from "@/lib/api";
|
||||
|
||||
export interface ConfigResponse {
|
||||
allow_registration: boolean;
|
||||
}
|
||||
|
||||
export function useConfig() {
|
||||
return useQuery<ConfigResponse>({
|
||||
queryKey: ["config"],
|
||||
queryFn: () => api.get("/config"),
|
||||
staleTime: Infinity, // Config rarely changes
|
||||
});
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { z } from "zod";
|
||||
import { Link } from "react-router-dom";
|
||||
import { useLogin } from "@/hooks/use-auth";
|
||||
import { useConfig } from "@/hooks/useConfig";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
@@ -22,6 +23,7 @@ type LoginFormValues = z.infer<typeof loginSchema>;
|
||||
|
||||
export default function LoginPage() {
|
||||
const { mutate: login, isPending } = useLogin();
|
||||
const { data: config } = useConfig();
|
||||
|
||||
const form = useForm<LoginFormValues>({
|
||||
resolver: zodResolver(loginSchema),
|
||||
@@ -95,12 +97,14 @@ export default function LoginPage() {
|
||||
</Form>
|
||||
</CardContent>
|
||||
<CardFooter className="flex justify-center">
|
||||
<p className="text-sm text-gray-500 dark:text-gray-400">
|
||||
Don't have an account?{" "}
|
||||
<Link to="/register" className="font-semibold text-primary hover:underline">
|
||||
Sign up
|
||||
</Link>
|
||||
</p>
|
||||
{config?.allow_registration !== false && (
|
||||
<p className="text-sm text-gray-500 dark:text-gray-400">
|
||||
Don't have an account?{" "}
|
||||
<Link to="/register" className="font-semibold text-primary hover:underline">
|
||||
Sign up
|
||||
</Link>
|
||||
</p>
|
||||
)}
|
||||
</CardFooter>
|
||||
</Card>
|
||||
<SettingsDialog open={settingsOpen} onOpenChange={setSettingsOpen} dataManagementEnabled={false} />
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { useState } from "react";
|
||||
import { useState, useEffect } from "react";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { Settings } from "lucide-react";
|
||||
import { SettingsDialog } from "@/components/settings-dialog";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { z } from "zod";
|
||||
import { Link } from "react-router-dom";
|
||||
import { Link, useNavigate } from "react-router-dom";
|
||||
import { useRegister } from "@/hooks/use-auth";
|
||||
import { useConfig } from "@/hooks/useConfig";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
@@ -26,6 +27,19 @@ type RegisterFormValues = z.infer<typeof registerSchema>;
|
||||
|
||||
export default function RegisterPage() {
|
||||
const { mutate: register, isPending } = useRegister();
|
||||
const { data: config, isLoading: isConfigLoading } = useConfig();
|
||||
const navigate = useNavigate();
|
||||
|
||||
useEffect(() => {
|
||||
if (!isConfigLoading && config?.allow_registration === false) {
|
||||
toast.error("Registration is currently disabled");
|
||||
navigate("/login");
|
||||
}
|
||||
}, [config, isConfigLoading, navigate]);
|
||||
|
||||
if (isConfigLoading || config?.allow_registration === false) {
|
||||
return null; // Or a loading spinner
|
||||
}
|
||||
|
||||
const form = useForm<RegisterFormValues>({
|
||||
resolver: zodResolver(registerSchema),
|
||||
|
||||
Reference in New Issue
Block a user