From 0682118879069ea6ee4fcacaab3a97e12926b986 Mon Sep 17 00:00:00 2001 From: Eduard Heimbuch Date: Thu, 26 Nov 2020 11:31:33 +0100 Subject: [PATCH] Cleanup --- .../repos/components/form/RepositoryForm.tsx | 63 +++++++++---------- .../src/repos/containers/AddRepository.tsx | 4 +- scm-ui/ui-webapp/src/repos/modules/repos.ts | 2 + 3 files changed, 31 insertions(+), 38 deletions(-) diff --git a/scm-ui/ui-webapp/src/repos/components/form/RepositoryForm.tsx b/scm-ui/ui-webapp/src/repos/components/form/RepositoryForm.tsx index 428e6e4c50..2dad770273 100644 --- a/scm-ui/ui-webapp/src/repos/components/form/RepositoryForm.tsx +++ b/scm-ui/ui-webapp/src/repos/components/form/RepositoryForm.tsx @@ -79,19 +79,19 @@ const RepositoryForm: FC = ({ indexResources, creationMode }) => { - const [repo, setRepo] = useState({ + const [repo, setRepo] = useState({ name: "", namespace: "", type: "", contact: "", description: "", - contextEntries: {}, _links: {} }); const [initRepository, setInitRepository] = useState(false); const [namespaceValidationError, setNamespaceValidationError] = useState(false); const [nameValidationError, setNameValidationError] = useState(false); const [contactValidationError, setContactValidationError] = useState(false); + const [contextEntries, setContextEntries] = useState({}); const [importUrl, setImportUrl] = useState(""); const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); @@ -100,12 +100,14 @@ const RepositoryForm: FC = ({ useEffect(() => { if (repository) { - setRepo({ ...repository, contextEntries: {} }); + setRepo({ ...repository }); } }, [repository]); const isImportMode = () => creationMode === "IMPORT"; const isCreateMode = () => creationMode === "CREATE"; + const isEditMode = () => !!repository; + const isModifiable = () => !!repository && !!repository._links.update; const isValid = () => { return !( @@ -124,35 +126,13 @@ const RepositoryForm: FC = ({ if (importRepository && isImportMode()) { importRepository({ ...repo, url: importUrl, username, password }); } else if (createRepository && isCreateMode()) { - createRepository(repo, initRepository); + createRepository({ ...repo, contextEntries }, initRepository); } else if (modifyRepository) { modifyRepository(repo); } } }; - const isEditMode = () => { - return !!repository; - }; - - const isModifiable = () => { - return !!repository && !!repository._links.update; - }; - - const toggleInitCheckbox = () => { - setInitRepository(!initRepository); - }; - - const setCreationContextEntry = (key: string, value: any) => { - setRepo({ - ...repo, - contextEntries: { - ...repo.contextEntries, - [key]: value - } - }); - }; - const createSelectOptions = (repositoryTypes?: RepositoryType[]) => { if (repositoryTypes) { return repositoryTypes.map(repositoryType => { @@ -270,6 +250,17 @@ const RepositoryForm: FC = ({ ); }; + const toggleInitCheckbox = () => { + setInitRepository(!initRepository); + }; + + const setCreationContextEntry = (key: string, value: any) => { + setContextEntries({ + ...contextEntries, + [key]: value + }); + }; + const handleNamespaceChange = (namespace: string) => { setNamespaceValidationError(!validator.isNamespaceValid(namespace)); setRepo({ ...repo, namespace }); @@ -287,6 +278,7 @@ const RepositoryForm: FC = ({ const handleImportUrlChange = (url: string) => { if (!repo.name) { + // If the repository name is not fill we set a name suggestion const match = url.match(/([^\/]+)\.git/i); if (match && match[1]) { handleNameChange(match[1]); @@ -297,14 +289,15 @@ const RepositoryForm: FC = ({ const disabled = !isModifiable() && isEditMode(); - const getSubmitButtonTranslationKey = () => - isImportMode() ? "repositoryForm.submitImport" : "repositoryForm.submitCreate"; - - const submitButton = disabled ? null : ( - } - /> - ); + const submitButton = () => { + if (disabled) { + return null; + } + const translationKey = isImportMode() ? "repositoryForm.submitImport" : "repositoryForm.submitCreate"; + return } + />; + }; return ( <> @@ -326,7 +319,7 @@ const RepositoryForm: FC = ({ helpText={t("help.descriptionHelpText")} disabled={disabled} /> - {submitButton} + {submitButton()} ); diff --git a/scm-ui/ui-webapp/src/repos/containers/AddRepository.tsx b/scm-ui/ui-webapp/src/repos/containers/AddRepository.tsx index ea798addb7..800f63d502 100644 --- a/scm-ui/ui-webapp/src/repos/containers/AddRepository.tsx +++ b/scm-ui/ui-webapp/src/repos/containers/AddRepository.tsx @@ -96,9 +96,7 @@ class AddRepository extends React.Component { } repoCreated = (repo: Repository) => { - const { history } = this.props; - - history.push("/repo/" + repo.namespace + "/" + repo.name); + this.props.history.push("/repo/" + repo.namespace + "/" + repo.name); }; resolveLocation = () => { diff --git a/scm-ui/ui-webapp/src/repos/modules/repos.ts b/scm-ui/ui-webapp/src/repos/modules/repos.ts index 923f072687..06160d6c80 100644 --- a/scm-ui/ui-webapp/src/repos/modules/repos.ts +++ b/scm-ui/ui-webapp/src/repos/modules/repos.ts @@ -253,6 +253,7 @@ export function importRepoFromUrl(link: string, repository: RepositoryImport, ca .then(response => { const location = response.headers.get("Location"); dispatch(importRepoSuccess()); + // @ts-ignore Location is always set if the repository import was successful return apiClient.get(location); }) .then(response => response.json()) @@ -308,6 +309,7 @@ export function createRepo( .then(response => { const location = response.headers.get("Location"); dispatch(createRepoSuccess()); + // @ts-ignore Location is always set if the repository creation was successful return apiClient.get(location); }) .then(response => response.json())