2021-03-05 08:00:13 -05:00
|
|
|
import React, { useCallback, useEffect, useMemo, useState } from 'react'
|
2021-01-06 05:30:08 -05:00
|
|
|
import PropTypes from 'prop-types'
|
2021-03-05 08:00:13 -05:00
|
|
|
import { cloneProject } from '../utils/api'
|
2021-01-06 05:30:08 -05:00
|
|
|
import CloneProjectModalContent from './clone-project-modal-content'
|
|
|
|
|
2021-03-05 08:00:13 -05:00
|
|
|
function CloneProjectModal({
|
|
|
|
show,
|
|
|
|
handleHide,
|
|
|
|
projectId,
|
|
|
|
projectName = '',
|
2021-04-27 03:52:58 -04:00
|
|
|
openProject,
|
2021-03-05 08:00:13 -05:00
|
|
|
}) {
|
2021-01-06 05:30:08 -05:00
|
|
|
const [inFlight, setInFlight] = useState(false)
|
|
|
|
const [error, setError] = useState()
|
2021-03-05 08:00:13 -05:00
|
|
|
const [clonedProjectName, setClonedProjectName] = useState('')
|
2021-01-06 05:30:08 -05:00
|
|
|
|
2021-03-05 08:00:13 -05:00
|
|
|
// set the cloned project name when the modal opens
|
2021-01-06 05:30:08 -05:00
|
|
|
useEffect(() => {
|
|
|
|
if (show) {
|
2021-03-05 08:00:13 -05:00
|
|
|
setClonedProjectName(`${projectName} (Copy)`)
|
2021-01-06 05:30:08 -05:00
|
|
|
}
|
2021-03-05 08:00:13 -05:00
|
|
|
}, [show, projectName])
|
2021-01-06 05:30:08 -05:00
|
|
|
|
2021-03-05 08:00:13 -05:00
|
|
|
// reset error when the modal is opened
|
|
|
|
useEffect(() => {
|
2021-03-18 05:52:36 -04:00
|
|
|
if (show) {
|
|
|
|
setError(undefined)
|
|
|
|
}
|
|
|
|
}, [show])
|
2021-01-06 05:30:08 -05:00
|
|
|
|
|
|
|
// close the modal if not in flight
|
|
|
|
const cancel = useCallback(() => {
|
|
|
|
if (!inFlight) {
|
|
|
|
handleHide()
|
|
|
|
}
|
|
|
|
}, [handleHide, inFlight])
|
|
|
|
|
2021-03-05 08:00:13 -05:00
|
|
|
// valid if the cloned project has a name
|
|
|
|
const valid = useMemo(() => !!clonedProjectName, [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
|
|
|
|
cloneProject(projectId, clonedProjectName)
|
|
|
|
.then(data => {
|
|
|
|
// open the cloned project
|
|
|
|
openProject(data.project_id)
|
|
|
|
})
|
|
|
|
.catch(({ response, data }) => {
|
2021-03-18 05:52:36 -04:00
|
|
|
if (response?.status === 400) {
|
2021-03-05 08:00:13 -05:00
|
|
|
setError(data.message)
|
|
|
|
} else {
|
|
|
|
setError(true)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
setInFlight(false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-06 05:30:08 -05:00
|
|
|
return (
|
2021-03-05 08:00:13 -05:00
|
|
|
<CloneProjectModalContent
|
|
|
|
show={show}
|
|
|
|
cancel={cancel}
|
|
|
|
inFlight={inFlight}
|
|
|
|
valid={valid}
|
|
|
|
error={error}
|
|
|
|
clonedProjectName={clonedProjectName}
|
|
|
|
setClonedProjectName={setClonedProjectName}
|
|
|
|
handleSubmit={handleSubmit}
|
|
|
|
/>
|
2021-01-06 05:30:08 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
CloneProjectModal.propTypes = {
|
|
|
|
handleHide: PropTypes.func.isRequired,
|
|
|
|
projectId: PropTypes.string.isRequired,
|
|
|
|
projectName: PropTypes.string,
|
2021-03-05 08:00:13 -05:00
|
|
|
openProject: PropTypes.func.isRequired,
|
2021-04-27 03:52:58 -04:00
|
|
|
show: PropTypes.bool.isRequired,
|
2021-01-06 05:30:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export default CloneProjectModal
|