saleor-dashboard/src/webhooks/views/WebhooksDetails.tsx

167 lines
6 KiB
TypeScript
Raw Normal View History

2019-10-09 06:01:52 +00:00
import { WindowTitle } from "@saleor/components/WindowTitle";
import SearchServiceAccount from "@saleor/containers/SearchServiceAccount";
2019-10-09 06:01:52 +00:00
import useNavigator from "@saleor/hooks/useNavigator";
import useNotifier from "@saleor/hooks/useNotifier";
2019-10-10 05:38:21 +00:00
import { commonMessages } from "@saleor/intl";
2019-10-10 14:19:06 +00:00
import { WebhookEventTypeEnum } from "@saleor/types/globalTypes";
2019-10-10 05:38:21 +00:00
import WebhookDeleteDialog from "@saleor/webhooks/components/WebhookDeleteDialog";
import { WebhookDelete } from "@saleor/webhooks/types/WebhookDelete";
2019-10-11 13:35:33 +00:00
import { WebhookUpdate } from "@saleor/webhooks/types/WebhookUpdate";
2019-10-09 06:01:52 +00:00
import React from "react";
import { useIntl } from "react-intl";
import { DEFAULT_INITIAL_SEARCH_DATA } from "../../config";
2019-10-09 06:01:52 +00:00
import { getMutationState, maybe } from "../../misc";
import WebhooksDetailsPage from "../components/WebhooksDetailsPage";
2019-10-10 05:38:21 +00:00
import { TypedWebhookDelete, TypedWebhookUpdate } from "../mutations";
2019-10-11 13:35:33 +00:00
import { TypedWebhooksDetailsQuery } from "../queries";
2019-10-10 05:38:21 +00:00
import {
webhooksListUrl,
WebhooksListUrlQueryParams,
webhooksUrl,
WebhookUrlDialog
} from "../urls";
2019-10-09 06:01:52 +00:00
2019-10-09 06:56:46 +00:00
export interface WebhooksDetailsProps {
2019-10-09 06:01:52 +00:00
id: string;
2019-10-09 06:56:46 +00:00
params: WebhooksListUrlQueryParams;
2019-10-09 06:01:52 +00:00
}
2019-10-17 11:59:18 +00:00
export const WebhooksDetails: React.FC<WebhooksDetailsProps> = ({
id,
params
}) => {
2019-10-09 06:01:52 +00:00
const navigate = useNavigator();
const notify = useNotifier();
const intl = useIntl();
2019-10-10 05:38:21 +00:00
const closeModal = () =>
navigate(
webhooksUrl(id, {
...params,
action: undefined,
id: undefined
}),
true
);
const openModal = (action: WebhookUrlDialog, tokenId?: string) =>
navigate(
webhooksUrl(id, {
...params,
action,
id: tokenId
})
);
const onWebhookDelete = (data: WebhookDelete) => {
if (data.webhookDelete.errors.length === 0) {
notify({
text: intl.formatMessage(commonMessages.savedChanges)
});
navigate(webhooksListUrl());
}
};
2019-10-11 13:35:33 +00:00
const onWebhookUpdate = (data: WebhookUpdate) => {
if (data.webhookUpdate.errors.length === 0) {
notify({
text: intl.formatMessage(commonMessages.savedChanges)
});
navigate(webhooksUrl(data.webhookUpdate.webhook.id));
}
};
2019-10-09 06:01:52 +00:00
return (
<SearchServiceAccount variables={DEFAULT_INITIAL_SEARCH_DATA}>
{({ search: searchServiceAccount, result: searchServiceAccountOpt }) => (
<TypedWebhookUpdate onCompleted={onWebhookUpdate}>
{(webhookUpdate, webhookUpdateOpts) => (
<TypedWebhookDelete onCompleted={onWebhookDelete}>
{(webhookDelete, webhookDeleteOpts) => (
<TypedWebhooksDetailsQuery variables={{ id }}>
2019-10-17 11:59:18 +00:00
{webhookDetails => {
const formTransitionState = getMutationState(
webhookUpdateOpts.called,
webhookUpdateOpts.loading,
maybe(() => webhookUpdateOpts.data.webhookUpdate.errors)
);
2019-10-10 05:38:21 +00:00
const handleRemoveConfirm = () =>
webhookDelete({
variables: {
id
}
});
2019-10-10 05:38:21 +00:00
const formErrors = maybe(
() => webhookUpdateOpts.data.webhookUpdate.errors,
[]
);
2019-10-09 06:01:52 +00:00
const deleteTransitionState = getMutationState(
webhookDeleteOpts.called,
webhookDeleteOpts.loading,
maybe(() => webhookDeleteOpts.data.webhookDelete.errors)
);
2019-10-09 06:01:52 +00:00
return (
<>
<WindowTitle
2019-10-17 11:59:18 +00:00
title={maybe(() => webhookDetails.data.webhook.name)}
/>
<WebhooksDetailsPage
2019-10-17 11:59:18 +00:00
disabled={webhookDetails.loading}
errors={formErrors}
saveButtonBarState={formTransitionState}
2019-10-17 11:59:18 +00:00
webhook={maybe(() => webhookDetails.data.webhook)}
fetchServiceAccounts={searchServiceAccount}
services={maybe(() =>
2019-10-17 12:38:01 +00:00
searchServiceAccountOpt.data.search.edges.map(
edge => edge.node
)
)}
onBack={() => navigate(webhooksListUrl())}
onDelete={() => openModal("remove")}
onSubmit={data => {
webhookUpdate({
variables: {
id,
input: {
events: data.allEvents
? [WebhookEventTypeEnum.ANY_EVENTS]
2019-10-17 14:39:37 +00:00
: data.events,
isActive: data.isActive,
name: data.name,
secretKey: data.secretKey,
serviceAccount: data.serviceAccount,
targetUrl: data.targetUrl
}
}
});
}}
/>
<WebhookDeleteDialog
confirmButtonState={deleteTransitionState}
name={maybe(
2019-10-17 11:59:18 +00:00
() => webhookDetails.data.webhook.name,
"..."
)}
onClose={closeModal}
onConfirm={handleRemoveConfirm}
open={params.action === "remove"}
/>
</>
);
}}
</TypedWebhooksDetailsQuery>
)}
</TypedWebhookDelete>
2019-10-10 05:38:21 +00:00
)}
</TypedWebhookUpdate>
2019-10-09 06:01:52 +00:00
)}
</SearchServiceAccount>
2019-10-09 06:01:52 +00:00
);
};
2019-10-09 06:56:46 +00:00
WebhooksDetails.displayName = "WebhooksDetails";
export default WebhooksDetails;