All files / pages/admin AdminFundingRequestsPage.tsx

77.27% Statements 17/22
75% Branches 18/24
87.5% Functions 7/8
94.44% Lines 17/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156                                                  6x 6x 6x 6x       6x 6x 6x 6x               7x   7x 1x     7x                 1x                                         10x 10x   10x                                                                                 6x                                                                
import {type ReactElement} from 'react';
import {
	Box,
	Button,
	Chip,
	CircularProgress,
	Container,
	Paper,
	Stack,
	Table,
	TableBody,
	TableCell,
	TableContainer,
	TableHead,
	TableRow,
	Typography
} from '@mui/material';
 
import {
	type AdminFundingRequest,
	useGetPendingFundingRequestsQuery,
	useUpdateFundingRequestStatusMutation
} from '../../services/fundingApi';
 
function formatMoney(value: string | undefined): string {
	Iif (value === undefined) return '—';
	const num = Number.parseFloat(value);
	Iif (Number.isNaN(num)) return value;
	return num.toLocaleString('en-US', {style: 'currency', currency: 'USD'});
}
 
function formatDate(value: string | undefined): string {
	Iif (value === undefined) return '—';
	const d = new Date(value);
	Iif (Number.isNaN(d.getTime())) return value;
	return d.toLocaleDateString('en-US');
}
 
interface RowActionsProps {
	request: AdminFundingRequest;
}
 
function RowActions({request}: RowActionsProps): ReactElement {
	const [updateStatus, {isLoading}] = useUpdateFundingRequestStatusMutation();
 
	const act = (status: 'completed' | 'rejected'): void => {
		void updateStatus({fundingRequestId: request.fundingRequestId, status});
	};
 
	return (
        <Stack direction="row" spacing={1} sx={{
            justifyContent: "center"
        }}>
            <Button
				size="small"
				variant="contained"
				disabled={isLoading}
				onClick={(): void => {
					act('completed');
				}}
			>
				Mark Completed
			</Button>
            <Button
				size="small"
				variant="outlined"
				color="error"
				disabled={isLoading}
				onClick={(): void => {
					act('rejected');
				}}
			>
				Reject
			</Button>
        </Stack>
    );
}
 
export default function AdminFundingRequestsPage(): ReactElement {
	const {data: pending, isLoading} = useGetPendingFundingRequestsQuery();
	const rows: AdminFundingRequest[] = pending ?? [];
 
	return (
        <Container maxWidth="lg" sx={{py: 4}}>
            <Typography variant="h4" gutterBottom>
				Funding Requests
			</Typography>
            <Typography
                variant="body2"
                sx={{
                    color: "text.secondary",
                    mb: 3
                }}>
				Investor-initiated deposit (&ldquo;Invest&rdquo;) and withdrawal requests awaiting
				action. Process the money movement by wire/bank, record the transaction, then mark
				the request completed (or reject it if it isn&apos;t proceeding).
			</Typography>
            <Paper>
				{isLoading ? (
					<Box sx={{display: 'flex', justifyContent: 'center', py: 4}}>
						<CircularProgress/>
					</Box>
				) : rows.length === 0 ? (
					<Box sx={{textAlign: 'center', py: 4}}>
						<Typography sx={{
                            color: "text.secondary"
                        }}>No funding requests pending.</Typography>
					</Box>
				) : (
					<TableContainer>
						<Table>
							<TableHead>
								<TableRow>
									<TableCell>Investor</TableCell>
									<TableCell>Type</TableCell>
									<TableCell align="right">Amount</TableCell>
									<TableCell>Note</TableCell>
									<TableCell>Submitted</TableCell>
									<TableCell align="center">Actions</TableCell>
								</TableRow>
							</TableHead>
							<TableBody>
								{rows.map((r) => (
									<TableRow key={r.fundingRequestId}>
										<TableCell>
											<Typography variant="body2">{r.investorName ?? '—'}</Typography>
											<Typography variant="caption" sx={{
                                                color: "text.secondary"
                                            }}>
												{r.investorEmail ?? ''}
											</Typography>
										</TableCell>
										<TableCell>
											<Chip
												label={r.requestType === 'deposit' ? 'Invest' : 'Withdrawal'}
												size="small"
												color={r.requestType === 'deposit' ? 'success' : 'warning'}
											/>
										</TableCell>
										<TableCell align="right">{formatMoney(r.amount)}</TableCell>
										<TableCell>{r.note ?? '—'}</TableCell>
										<TableCell>{formatDate(r.createdAt)}</TableCell>
										<TableCell align="center">
											<RowActions request={r}/>
										</TableCell>
									</TableRow>
								))}
							</TableBody>
						</Table>
					</TableContainer>
				)}
			</Paper>
        </Container>
    );
}