fix: handle register response correctly
Docker Build and Push / build-and-push (push) Successful in 5m22s
Docker Build and Push / build-and-push (push) Successful in 5m22s
- get sorting data from backend
This commit is contained in:
+125
-24
@@ -1,6 +1,6 @@
|
|||||||
import { useState, useEffect } from "react"
|
import { useState, useEffect, useRef, useCallback } from "react"
|
||||||
import { useNavigate, Link } from "react-router"
|
import { useNavigate, Link } from "react-router"
|
||||||
import { Plus, Search } from "lucide-react"
|
import { Plus, Search, Filter, ArrowUpDown, Loader2 } from "lucide-react"
|
||||||
import { Navbar } from "@/components/shared/navbar"
|
import { Navbar } from "@/components/shared/navbar"
|
||||||
import { Footer } from "@/components/shared/footer"
|
import { Footer } from "@/components/shared/footer"
|
||||||
import { FormInput } from "@/components/shared/form-input"
|
import { FormInput } from "@/components/shared/form-input"
|
||||||
@@ -10,13 +10,25 @@ import { useAuth } from "@/app/context/auth-context"
|
|||||||
import { getForms } from "@/lib/api"
|
import { getForms } from "@/lib/api"
|
||||||
import type { FormSummary } from "@/lib/types"
|
import type { FormSummary } from "@/lib/types"
|
||||||
|
|
||||||
|
type StatusFilter = "all" | "has_responses" | "no_responses"
|
||||||
|
type SortBy = "created_at" | "updated_at"
|
||||||
|
type SortDir = "newest" | "oldest"
|
||||||
|
|
||||||
|
const DEBOUNCE_MS = 400
|
||||||
|
|
||||||
export default function FormsPage() {
|
export default function FormsPage() {
|
||||||
const { user, loading } = useAuth()
|
const { user, loading } = useAuth()
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
const [search, setSearch] = useState("")
|
const [search, setSearch] = useState("")
|
||||||
|
const [statusFilter, setStatusFilter] = useState<StatusFilter>("all")
|
||||||
|
const [sortBy, setSortBy] = useState<SortBy>("created_at")
|
||||||
|
const [sortDir, setSortDir] = useState<SortDir>("newest")
|
||||||
const [forms, setForms] = useState<FormSummary[]>([])
|
const [forms, setForms] = useState<FormSummary[]>([])
|
||||||
|
const [totalForms, setTotalForms] = useState(0)
|
||||||
const [loadingForms, setLoadingForms] = useState(true)
|
const [loadingForms, setLoadingForms] = useState(true)
|
||||||
const [error, setError] = useState<string | null>(null)
|
const [error, setError] = useState<string | null>(null)
|
||||||
|
const debounceRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
||||||
|
const isInitialLoad = useRef(true)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!loading && !user) {
|
if (!loading && !user) {
|
||||||
@@ -24,21 +36,62 @@ export default function FormsPage() {
|
|||||||
}
|
}
|
||||||
}, [user, loading, navigate])
|
}, [user, loading, navigate])
|
||||||
|
|
||||||
useEffect(() => {
|
const fetchForms = useCallback(async (params: {
|
||||||
if (!user) return
|
search: string
|
||||||
|
statusFilter: StatusFilter
|
||||||
async function fetchForms() {
|
sortBy: SortBy
|
||||||
|
sortDir: SortDir
|
||||||
|
}) => {
|
||||||
|
setError(null)
|
||||||
|
setLoadingForms(true)
|
||||||
try {
|
try {
|
||||||
const data = await getForms()
|
const data = await getForms({
|
||||||
|
search: params.search || undefined,
|
||||||
|
status: params.statusFilter === "all" ? undefined : params.statusFilter,
|
||||||
|
sort_by: params.sortBy,
|
||||||
|
sort_dir: params.sortDir,
|
||||||
|
})
|
||||||
setForms(data)
|
setForms(data)
|
||||||
|
if (isInitialLoad.current) {
|
||||||
|
setTotalForms(data.length)
|
||||||
|
isInitialLoad.current = false
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
setError("Failed to load forms. Please try again.")
|
setError("Failed to load forms. Please try again.")
|
||||||
} finally {
|
} finally {
|
||||||
setLoadingForms(false)
|
setLoadingForms(false)
|
||||||
}
|
}
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
// Initial fetch
|
||||||
|
useEffect(() => {
|
||||||
|
if (!user) return
|
||||||
|
fetchForms({ search, statusFilter, sortBy, sortDir })
|
||||||
|
}, [user]) // eslint-disable-line react-hooks/exhaustive-deps
|
||||||
|
|
||||||
|
// Debounced fetch when filters change (skip initial)
|
||||||
|
useEffect(() => {
|
||||||
|
if (!user || isInitialLoad.current) return
|
||||||
|
|
||||||
|
if (debounceRef.current) clearTimeout(debounceRef.current)
|
||||||
|
|
||||||
|
debounceRef.current = setTimeout(() => {
|
||||||
|
fetchForms({ search, statusFilter, sortBy, sortDir })
|
||||||
|
}, DEBOUNCE_MS)
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
if (debounceRef.current) clearTimeout(debounceRef.current)
|
||||||
|
}
|
||||||
|
}, [search, statusFilter, sortBy, sortDir, user, fetchForms])
|
||||||
|
|
||||||
|
const hasActiveFilters = search !== "" || statusFilter !== "all"
|
||||||
|
|
||||||
|
function clearAllFilters() {
|
||||||
|
setSearch("")
|
||||||
|
setStatusFilter("all")
|
||||||
|
setSortBy("created_at")
|
||||||
|
setSortDir("newest")
|
||||||
}
|
}
|
||||||
fetchForms()
|
|
||||||
}, [user])
|
|
||||||
|
|
||||||
if (loading || loadingForms) {
|
if (loading || loadingForms) {
|
||||||
return (
|
return (
|
||||||
@@ -50,13 +103,6 @@ export default function FormsPage() {
|
|||||||
|
|
||||||
if (!user) return null
|
if (!user) return null
|
||||||
|
|
||||||
const filtered = forms.filter((form) => {
|
|
||||||
return (
|
|
||||||
form.title.toLowerCase().includes(search.toLowerCase()) ||
|
|
||||||
form.description.toLowerCase().includes(search.toLowerCase())
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex min-h-screen flex-col bg-background">
|
<div className="flex min-h-screen flex-col bg-background">
|
||||||
<Navbar />
|
<Navbar />
|
||||||
@@ -78,21 +124,76 @@ export default function FormsPage() {
|
|||||||
</Link>
|
</Link>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="mb-6">
|
<div className="mb-6 flex flex-col gap-3">
|
||||||
<div className="relative max-w-md">
|
<div className="relative w-full sm:max-w-md">
|
||||||
<Search className="pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground" />
|
<Search className="pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground" />
|
||||||
<FormInput
|
<FormInput
|
||||||
placeholder="Search forms..."
|
placeholder="Search forms by title..."
|
||||||
value={search}
|
value={search}
|
||||||
onChange={(e) => setSearch(e.target.value)}
|
onChange={(e) => setSearch(e.target.value)}
|
||||||
className="pl-9"
|
className="pl-9"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div className="grid grid-cols-2 gap-2 sm:flex sm:items-center sm:gap-2">
|
||||||
|
<div className="relative col-span-2 sm:col-span-1">
|
||||||
|
<Filter className="pointer-events-none absolute left-2.5 top-1/2 h-3.5 w-3.5 -translate-y-1/2 text-muted-foreground" />
|
||||||
|
<select
|
||||||
|
value={statusFilter}
|
||||||
|
onChange={(e) => setStatusFilter(e.target.value as StatusFilter)}
|
||||||
|
className="h-10 w-full appearance-none rounded-lg border border-border bg-card pl-8 pr-8 text-sm text-foreground shadow-sm focus:border-ring focus:outline-none focus:ring-2 focus:ring-ring/30"
|
||||||
|
>
|
||||||
|
<option value="all">All Status</option>
|
||||||
|
<option value="has_responses">Has Responses</option>
|
||||||
|
<option value="no_responses">No Responses</option>
|
||||||
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p className="mb-4 text-sm text-muted-foreground">
|
<div className="relative">
|
||||||
Showing {filtered.length} of {forms.length} forms
|
<ArrowUpDown className="pointer-events-none absolute left-2.5 top-1/2 h-3.5 w-3.5 -translate-y-1/2 text-muted-foreground" />
|
||||||
|
<select
|
||||||
|
value={sortBy}
|
||||||
|
onChange={(e) => setSortBy(e.target.value as SortBy)}
|
||||||
|
className="h-10 w-full appearance-none rounded-lg border border-border bg-card pl-8 pr-8 text-sm text-foreground shadow-sm focus:border-ring focus:outline-none focus:ring-2 focus:ring-ring/30"
|
||||||
|
>
|
||||||
|
<option value="created_at">Sort: Created</option>
|
||||||
|
<option value="updated_at">Sort: Updated</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="relative">
|
||||||
|
<select
|
||||||
|
value={sortDir}
|
||||||
|
onChange={(e) => setSortDir(e.target.value as SortDir)}
|
||||||
|
className="h-10 w-full appearance-none rounded-lg border border-border bg-card pl-8 pr-8 text-sm text-foreground shadow-sm focus:border-ring focus:outline-none focus:ring-2 focus:ring-ring/30"
|
||||||
|
>
|
||||||
|
<option value="newest">Newest first</option>
|
||||||
|
<option value="oldest">Oldest first</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="mb-4 flex items-center justify-between">
|
||||||
|
<p className="text-sm text-muted-foreground">
|
||||||
|
{loadingForms ? (
|
||||||
|
<span className="inline-flex items-center gap-1.5">
|
||||||
|
<Loader2 className="h-3.5 w-3.5 animate-spin" />
|
||||||
|
Searching...
|
||||||
|
</span>
|
||||||
|
) : (
|
||||||
|
<>Showing {forms.length}{totalForms > 0 ? ` of ${totalForms}` : ""} forms</>
|
||||||
|
)}
|
||||||
</p>
|
</p>
|
||||||
|
{hasActiveFilters && (
|
||||||
|
<button
|
||||||
|
onClick={clearAllFilters}
|
||||||
|
className="text-xs text-primary hover:underline"
|
||||||
|
>
|
||||||
|
Clear all filters
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
{error ? (
|
{error ? (
|
||||||
<div className="flex flex-col items-center justify-center rounded-xl border border-dashed border-border py-16">
|
<div className="flex flex-col items-center justify-center rounded-xl border border-dashed border-border py-16">
|
||||||
@@ -106,9 +207,9 @@ export default function FormsPage() {
|
|||||||
Retry
|
Retry
|
||||||
</FormButton>
|
</FormButton>
|
||||||
</div>
|
</div>
|
||||||
) : filtered.length > 0 ? (
|
) : forms.length > 0 ? (
|
||||||
<div className="grid gap-5 sm:grid-cols-2">
|
<div className="grid gap-5 sm:grid-cols-2">
|
||||||
{filtered.map((form) => (
|
{forms.map((form) => (
|
||||||
<FormCard key={form.id} form={form} />
|
<FormCard key={form.id} form={form} />
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
@@ -121,7 +222,7 @@ export default function FormsPage() {
|
|||||||
variant="ghost"
|
variant="ghost"
|
||||||
size="sm"
|
size="sm"
|
||||||
className="mt-3"
|
className="mt-3"
|
||||||
onClick={() => setSearch("")}
|
onClick={clearAllFilters}
|
||||||
>
|
>
|
||||||
Clear filters
|
Clear filters
|
||||||
</FormButton>
|
</FormButton>
|
||||||
|
|||||||
+3
-11
@@ -1,13 +1,5 @@
|
|||||||
import type { Route } from "./+types/home";
|
import { redirect } from "react-router";
|
||||||
import { Welcome } from "../welcome/welcome";
|
|
||||||
|
|
||||||
export function meta({}: Route.MetaArgs) {
|
export function loader() {
|
||||||
return [
|
throw redirect("/forms");
|
||||||
{ title: "New React Router App" },
|
|
||||||
{ name: "description", content: "Welcome to React Router!" },
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function Home() {
|
|
||||||
return <Welcome />;
|
|
||||||
}
|
}
|
||||||
|
|||||||
+16
-4
@@ -120,8 +120,6 @@ export async function register(email: string, password: string) {
|
|||||||
const data = await res.json().catch(() => null)
|
const data = await res.json().catch(() => null)
|
||||||
throw new Error(data?.message ?? "Registration failed")
|
throw new Error(data?.message ?? "Registration failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.json()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function logout() {
|
export async function logout() {
|
||||||
@@ -145,8 +143,22 @@ export async function logout() {
|
|||||||
clearTokens()
|
clearTokens()
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getForms(): Promise<FormSummary[]> {
|
export interface GetFormsParams {
|
||||||
const res = await fetchWithAuth(`${import.meta.env.VITE_API_BASE_URL}/api/forms`)
|
search?: string
|
||||||
|
status?: "has_responses" | "no_responses"
|
||||||
|
sort_by?: "created_at" | "updated_at"
|
||||||
|
sort_dir?: "newest" | "oldest"
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getForms(params: GetFormsParams = {}): Promise<FormSummary[]> {
|
||||||
|
const url = new URL(`${import.meta.env.VITE_API_BASE_URL}/api/forms`)
|
||||||
|
|
||||||
|
if (params.search) url.searchParams.set("search", params.search)
|
||||||
|
if (params.status) url.searchParams.set("status", params.status)
|
||||||
|
if (params.sort_by) url.searchParams.set("sort_by", params.sort_by)
|
||||||
|
if (params.sort_dir) url.searchParams.set("sort_dir", params.sort_dir)
|
||||||
|
|
||||||
|
const res = await fetchWithAuth(url.toString())
|
||||||
|
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
throw new Error("Failed to fetch forms")
|
throw new Error("Failed to fetch forms")
|
||||||
|
|||||||
Reference in New Issue
Block a user