Refactor: merge related hooks (8 files → 5)
- useChampionship + useChampionships → useChampionships - useMyRegistrations + useRegisterForChampionship → useRegistrations - useLoginForm + useRegisterForm → useAuthForms Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { use } from "react";
|
import { use } from "react";
|
||||||
import { useChampionship } from "@/hooks/useChampionship";
|
import { useChampionship } from "@/hooks/useChampionships";
|
||||||
import { useMyRegistrations } from "@/hooks/useMyRegistrations";
|
import { useMyRegistrations } from "@/hooks/useRegistrations";
|
||||||
import { useRegisterForChampionship } from "@/hooks/useRegisterForChampionship";
|
import { useRegisterForChampionship } from "@/hooks/useRegistrations";
|
||||||
import { useAuth } from "@/hooks/useAuth";
|
import { useAuth } from "@/hooks/useAuth";
|
||||||
import { RegistrationTimeline } from "@/components/RegistrationTimeline";
|
import { RegistrationTimeline } from "@/components/RegistrationTimeline";
|
||||||
import { StatusBadge } from "@/components/StatusBadge";
|
import { StatusBadge } from "@/components/StatusBadge";
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useMyRegistrations } from "@/hooks/useMyRegistrations";
|
import { useMyRegistrations } from "@/hooks/useRegistrations";
|
||||||
import { RegistrationCard } from "@/components/RegistrationCard";
|
import { RegistrationCard } from "@/components/RegistrationCard";
|
||||||
|
|
||||||
export default function RegistrationsPage() {
|
export default function RegistrationsPage() {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { useLoginForm } from "@/hooks/useLoginForm";
|
import { useLoginForm } from "@/hooks/useAuthForms";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { Input } from "@/components/ui/input";
|
import { Input } from "@/components/ui/input";
|
||||||
import { Label } from "@/components/ui/label";
|
import { Label } from "@/components/ui/label";
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { useRegisterForm } from "@/hooks/useRegisterForm";
|
import { useRegisterForm } from "@/hooks/useAuthForms";
|
||||||
import { MemberFields } from "@/components/auth/MemberFields";
|
import { MemberFields } from "@/components/auth/MemberFields";
|
||||||
import { OrganizerFields } from "@/components/auth/OrganizerFields";
|
import { OrganizerFields } from "@/components/auth/OrganizerFields";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
|
|||||||
@@ -4,6 +4,29 @@ import { useState } from "react";
|
|||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import { useAuth } from "@/hooks/useAuth";
|
import { useAuth } from "@/hooks/useAuth";
|
||||||
|
|
||||||
|
export function useLoginForm() {
|
||||||
|
const router = useRouter();
|
||||||
|
const login = useAuth((s) => s.login);
|
||||||
|
const isLoading = useAuth((s) => s.isLoading);
|
||||||
|
|
||||||
|
const [email, setEmail] = useState("");
|
||||||
|
const [password, setPassword] = useState("");
|
||||||
|
const [error, setError] = useState("");
|
||||||
|
|
||||||
|
async function submit(e: React.SyntheticEvent) {
|
||||||
|
e.preventDefault();
|
||||||
|
setError("");
|
||||||
|
try {
|
||||||
|
await login(email, password);
|
||||||
|
router.push("/championships");
|
||||||
|
} catch {
|
||||||
|
setError("Invalid email or password");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return { email, setEmail, password, setPassword, error, isLoading, submit };
|
||||||
|
}
|
||||||
|
|
||||||
export function useRegisterForm() {
|
export function useRegisterForm() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const register = useAuth((s) => s.register);
|
const register = useAuth((s) => s.register);
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
"use client";
|
|
||||||
|
|
||||||
import { useQuery } from "@tanstack/react-query";
|
|
||||||
import { championshipsApi } from "@/lib/api/championships";
|
|
||||||
|
|
||||||
export function useChampionship(id: string) {
|
|
||||||
return useQuery({
|
|
||||||
queryKey: ["championship", id],
|
|
||||||
queryFn: () => championshipsApi.get(id),
|
|
||||||
enabled: !!id,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
@@ -9,3 +9,11 @@ export function useChampionships() {
|
|||||||
queryFn: () => championshipsApi.list(),
|
queryFn: () => championshipsApi.list(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function useChampionship(id: string) {
|
||||||
|
return useQuery({
|
||||||
|
queryKey: ["championship", id],
|
||||||
|
queryFn: () => championshipsApi.get(id),
|
||||||
|
enabled: !!id,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,28 +0,0 @@
|
|||||||
"use client";
|
|
||||||
|
|
||||||
import { useState } from "react";
|
|
||||||
import { useRouter } from "next/navigation";
|
|
||||||
import { useAuth } from "@/hooks/useAuth";
|
|
||||||
|
|
||||||
export function useLoginForm() {
|
|
||||||
const router = useRouter();
|
|
||||||
const login = useAuth((s) => s.login);
|
|
||||||
const isLoading = useAuth((s) => s.isLoading);
|
|
||||||
|
|
||||||
const [email, setEmail] = useState("");
|
|
||||||
const [password, setPassword] = useState("");
|
|
||||||
const [error, setError] = useState("");
|
|
||||||
|
|
||||||
async function submit(e: React.SyntheticEvent) {
|
|
||||||
e.preventDefault();
|
|
||||||
setError("");
|
|
||||||
try {
|
|
||||||
await login(email, password);
|
|
||||||
router.push("/championships");
|
|
||||||
} catch {
|
|
||||||
setError("Invalid email or password");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return { email, setEmail, password, setPassword, error, isLoading, submit };
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
"use client";
|
|
||||||
|
|
||||||
import { useQuery } from "@tanstack/react-query";
|
|
||||||
import { registrationsApi } from "@/lib/api/registrations";
|
|
||||||
|
|
||||||
export function useMyRegistrations() {
|
|
||||||
return useQuery({
|
|
||||||
queryKey: ["registrations", "my"],
|
|
||||||
queryFn: () => registrationsApi.my(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
@@ -1,8 +1,15 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
||||||
import { registrationsApi } from "@/lib/api/registrations";
|
import { registrationsApi } from "@/lib/api/registrations";
|
||||||
|
|
||||||
|
export function useMyRegistrations() {
|
||||||
|
return useQuery({
|
||||||
|
queryKey: ["registrations", "my"],
|
||||||
|
queryFn: () => registrationsApi.my(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export function useRegisterForChampionship(championshipId: string) {
|
export function useRegisterForChampionship(championshipId: string) {
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
Reference in New Issue
Block a user