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

102 lines
3.5 KiB
TypeScript
Raw Normal View History

2019-10-09 06:01:52 +00:00
import { WindowTitle } from "@saleor/components/WindowTitle";
import useNavigator from "@saleor/hooks/useNavigator";
import useNotifier from "@saleor/hooks/useNotifier";
import React from "react";
import { useIntl } from "react-intl";
import { getMutationState, maybe } from "../../misc";
import WebhooksDetailsPage from "../components/WebhooksDetailsPage";
2019-10-09 06:56:46 +00:00
import { TypedWebhookUpdate } from "../mutations";
2019-10-09 20:50:03 +00:00
import { TypedServiceListQuery, TypedWebhooksDetailsQuery } from "../queries";
2019-10-09 06:56:46 +00:00
import { webhooksListUrl, WebhooksListUrlQueryParams } 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-09 06:56:46 +00:00
export const WebhooksDetails: React.StatelessComponent<
WebhooksDetailsProps
> = ({ id }) => {
2019-10-09 06:01:52 +00:00
const navigate = useNavigator();
const notify = useNotifier();
const intl = useIntl();
return (
2019-10-09 06:56:46 +00:00
<TypedWebhookUpdate>
{(webhookUpdate, webhookUpdateOpts) => (
<TypedWebhooksDetailsQuery variables={{ id }}>
{WebhookDetails => {
2019-10-09 06:01:52 +00:00
const formTransitionState = getMutationState(
2019-10-09 06:56:46 +00:00
webhookUpdateOpts.called,
webhookUpdateOpts.loading,
maybe(() => webhookUpdateOpts.data.webhookUpdate.errors)
2019-10-09 06:01:52 +00:00
);
const formErrors = maybe(
2019-10-09 06:56:46 +00:00
() => webhookUpdateOpts.data.webhookUpdate.errors,
2019-10-09 06:01:52 +00:00
[]
);
if (formErrors.length) {
formErrors.map(error => {
notify({
text: error.message
});
});
} else {
2019-10-09 06:56:46 +00:00
if (webhookUpdateOpts.data) {
2019-10-09 06:01:52 +00:00
notify({
text: intl.formatMessage({
defaultMessage: "Succesfully updated plugin settings",
description: "plugin success message"
})
});
}
}
return (
2019-10-09 20:50:03 +00:00
<TypedServiceListQuery variables={{ first: 99 }}>
{({ data }) => (
<>
<WindowTitle
title={maybe(() => WebhookDetails.data.webhook.name)}
/>
<WebhooksDetailsPage
disabled={WebhookDetails.loading}
errors={formErrors}
saveButtonBarState={formTransitionState}
webhook={maybe(() => WebhookDetails.data.webhook)}
services={maybe(() =>
data.serviceAccounts.edges.map(edge => edge.node)
)}
onBack={() => navigate(webhooksListUrl())}
onSubmit={data => {
webhookUpdate({
variables: {
id,
input: {
events: data.events,
isActive: data.isActive,
name: data.name,
secretKey: data.secretKey,
serviceAccount: data.serviceAccount,
targetUrl: data.targetUrl
}
}
});
}}
/>
</>
)}
</TypedServiceListQuery>
2019-10-09 06:01:52 +00:00
);
}}
2019-10-09 06:56:46 +00:00
</TypedWebhooksDetailsQuery>
2019-10-09 06:01:52 +00:00
)}
2019-10-09 06:56:46 +00:00
</TypedWebhookUpdate>
2019-10-09 06:01:52 +00:00
);
};
2019-10-09 06:56:46 +00:00
WebhooksDetails.displayName = "WebhooksDetails";
export default WebhooksDetails;