saleor-dashboard/src/apps/views/AppInstall/AppInstall.tsx
Dominik Żegleń 5b85d6c086
Use graphql-codegen (#1874)
* Use generated hooks in apps

* Remove unused files

* Use proper types in apps

* Use generated hooks in attributes

* Use generated hooks in auth module

* Use generated hooks in categories

* Use generated hooks in channels

* Use generated types in collections

* Remove legacy types from background tasks

* Use generated hooks in customers

* Use generated hooks in discounts

* Use generated hook in file upload

* Use generated types in gift cards

* Use generated types in home

* Use generated hooks in navigation

* Use generated hooks in orders

* Use generated hooks in pages

* Use generated hooks in page types

* Use generated hooks in permission groups

* Use generated hooks in plugins

* Use generated hooks in products

* Use fragment to mark product variants

* Improve code a bit

* Use generated hooks in page types

* Use generated types in searches

* Use generated hooks in shipping

* Use generated hooks in site settings

* Use generated hooks in staff members

* Use generated hooks in taxes

* Place all gql generated files in one directory

* Use generated hooks in translations

* Use global types from new generated module

* Use generated hooks in warehouses

* Use generated hooks in webhooks

* Use generated fragment types

* Unclutter types

* Remove hoc components

* Split hooks and types

* Fetch introspection file

* Delete obsolete schema file

* Fix rebase artifacts

* Fix autoreplace

* Fix auth provider tests

* Fix urls

* Remove leftover types

* Fix rebase artifacts
2022-03-09 09:56:55 +01:00

110 lines
3.3 KiB
TypeScript

import { WindowTitle } from "@saleor/components/WindowTitle";
import { useAppFetchMutation, useAppInstallMutation } from "@saleor/graphql";
import useLocalStorage from "@saleor/hooks/useLocalStorage";
import useNavigator from "@saleor/hooks/useNavigator";
import useNotifier from "@saleor/hooks/useNotifier";
import { extractMutationErrors } from "@saleor/misc";
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";
import { messages } from "./messages";
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];
const [fetchManifest, fetchManifestOpts] = useAppFetchMutation({
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;
return extractMutationErrors(
installApp({
variables: {
input: {
appName: manifest?.name,
manifestUrl,
permissions: manifest?.permissions.map(
permission => permission.code
)
}
}
})
);
};
useEffect(() => {
if (manifestUrl) {
fetchManifest({ variables: { manifestUrl } });
} else {
navigate(appsListUrl());
}
}, []);
return (
<>
<WindowTitle title={intl.formatMessage(messages.installApp)} />
{!!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;