2022-01-11 12:34:48 +00:00
|
|
|
import NotFoundPage from "@saleor/components/NotFoundPage";
|
2020-07-22 10:54:15 +00:00
|
|
|
import useNavigator from "@saleor/hooks/useNavigator";
|
|
|
|
import useNotifier from "@saleor/hooks/useNotifier";
|
|
|
|
import getAppErrorMessage from "@saleor/utils/errors/app";
|
|
|
|
import createDialogActionHandlers from "@saleor/utils/handlers/dialogActionHandlers";
|
|
|
|
import React from "react";
|
|
|
|
import { useIntl } from "react-intl";
|
|
|
|
|
|
|
|
import AppActivateDialog from "../../components/AppActivateDialog";
|
|
|
|
import AppDeactivateDialog from "../../components/AppDeactivateDialog";
|
|
|
|
import AppDetailsPage from "../../components/AppDetailsPage";
|
|
|
|
import {
|
|
|
|
useAppActivateMutation,
|
|
|
|
useAppDeactivateMutation
|
|
|
|
} from "../../mutations";
|
|
|
|
import { useAppDetails } from "../../queries";
|
|
|
|
import {
|
|
|
|
AppDetailsUrlDialog,
|
|
|
|
AppDetailsUrlQueryParams,
|
|
|
|
appSettingsUrl,
|
|
|
|
appsListPath,
|
|
|
|
appUrl
|
|
|
|
} from "../../urls";
|
|
|
|
|
|
|
|
interface AppDetailsProps {
|
|
|
|
id: string;
|
|
|
|
params: AppDetailsUrlQueryParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const AppDetails: React.FC<AppDetailsProps> = ({ id, params }) => {
|
|
|
|
const { data, loading, refetch } = useAppDetails({
|
|
|
|
displayLoader: true,
|
|
|
|
variables: { id }
|
|
|
|
});
|
2022-01-11 12:34:48 +00:00
|
|
|
|
|
|
|
const appExists = data?.app !== null;
|
|
|
|
|
2020-07-22 10:54:15 +00:00
|
|
|
const navigate = useNavigator();
|
|
|
|
const notify = useNotifier();
|
|
|
|
const intl = useIntl();
|
|
|
|
const mutationOpts = { variables: { id } };
|
|
|
|
const [activateApp, activateAppResult] = useAppActivateMutation({
|
|
|
|
onCompleted: data => {
|
|
|
|
const errors = data?.appActivate?.errors;
|
|
|
|
if (errors?.length === 0) {
|
|
|
|
notify({
|
|
|
|
status: "success",
|
|
|
|
text: intl.formatMessage({
|
|
|
|
defaultMessage: "App activated",
|
|
|
|
description: "snackbar text"
|
|
|
|
})
|
|
|
|
});
|
|
|
|
refetch();
|
|
|
|
closeModal();
|
|
|
|
} else {
|
2022-01-11 12:34:48 +00:00
|
|
|
if (appExists) {
|
|
|
|
errors.forEach(error =>
|
|
|
|
notify({
|
|
|
|
status: "error",
|
|
|
|
text: getAppErrorMessage(error, intl)
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2020-07-22 10:54:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const [deactivateApp, deactivateAppResult] = useAppDeactivateMutation({
|
|
|
|
onCompleted: data => {
|
|
|
|
const errors = data?.appDeactivate?.errors;
|
|
|
|
if (errors.length === 0) {
|
|
|
|
notify({
|
|
|
|
status: "success",
|
|
|
|
text: intl.formatMessage({
|
|
|
|
defaultMessage: "App deactivated",
|
|
|
|
description: "snackbar text"
|
|
|
|
})
|
|
|
|
});
|
|
|
|
refetch();
|
|
|
|
closeModal();
|
|
|
|
} else {
|
2022-01-11 12:34:48 +00:00
|
|
|
if (appExists) {
|
|
|
|
errors.forEach(error =>
|
|
|
|
notify({
|
|
|
|
status: "error",
|
|
|
|
text: getAppErrorMessage(error, intl)
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2020-07-22 10:54:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const [openModal, closeModal] = createDialogActionHandlers<
|
|
|
|
AppDetailsUrlDialog,
|
|
|
|
AppDetailsUrlQueryParams
|
|
|
|
>(navigate, params => appUrl(id, params), params);
|
|
|
|
|
|
|
|
const handleActivateConfirm = () => {
|
|
|
|
activateApp(mutationOpts);
|
|
|
|
};
|
|
|
|
const handleDeactivateConfirm = () => {
|
|
|
|
deactivateApp(mutationOpts);
|
|
|
|
};
|
|
|
|
|
2022-01-11 12:34:48 +00:00
|
|
|
if (!appExists) {
|
|
|
|
return <NotFoundPage onBack={() => navigate(appsListPath)} />;
|
|
|
|
}
|
|
|
|
|
2020-07-22 10:54:15 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<AppActivateDialog
|
|
|
|
confirmButtonState={activateAppResult.status}
|
|
|
|
name={data?.app.name}
|
|
|
|
onClose={closeModal}
|
|
|
|
onConfirm={handleActivateConfirm}
|
|
|
|
open={params.action === "app-activate"}
|
|
|
|
/>
|
|
|
|
<AppDeactivateDialog
|
|
|
|
confirmButtonState={deactivateAppResult.status}
|
|
|
|
name={data?.app.name}
|
|
|
|
onClose={closeModal}
|
|
|
|
onConfirm={handleDeactivateConfirm}
|
|
|
|
open={params.action === "app-deactivate"}
|
|
|
|
/>
|
|
|
|
<AppDetailsPage
|
|
|
|
data={data?.app}
|
|
|
|
loading={loading}
|
|
|
|
navigateToAppSettings={() => navigate(appSettingsUrl(id))}
|
|
|
|
onAppActivateOpen={() => openModal("app-activate")}
|
|
|
|
onAppDeactivateOpen={() => openModal("app-deactivate")}
|
|
|
|
onBack={() => navigate(appsListPath)}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default AppDetails;
|