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'
|
2023-09-28 06:12:18 -04:00
|
|
|
import { useCallback, useMemo, useState } from 'react'
|
2021-09-14 04:54:21 -04:00
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import { postJSON } from '../../../infrastructure/fetch-json'
|
2023-09-28 06:12:18 -04:00
|
|
|
import { CloneProjectTag } from './clone-project-tag'
|
2024-07-10 09:34:19 -04:00
|
|
|
import {
|
|
|
|
OLModalBody,
|
|
|
|
OLModalFooter,
|
|
|
|
OLModalHeader,
|
|
|
|
OLModalTitle,
|
|
|
|
} from '@/features/ui/components/ol/ol-modal'
|
|
|
|
import Notification from '@/shared/components/notification'
|
|
|
|
import OLForm from '@/features/ui/components/ol/ol-form'
|
|
|
|
import OLFormGroup from '@/features/ui/components/ol/ol-form-group'
|
|
|
|
import OLFormControl from '@/features/ui/components/ol/ol-form-control'
|
|
|
|
import OLFormLabel from '@/features/ui/components/ol/ol-form-label'
|
|
|
|
import OLButton from '@/features/ui/components/ol/ol-button'
|
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,
|
2023-09-28 06:12:18 -04:00
|
|
|
projectTags,
|
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)`
|
|
|
|
)
|
|
|
|
|
2023-09-28 06:12:18 -04:00
|
|
|
const [clonedProjectTags, setClonedProjectTags] = useState(projectTags)
|
|
|
|
|
2021-09-14 04:54:21 -04:00
|
|
|
// 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`, {
|
2023-09-28 06:12:18 -04:00
|
|
|
body: {
|
|
|
|
projectName: clonedProjectName,
|
|
|
|
tags: clonedProjectTags.map(tag => ({ id: tag._id })),
|
|
|
|
},
|
2021-09-14 04:54:21 -04:00
|
|
|
})
|
|
|
|
.then(data => {
|
|
|
|
// open the cloned project
|
2023-09-28 06:12:18 -04:00
|
|
|
handleAfterCloned(data, clonedProjectTags)
|
2021-09-14 04:54:21 -04:00
|
|
|
})
|
|
|
|
.catch(({ response, data }) => {
|
|
|
|
if (response?.status === 400) {
|
|
|
|
setError(data.message)
|
|
|
|
} else {
|
|
|
|
setError(true)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
setInFlight(false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-09-28 06:12:18 -04:00
|
|
|
const removeTag = useCallback(tag => {
|
|
|
|
setClonedProjectTags(value => value.filter(item => item._id !== tag._id))
|
|
|
|
}, [])
|
|
|
|
|
2021-01-06 05:30:08 -05:00
|
|
|
return (
|
2021-09-14 04:54:21 -04:00
|
|
|
<>
|
2024-07-10 09:34:19 -04:00
|
|
|
<OLModalHeader closeButton>
|
|
|
|
<OLModalTitle>{t('copy_project')}</OLModalTitle>
|
|
|
|
</OLModalHeader>
|
|
|
|
|
|
|
|
<OLModalBody>
|
|
|
|
<OLForm id="clone-project-form" onSubmit={handleSubmit}>
|
|
|
|
<OLFormGroup controlId="clone-project-form-name">
|
|
|
|
<OLFormLabel>{t('new_name')}</OLFormLabel>
|
|
|
|
<OLFormControl
|
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
|
|
|
/>
|
2024-07-10 09:34:19 -04:00
|
|
|
</OLFormGroup>
|
2023-09-28 06:12:18 -04:00
|
|
|
|
|
|
|
{clonedProjectTags.length > 0 && (
|
2024-07-10 09:34:19 -04:00
|
|
|
<OLFormGroup
|
|
|
|
controlId="clone-project-tags-list"
|
2024-07-16 06:40:21 -04:00
|
|
|
className="clone-project-tag"
|
2024-07-10 09:34:19 -04:00
|
|
|
>
|
|
|
|
<OLFormLabel>{t('tags')}: </OLFormLabel>
|
2023-09-28 06:12:18 -04:00
|
|
|
<div role="listbox" id="clone-project-tags-list">
|
|
|
|
{clonedProjectTags.map(tag => (
|
|
|
|
<CloneProjectTag
|
|
|
|
key={tag._id}
|
|
|
|
tag={tag}
|
|
|
|
removeTag={removeTag}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</div>
|
2024-07-10 09:34:19 -04:00
|
|
|
</OLFormGroup>
|
2023-09-28 06:12:18 -04:00
|
|
|
)}
|
2024-07-10 09:34:19 -04:00
|
|
|
</OLForm>
|
2021-01-06 05:30:08 -05:00
|
|
|
|
|
|
|
{error && (
|
2024-07-10 09:34:19 -04:00
|
|
|
<Notification
|
|
|
|
content={error.length ? error : t('generic_something_went_wrong')}
|
|
|
|
type="error"
|
|
|
|
/>
|
2021-01-06 05:30:08 -05:00
|
|
|
)}
|
2024-07-10 09:34:19 -04:00
|
|
|
</OLModalBody>
|
2021-01-06 05:30:08 -05:00
|
|
|
|
2024-07-10 09:34:19 -04:00
|
|
|
<OLModalFooter>
|
|
|
|
<OLButton variant="secondary" disabled={inFlight} onClick={handleHide}>
|
2021-09-14 04:54:21 -04:00
|
|
|
{t('cancel')}
|
2024-07-10 09:34:19 -04:00
|
|
|
</OLButton>
|
|
|
|
<OLButton
|
|
|
|
variant="primary"
|
|
|
|
disabled={inFlight || !valid}
|
2021-01-06 05:30:08 -05:00
|
|
|
form="clone-project-form"
|
|
|
|
type="submit"
|
|
|
|
>
|
2021-09-14 04:54:21 -04:00
|
|
|
{inFlight ? <>{t('copying')}…</> : t('copy')}
|
2024-07-10 09:34:19 -04:00
|
|
|
</OLButton>
|
|
|
|
</OLModalFooter>
|
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,
|
2023-09-28 06:12:18 -04:00
|
|
|
projectTags: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape({
|
|
|
|
_id: PropTypes.string.isRequired,
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
color: PropTypes.string,
|
|
|
|
})
|
|
|
|
),
|
2021-01-06 05:30:08 -05:00
|
|
|
}
|