-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(RenameDataset): add modal to rename dataset
Since we already had a pretty slick way to rename a dataset coded up in `DatasetReference` , I decided to just generalize/add functionality that would allow us to use the `DatasetReferenceComponent` as a part of the `RenameModal`. This involved adding optional functions that could inject additional functionality at key points: when the dataset was marked valid/invalid, when the user made any change to the text. The `onSubmit` function became optional, if you add an `onSubmit` function it will submit when the user hits `enter`, otherwise, you control the submitting outside of the component, which is exactly what we do now in the `RenameDataset` modal: we use the `onChange` function to keep track of what the user is typing, and the `isValid` component to keep track of any errors, displaying them to the user. We've also added the `RenameDataset` modal to storybook.
- Loading branch information
Showing
10 changed files
with
249 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import React, { useState } from 'react' | ||
|
||
import { dismissModal } from '../../actions/ui' | ||
import { RenameDatasetModal } from '../../../app/models/modals' | ||
import { | ||
selectModal, | ||
selectDatasetRef | ||
} from '../../selections' | ||
import { ApiAction } from '../../store/api' | ||
import { connectComponentToProps } from '../../utils/connectComponentToProps' | ||
|
||
import Modal from './Modal' | ||
import Error from './Error' | ||
import { DatasetReferenceComponent } from '../DatasetReference' | ||
import Buttons from './Buttons' | ||
import { renameDataset } from '../../actions/api' | ||
import { ValidationError } from '../../utils/formValidation' | ||
|
||
interface RenameDatasetProps { | ||
modal: RenameDatasetModal | ||
onDismissed: () => void | ||
onSubmit: (username: string, oldName: string, newName: string) => Promise<ApiAction> | ||
} | ||
|
||
export const RenameDatasetComponent: React.FC<RenameDatasetProps> = (props: RenameDatasetProps) => { | ||
const { onDismissed, onSubmit, modal } = props | ||
|
||
const [dismissable, setDismissable] = useState(true) | ||
const [error, setError] = useState<ValidationError>(null) | ||
const [loading, setLoading] = useState(false) | ||
const [newName, setNewName] = useState(modal.name) | ||
|
||
const handleSubmit = () => { | ||
setDismissable(false) | ||
setLoading(true) | ||
error && setError(null) | ||
onSubmit(modal.username, modal.name, newName).then(() => { | ||
onDismissed() | ||
}) | ||
} | ||
|
||
const handleDatasetReferenceSubmit = (newName: string) => { | ||
setNewName(newName) | ||
} | ||
|
||
return ( | ||
<Modal | ||
id='rename-dataset' | ||
title='Rename Dataset' | ||
onDismissed={onDismissed} | ||
onSubmit={() => {}} | ||
dismissable={dismissable} | ||
setDismissable={setDismissable} | ||
> | ||
<div className='content-wrap'> | ||
<div className='content'> | ||
<DatasetReferenceComponent | ||
qriRef={{ username: modal.username, name: newName || modal.name }} | ||
onChange={handleDatasetReferenceSubmit} | ||
isValid={setError} | ||
focusOnFirstRender | ||
/> | ||
<div className='margin-top'> | ||
<Error text={error} id='rename-dataset-error' /> | ||
</div> | ||
</div> | ||
</div> | ||
<Buttons | ||
cancelText='cancel' | ||
onCancel={onDismissed} | ||
submitText='rename' | ||
onSubmit={handleSubmit} | ||
disabled={modal.name === newName || !!error} | ||
loading={loading} | ||
/> | ||
</Modal> | ||
) | ||
} | ||
|
||
export default connectComponentToProps( | ||
RenameDatasetComponent, | ||
(state: any, ownProps: RenameDatasetProps) => { | ||
return { | ||
...ownProps, | ||
modal: selectModal(state), | ||
qriRef: selectDatasetRef(state) | ||
} | ||
}, | ||
{ | ||
onDismissed: dismissModal, | ||
onSubmit: renameDataset | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.