Skip to content

Commit eaeec68

Browse files
tcnichollongshuicy
andauthored
add my datasets tab (#965)
* adding new tab for my datasets, does not work yet * new tab * pagination should be fixed for my datasets tab * admin mode should still just return 'mine' * using just listDatasets. need to remove unnecessary 'myDatasets' * removing mydatasets * removing mydatasets * adding state and actions for my datasets * changing tab back to 'datasets' from 'my datasets' needs to change the value of 'mine' and list datasets * just need an else * previous is no longer selectable when moving to my datasets. we start on the first page * switch to first page when switch to 'datasets' * run precommit * combine useEffect logic --------- Co-authored-by: Chen Wang <[email protected]>
1 parent f365060 commit eaeec68

File tree

6 files changed

+100
-4
lines changed

6 files changed

+100
-4
lines changed

backend/app/routers/datasets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ async def get_datasets(
219219
admin=Depends(get_admin),
220220
admin_mode: bool = Depends(get_admin_mode),
221221
):
222-
if admin and admin_mode:
222+
if admin and admin_mode and not mine:
223223
datasets_and_count = await DatasetDBViewList.aggregate(
224224
[_get_page_query(skip, limit, sort_field="created", ascending=False)],
225225
).to_list()

frontend/src/actions/dataset.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,25 @@ export function fetchDatasets(skip = 0, limit = 21, mine = false) {
261261
};
262262
}
263263

264+
export const RECEIVE_MY_DATASETS = "RECEIVE_MY_DATASETS";
265+
266+
export function fetchMyDatasets(skip = 0, limit = 21, mine = true) {
267+
return (dispatch) => {
268+
// TODO: Parameters for dates? paging?
269+
return V2.DatasetsService.getDatasetsApiV2DatasetsGet(skip, limit, mine)
270+
.then((json) => {
271+
dispatch({
272+
type: RECEIVE_MY_DATASETS,
273+
datasets: json,
274+
receivedAt: Date.now(),
275+
});
276+
})
277+
.catch((reason) => {
278+
dispatch(handleErrors(reason, fetchMyDatasets(skip, limit, mine)));
279+
});
280+
};
281+
}
282+
264283
export const CREATE_DATASET = "CREATE_DATASET";
265284

266285
export function datasetCreated(formData, licenseId, licenseFormData) {

frontend/src/components/Explore.tsx

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,30 @@ export const Explore = (): JSX.Element => {
4343
const [currPageNum, setCurrPageNum] = useState<number>(1);
4444
const [limit] = useState<number>(config.defaultDatasetPerPage);
4545
// TODO add switch to turn on and off "mine" dataset
46-
const [mine] = useState<boolean>(false);
46+
const [mine, setMine] = useState<boolean>(false);
4747
const [selectedTabIndex, setSelectedTabIndex] = useState(0);
4848
const [errorOpen, setErrorOpen] = useState(false);
4949

5050
// Admin mode will fetch all datasets
5151
useEffect(() => {
52-
listDatasets((currPageNum - 1) * limit, limit, mine);
53-
}, [adminMode, deletedDataset]);
52+
if (adminMode) listDatasets(0, limit, true);
53+
else listDatasets((currPageNum - 1) * limit, limit, mine);
54+
}, [adminMode, deletedDataset, mine, currPageNum, limit]);
5455

5556
// switch tabs
5657
const handleTabChange = (
5758
_event: React.ChangeEvent<{}>,
5859
newTabIndex: number
5960
) => {
61+
if (newTabIndex === 1) {
62+
setMine(true);
63+
setCurrPageNum(1);
64+
listDatasets(0, limit, true);
65+
} else {
66+
setMine(false);
67+
setCurrPageNum(1);
68+
listDatasets(0, limit, false);
69+
}
6070
setSelectedTabIndex(newTabIndex);
6171
};
6272

