2020-07-22 10:54:15 +00:00
|
|
|
import { WindowTitle } from "@saleor/components/WindowTitle";
|
2022-03-09 08:56:55 +00:00
|
|
|
import { useAppFetchMutation, useAppInstallMutation } from "@saleor/graphql";
|
2020-07-22 10:54:15 +00:00
|
|
|
import useLocalStorage from "@saleor/hooks/useLocalStorage";
|
|
|
|
import useNavigator from "@saleor/hooks/useNavigator";
|
|
|
|
import useNotifier from "@saleor/hooks/useNotifier";
|
2022-02-01 09:58:06 +00:00
|
|
|
import { extractMutationErrors } from "@saleor/misc";
|
2020-07-22 10:54:15 +00:00
|
|
|
import getAppErrorMessage from "@saleor/utils/errors/app";
|
|
|
|
import React, { useEffect } from "react";
|
|
|
|
import { useIntl } from "react-intl";
|
|
|
|
import { RouteComponentProps } from "react-router-dom";
|
|
|
|
|
|
|
|
import AppInstallErrorPage from "../../components/AppInstallErrorPage";
|
|
|
|
import AppInstallPage from "../../components/AppInstallPage";
|
|
|
|
import {
|
|
|
|
AppInstallUrlQueryParams,
|
|
|
|
appsListUrl,
|
|
|
|
MANIFEST_ATTR
|
|
|
|
} from "../../urls";
|
2022-02-02 15:30:34 +00:00
|
|
|
import { messages } from "./messages";
|
2020-07-22 10:54:15 +00:00
|
|
|
|
|
|
|
interface InstallAppCreateProps extends RouteComponentProps {
|
|
|
|
params: AppInstallUrlQueryParams;
|
|
|
|
}
|
|
|
|
export const InstallAppCreate: React.FC<InstallAppCreateProps> = ({
|
|
|
|
params
|
|
|
|
}) => {
|
|
|
|
const [, setActiveInstallations] = useLocalStorage("activeInstallations", []);
|
|
|
|
const navigate = useNavigator();
|
|
|
|
const notify = useNotifier();
|
|
|
|
const intl = useIntl();
|
|
|
|
const manifestUrl = params[MANIFEST_ATTR];
|
|
|
|
|
2022-03-09 08:56:55 +00:00
|
|
|
const [fetchManifest, fetchManifestOpts] = useAppFetchMutation({
|
2020-07-22 10:54:15 +00:00
|
|
|
onCompleted: data => {
|
|
|
|
if (data.appFetchManifest.errors.length) {
|
|
|
|
data.appFetchManifest.errors.forEach(error => {
|
|
|
|
notify({
|
|
|
|
status: "error",
|
|
|
|
text: getAppErrorMessage(error, intl)
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const [installApp] = useAppInstallMutation({
|
|
|
|
onCompleted: data => {
|
|
|
|
const installationData = data.appInstall.appInstallation;
|
|
|
|
if (data.appInstall.errors.length === 0) {
|
|
|
|
setActiveInstallations(activeInstallations => [
|
|
|
|
...activeInstallations,
|
|
|
|
{ id: installationData.id, name: installationData.appName }
|
|
|
|
]);
|
|
|
|
navigateToAppsList();
|
|
|
|
} else {
|
|
|
|
data.appInstall.errors.forEach(error => {
|
|
|
|
notify({
|
|
|
|
status: "error",
|
|
|
|
text: getAppErrorMessage(error, intl)
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const navigateToAppsList = () => navigate(appsListUrl());
|
|
|
|
|
|
|
|
const handleSubmit = () => {
|
|
|
|
const manifest = fetchManifestOpts?.data?.appFetchManifest?.manifest;
|
2022-02-01 09:58:06 +00:00
|
|
|
return extractMutationErrors(
|
|
|
|
installApp({
|
|
|
|
variables: {
|
|
|
|
input: {
|
|
|
|
appName: manifest?.name,
|
|
|
|
manifestUrl,
|
|
|
|
permissions: manifest?.permissions.map(
|
|
|
|
permission => permission.code
|
|
|
|
)
|
|
|
|
}
|
2020-07-22 10:54:15 +00:00
|
|
|
}
|
2022-02-01 09:58:06 +00:00
|
|
|
})
|
|
|
|
);
|
2020-07-22 10:54:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (manifestUrl) {
|
|
|
|
fetchManifest({ variables: { manifestUrl } });
|
|
|
|
} else {
|
|
|
|
navigate(appsListUrl());
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2022-02-02 15:30:34 +00:00
|
|
|
<WindowTitle title={intl.formatMessage(messages.installApp)} />
|
2020-07-22 10:54:15 +00:00
|
|
|
{!!fetchManifestOpts.data?.appFetchManifest?.errors?.length ||
|
|
|
|
!!fetchManifestOpts.error ? (
|
|
|
|
<AppInstallErrorPage onBack={() => navigate("/")} />
|
|
|
|
) : (
|
|
|
|
<AppInstallPage
|
|
|
|
data={fetchManifestOpts?.data?.appFetchManifest?.manifest}
|
|
|
|
navigateToAppsList={navigateToAppsList}
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
loading={fetchManifestOpts?.loading}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default InstallAppCreate;
|