This commit is contained in:
Eduard Heimbuch
2020-11-26 11:31:33 +01:00
parent 485138e5f2
commit 0682118879
3 changed files with 31 additions and 38 deletions

View File

@@ -79,19 +79,19 @@ const RepositoryForm: FC<Props> = ({
indexResources, indexResources,
creationMode creationMode
}) => { }) => {
const [repo, setRepo] = useState<RepositoryCreation>({ const [repo, setRepo] = useState<Repository>({
name: "", name: "",
namespace: "", namespace: "",
type: "", type: "",
contact: "", contact: "",
description: "", description: "",
contextEntries: {},
_links: {} _links: {}
}); });
const [initRepository, setInitRepository] = useState(false); const [initRepository, setInitRepository] = useState(false);
const [namespaceValidationError, setNamespaceValidationError] = useState(false); const [namespaceValidationError, setNamespaceValidationError] = useState(false);
const [nameValidationError, setNameValidationError] = useState(false); const [nameValidationError, setNameValidationError] = useState(false);
const [contactValidationError, setContactValidationError] = useState(false); const [contactValidationError, setContactValidationError] = useState(false);
const [contextEntries, setContextEntries] = useState({});
const [importUrl, setImportUrl] = useState(""); const [importUrl, setImportUrl] = useState("");
const [username, setUsername] = useState(""); const [username, setUsername] = useState("");
const [password, setPassword] = useState(""); const [password, setPassword] = useState("");
@@ -100,12 +100,14 @@ const RepositoryForm: FC<Props> = ({
useEffect(() => { useEffect(() => {
if (repository) { if (repository) {
setRepo({ ...repository, contextEntries: {} }); setRepo({ ...repository });
} }
}, [repository]); }, [repository]);
const isImportMode = () => creationMode === "IMPORT"; const isImportMode = () => creationMode === "IMPORT";
const isCreateMode = () => creationMode === "CREATE"; const isCreateMode = () => creationMode === "CREATE";
const isEditMode = () => !!repository;
const isModifiable = () => !!repository && !!repository._links.update;
const isValid = () => { const isValid = () => {
return !( return !(
@@ -124,35 +126,13 @@ const RepositoryForm: FC<Props> = ({
if (importRepository && isImportMode()) { if (importRepository && isImportMode()) {
importRepository({ ...repo, url: importUrl, username, password }); importRepository({ ...repo, url: importUrl, username, password });
} else if (createRepository && isCreateMode()) { } else if (createRepository && isCreateMode()) {
createRepository(repo, initRepository); createRepository({ ...repo, contextEntries }, initRepository);
} else if (modifyRepository) { } else if (modifyRepository) {
modifyRepository(repo); 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[]) => { const createSelectOptions = (repositoryTypes?: RepositoryType[]) => {
if (repositoryTypes) { if (repositoryTypes) {
return repositoryTypes.map(repositoryType => { return repositoryTypes.map(repositoryType => {
@@ -270,6 +250,17 @@ const RepositoryForm: FC<Props> = ({
); );
}; };
const toggleInitCheckbox = () => {
setInitRepository(!initRepository);
};
const setCreationContextEntry = (key: string, value: any) => {
setContextEntries({
...contextEntries,
[key]: value
});
};
const handleNamespaceChange = (namespace: string) => { const handleNamespaceChange = (namespace: string) => {
setNamespaceValidationError(!validator.isNamespaceValid(namespace)); setNamespaceValidationError(!validator.isNamespaceValid(namespace));
setRepo({ ...repo, namespace }); setRepo({ ...repo, namespace });
@@ -287,6 +278,7 @@ const RepositoryForm: FC<Props> = ({
const handleImportUrlChange = (url: string) => { const handleImportUrlChange = (url: string) => {
if (!repo.name) { if (!repo.name) {
// If the repository name is not fill we set a name suggestion
const match = url.match(/([^\/]+)\.git/i); const match = url.match(/([^\/]+)\.git/i);
if (match && match[1]) { if (match && match[1]) {
handleNameChange(match[1]); handleNameChange(match[1]);
@@ -297,14 +289,15 @@ const RepositoryForm: FC<Props> = ({
const disabled = !isModifiable() && isEditMode(); const disabled = !isModifiable() && isEditMode();
const getSubmitButtonTranslationKey = () => const submitButton = () => {
isImportMode() ? "repositoryForm.submitImport" : "repositoryForm.submitCreate"; if (disabled) {
return null;
const submitButton = disabled ? null : ( }
<Level const translationKey = isImportMode() ? "repositoryForm.submitImport" : "repositoryForm.submitCreate";
right={<SubmitButton disabled={!isValid()} loading={loading} label={t(getSubmitButtonTranslationKey())} />} return <Level
/> right={<SubmitButton disabled={!isValid()} loading={loading} label={t(translationKey)} />}
); />;
};
return ( return (
<> <>
@@ -326,7 +319,7 @@ const RepositoryForm: FC<Props> = ({
helpText={t("help.descriptionHelpText")} helpText={t("help.descriptionHelpText")}
disabled={disabled} disabled={disabled}
/> />
{submitButton} {submitButton()}
</form> </form>
</> </>
); );

View File

@@ -96,9 +96,7 @@ class AddRepository extends React.Component<Props> {
} }
repoCreated = (repo: Repository) => { repoCreated = (repo: Repository) => {
const { history } = this.props; this.props.history.push("/repo/" + repo.namespace + "/" + repo.name);
history.push("/repo/" + repo.namespace + "/" + repo.name);
}; };
resolveLocation = () => { resolveLocation = () => {

View File

@@ -253,6 +253,7 @@ export function importRepoFromUrl(link: string, repository: RepositoryImport, ca
.then(response => { .then(response => {
const location = response.headers.get("Location"); const location = response.headers.get("Location");
dispatch(importRepoSuccess()); dispatch(importRepoSuccess());
// @ts-ignore Location is always set if the repository import was successful
return apiClient.get(location); return apiClient.get(location);
}) })
.then(response => response.json()) .then(response => response.json())
@@ -308,6 +309,7 @@ export function createRepo(
.then(response => { .then(response => {
const location = response.headers.get("Location"); const location = response.headers.get("Location");
dispatch(createRepoSuccess()); dispatch(createRepoSuccess());
// @ts-ignore Location is always set if the repository creation was successful
return apiClient.get(location); return apiClient.get(location);
}) })
.then(response => response.json()) .then(response => response.json())