-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
Refactor Sample Data Home section which also fixes flaky sample data functional test #21655
Changes from 5 commits
04b9511
53e48eb
d8a46d7
2c929c2
a74c4cf
9a62025
85b5f4b
75de383
ef9db61
38eef0b
52ab6df
7f1f367
85aace1
4a5f9cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,23 +33,32 @@ import { | |
uninstallSampleDataSet | ||
} from '../sample_data_sets'; | ||
|
||
const INSTALLED_STATUS = 'installed'; | ||
const UNINSTALLED_STATUS = 'not_installed'; | ||
|
||
export class SampleDataSetCard extends React.Component { | ||
|
||
constructor(props) { | ||
super(props); | ||
|
||
this.state = { | ||
isProcessingRequest: false, | ||
status: this.props.status, | ||
}; | ||
} | ||
|
||
static getDerivedStateFromProps(nextProps) { | ||
return { | ||
status: nextProps.status, | ||
}; | ||
} | ||
|
||
startRequest = async () => { | ||
install = async () => { | ||
const { | ||
getConfig, | ||
setConfig, | ||
id, | ||
name, | ||
onRequestComplete, | ||
defaultIndex, | ||
clearIndexPatternsCache, | ||
} = this.props; | ||
|
@@ -58,36 +67,53 @@ export class SampleDataSetCard extends React.Component { | |
isProcessingRequest: true, | ||
}); | ||
|
||
if (this.isInstalled()) { | ||
await uninstallSampleDataSet(id, name, defaultIndex, getConfig, setConfig, clearIndexPatternsCache); | ||
} else { | ||
await installSampleDataSet(id, name, defaultIndex, getConfig, setConfig, clearIndexPatternsCache); | ||
} | ||
const isSuccess = await installSampleDataSet(id, name, defaultIndex, getConfig, setConfig, clearIndexPatternsCache); | ||
|
||
this.setState({ | ||
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. accessing "this" here and below after an async call, i think we need the isMounted checks. |
||
isProcessingRequest: false, | ||
status: isSuccess ? INSTALLED_STATUS : UNINSTALLED_STATUS | ||
}); | ||
} | ||
|
||
uninstall = async () => { | ||
const { | ||
getConfig, | ||
setConfig, | ||
id, | ||
name, | ||
defaultIndex, | ||
clearIndexPatternsCache, | ||
} = this.props; | ||
|
||
this.setState({ | ||
isProcessingRequest: 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. This is just another status. Can we merge the props into one? Status = PROCESSING, INSTALLED, UNINSTALLED. and I guess maybe UNKOWN? How do I simulate UNKNOWN? I can't seem to trigger it. I think even if you handle the isMounted checks below you can get into a weird state by hitting 'Add' or 'Remove' the quickly switching tabs and going back. isProcessingREquest is state on this button so that gets lost but the request is still processing. Then I think you will end up with indicating the wrong status. 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.
|
||
}); | ||
|
||
onRequestComplete(); | ||
const isSuccess = await uninstallSampleDataSet(id, name, defaultIndex, getConfig, setConfig, clearIndexPatternsCache); | ||
|
||
this.setState({ | ||
isProcessingRequest: false, | ||
status: isSuccess ? UNINSTALLED_STATUS : INSTALLED_STATUS | ||
}); | ||
} | ||
|
||
isInstalled = () => { | ||
if (this.props.status === 'installed') { | ||
if (this.state.status === 'installed') { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
renderBtn = () => { | ||
switch (this.props.status) { | ||
case 'installed': | ||
switch (this.state.status) { | ||
case INSTALLED_STATUS: | ||
return ( | ||
<EuiFlexGroup justifyContent="spaceBetween"> | ||
<EuiFlexItem grow={false}> | ||
<EuiButtonEmpty | ||
isLoading={this.state.isProcessingRequest} | ||
onClick={this.startRequest} | ||
onClick={this.uninstall} | ||
color="danger" | ||
data-test-subj={`removeSampleDataSet${this.props.id}`} | ||
> | ||
|
@@ -105,13 +131,13 @@ export class SampleDataSetCard extends React.Component { | |
</EuiFlexGroup> | ||
); | ||
|
||
case 'not_installed': | ||
case UNINSTALLED_STATUS: | ||
return ( | ||
<EuiFlexGroup justifyContent="flexEnd"> | ||
<EuiFlexItem grow={false}> | ||
<EuiButton | ||
isLoading={this.state.isProcessingRequest} | ||
onClick={this.startRequest} | ||
onClick={this.install} | ||
data-test-subj={`addSampleDataSet${this.props.id}`} | ||
> | ||
{this.state.isProcessingRequest ? 'Adding' : 'Add'} | ||
|
@@ -162,12 +188,11 @@ SampleDataSetCard.propTypes = { | |
name: PropTypes.string.isRequired, | ||
launchUrl: PropTypes.string.isRequired, | ||
status: PropTypes.oneOf([ | ||
'installed', | ||
'not_installed', | ||
INSTALLED_STATUS, | ||
UNINSTALLED_STATUS, | ||
'unknown', | ||
]).isRequired, | ||
statusMsg: PropTypes.string, | ||
onRequestComplete: PropTypes.func.isRequired, | ||
getConfig: PropTypes.func.isRequired, | ||
setConfig: PropTypes.func.isRequired, | ||
clearIndexPatternsCache: PropTypes.func.isRequired, | ||
|
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 guess these are returned from the objects in
listSampleDataSets
? Our folder structure of core plugins makes it difficult to push common consts in an intuitive place (the server side code of this is far away). If we typescripted this and the files in../sample_data_sets
we could at least show that the object being returned has astatus
and is one ofinstalled
ornot_installed
. Suppose that's fine for this PR but I think it'd be really helpful to understand and follow this code if it was in typescript.