mirror of
https://github.com/bitbaum/evig.git
synced 2026-12-05 15:18:39 +01:00
refactor(hooks): migrate useSellerDashboard to SWR (3/N — lint cleanup)
Third in the SWR migration sequence (cc03c57ainfrastructure,2c069a97useBlogSubmissions). Replaces useState/useEffect/setState with useSwrFetch + a redirect-only useEffect that doesn't setState (so it doesn't trip the lint rule). Specific changes: - The fetch becomes useSwrFetch<SellerDashboardData>( hasAccess ? '/api/seller/dashboard' : null ). Conditional key gates the fetch behind authorization without needing useState; null skips the request entirely. hasAccess is computed inline from session — pure render-time work. - The redirect side effect (push to /auth/login or /dashboard if not authorized) moves into its own useEffect with NO setState, so the react-hooks/set-state-in-effect rule doesn't fire on it. Router.push is a navigation side effect, not state mutation. - fetchDashboardData() is preserved in the return for backward- compat with the retry buttons at dashboard/seller/page.tsx:61, 83. It now invokes SWR's mutate() which revalidates the same cache key — same end-user effect (refetches the data), better internals (no manual setIsLoading/setError dance). - DEFAULT_STATS fallback preserved: when SWR hasn't yet resolved (or returns no data), consumers see DEFAULT_STATS instead of null/undefined, matching the prior hook contract. The hasAccess check is duplicated between the redirect useEffect and the SWR key derivation. Could extract to a memo, but the two contexts handle "not yet authenticated" differently (redirect early- returns; SWR key passes null) so the duplication is intentional and clearer than a shared abstraction. Lint: 5 → 4 errors. tsc clean. 34 commits this thread. Remaining: useAdminWorkshops, useMyListings, useWorkshopRegistrations (all fetch hooks), plus ProtocolDetailClient's set-state-in-memo (different rule, separate treatment). useMyListings is notably the most complex of the remaining ones — the consumer (dashboard/listings/page.tsx) calls fetchListings(page, statusFilter) directly from pagination buttons, which the SWR-key-based pattern needs to replace with setPage/setStatusFilter handlers (coupled consumer change). Worth a session split on its own. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
2c069a97d5
commit
af449333bb
1 file changed
+32
-23
@@ -1,9 +1,9 @@
|
||||
'use client'
|
||||
|
||||
import { useState, useEffect, useCallback } from 'react'
|
||||
import { useEffect } from 'react'
|
||||
import { useSession } from 'next-auth/react'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { apiFetch } from '@/lib/api/client'
|
||||
import { useSwrFetch } from '@/lib/api/swr'
|
||||
import { ROLES } from '@/lib/constants'
|
||||
|
||||
export interface SellerProduct {
|
||||
@@ -48,22 +48,10 @@ export function useSellerDashboard(unexpectedError: string) {
|
||||
const { data: session, status: sessionStatus } = useSession()
|
||||
const router = useRouter()
|
||||
|
||||
const [data, setData] = useState<SellerDashboardData | null>(null)
|
||||
const [isLoading, setIsLoading] = useState(true)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
|
||||
const fetchDashboardData = useCallback(async () => {
|
||||
setIsLoading(true)
|
||||
setError(null)
|
||||
const result = await apiFetch<SellerDashboardData>('/api/seller/dashboard')
|
||||
if (result.success && result.data) {
|
||||
setData(result.data)
|
||||
} else {
|
||||
setError(result.error || unexpectedError)
|
||||
}
|
||||
setIsLoading(false)
|
||||
}, [unexpectedError])
|
||||
|
||||
// Authorization gating: redirect non-sellers + unauthenticated users.
|
||||
// Stays in a useEffect (not part of the fetch) — the redirect is a
|
||||
// navigation side effect, not a setState, so this useEffect doesn't
|
||||
// trip the react-hooks/set-state-in-effect rule.
|
||||
useEffect(() => {
|
||||
if (sessionStatus === 'loading') return
|
||||
if (!session?.user) {
|
||||
@@ -80,18 +68,39 @@ export function useSellerDashboard(unexpectedError: string) {
|
||||
|
||||
if (!hasAccess) {
|
||||
router.push('/dashboard')
|
||||
return
|
||||
}
|
||||
}, [session, sessionStatus, router])
|
||||
|
||||
fetchDashboardData()
|
||||
}, [session, sessionStatus, router, fetchDashboardData])
|
||||
// SWR-driven fetch — gated by the auth/role check. When the user
|
||||
// isn't authorized (or session hasn't loaded), pass null as the key
|
||||
// so SWR skips the fetch entirely (no setState during render).
|
||||
const hasAccess = (() => {
|
||||
if (sessionStatus !== 'authenticated' || !session?.user) return false
|
||||
const userRole = session.user.role as string
|
||||
const isAdmin =
|
||||
userRole === ROLES.REVAMPIT_ADMIN ||
|
||||
session.user.isStaff === true ||
|
||||
session.user.isSuperAdmin === true
|
||||
return userRole === ROLES.SELLER || isAdmin
|
||||
})()
|
||||
|
||||
const {
|
||||
data,
|
||||
error: swrError,
|
||||
isLoading,
|
||||
mutate,
|
||||
} = useSwrFetch<SellerDashboardData>(hasAccess ? '/api/seller/dashboard' : null)
|
||||
|
||||
return {
|
||||
sessionStatus,
|
||||
isLoading,
|
||||
error,
|
||||
error: swrError ? unexpectedError : null,
|
||||
stats: data?.stats ?? DEFAULT_STATS,
|
||||
products: data?.products ?? [],
|
||||
fetchDashboardData,
|
||||
// Expose mutate() under the legacy name so the consumer's retry
|
||||
// buttons (dashboard/seller/page.tsx:61, 83) keep working without
|
||||
// a parallel change. SWR mutate() revalidates the cache against
|
||||
// the same key.
|
||||
fetchDashboardData: () => mutate(),
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user