|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useState, useEffect, useCallback } from 'react'; |
| 4 | +import { RefreshCw, CheckCircle, XCircle } from 'lucide-react'; |
| 5 | +import { Button } from '@/components/ui/Button'; |
| 6 | +import { GrantModerationService } from '@/services/grant-moderation.service'; |
| 7 | +import { DeclineGrantModal } from '@/components/Moderators/DeclineGrantModal'; |
| 8 | +import { FeedItemGrant } from '@/components/Feed/items/FeedItemGrant'; |
| 9 | +import { FeedEntry, FeedGrantContent } from '@/types/feed'; |
| 10 | +import toast from 'react-hot-toast'; |
| 11 | + |
| 12 | +export default function PendingWorksPage() { |
| 13 | + const [entries, setEntries] = useState<FeedEntry[]>([]); |
| 14 | + const [isLoading, setIsLoading] = useState(true); |
| 15 | + const [isRefreshing, setIsRefreshing] = useState(false); |
| 16 | + const [actioningId, setActioningId] = useState<number | null>(null); |
| 17 | + const [declineTarget, setDeclineTarget] = useState<{ grantId: number; entryId: string } | null>( |
| 18 | + null |
| 19 | + ); |
| 20 | + |
| 21 | + const fetchEntries = useCallback(async () => { |
| 22 | + setIsLoading(true); |
| 23 | + try { |
| 24 | + const response = await GrantModerationService.fetchPendingGrants(); |
| 25 | + setEntries(response.entries); |
| 26 | + } catch { |
| 27 | + toast.error('Failed to load pending submissions'); |
| 28 | + } finally { |
| 29 | + setIsLoading(false); |
| 30 | + setIsRefreshing(false); |
| 31 | + } |
| 32 | + }, []); |
| 33 | + |
| 34 | + useEffect(() => { |
| 35 | + fetchEntries(); |
| 36 | + }, [fetchEntries]); |
| 37 | + |
| 38 | + const handleRefresh = async () => { |
| 39 | + setIsRefreshing(true); |
| 40 | + await fetchEntries(); |
| 41 | + }; |
| 42 | + |
| 43 | + const handleApprove = async (grantId: number, entryId: string) => { |
| 44 | + setActioningId(grantId); |
| 45 | + try { |
| 46 | + await GrantModerationService.approveGrant(grantId); |
| 47 | + toast.success('RFP approved successfully'); |
| 48 | + setEntries((prev) => prev.filter((e) => e.id !== entryId)); |
| 49 | + } catch (err) { |
| 50 | + toast.error(err instanceof Error ? err.message : 'Failed to approve RFP'); |
| 51 | + } finally { |
| 52 | + setActioningId(null); |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + const handleDecline = async (data: { reasonChoice: string; reason: string }) => { |
| 57 | + if (!declineTarget) return; |
| 58 | + setActioningId(declineTarget.grantId); |
| 59 | + try { |
| 60 | + await GrantModerationService.declineGrant(declineTarget.grantId, { |
| 61 | + reason_choice: data.reasonChoice, |
| 62 | + ...(data.reason && { reason: data.reason }), |
| 63 | + }); |
| 64 | + toast.success('RFP declined'); |
| 65 | + setEntries((prev) => prev.filter((e) => e.id !== declineTarget.entryId)); |
| 66 | + setDeclineTarget(null); |
| 67 | + } catch (err) { |
| 68 | + toast.error(err instanceof Error ? err.message : 'Failed to decline RFP'); |
| 69 | + } finally { |
| 70 | + setActioningId(null); |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + return ( |
| 75 | + <div className="h-full flex flex-col p-4"> |
| 76 | + <div className="bg-white"> |
| 77 | + <div className="flex items-center justify-between mb-4"> |
| 78 | + <div> |
| 79 | + <h1 className="text-2xl font-semibold text-gray-900">Pending</h1> |
| 80 | + <p className="text-sm text-gray-600 mt-1"> |
| 81 | + Review and approve or decline pending submissions |
| 82 | + </p> |
| 83 | + </div> |
| 84 | + |
| 85 | + <Button |
| 86 | + variant="outlined" |
| 87 | + size="sm" |
| 88 | + onClick={handleRefresh} |
| 89 | + disabled={isRefreshing} |
| 90 | + className="flex items-center space-x-2" |
| 91 | + > |
| 92 | + <RefreshCw className={`h-4 w-4 ${isRefreshing ? 'animate-spin' : ''}`} /> |
| 93 | + <span className="hidden tablet:!block">Refresh</span> |
| 94 | + </Button> |
| 95 | + </div> |
| 96 | + </div> |
| 97 | + |
| 98 | + <div className="flex-1 overflow-auto"> |
| 99 | + <div className="max-w-4xl mx-auto space-y-4"> |
| 100 | + {isLoading && ( |
| 101 | + <div className="space-y-4"> |
| 102 | + {[1, 2, 3].map((i) => ( |
| 103 | + <div |
| 104 | + key={i} |
| 105 | + className="bg-white border border-gray-200 rounded-lg p-6 animate-pulse" |
| 106 | + > |
| 107 | + <div className="h-5 bg-gray-200 rounded w-2/3 mb-3" /> |
| 108 | + <div className="h-4 bg-gray-100 rounded w-1/3 mb-4" /> |
| 109 | + <div className="h-4 bg-gray-100 rounded w-full mb-2" /> |
| 110 | + <div className="h-4 bg-gray-100 rounded w-4/5" /> |
| 111 | + </div> |
| 112 | + ))} |
| 113 | + </div> |
| 114 | + )} |
| 115 | + |
| 116 | + {!isLoading && entries.length === 0 && ( |
| 117 | + <div className="text-center py-12"> |
| 118 | + <div className="flex flex-col items-center justify-center"> |
| 119 | + <div className="w-16 h-16 bg-green-100 rounded-full flex items-center justify-center mb-4"> |
| 120 | + <CheckCircle className="h-8 w-8 text-green-600" /> |
| 121 | + </div> |
| 122 | + <h3 className="text-lg font-medium text-gray-900 mb-2">All caught up!</h3> |
| 123 | + <p className="text-gray-600 text-center max-w-md"> |
| 124 | + No pending submissions require your review at the moment. |
| 125 | + </p> |
| 126 | + </div> |
| 127 | + </div> |
| 128 | + )} |
| 129 | + |
| 130 | + {entries.map((entry) => { |
| 131 | + const grant = entry.content as FeedGrantContent; |
| 132 | + const grantId = grant.grant?.id; |
| 133 | + const isActioning = actioningId === grantId; |
| 134 | + |
| 135 | + return ( |
| 136 | + <FeedItemGrant |
| 137 | + key={entry.id} |
| 138 | + entry={entry} |
| 139 | + showActions={false} |
| 140 | + showHeader={false} |
| 141 | + footer={ |
| 142 | + grantId ? ( |
| 143 | + <div className="flex items-center gap-2 px-4 py-3"> |
| 144 | + <Button |
| 145 | + variant="ghost" |
| 146 | + size="sm" |
| 147 | + onClick={() => handleApprove(grantId, entry.id)} |
| 148 | + disabled={isActioning} |
| 149 | + className="text-green-600 hover:text-green-700 hover:bg-green-50" |
| 150 | + > |
| 151 | + <CheckCircle className="h-4 w-4 mr-1" /> |
| 152 | + {isActioning ? 'Approving...' : 'Approve'} |
| 153 | + </Button> |
| 154 | + |
| 155 | + <Button |
| 156 | + variant="ghost" |
| 157 | + size="sm" |
| 158 | + onClick={() => setDeclineTarget({ grantId, entryId: entry.id })} |
| 159 | + disabled={isActioning} |
| 160 | + className="text-red-600 hover:text-red-700 hover:bg-red-50" |
| 161 | + > |
| 162 | + <XCircle className="h-4 w-4 mr-1" /> |
| 163 | + Decline |
| 164 | + </Button> |
| 165 | + </div> |
| 166 | + ) : undefined |
| 167 | + } |
| 168 | + /> |
| 169 | + ); |
| 170 | + })} |
| 171 | + </div> |
| 172 | + </div> |
| 173 | + |
| 174 | + {declineTarget && ( |
| 175 | + <DeclineGrantModal |
| 176 | + isOpen={!!declineTarget} |
| 177 | + onClose={() => setDeclineTarget(null)} |
| 178 | + onConfirm={handleDecline} |
| 179 | + isSubmitting={actioningId === declineTarget.grantId} |
| 180 | + /> |
| 181 | + )} |
| 182 | + </div> |
| 183 | + ); |
| 184 | +} |
0 commit comments