import React, { useState } from 'react'
import {
Color,
Container,
Layout,
Text,
Button,
ButtonVariation,
Intent,
FormInput,
Formik,
useToaster,
ButtonSize,
StringSubstitute
} from '@harness/uicore'
import { useHistory } from 'react-router-dom'
import { useMutate } from 'restful-react'
import { getErrorMessage, permissionProps, voidFn } from 'utils/Utils'
import { useStrings } from 'framework/strings'
import type { TypesRepository } from 'services/code'
import { useConfirmAction } from 'hooks/useConfirmAction'
import { useAppContext } from 'AppContext'
import { useGetSpaceParam } from 'hooks/useGetSpaceParam'
import css from '../RepositorySettings.module.scss'
enum ACCESS_MODES {
VIEW,
EDIT
}
interface GeneralSettingsProps {
repoMetadata: TypesRepository | undefined
refetch: () => void
}
const GeneralSettingsContent = (props: GeneralSettingsProps) => {
const { repoMetadata, refetch } = props
const [editDesc, setEditDesc] = useState(ACCESS_MODES.VIEW)
const { showError, showSuccess } = useToaster()
const history = useHistory()
const { routes } = useAppContext()
const space = useGetSpaceParam()
const { standalone } = useAppContext()
const { hooks } = useAppContext()
const { getString } = useStrings()
const { mutate } = useMutate({
verb: 'PATCH',
path: `/api/v1/repos/${repoMetadata?.path}/+/`
})
const { mutate: deleteRepo } = useMutate({
verb: 'DELETE',
path: `/api/v1/repos/${repoMetadata?.path}/+/`
})
const contentText = () => (
{' '}
{repoMetadata?.uid}
}}
/>
)
const confirmDeleteBranch = useConfirmAction({
title: getString('deleteRepoTitle'),
confirmText: getString('confirm'),
intent: Intent.DANGER,
message: contentText(),
action: async () => {
deleteRepo({})
.then(() => {
showSuccess(getString('repoDeleted', { repo: repoMetadata?.uid }), 5000)
history.push(routes.toCODERepositories({ space }))
})
.catch((error: Unknown) => {
showError(getErrorMessage(error), 0, 'failedToDeleteBranch')
})
}
})
const permEditResult = hooks?.usePermissionTranslate?.(
{
resource: {
resourceType: 'CODE_REPOSITORY'
},
permissions: ['code_repo_edit']
},
[space]
)
const permDeleteResult = hooks?.usePermissionTranslate?.(
{
resource: {
resourceType: 'CODE_REPOSITORY'
},
permissions: ['code_repo_delete']
},
[space]
)
return (
{formik => {
return (
{getString('repositoryName')}
{repoMetadata?.uid}
{getString('description')}
{editDesc === ACCESS_MODES.EDIT ? (
) : (
{formik?.values?.desc || repoMetadata?.description}
{
setEditDesc(ACCESS_MODES.EDIT)
}}
{...permissionProps(permEditResult, standalone)}
/>
)}
{getString('dangerDeleteRepo')}
{
confirmDeleteBranch()
}}
variation={ButtonVariation.SECONDARY}
text={getString('delete')}
{...permissionProps(permDeleteResult, standalone)}>
)
}}
)
}
export default GeneralSettingsContent