@@ -80,6 +90,7 @@ export const Explore = (): JSX.Element => {
8090
aria-label="dashboard tabs"
8191
>
8292
<Tab sx={tab} label="Datasets" {...a11yProps(0)} />
93+
<Tab sx={tab} label="My Datasets" {...a11yProps(1)} />
8394
</Tabs>
8495
</Box>
8596
<TabPanel value={selectedTabIndex} index={0}>
@@ -137,6 +148,61 @@ export const Explore = (): JSX.Element => {
137148
<></>
138149
)}
139150
</TabPanel>
151+
<TabPanel value={selectedTabIndex} index={1}>
152+
<Grid container spacing={2}>
153+
{datasets !== undefined ? (
154+
datasets.map((dataset: DatasetOut) => {
155+
return (
156+
<Grid item key={dataset.id} xs={12} sm={6} md={4} lg={3}>
157+
<DatasetCard
158+
id={dataset.id}
159+
name={dataset.name}
160+
author={`${dataset.creator.first_name} ${dataset.creator.last_name}`}
161+
created={dataset.created}
162+
description={dataset.description}
163+
thumbnailId={dataset.thumbnail_id}
164+
/>
165+
</Grid>
166+
);
167+
})
168+
) : (
169+
<></>
170+
)}
171+
{datasets.length === 0 ? (
172+
<Grid container justifyContent="center">
173+
<Box textAlign="center">
174+
<p>
175+
Nobody has created any datasets on this instance. Click
176+
below to create a dataset!
177+
</p>
178+
<Button
179+
component={RouterLink}
180+
to="/create-dataset"
181+
variant="contained"
182+
sx={{ m: 2 }}
183+
>
184+
Create Dataset
185+
</Button>
186+
</Box>
187+
</Grid>
188+
) : (
189+
<></>
190+
)}
191+
</Grid>
192+
{datasets.length !== 0 ? (
193+
<Box display="flex" justifyContent="center" sx={{ m: 1 }}>
194+
<Pagination
195+
count={Math.ceil(pageMetadata.total_count / limit)}
196+
page={currPageNum}
197+
onChange={handlePageChange}
198+
shape="rounded"
199+
variant="outlined"
200+
/>
201+
</Box>
202+
) : (
203+
<></>
204+
)}
205+
</TabPanel>
140206
<TabPanel value={selectedTabIndex} index={4} />
141207
<TabPanel value={selectedTabIndex} index={2} />
142208
<TabPanel value={selectedTabIndex} index={3} />

frontend/src/reducers/dataset.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
RECEIVE_DATASET_ROLES,
99
RECEIVE_DATASETS,
1010
RECEIVE_FOLDERS_FILES_IN_DATASET,
11+
RECEIVE_MY_DATASETS,
1112
REMOVE_DATASET_GROUP_ROLE,
1213
REMOVE_DATASET_USER_ROLE,
1314
RESET_CREATE_DATASET,
@@ -51,6 +52,7 @@ const defaultState: DatasetState = {
5152
about: <Dataset>{ creator: <UserOut>{} },
5253
datasetRole: <AuthorizationBase>{},
5354
datasets: <Paged>{ metadata: <PageMetadata>{}, data: <Dataset[]>[] },
55+
myDatasets: <Paged>{ metadata: <PageMetadata>{}, data: <Dataset[]>[] },
5456
newDataset: <Dataset>{},
5557
newFile: <FileOut>{},
5658
newFiles: <FileOut[]>[],
@@ -112,6 +114,8 @@ const dataset = (state = defaultState, action: DataAction) => {
112114
return Object.assign({}, state, { about: action.about });
113115
case RECEIVE_DATASETS:
114116
return Object.assign({}, state, { datasets: action.datasets });
117+
case RECEIVE_MY_DATASETS:
118+
return Object.assign({}, state, { myDatasets: action.myDatasets });
115119
case CREATE_DATASET:
116120
return Object.assign({}, state, { newDataset: action.dataset });
117121
case RESET_CREATE_DATASET:

frontend/src/types/action.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ interface RECEIVE_DATASETS {
8888
datasets: Paged;
8989
}
9090

91+
interface RECEIVE_MY_DATASETS {
92+
type: "RECEIVE_MY_DATASETS";
93+
myDatasets: Paged;
94+
}
95+
9196
interface RECEIVE_PUBLIC_DATASETS {
9297
type: "RECEIVE_PUBLIC_DATASETS";
9398
publicDatasets: Dataset[];
@@ -661,6 +666,7 @@ export type DataAction =
661666
| RECEIVE_DATASET_ABOUT
662667
| RECEIVE_DATASET_ROLE
663668
| RECEIVE_DATASETS
669+
| RECEIVE_MY_DATASETS
664670
| RECEIVE_PUBLIC_DATASETS
665671
| RECEIVE_PUBLIC_DATASET_ABOUT
666672
| RECEIVE_FILES_IN_PUBLIC_DATASET

frontend/src/types/data.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ export interface DatasetState {
123123
files: Paged;
124124
folders: Paged;
125125
datasets: Paged;
126+
myDatasets: Paged;
126127
deletedDataset: DatasetOut;
127128
deletedFolder: FolderOut;
128129
deletedFile: FileOut;

0 commit comments

Comments
 (0)