implement react-query for edge-cases (#1711)

When initially implementing react-query, we focussed on core features. This pull request now replaces the remaining apiClient usages in ui-components and ui-webapp with react-query hooks.
This commit is contained in:
Konstantin Schaper
2021-06-28 13:19:03 +02:00
committed by GitHub
parent 2cd46ce8a0
commit e1239aff92
50 changed files with 1101 additions and 1116 deletions

View File

@@ -23,8 +23,7 @@
*/
import React, { FC } from "react";
import { FileUpload, LabelWithHelpIcon, Checkbox, InputField } from "@scm-manager/ui-components";
import { File } from "@scm-manager/ui-types";
import { Checkbox, FileUpload, InputField, LabelWithHelpIcon } from "@scm-manager/ui-components";
import { useTranslation } from "react-i18next";
type Props = {
@@ -44,7 +43,7 @@ const ImportFromBundleForm: FC<Props> = ({
setCompressed,
password,
setPassword,
disabled
disabled,
}) => {
const [t] = useTranslation("repos");
@@ -54,7 +53,7 @@ const ImportFromBundleForm: FC<Props> = ({
<div className="column is-half is-vcentered">
<LabelWithHelpIcon label={t("import.bundle.title")} helpText={t("import.bundle.helpText")} />
<FileUpload
handleFile={(file: File) => {
handleFile={(file) => {
setFile(file);
setValid(!!file);
}}
@@ -75,7 +74,7 @@ const ImportFromBundleForm: FC<Props> = ({
<div className="column is-half is-vcentered">
<InputField
value={password}
onChange={value => setPassword(value)}
onChange={(value) => setPassword(value)}
type="password"
label={t("import.bundle.password.title")}
helpText={t("import.bundle.password.helpText")}

View File

@@ -21,77 +21,56 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import React, { FC, FormEvent, useState } from "react";
import { File, RepositoryCreation } from "@scm-manager/ui-types";
import { apiClient, ErrorNotification, Level, SubmitButton } from "@scm-manager/ui-components";
import React, { FC, FormEvent, useEffect, useState } from "react";
import { Repository, RepositoryCreation, RepositoryType } from "@scm-manager/ui-types";
import { ErrorNotification, Level, SubmitButton } from "@scm-manager/ui-components";
import { useTranslation } from "react-i18next";
import { useHistory } from "react-router-dom";
import ImportFullRepositoryForm from "./ImportFullRepositoryForm";
import { SubFormProps } from "../types";
import { extensionPoints } from "@scm-manager/ui-extensions";
import { useImportFullRepository } from "@scm-manager/ui-api";
type Props = {
url: string;
repositoryType: string;
repositoryType: RepositoryType;
setImportPending: (pending: boolean) => void;
nameForm: React.ComponentType<SubFormProps>;
informationForm: React.ComponentType<SubFormProps>;
setImportedRepository: (repository: Repository) => void;
nameForm: extensionPoints.RepositoryCreatorComponentProps["nameForm"];
informationForm: extensionPoints.RepositoryCreatorComponentProps["informationForm"];
};
const ImportFullRepository: FC<Props> = ({
url,
repositoryType,
setImportPending,
setImportedRepository,
nameForm: NameForm,
informationForm: InformationForm
informationForm: InformationForm,
}) => {
const [repo, setRepo] = useState<RepositoryCreation>({
name: "",
namespace: "",
type: repositoryType,
type: repositoryType.name,
contact: "",
description: "",
contextEntries: []
contextEntries: [],
});
const [password, setPassword] = useState("");
const [valid, setValid] = useState({ namespaceAndName: false, contact: true, file: false });
const [loading, setLoading] = useState(false);
const [error, setError] = useState<Error | undefined>();
const [file, setFile] = useState<File | null>(null);
const history = useHistory();
const [t] = useTranslation("repos");
const { importFullRepository, importedRepository, isLoading, error } = useImportFullRepository(repositoryType);
const handleImportLoading = (loading: boolean) => {
setImportPending(loading);
setLoading(loading);
};
useEffect(() => setRepo({...repo, type: repositoryType.name}), [repositoryType]);
useEffect(() => setImportPending(isLoading), [isLoading]);
useEffect(() => {
if (importedRepository) {
setImportedRepository(importedRepository);
}
}, [importedRepository]);
const isValid = () => Object.values(valid).every(v => v);
const isValid = () => Object.values(valid).every((v) => v);
const submit = (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();
const currentPath = history.location.pathname;
setError(undefined);
handleImportLoading(true);
apiClient
.postBinary(url, formData => {
formData.append("bundle", file, file?.name);
formData.append("repository", JSON.stringify({ ...repo, password }));
})
.then(response => {
const location = response.headers.get("Location");
return apiClient.get(location!);
})
.then(response => response.json())
.then(repo => {
handleImportLoading(false);
if (history.location.pathname === currentPath) {
history.push(`/repo/${repo.namespace}/${repo.name}/code/sources`);
}
})
.catch(error => {
setError(error);
handleImportLoading(false);
});
importFullRepository(repo, file!, password);
};
return (
@@ -108,16 +87,16 @@ const ImportFullRepository: FC<Props> = ({
repository={repo}
onChange={setRepo as React.Dispatch<React.SetStateAction<RepositoryCreation>>}
setValid={(namespaceAndName: boolean) => setValid({ ...valid, namespaceAndName })}
disabled={loading}
disabled={isLoading}
/>
<InformationForm
repository={repo}
onChange={setRepo as React.Dispatch<React.SetStateAction<RepositoryCreation>>}
disabled={loading}
disabled={isLoading}
setValid={(contact: boolean) => setValid({ ...valid, contact })}
/>
<Level
right={<SubmitButton disabled={!isValid()} loading={loading} label={t("repositoryForm.submitImport")} />}
right={<SubmitButton disabled={!isValid()} loading={isLoading} label={t("repositoryForm.submitImport")} />}
/>
</form>
);

View File

@@ -24,7 +24,6 @@
import React, { FC } from "react";
import { FileUpload, InputField, LabelWithHelpIcon } from "@scm-manager/ui-components";
import { File } from "@scm-manager/ui-types";
import { useTranslation } from "react-i18next";
type Props = {
@@ -51,7 +50,7 @@ const ImportFullRepositoryForm: FC<Props> = ({ setFile, setValid, password, setP
<div className="column is-half is-vcentered">
<InputField
value={password}
onChange={value => setPassword(value)}
onChange={(value) => setPassword(value)}
type="password"
label={t("import.bundle.password.title")}
helpText={t("import.bundle.password.helpText")}

View File

@@ -21,78 +21,58 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import React, { FC, FormEvent, useState } from "react";
import { File, RepositoryCreation } from "@scm-manager/ui-types";
import { apiClient, ErrorNotification, Level, SubmitButton } from "@scm-manager/ui-components";
import React, { FC, FormEvent, useEffect, useState } from "react";
import { Repository, RepositoryCreation, RepositoryType } from "@scm-manager/ui-types";
import { ErrorNotification, Level, SubmitButton } from "@scm-manager/ui-components";
import { useTranslation } from "react-i18next";
import { useHistory } from "react-router-dom";
import ImportFromBundleForm from "./ImportFromBundleForm";
import { SubFormProps } from "../types";
import { extensionPoints } from "@scm-manager/ui-extensions";
import { useImportRepositoryFromBundle } from "@scm-manager/ui-api";
type Props = {
url: string;
repositoryType: string;
repositoryType: RepositoryType;
setImportPending: (pending: boolean) => void;
nameForm: React.ComponentType<SubFormProps>;
informationForm: React.ComponentType<SubFormProps>;
setImportedRepository: (repository: Repository) => void;
nameForm: extensionPoints.RepositoryCreatorComponentProps["nameForm"];
informationForm: extensionPoints.RepositoryCreatorComponentProps["informationForm"];
};
const ImportRepositoryFromBundle: FC<Props> = ({
url,
repositoryType,
setImportPending,
setImportedRepository,
nameForm: NameForm,
informationForm: InformationForm
informationForm: InformationForm,
}) => {
const [repo, setRepo] = useState<RepositoryCreation>({
name: "",
namespace: "",
type: repositoryType,
type: repositoryType.name,
contact: "",
description: "",
contextEntries: []
contextEntries: [],
});
const [password, setPassword] = useState("");
const [valid, setValid] = useState({ namespaceAndName: false, contact: true, file: false });
const [loading, setLoading] = useState(false);
const [error, setError] = useState<Error | undefined>();
const [file, setFile] = useState<File | null>(null);
const [compressed, setCompressed] = useState(true);
const history = useHistory();
const [t] = useTranslation("repos");
const { importRepositoryFromBundle, importedRepository, error, isLoading } =
useImportRepositoryFromBundle(repositoryType);
const handleImportLoading = (loading: boolean) => {
setImportPending(loading);
setLoading(loading);
};
useEffect(() => setRepo({...repo, type: repositoryType.name}), [repositoryType]);
useEffect(() => setImportPending(isLoading), [isLoading]);
useEffect(() => {
if (importedRepository) {
setImportedRepository(importedRepository);
}
}, [importedRepository]);
const isValid = () => Object.values(valid).every(v => v);
const isValid = () => Object.values(valid).every((v) => v);
const submit = (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();
const currentPath = history.location.pathname;
setError(undefined);
handleImportLoading(true);
apiClient
.postBinary(compressed ? url + "?compressed=true" : url, formData => {
formData.append("bundle", file, file?.name);
formData.append("repository", JSON.stringify({ ...repo, password }));
})
.then(response => {
const location = response.headers.get("Location");
return apiClient.get(location!);
})
.then(response => response.json())
.then(repo => {
handleImportLoading(false);
if (history.location.pathname === currentPath) {
history.push(`/repo/${repo.namespace}/${repo.name}/code/sources`);
}
})
.catch(error => {
setError(error);
handleImportLoading(false);
});
importRepositoryFromBundle(repo, file!, compressed, password);
};
return (
@@ -105,23 +85,23 @@ const ImportRepositoryFromBundle: FC<Props> = ({
setCompressed={setCompressed}
password={password}
setPassword={setPassword}
disabled={loading}
disabled={isLoading}
/>
<hr />
<NameForm
repository={repo}
onChange={setRepo as React.Dispatch<React.SetStateAction<RepositoryCreation>>}
setValid={(namespaceAndName: boolean) => setValid({ ...valid, namespaceAndName })}
disabled={loading}
disabled={isLoading}
/>
<InformationForm
repository={repo}
onChange={setRepo as React.Dispatch<React.SetStateAction<RepositoryCreation>>}
disabled={loading}
disabled={isLoading}
setValid={(contact: boolean) => setValid({ ...valid, contact })}
/>
<Level
right={<SubmitButton disabled={!isValid()} loading={loading} label={t("repositoryForm.submitImport")} />}
right={<SubmitButton disabled={!isValid()} loading={isLoading} label={t("repositoryForm.submitImport")} />}
/>
</form>
);

View File

@@ -21,102 +21,84 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import React, { FC, FormEvent, useState } from "react";
import { RepositoryCreation, RepositoryUrlImport } from "@scm-manager/ui-types";
import React, { FC, FormEvent, useEffect, useState } from "react";
import { Repository, RepositoryCreation, RepositoryType, RepositoryUrlImport } from "@scm-manager/ui-types";
import ImportFromUrlForm from "./ImportFromUrlForm";
import { apiClient, ErrorNotification, Level, SubmitButton } from "@scm-manager/ui-components";
import { ErrorNotification, Level, SubmitButton } from "@scm-manager/ui-components";
import { useTranslation } from "react-i18next";
import { useHistory } from "react-router-dom";
import { SubFormProps } from "../types";
import { useImportRepositoryFromUrl } from "@scm-manager/ui-api";
import { extensionPoints } from "@scm-manager/ui-extensions";
type Props = {
url: string;
repositoryType: string;
repositoryType: RepositoryType;
setImportPending: (pending: boolean) => void;
nameForm: React.ComponentType<SubFormProps>;
informationForm: React.ComponentType<SubFormProps>;
setImportedRepository: (repository: Repository) => void;
nameForm: extensionPoints.RepositoryCreatorComponentProps["nameForm"];
informationForm: extensionPoints.RepositoryCreatorComponentProps["informationForm"];
};
const ImportRepositoryFromUrl: FC<Props> = ({
url,
repositoryType,
setImportPending,
setImportedRepository,
nameForm: NameForm,
informationForm: InformationForm
informationForm: InformationForm,
}) => {
const [repo, setRepo] = useState<RepositoryUrlImport>({
name: "",
namespace: "",
type: repositoryType,
type: repositoryType.name,
contact: "",
description: "",
importUrl: "",
username: "",
password: "",
contextEntries: []
contextEntries: [],
});
const [valid, setValid] = useState({ namespaceAndName: false, contact: true, importUrl: false });
const [loading, setLoading] = useState(false);
const [error, setError] = useState<Error | undefined>();
const history = useHistory();
const [t] = useTranslation("repos");
const { importRepositoryFromUrl, importedRepository, error, isLoading } = useImportRepositoryFromUrl(repositoryType);
const isValid = () => Object.values(valid).every(v => v);
useEffect(() => setRepo({...repo, type: repositoryType.name}), [repositoryType]);
useEffect(() => setImportPending(isLoading), [isLoading]);
useEffect(() => {
if (importedRepository) {
setImportedRepository(importedRepository);
}
}, [importedRepository]);
const handleImportLoading = (loading: boolean) => {
setImportPending(loading);
setLoading(loading);
};
const isValid = () => Object.values(valid).every((v) => v);
const submit = (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();
setError(undefined);
const currentPath = history.location.pathname;
handleImportLoading(true);
apiClient
.post(url, repo, "application/vnd.scmm-repository+json;v=2")
.then(response => {
const location = response.headers.get("Location");
return apiClient.get(location!);
})
.then(response => response.json())
.then(repo => {
handleImportLoading(false);
if (history.location.pathname === currentPath) {
history.push(`/repo/${repo.namespace}/${repo.name}/code/sources`);
}
})
.catch(error => {
setError(error);
handleImportLoading(false);
});
importRepositoryFromUrl(repo);
};
return (
<form onSubmit={submit}>
<ErrorNotification error={error} />
{error ? <ErrorNotification error={error} /> : null}
<ImportFromUrlForm
repository={repo}
onChange={setRepo}
setValid={(importUrl: boolean) => setValid({ ...valid, importUrl })}
disabled={loading}
disabled={isLoading}
/>
<hr />
<NameForm
repository={repo}
onChange={setRepo as React.Dispatch<React.SetStateAction<RepositoryCreation>>}
setValid={(namespaceAndName: boolean) => setValid({ ...valid, namespaceAndName })}
disabled={loading}
disabled={isLoading}
/>
<InformationForm
repository={repo}
onChange={setRepo as React.Dispatch<React.SetStateAction<RepositoryCreation>>}
disabled={loading}
disabled={isLoading}
setValid={(contact: boolean) => setValid({ ...valid, contact })}
/>
<Level
right={<SubmitButton disabled={!isValid()} loading={loading} label={t("repositoryForm.submitImport")} />}
right={<SubmitButton disabled={!isValid()} loading={isLoading} label={t("repositoryForm.submitImport")} />}
/>
</form>
);