2021-03-05 08:00:13 -05:00
|
|
|
import PropTypes from 'prop-types'
|
2021-09-14 04:54:21 -04:00
|
|
|
import { useMemo, useState } from 'react'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
2021-01-06 05:30:08 -05:00
|
|
|
import {
|
|
|
|
Modal,
|
|
|
|
Alert,
|
|
|
|
Button,
|
|
|
|
ControlLabel,
|
2021-03-05 08:00:13 -05:00
|
|
|
FormControl,
|
2021-04-27 03:52:58 -04:00
|
|
|
FormGroup,
|
2021-01-06 05:30:08 -05:00
|
|
|
} from 'react-bootstrap'
|
2021-09-14 04:54:21 -04:00
|
|
|
import { useProjectContext } from '../../../shared/context/project-context'
|
|
|
|
import { postJSON } from '../../../infrastructure/fetch-json'
|
2021-01-06 05:30:08 -05:00
|
|
|
|
2021-03-05 08:00:13 -05:00
|
|
|
export default function CloneProjectModalContent({
|
2021-09-14 04:54:21 -04:00
|
|
|
handleHide,
|
2021-03-05 08:00:13 -05:00
|
|
|
inFlight,
|
2021-09-14 04:54:21 -04:00
|
|
|
setInFlight,
|
|
|
|
openProject,
|
2021-01-06 05:30:08 -05:00
|
|
|
}) {
|
2021-09-14 04:54:21 -04:00
|
|
|
const { _id: projectId, name: projectName } = useProjectContext()
|
|
|
|
const { t } = useTranslation()
|
|
|
|
|
|
|
|
const [error, setError] = useState()
|
|
|
|
const [clonedProjectName, setClonedProjectName] = useState(
|
|
|
|
`${projectName} (Copy)`
|
|
|
|
)
|
|
|
|
|
|
|
|
// valid if the cloned project has a name
|
|
|
|
const valid = useMemo(() => clonedProjectName.trim().length > 0, [
|
|
|
|
clonedProjectName,
|
|
|
|
])
|
|
|
|
|
|
|
|
// form submission: clone the project if the name is valid
|
|
|
|
const handleSubmit = event => {
|
|
|
|
event.preventDefault()
|
|
|
|
|
|
|
|
if (!valid) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
setError(false)
|
|
|
|
setInFlight(true)
|
|
|
|
|
|
|
|
// clone the project
|
|
|
|
postJSON(`/project/${projectId}/clone`, {
|
|
|
|
body: { projectName: clonedProjectName },
|
|
|
|
})
|
|
|
|
.then(data => {
|
|
|
|
// open the cloned project
|
|
|
|
openProject(data.project_id)
|
|
|
|
})
|
|
|
|
.catch(({ response, data }) => {
|
|
|
|
if (response?.status === 400) {
|
|
|
|
setError(data.message)
|
|
|
|
} else {
|
|
|
|
setError(true)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
setInFlight(false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-06 05:30:08 -05:00
|
|
|
return (
|
2021-09-14 04:54:21 -04:00
|
|
|
<>
|
2021-01-06 05:30:08 -05:00
|
|
|
<Modal.Header closeButton>
|
2021-09-14 04:54:21 -04:00
|
|
|
<Modal.Title>{t('copy_project')}</Modal.Title>
|
2021-01-06 05:30:08 -05:00
|
|
|
</Modal.Header>
|
|
|
|
|
|
|
|
<Modal.Body>
|
|
|
|
<form id="clone-project-form" onSubmit={handleSubmit}>
|
|
|
|
<FormGroup>
|
2021-03-05 08:00:13 -05:00
|
|
|
<ControlLabel htmlFor="clone-project-form-name">
|
2021-09-14 04:54:21 -04:00
|
|
|
{t('new_name')}
|
2021-01-06 05:30:08 -05:00
|
|
|
</ControlLabel>
|
|
|
|
|
|
|
|
<FormControl
|
2021-03-05 08:00:13 -05:00
|
|
|
id="clone-project-form-name"
|
2021-01-06 05:30:08 -05:00
|
|
|
type="text"
|
|
|
|
placeholder="New Project Name"
|
|
|
|
required
|
|
|
|
value={clonedProjectName}
|
|
|
|
onChange={event => setClonedProjectName(event.target.value)}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
</form>
|
|
|
|
|
|
|
|
{error && (
|
|
|
|
<Alert bsStyle="danger">
|
2021-09-14 04:54:21 -04:00
|
|
|
{error.length ? error : t('generic_something_went_wrong')}
|
2021-01-06 05:30:08 -05:00
|
|
|
</Alert>
|
|
|
|
)}
|
|
|
|
</Modal.Body>
|
|
|
|
|
|
|
|
<Modal.Footer>
|
2021-09-14 04:54:21 -04:00
|
|
|
<Button type="button" disabled={inFlight} onClick={handleHide}>
|
|
|
|
{t('cancel')}
|
2021-01-06 05:30:08 -05:00
|
|
|
</Button>
|
|
|
|
|
|
|
|
<Button
|
|
|
|
form="clone-project-form"
|
|
|
|
type="submit"
|
|
|
|
bsStyle="primary"
|
|
|
|
disabled={inFlight || !valid}
|
|
|
|
>
|
2021-09-14 04:54:21 -04:00
|
|
|
{inFlight ? <>{t('copying')}…</> : t('copy')}
|
2021-01-06 05:30:08 -05:00
|
|
|
</Button>
|
|
|
|
</Modal.Footer>
|
2021-09-14 04:54:21 -04:00
|
|
|
</>
|
2021-01-06 05:30:08 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
CloneProjectModalContent.propTypes = {
|
2021-09-14 04:54:21 -04:00
|
|
|
handleHide: PropTypes.func.isRequired,
|
|
|
|
inFlight: PropTypes.bool,
|
|
|
|
setInFlight: PropTypes.func.isRequired,
|
|
|
|
openProject: PropTypes.func.isRequired,
|
2021-01-06 05:30:08 -05:00
|
|
|
}
|