|
| 1 | +import { |
| 2 | + Box, |
| 3 | + Typography, |
| 4 | + CircularProgress, |
| 5 | + Pagination, |
| 6 | + FormControl, |
| 7 | + InputLabel, |
| 8 | + Select, |
| 9 | + MenuItem, |
| 10 | + Card, |
| 11 | + CardContent, |
| 12 | + Grid, |
| 13 | + Button, |
| 14 | +} from "@mui/material"; |
| 15 | +import { SelectChangeEvent } from "@mui/material"; |
| 16 | +import { Colors } from "design/theme"; |
| 17 | +import { useAppDispatch } from "hooks/useAppDispatch"; |
| 18 | +import { useAppSelector } from "hooks/useAppSelector"; |
| 19 | +import React, { useState, useEffect } from "react"; |
| 20 | +import { useParams, useNavigate } from "react-router-dom"; |
| 21 | +import { |
| 22 | + fetchDbInfo, |
| 23 | + loadPaginatedData, |
| 24 | +} from "redux/neurojson/neurojson.action"; |
| 25 | +import { resetData } from "redux/neurojson/neurojson.slice"; |
| 26 | +import { Row } from "redux/neurojson/types/neurojson.interface"; |
| 27 | +import RoutesEnum from "types/routes.enum"; |
| 28 | + |
| 29 | +const NewDatasetPage: React.FC = () => { |
| 30 | + const dispatch = useAppDispatch(); |
| 31 | + const navigate = useNavigate(); |
| 32 | + const { dbName } = useParams<{ dbName: string }>(); |
| 33 | + const { loading, error, data, limit } = useAppSelector( |
| 34 | + (state) => state.neurojson |
| 35 | + ); |
| 36 | + |
| 37 | + const [page, setPage] = useState(1); |
| 38 | + const [pageSize, setPageSize] = useState(10); |
| 39 | + |
| 40 | + const totalPages = Math.ceil(limit / pageSize); |
| 41 | + |
| 42 | + useEffect(() => { |
| 43 | + if (dbName) { |
| 44 | + dispatch(resetData()); |
| 45 | + dispatch(fetchDbInfo(dbName.toLowerCase())); |
| 46 | + dispatch( |
| 47 | + loadPaginatedData({ |
| 48 | + dbName: dbName.toLowerCase(), |
| 49 | + offset: (page - 1) * pageSize, |
| 50 | + limit: pageSize, |
| 51 | + }) |
| 52 | + ); |
| 53 | + } |
| 54 | + }, [dbName, page, pageSize, dispatch]); |
| 55 | + |
| 56 | + const handlePageChange = ( |
| 57 | + event: React.ChangeEvent<unknown>, |
| 58 | + value: number |
| 59 | + ) => { |
| 60 | + setPage(value); |
| 61 | + }; |
| 62 | + |
| 63 | + const handlePageSizeChange = (event: SelectChangeEvent<number>) => { |
| 64 | + setPageSize(Number(event.target.value)); |
| 65 | + setPage(1); |
| 66 | + }; |
| 67 | + |
| 68 | + return ( |
| 69 | + <Box sx={{ p: 4 }}> |
| 70 | + <Box |
| 71 | + display="flex" |
| 72 | + justifyContent="space-between" |
| 73 | + alignItems="center" |
| 74 | + mb={3} |
| 75 | + > |
| 76 | + <Box |
| 77 | + sx={{ |
| 78 | + display: "flex", |
| 79 | + flexDirection: "column", |
| 80 | + // border: "2px yellow solid", |
| 81 | + }} |
| 82 | + > |
| 83 | + <Typography sx={{ color: Colors.green, fontSize: "2rem" }}> |
| 84 | + Database: {dbName || "N/A"} |
| 85 | + </Typography> |
| 86 | + <Typography sx={{ color: Colors.lightGray, fontSize: "1.2rem" }}> |
| 87 | + Total Datasets: {limit} |
| 88 | + </Typography> |
| 89 | + </Box> |
| 90 | + |
| 91 | + <Box display="flex" alignItems="center" gap={2}> |
| 92 | + <Typography sx={{ color: Colors.lightGray }}> |
| 93 | + Datasets per Page: |
| 94 | + </Typography> |
| 95 | + <FormControl |
| 96 | + size="small" |
| 97 | + sx={{ |
| 98 | + minWidth: 140, |
| 99 | + backgroundColor: Colors.white, |
| 100 | + borderRadius: "4px", |
| 101 | + "& .MuiInputBase-input": { |
| 102 | + color: Colors.darkPurple, |
| 103 | + }, |
| 104 | + "& .MuiOutlinedInput-root": { |
| 105 | + "& fieldset": { |
| 106 | + borderColor: "none", |
| 107 | + }, |
| 108 | + "&:hover fieldset": { |
| 109 | + borderColor: Colors.darkGreen, |
| 110 | + }, |
| 111 | + "&.Mui-focused fieldset": { |
| 112 | + borderColor: Colors.darkGreen, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }} |
| 116 | + > |
| 117 | + <Select value={pageSize} onChange={handlePageSizeChange}> |
| 118 | + {[10, 25, 50, 100].map((size) => ( |
| 119 | + <MenuItem key={size} value={size}> |
| 120 | + {size} |
| 121 | + </MenuItem> |
| 122 | + ))} |
| 123 | + </Select> |
| 124 | + </FormControl> |
| 125 | + </Box> |
| 126 | + </Box> |
| 127 | + |
| 128 | + {loading ? ( |
| 129 | + <CircularProgress sx={{ display: "block", margin: "32px auto" }} /> |
| 130 | + ) : error ? ( |
| 131 | + <Typography color="error">{error}</Typography> |
| 132 | + ) : ( |
| 133 | + <> |
| 134 | + <Box> |
| 135 | + <Box |
| 136 | + sx={{ |
| 137 | + mb: 2, |
| 138 | + mt: 2, |
| 139 | + // border: "2px red solid", |
| 140 | + display: "flex", |
| 141 | + justifyContent: "center", |
| 142 | + }} |
| 143 | + > |
| 144 | + <Pagination |
| 145 | + count={totalPages} |
| 146 | + page={page} |
| 147 | + onChange={handlePageChange} |
| 148 | + showFirstButton |
| 149 | + showLastButton |
| 150 | + sx={{ |
| 151 | + "& .MuiPaginationItem-root": { |
| 152 | + color: Colors.lightGray, |
| 153 | + }, |
| 154 | + "& .MuiPaginationItem-root.Mui-selected": { |
| 155 | + backgroundColor: Colors.purple, |
| 156 | + color: Colors.lightGray, |
| 157 | + fontWeight: "bold", |
| 158 | + }, |
| 159 | + }} |
| 160 | + /> |
| 161 | + </Box> |
| 162 | + </Box> |
| 163 | + <Grid container spacing={3}> |
| 164 | + {data.map((doc: Row, idx: number) => { |
| 165 | + const index = (page - 1) * pageSize + idx + 1; |
| 166 | + return ( |
| 167 | + <Grid item xs={12} sm={6} key={doc.id}> |
| 168 | + <Card> |
| 169 | + <CardContent> |
| 170 | + <Typography |
| 171 | + variant="h6" |
| 172 | + onClick={() => |
| 173 | + navigate( |
| 174 | + `${RoutesEnum.DATABASES}/${encodeURIComponent( |
| 175 | + dbName ?? "" |
| 176 | + )}/${encodeURIComponent(doc.id ?? "")}` |
| 177 | + ) |
| 178 | + } |
| 179 | + sx={{ cursor: "pointer", color: Colors.darkPurple }} |
| 180 | + > |
| 181 | + {doc.value.name || "Untitled Dataset"} |
| 182 | + </Typography> |
| 183 | + <Typography variant="body2" color="text.secondary"> |
| 184 | + ID: {doc.id} |
| 185 | + </Typography> |
| 186 | + </CardContent> |
| 187 | + </Card> |
| 188 | + </Grid> |
| 189 | + ); |
| 190 | + })} |
| 191 | + </Grid> |
| 192 | + </> |
| 193 | + )} |
| 194 | + </Box> |
| 195 | + ); |
| 196 | +}; |
| 197 | + |
| 198 | +export default NewDatasetPage; |
0 commit comments