2022-10-03 09:20:34 -04:00
|
|
|
/* eslint-disable jsx-a11y/no-autofocus */
|
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 { 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,
|
2022-09-15 05:59:11 -04:00
|
|
|
handleAfterCloned,
|
|
|
|
projectId,
|
|
|
|
projectName,
|
2021-01-06 05:30:08 -05:00
|
|
|
}) {
|
2021-09-14 04:54:21 -04:00
|
|
|
const { t } = useTranslation()
|
|
|
|
|
|
|
|
const [error, setError] = useState()
|
|
|
|
const [clonedProjectName, setClonedProjectName] = useState(
|
|
|
|
`${projectName} (Copy)`
|
|
|
|
)
|
|
|
|
|
|
|
|
// valid if the cloned project has a name
|
2022-01-10 05:23:05 -05:00
|
|
|
const valid = useMemo(
|
|
|
|
() => clonedProjectName.trim().length > 0,
|
|
|
|
[clonedProjectName]
|
|
|
|
)
|
2021-09-14 04:54:21 -04:00
|
|
|
|
|
|
|
// 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
|
2022-09-15 05:59:11 -04:00
|
|
|
handleAfterCloned(data)
|
2021-09-14 04:54:21 -04:00
|
|
|
})
|
|
|
|
.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)}
|
2022-10-03 09:20:34 -04:00
|
|
|
autoFocus
|
2021-01-06 05:30:08 -05:00
|
|
|
/>
|
|
|
|
</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>
|
2022-12-07 05:51:18 -05:00
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
bsStyle={null}
|
|
|
|
className="btn-secondary"
|
|
|
|
disabled={inFlight}
|
|
|
|
onClick={handleHide}
|
|
|
|
>
|
2021-09-14 04:54:21 -04:00
|
|
|
{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,
|
2022-09-15 05:59:11 -04:00
|
|
|
handleAfterCloned: PropTypes.func.isRequired,
|
|
|
|
projectId: PropTypes.string,
|
|
|
|
projectName: PropTypes.string,
|
2021-01-06 05:30:08 -05:00
|
|
|
}
|