-
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
360 - Refactor Dataset Page to handle dataset ID via URL query #364
Changes from 4 commits
ca6e71a
978bc36
1dab255
679e50c
cb76a18
6cdcb00
4fba955
41e8260
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you give me feedback on this suggestion?
In the future we may consider makeing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for these great suggestions 👍 I totally agree with keeping the prop as |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -7,26 +7,30 @@ import { Button } from 'components/shared/Button' | |||||
import { Modal } from 'components/shared/Modal' | ||||||
import { MoveToDatasetModal } from './MoveToDatasetModal' | ||||||
|
||||||
export const MoveToDatasetButton = ({ dataset, disabled, selectedDataset }) => { | ||||||
export const MoveToDatasetButton = ({ dataset }) => { | ||||||
const { push } = useRouter() | ||||||
const { addSamples, getTotalSamples } = useDatasetManager() | ||||||
const { | ||||||
dataset: { id: myDatasetId, data: myDatasetData }, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
And then access the attributes in the same way so that it is consistent. |
||||||
addSamples, | ||||||
getTotalSamples | ||||||
} = useDatasetManager() | ||||||
const { openModal, closeModal } = useModal() | ||||||
const id = `move-to-dataset-${dataset?.id}` | ||||||
const id = `move-to-dataset-${myDatasetId}` | ||||||
const radioOptions = [ | ||||||
{ label: 'Append samples to My Dataset', value: 'append' }, | ||||||
{ label: 'Replace samples in My Dataset', value: 'replace' } | ||||||
] | ||||||
const defaultValue = radioOptions[0].value | ||||||
const [value, setValue] = useState(defaultValue) | ||||||
const newTotalSamples = getTotalSamples(selectedDataset.data) | ||||||
const totalSamples = getTotalSamples(dataset.data) | ||||||
const newTotalSamples = getTotalSamples(dataset.data) | ||||||
const totalSamples = getTotalSamples(myDatasetData) | ||||||
const pathname = '/download' | ||||||
|
||||||
const handleMoveToDataset = async () => { | ||||||
if (totalSamples > 0) { | ||||||
openModal(id) | ||||||
} else { | ||||||
await addSamples(selectedDataset.data) | ||||||
await addSamples(dataset.data) | ||||||
push( | ||||||
{ | ||||||
pathname, | ||||||
|
@@ -47,7 +51,6 @@ export const MoveToDatasetButton = ({ dataset, disabled, selectedDataset }) => { | |||||
id={id} | ||||||
button={ | ||||||
<Button | ||||||
disabled={disabled} | ||||||
label="Move to Dataset" | ||||||
secondary | ||||||
responsive | ||||||
|
@@ -61,7 +64,7 @@ export const MoveToDatasetButton = ({ dataset, disabled, selectedDataset }) => { | |||||
id={id} | ||||||
closeModal={closeModal} | ||||||
defaultValue={defaultValue} | ||||||
dataset={selectedDataset} | ||||||
dataset={dataset} | ||||||
pathname={pathname} | ||||||
radioOptions={radioOptions} | ||||||
value={value} | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import { useEffect, useState } from 'react' | ||
import { useRouter } from 'next/router' | ||
import { Box } from 'grommet' | ||
import { useDatasetManager } from 'hooks/useDatasetManager' | ||
import { usePageRendered } from 'hooks/usePageRendered' | ||
|
@@ -25,48 +26,48 @@ export const getServerSideProps = ({ query }) => { | |
return { props: { query } } | ||
} | ||
|
||
export const Dataset = ({ query }) => { | ||
export const Dataset = ({ query: { dataset_id: datasetId, start } }) => { | ||
const pageRendered = usePageRendered() | ||
const { push } = useRouter() | ||
const { setResponsive } = useResponsive() | ||
const { dataset_id: idFromQuery, start } = query | ||
const { dataset, datasetId, error, loading, getDataset, regeneratedDataset } = | ||
useDatasetManager() | ||
const { | ||
datasetId: myDatasetId, | ||
error, | ||
loading, | ||
getDataset | ||
} = useDatasetManager() | ||
const { isProcessingDataset, polledDatasetState, pollDatasetId } = | ||
usePollDatasetStatus() | ||
const [selectedDataset, setSelectedDataset] = useState({}) // stores the dataset currently displayed on the page | ||
const isProcessed = selectedDataset?.is_processed && selectedDataset?.success // sets visibility of the download options in Dwonload Files Summary | ||
const isUnprocessedDataset = // sets visibility of the Download Dataset button | ||
!selectedDataset?.is_processing && | ||
!selectedDataset?.is_processed && | ||
selectedDataset?.success !== false | ||
const [dataset, setDataset] = useState({}) // dataset currently displayed on the page | ||
|
||
const getSelectedDataset = async (id) => { | ||
const getDatasetFromQuery = async (id) => { | ||
const response = await getDataset(id) | ||
setSelectedDataset(response) | ||
setDataset(response) | ||
} | ||
|
||
useEffect(() => { | ||
getSelectedDataset(idFromQuery) | ||
pollDatasetId(idFromQuery) // sets a processing datasets for polling | ||
}, [query]) | ||
// redirects users to /download if datasetId matches My dataset ID | ||
if (datasetId === myDatasetId) push('/download') | ||
|
||
getDatasetFromQuery(datasetId) | ||
pollDatasetId(datasetId) // sets a processing datasets for polling | ||
}, [datasetId]) | ||
|
||
useEffect(() => { | ||
// swaps selectedDataset to the last fetched polledDatasetState | ||
// swaps dataset to the last fetched polledDatasetState | ||
// (only if the dataset ID in URL was being processed) to update | ||
// DatasetPageHeader | ||
if (!isProcessingDataset && polledDatasetState) { | ||
setSelectedDataset(polledDatasetState) | ||
setDataset(polledDatasetState) | ||
} | ||
}, [isProcessingDataset, polledDatasetState]) | ||
|
||
if (!pageRendered) return null | ||
|
||
const isSameId = datasetId === idFromQuery | ||
|
||
if (start) { | ||
return ( | ||
<FixedContainer> | ||
<StartProcessing dataset={selectedDataset} /> | ||
<StartProcessing dataset={dataset} /> | ||
</FixedContainer> | ||
) | ||
} | ||
|
@@ -82,52 +83,41 @@ export const Dataset = ({ query }) => { | |
) | ||
} | ||
|
||
if (loading) { | ||
return ( | ||
<Box align="center" fill justify="center" margin={{ top: 'large' }}> | ||
<Spinner /> | ||
</Box> | ||
) | ||
} | ||
|
||
return ( | ||
<FixedContainer> | ||
{loading ? ( | ||
<Box align="center" fill justify="center" margin={{ top: 'large' }}> | ||
<Spinner /> | ||
</Box> | ||
) : ( | ||
<Box> | ||
{selectedDataset?.data && ( | ||
<> | ||
<DatasetPageHeader dataset={selectedDataset} /> | ||
<Box> | ||
{dataset?.data && ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can get rid of this check as well there should only be 2 states we care about. Loading and loaded. I would think that if dataset.data is undefined then loading would be true. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It has been cleaned up in 41e8260 👍 |
||
<> | ||
<DatasetPageHeader dataset={dataset} /> | ||
<Row | ||
border={{ side: 'bottom' }} | ||
pad={{ bottom: setResponsive('medium', 'small') }} | ||
> | ||
<Box> | ||
<MoveToDatasetButton dataset={dataset} /> | ||
</Box> | ||
<Row | ||
border={{ side: 'bottom' }} | ||
pad={{ bottom: setResponsive('medium', 'small') }} | ||
gap={setResponsive('medium', 'small')} | ||
margin={{ top: setResponsive('medium', 'none') }} | ||
> | ||
<Box> | ||
<MoveToDatasetButton | ||
dataset={dataset} | ||
selectedDataset={selectedDataset} | ||
disabled={isSameId} | ||
/> | ||
</Box> | ||
<Row | ||
gap={setResponsive('medium', 'small')} | ||
margin={{ top: setResponsive('medium', 'none') }} | ||
> | ||
<ShareDatasetButton dataset={selectedDataset} /> | ||
{isUnprocessedDataset && ( | ||
<DownloadDatasetButton dataset={selectedDataset} /> | ||
)} | ||
</Row> | ||
<ShareDatasetButton dataset={dataset} /> | ||
<DownloadDatasetButton dataset={dataset} /> | ||
</Row> | ||
<FilesSummary | ||
dataset={regeneratedDataset || selectedDataset} | ||
defaultDataset={selectedDataset} | ||
isProcessed={isProcessed} | ||
/> | ||
<DatasetSummary dataset={regeneratedDataset || selectedDataset} /> | ||
<DatasetDetails | ||
dataset={regeneratedDataset || selectedDataset} | ||
isImmutable | ||
/> | ||
</> | ||
)} | ||
</Box> | ||
)} | ||
</Row> | ||
<FilesSummary dataset={dataset} /> | ||
<DatasetSummary dataset={dataset} /> | ||
<DatasetDetails dataset={dataset} isImmutable /> | ||
</> | ||
)} | ||
</Box> | ||
</FixedContainer> | ||
) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should replace this with a helper that wraps the actual meaning of these state attributes. They are not the clearest names on the API. We can't really change them very easily there because external scripts might be using them so here we should just abstract away the specifics into the actionable states.
I think you can clean this up but the important part is that logically only one of these states can be true at one #time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
David, as chat on Slack (and noted in here as well #364 (comment)), I've filed a dedicated new issue (#370), for adding
getDatasetState
helper and refactoring the components that use dataset states.This should make the review easier while keeping the file changes minimal and reducing the merge conflicts (the PR for it is also ready (#373). Thank you David!