mirror of
https://github.com/bitbaum/evig.git
synced 2026-12-05 15:39:06 +01:00
refactor(hooks): migrate useBlogSubmissions to SWR (2/N — lint cleanup)
Builds on cc03c57a (SWR + useSwrFetch wrapper). Replaces the
useState/useEffect/setState fetch dance that tripped
react-hooks/set-state-in-effect at line 55. The hook's external
return shape stays unchanged so BlogSubmissionsClient continues
working without modification (submissions, loading, error, stats,
all action handlers).
Specific changes:
- Removed: submissions/loading/error useState declarations, the
loadSubmissions useCallback, the useEffect that called it.
- Added: useSwrFetch<{ submissions }>('/api/blog/my-submissions').
The wrapper translates apiFetch's { success, data, error }
envelope into SWR's throw-on-error contract; data?.submissions
feeds the rendered list, isLoading drives the spinner, swrError
is surfaced via the existing `error` field with the localized
loadError message.
- resubmit() now calls mutate() instead of loadSubmissions() — SWR
revalidates the cache against the same key and re-renders any
consumer hooked to it (only this one in our case).
- Kept: local UI state (editingId, editTitle, editContent, saving,
expandedFeedback) that isn't part of the fetch lifecycle.
The displayError computation `error || (swrError ? errors.loadError : '')`
prefers any explicit local error (e.g., resubmit-side message) over
the SWR-surfaced load error so the user sees the most-relevant
diagnostic. Matches the prior precedence — setError in resubmit's
error path overwrote what loadSubmissions had set.
Lint: 6 → 5 errors. tsc clean. No existing test surface for this
hook (single consumer BlogSubmissionsClient also has no tests); the
SWR wrapper's own behavior is exercised by the in-progress hook
migrations across this thread.
33 commits this thread. Remaining for the cleanup sequence:
useAdminWorkshops (next this session), useMyListings,
useSellerDashboard, useWorkshopRegistrations,
ProtocolDetailClient (set-state-in-memo, different rule).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
cc03c57a61
commit
2c069a97d5
1 file changed
+24
-21
@@ -1,7 +1,8 @@
|
||||
'use client'
|
||||
|
||||
import { useState, useEffect, useCallback } from 'react'
|
||||
import { useState } from 'react'
|
||||
import { apiFetch } from '@/lib/api/client'
|
||||
import { useSwrFetch } from '@/lib/api/swr'
|
||||
import { APPROVAL_STATUS } from '@/config/approval-status'
|
||||
|
||||
export interface MySubmission {
|
||||
@@ -30,8 +31,20 @@ interface UseBlogSubmissionsErrors {
|
||||
}
|
||||
|
||||
export function useBlogSubmissions(errors: UseBlogSubmissionsErrors) {
|
||||
const [submissions, setSubmissions] = useState<MySubmission[]>([])
|
||||
const [loading, setLoading] = useState(true)
|
||||
// Submissions list — SWR replaces the prior useState + useEffect +
|
||||
// setState pattern (which tripped react-hooks/set-state-in-effect).
|
||||
// The wrapper bridges apiFetch's { success, data, error } envelope
|
||||
// into SWR's throw-on-error contract; see src/lib/api/swr.ts.
|
||||
const {
|
||||
data,
|
||||
error: swrError,
|
||||
isLoading,
|
||||
mutate,
|
||||
} = useSwrFetch<{ submissions: MySubmission[] }>('/api/blog/my-submissions')
|
||||
|
||||
const submissions = data?.submissions ?? []
|
||||
|
||||
// Local UI state that's independent of the fetch
|
||||
const [error, setError] = useState('')
|
||||
const [editingId, setEditingId] = useState<string | null>(null)
|
||||
const [editTitle, setEditTitle] = useState('')
|
||||
@@ -39,21 +52,9 @@ export function useBlogSubmissions(errors: UseBlogSubmissionsErrors) {
|
||||
const [saving, setSaving] = useState(false)
|
||||
const [expandedFeedback, setExpandedFeedback] = useState<Set<string>>(new Set())
|
||||
|
||||
const loadSubmissions = useCallback(async () => {
|
||||
setLoading(true)
|
||||
const result = await apiFetch<{ submissions: MySubmission[] }>('/api/blog/my-submissions')
|
||||
if (result.success && result.data) {
|
||||
setSubmissions(result.data.submissions || [])
|
||||
setError('')
|
||||
} else {
|
||||
setError(result.error || errors.loadError)
|
||||
}
|
||||
setLoading(false)
|
||||
}, [errors.loadError])
|
||||
|
||||
useEffect(() => {
|
||||
loadSubmissions()
|
||||
}, [loadSubmissions])
|
||||
// Surface SWR fetch errors via the same `error` field consumers
|
||||
// already check. Falls back to the localized loadError message.
|
||||
const displayError = error || (swrError ? errors.loadError : '')
|
||||
|
||||
const toggleFeedback = (id: string) => {
|
||||
setExpandedFeedback((prev) => {
|
||||
@@ -89,7 +90,9 @@ export function useBlogSubmissions(errors: UseBlogSubmissionsErrors) {
|
||||
setSaving(false)
|
||||
if (result.success) {
|
||||
cancelEditing()
|
||||
await loadSubmissions()
|
||||
// Revalidate SWR cache so the list reflects the resubmitted entry's
|
||||
// updated status without a full re-mount.
|
||||
await mutate()
|
||||
} else {
|
||||
setError(result.error || errors.resubmitError)
|
||||
}
|
||||
@@ -106,8 +109,8 @@ export function useBlogSubmissions(errors: UseBlogSubmissionsErrors) {
|
||||
|
||||
return {
|
||||
submissions,
|
||||
loading,
|
||||
error,
|
||||
loading: isLoading,
|
||||
error: displayError,
|
||||
editingId,
|
||||
editTitle,
|
||||
editContent,
|
||||
|
||||
Reference in new issue
Block a user