mirror of
https://github.com/Sea-Haven-Industries/proposal-system.git
synced 2026-09-30 07:43:14 +00:00
GetStatsAsync now returns global counts for admins/sysadmins instead of filtering by submitter. Dashboard recent proposals query uses mine=false for admins so they see all proposals, not just their own.
188 lines
7.4 KiB
TypeScript
188 lines
7.4 KiB
TypeScript
import { useNavigate } from 'react-router-dom';
|
|
import { useQuery } from '@tanstack/react-query';
|
|
import {
|
|
Box,
|
|
Typography,
|
|
Card,
|
|
CardContent,
|
|
Grid,
|
|
Table,
|
|
TableBody,
|
|
TableCell,
|
|
TableContainer,
|
|
TableHead,
|
|
TableRow,
|
|
Chip,
|
|
Button,
|
|
Skeleton,
|
|
} from '@mui/material';
|
|
import AddCircleIcon from '@mui/icons-material/AddCircle';
|
|
import DescriptionIcon from '@mui/icons-material/Description';
|
|
import HourglassTopIcon from '@mui/icons-material/HourglassTop';
|
|
import CheckCircleIcon from '@mui/icons-material/CheckCircle';
|
|
import SendIcon from '@mui/icons-material/Send';
|
|
import { proposalsApi, type ProposalListItem, type ProposalStats } from '../../lib/api/proposals';
|
|
import { QUERY_KEYS } from '../../constants/queryKeys';
|
|
import { STATUS_COLORS, STATUS_LABELS, PRIORITY_ROW_SX } from '../../constants';
|
|
import { formatBidAmount, formatDate } from '../../lib/format';
|
|
import { useDocumentTitle } from '../../hooks/useDocumentTitle';
|
|
import { useAuth } from '../../hooks/useAuth';
|
|
|
|
function KpiCard({ icon, label, value }: { icon: React.ReactNode; label: string; value: string }) {
|
|
return (
|
|
<Card sx={{ borderRadius: '8px', transition: 'transform 0.2s, box-shadow 0.2s', '&:hover': { transform: 'translateY(-2px)', boxShadow: '0 4px 8px rgba(0,0,0,0.15)' } }}>
|
|
<CardContent sx={{ textAlign: 'center', py: 2.5 }}>
|
|
<Box sx={{ display: 'flex', justifyContent: 'center', mb: 1, color: '#0c4f6f' }}>{icon}</Box>
|
|
<Typography sx={{ fontSize: '12px', color: '#666', textTransform: 'uppercase', letterSpacing: '0.5px', mb: 1 }}>
|
|
{label}
|
|
</Typography>
|
|
<Typography sx={{ fontSize: '32px', fontWeight: 700, color: '#003a5d' }}>
|
|
{value}
|
|
</Typography>
|
|
</CardContent>
|
|
</Card>
|
|
);
|
|
}
|
|
|
|
export default function Dashboard() {
|
|
useDocumentTitle('Dashboard');
|
|
const navigate = useNavigate();
|
|
const { user } = useAuth();
|
|
const isAdmin = user?.role === 'Admin' || user?.role === 'SysAdmin';
|
|
|
|
const { data: recentData, isLoading: recentLoading, error: recentError } = useQuery<{ items: ProposalListItem[]; totalCount: number }>({
|
|
queryKey: [QUERY_KEYS.proposals, 'dashboard-recent', isAdmin],
|
|
queryFn: () => proposalsApi.getAll({ page: 1, pageSize: 5, mine: !isAdmin }),
|
|
});
|
|
|
|
const { data: stats, isLoading: statsLoading } = useQuery<ProposalStats>({
|
|
queryKey: [QUERY_KEYS.proposals, 'stats'],
|
|
queryFn: () => proposalsApi.getStats(),
|
|
});
|
|
|
|
const isLoading = recentLoading || statsLoading;
|
|
const proposals = recentData?.items ?? [];
|
|
const total = stats?.totalCount ?? 0;
|
|
const counts = {
|
|
inReview: stats?.inReviewCount ?? 0,
|
|
approved: stats?.approvedCount ?? 0,
|
|
sent: stats?.sentCount ?? 0,
|
|
};
|
|
|
|
return (
|
|
<Box>
|
|
<Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', mb: 2 }}>
|
|
<Typography variant="h5">Dashboard</Typography>
|
|
<Button
|
|
variant="contained"
|
|
startIcon={<AddCircleIcon />}
|
|
onClick={() => navigate('/proposals/new')}
|
|
>
|
|
New Proposal
|
|
</Button>
|
|
</Box>
|
|
|
|
<Grid container spacing={2} sx={{ mb: 3 }}>
|
|
<Grid size={{ xs: 6, sm: 3 }}>
|
|
{isLoading ? (
|
|
<Skeleton variant="rectangular" height={120} sx={{ borderRadius: 2 }} />
|
|
) : (
|
|
<KpiCard icon={<DescriptionIcon fontSize="large" />} label="Total Submitted" value={String(total)} />
|
|
)}
|
|
</Grid>
|
|
<Grid size={{ xs: 6, sm: 3 }}>
|
|
{isLoading ? (
|
|
<Skeleton variant="rectangular" height={120} sx={{ borderRadius: 2 }} />
|
|
) : (
|
|
<KpiCard icon={<HourglassTopIcon fontSize="large" />} label="In Review" value={String(counts.inReview)} />
|
|
)}
|
|
</Grid>
|
|
<Grid size={{ xs: 6, sm: 3 }}>
|
|
{isLoading ? (
|
|
<Skeleton variant="rectangular" height={120} sx={{ borderRadius: 2 }} />
|
|
) : (
|
|
<KpiCard icon={<CheckCircleIcon fontSize="large" />} label="Approved" value={String(counts.approved)} />
|
|
)}
|
|
</Grid>
|
|
<Grid size={{ xs: 6, sm: 3 }}>
|
|
{isLoading ? (
|
|
<Skeleton variant="rectangular" height={120} sx={{ borderRadius: 2 }} />
|
|
) : (
|
|
<KpiCard icon={<SendIcon fontSize="large" />} label="Sent" value={String(counts.sent)} />
|
|
)}
|
|
</Grid>
|
|
</Grid>
|
|
|
|
<Card>
|
|
<CardContent sx={{ pb: 1 }}>
|
|
<Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', mb: 2 }}>
|
|
<Typography variant="h6">Recent Proposals</Typography>
|
|
<Button size="small" onClick={() => navigate('/proposals')}>
|
|
View All
|
|
</Button>
|
|
</Box>
|
|
|
|
<TableContainer>
|
|
<Table size="small">
|
|
<TableHead>
|
|
<TableRow>
|
|
<TableCell>Proposal #</TableCell>
|
|
<TableCell>Customer</TableCell>
|
|
<TableCell>Category</TableCell>
|
|
<TableCell>Status</TableCell>
|
|
<TableCell align="right">Bid Amount</TableCell>
|
|
<TableCell>Submitted</TableCell>
|
|
</TableRow>
|
|
</TableHead>
|
|
<TableBody>
|
|
{isLoading
|
|
? Array.from({ length: 5 }).map((_, i) => (
|
|
<TableRow key={i}>
|
|
{Array.from({ length: 6 }).map((_, j) => (
|
|
<TableCell key={j}><Skeleton variant="text" /></TableCell>
|
|
))}
|
|
</TableRow>
|
|
))
|
|
: proposals.map((row) => (
|
|
<TableRow
|
|
key={row.id}
|
|
hover
|
|
sx={{ cursor: 'pointer', ...PRIORITY_ROW_SX[row.priority] }}
|
|
onClick={() => navigate(`/proposals/${row.id}`)}
|
|
>
|
|
<TableCell sx={{ fontWeight: 600 }}>{row.proposalNumber}</TableCell>
|
|
<TableCell>{row.customerName}</TableCell>
|
|
<TableCell>{row.serviceCategory}</TableCell>
|
|
<TableCell>
|
|
<Chip label={STATUS_LABELS[row.status] || row.status} size="small" color={STATUS_COLORS[row.status] || 'default'} />
|
|
</TableCell>
|
|
<TableCell align="right">{formatBidAmount(row.totalBidAmount)}</TableCell>
|
|
<TableCell>{formatDate(row.submittedAt)}</TableCell>
|
|
</TableRow>
|
|
))}
|
|
{!isLoading && recentError && (
|
|
<TableRow>
|
|
<TableCell colSpan={6} align="center" sx={{ py: 4 }}>
|
|
<Typography color="error">
|
|
Failed to load proposals. Please try again.
|
|
</Typography>
|
|
</TableCell>
|
|
</TableRow>
|
|
)}
|
|
{!isLoading && !recentError && proposals.length === 0 && (
|
|
<TableRow>
|
|
<TableCell colSpan={6} align="center" sx={{ py: 4 }}>
|
|
<Typography color="text.secondary">
|
|
No proposals yet. Submit your first one!
|
|
</Typography>
|
|
</TableCell>
|
|
</TableRow>
|
|
)}
|
|
</TableBody>
|
|
</Table>
|
|
</TableContainer>
|
|
</CardContent>
|
|
</Card>
|
|
</Box>
|
|
);
|
|
}
|