mirror of
https://github.com/Sea-Haven-Industries/proposal-system.git
synced 2026-10-06 04:12:03 +00:00
189 lines
7 KiB
TypeScript
189 lines
7 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';
|
||
|
|
|
||
|
|
const STATUS_COLORS: Record<string, 'default' | 'info' | 'warning' | 'success' | 'error'> = {
|
||
|
|
Draft: 'default',
|
||
|
|
InReview: 'info',
|
||
|
|
Approved: 'success',
|
||
|
|
Sent: 'success',
|
||
|
|
Revised: 'warning',
|
||
|
|
};
|
||
|
|
|
||
|
|
function formatCurrency(amount: number): string {
|
||
|
|
return new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(amount);
|
||
|
|
}
|
||
|
|
|
||
|
|
function formatDate(iso: string): string {
|
||
|
|
return new Date(iso).toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' });
|
||
|
|
}
|
||
|
|
|
||
|
|
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() {
|
||
|
|
const navigate = useNavigate();
|
||
|
|
|
||
|
|
const { data: recentData, isLoading: recentLoading } = useQuery<{ items: ProposalListItem[]; totalCount: number }>({
|
||
|
|
queryKey: [QUERY_KEYS.proposals, 'dashboard-recent'],
|
||
|
|
queryFn: () => proposalsApi.getAll({ page: 1, pageSize: 5 }),
|
||
|
|
});
|
||
|
|
|
||
|
|
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' }}
|
||
|
|
onClick={() => navigate(`/proposals/${row.id}`)}
|
||
|
|
>
|
||
|
|
<TableCell sx={{ fontWeight: 600 }}>{row.proposalNumber}</TableCell>
|
||
|
|
<TableCell>{row.customerName}</TableCell>
|
||
|
|
<TableCell>{row.serviceCategory}</TableCell>
|
||
|
|
<TableCell>
|
||
|
|
<Chip label={row.status} size="small" color={STATUS_COLORS[row.status] || 'default'} />
|
||
|
|
</TableCell>
|
||
|
|
<TableCell align="right">{formatCurrency(row.totalBidAmount)}</TableCell>
|
||
|
|
<TableCell>{formatDate(row.submittedAt)}</TableCell>
|
||
|
|
</TableRow>
|
||
|
|
))}
|
||
|
|
{!isLoading && 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>
|
||
|
|
);
|
||
|
|
}
|