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

104 lines
3.5 KiB
TypeScript
Raw Normal View History

2019-10-09 20:50:03 +00:00
import { WindowTitle } from "@saleor/components/WindowTitle";
import SearchServiceAccount from "@saleor/containers/SearchServiceAccount";
2019-10-09 20:50:03 +00:00
import useNavigator from "@saleor/hooks/useNavigator";
import useNotifier from "@saleor/hooks/useNotifier";
import { commonMessages } from "@saleor/intl";
2019-10-10 14:19:06 +00:00
import { WebhookEventTypeEnum } from "@saleor/types/globalTypes";
2019-10-10 11:40:31 +00:00
import { WebhookCreate as WebhookCreateData } from "@saleor/webhooks/types/WebhookCreate";
2019-10-09 20:50:03 +00:00
import React from "react";
import { useIntl } from "react-intl";
import { DEFAULT_INITIAL_SEARCH_DATA } from "../../config";
2019-10-09 20:50:03 +00:00
import { getMutationState, maybe } from "../../misc";
import WebhookCreatePage, { FormData } from "../components/WebhookCreatePage";
import { TypedWebhookCreate } from "../mutations";
import {
webhooksListUrl,
WebhooksListUrlQueryParams,
webhooksUrl
} from "../urls";
export interface WebhooksCreateProps {
id: string;
params: WebhooksListUrlQueryParams;
}
export const WebhooksCreate: React.StatelessComponent<
WebhooksCreateProps
> = () => {
const navigate = useNavigator();
const notify = useNotifier();
const intl = useIntl();
const onSubmit = (data: WebhookCreateData) => {
2019-10-18 12:01:26 +00:00
if (data.webhookCreate.webhookErrors.length === 0) {
2019-10-09 20:50:03 +00:00
notify({
text: intl.formatMessage(commonMessages.savedChanges)
});
navigate(webhooksUrl(data.webhookCreate.webhook.id));
}
};
const handleBack = () => navigate(webhooksListUrl());
return (
<SearchServiceAccount variables={DEFAULT_INITIAL_SEARCH_DATA}>
{({ search: searchServiceAccount, result: searchServiceAccountOpt }) => (
<TypedWebhookCreate onCompleted={onSubmit}>
2019-10-17 11:59:18 +00:00
{(webhookCreate, webhookCreateOpts) => {
const handleSubmit = (data: FormData) =>
2019-10-17 11:59:18 +00:00
webhookCreate({
variables: {
input: {
events: data.allEvents
? [WebhookEventTypeEnum.ANY_EVENTS]
: data.events,
isActive: data.isActive,
name: data.name,
secretKey: data.secretKey,
serviceAccount: data.serviceAccount,
targetUrl: data.targetUrl
}
}
});
2019-10-09 20:50:03 +00:00
const formTransitionState = getMutationState(
webhookCreateOpts.called,
webhookCreateOpts.loading,
2019-10-18 12:01:26 +00:00
maybe(() => webhookCreateOpts.data.webhookCreate.webhookErrors)
);
2019-10-09 20:50:03 +00:00
return (
<>
<WindowTitle
title={intl.formatMessage({
defaultMessage: "Create Webhook",
description: "window title"
})}
/>
<WebhookCreatePage
disabled={false}
errors={maybe(
2019-10-18 12:01:26 +00:00
() => webhookCreateOpts.data.webhookCreate.webhookErrors,
[]
)}
2019-10-17 11:59:18 +00:00
fetchServiceAccounts={searchServiceAccount}
services={maybe(() =>
2019-10-17 12:38:01 +00:00
searchServiceAccountOpt.data.search.edges.map(
edge => edge.node
)
)}
onBack={handleBack}
onSubmit={handleSubmit}
saveButtonBarState={formTransitionState}
/>
</>
);
}}
</TypedWebhookCreate>
)}
</SearchServiceAccount>
2019-10-09 20:50:03 +00:00
);
};
WebhooksCreate.displayName = "WebhooksCreate";
export default WebhooksCreate;