Fix webhook events type

This commit is contained in:
Krzysztof Bialoglowicz 2019-10-10 13:40:31 +02:00
parent d802ae90ce
commit 8d1890162c
14 changed files with 147 additions and 128 deletions

View file

@ -3196,11 +3196,11 @@ type ProductVariant implements Node {
name: String! name: String!
product: Product! product: Product!
trackInventory: Boolean! trackInventory: Boolean!
quantity: Int!
quantityAllocated: Int! quantityAllocated: Int!
weight: Weight weight: Weight
privateMeta: [MetaStore]! privateMeta: [MetaStore]!
meta: [MetaStore]! meta: [MetaStore]!
quantity: Int!
stockQuantity: Int! stockQuantity: Int!
priceOverride: Money priceOverride: Money
price: Money @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.10, has been replaced by 'pricing.priceUndiscounted'") price: Money @deprecated(reason: "DEPRECATED: Will be removed in Saleor 2.10, has been replaced by 'pricing.priceUndiscounted'")
@ -4315,7 +4315,7 @@ enum WebhookErrorCode {
} }
type WebhookEvent { type WebhookEvent {
eventType: String eventType: WebhookEventTypeEnum
} }
enum WebhookEventTypeEnum { enum WebhookEventTypeEnum {

View file

@ -662,11 +662,6 @@ export interface SeoInput {
description?: string | null; description?: string | null;
} }
export interface ServiceAccountFilterInput {
search?: string | null;
isActive?: boolean | null;
}
export interface ServiceAccountInput { export interface ServiceAccountInput {
name?: string | null; name?: string | null;
isActive?: boolean | null; isActive?: boolean | null;

View file

@ -9,6 +9,7 @@ import SaveButtonBar from "@saleor/components/SaveButtonBar";
import { sectionNames } from "@saleor/intl"; import { sectionNames } from "@saleor/intl";
import { maybe } from "@saleor/misc"; import { maybe } from "@saleor/misc";
import { UserError } from "@saleor/types"; import { UserError } from "@saleor/types";
import { WebhookEventTypeEnum } from "@saleor/types/globalTypes";
import React from "react"; import React from "react";
import { useIntl } from "react-intl"; import { useIntl } from "react-intl";
import { ServiceList_serviceAccounts_edges_node } from "../../types/ServiceList"; import { ServiceList_serviceAccounts_edges_node } from "../../types/ServiceList";
@ -18,12 +19,13 @@ import WebhookStatus from "../WebhookStatus";
export interface FormData { export interface FormData {
id: string; id: string;
events: string[]; events: WebhookEventTypeEnum[];
isActive: boolean; isActive: boolean;
name: string; name: string;
secretKey: string | null; secretKey: string | null;
targetUrl: string; targetUrl: string;
serviceAccount: string; serviceAccount: string;
allEvents: boolean;
} }
export interface WebhookCreatePageProps { export interface WebhookCreatePageProps {
@ -45,6 +47,7 @@ const WebhookCreatePage: React.StatelessComponent<WebhookCreatePageProps> = ({
}) => { }) => {
const intl = useIntl(); const intl = useIntl();
const initialForm: FormData = { const initialForm: FormData = {
allEvents: false,
events: [], events: [],
id: null, id: null,
isActive: false, isActive: false,

View file

@ -1,54 +1,48 @@
import Card from "@material-ui/core/Card"; import Card from "@material-ui/core/Card";
import CardContent from "@material-ui/core/CardContent"; import CardContent from "@material-ui/core/CardContent";
import Typography from "@material-ui/core/Typography"; import Typography from "@material-ui/core/Typography";
import makeStyles from "@material-ui/styles/makeStyles";
import CardTitle from "@saleor/components/CardTitle"; import CardTitle from "@saleor/components/CardTitle";
import ControlledCheckbox from "@saleor/components/ControlledCheckbox"; import ControlledCheckbox from "@saleor/components/ControlledCheckbox";
import Hr from "@saleor/components/Hr";
import React from "react"; import React from "react";
import { useIntl } from "react-intl"; import { useIntl } from "react-intl";
import { WebhookEventTypeEnum } from "../../../types/globalTypes"; import { WebhookEventTypeEnum } from "../../../types/globalTypes";
import { FormData } from "../WebhooksDetailsPage";
interface WebhookEventsProps { interface WebhookEventsProps {
data: FormData; data: {
allEvents: boolean;
events: string[];
};
disabled: boolean; disabled: boolean;
onChange: (event: React.ChangeEvent<any>) => void; onChange: (event: React.ChangeEvent<any>, cb?: () => void) => void;
} }
const useStyles = makeStyles(() => ({
item: {
paddingBottom: 10,
paddingTop: 10
}
}));
const WebhookEvents: React.StatelessComponent<WebhookEventsProps> = ({ const WebhookEvents: React.StatelessComponent<WebhookEventsProps> = ({
data, data,
disabled, disabled,
onChange onChange
}) => { }) => {
const intl = useIntl(); const intl = useIntl();
const [events, setEvents] = React.useState(data.events);
const eventsEnum = Object.values(WebhookEventTypeEnum); const eventsEnum = Object.values(WebhookEventTypeEnum);
const addOrRemove = (array, value) => { const handleAllEventsChange = (event: React.ChangeEvent<any>) =>
const index = array.indexOf(value); onChange(event, () =>
onChange({
if (index === -1) { target: {
array.push(value); name: "events",
} else { value: event.target.value ? eventsEnum.map(event => event) : []
array.splice(index, 1);
} }
}; } as any)
);
console.log(data.events); const handleEventsChange = (event: React.ChangeEvent<any>) => {
onChange({
const eventsOnChange = event => { target: {
const newData = events; name: "events",
addOrRemove(newData, event.target.name); value: event.target.value
setEvents(newData); ? data.events.concat([event.target.name])
console.log(events.indexOf(event.target.name)); : data.events.filter(events => events !== event.target.name)
}
} as any);
}; };
return ( return (
@ -67,17 +61,33 @@ const WebhookEvents: React.StatelessComponent<WebhookEventsProps> = ({
description: "webhook events" description: "webhook events"
})} })}
</Typography> </Typography>
{eventsEnum.map((event, index) => ( <ControlledCheckbox
checked={data.allEvents}
disabled={disabled}
label={intl.formatMessage({
defaultMessage: "All events",
description: "checkbox label"
})}
name="allEvents"
onChange={handleAllEventsChange}
/>
<Hr />
{!data.allEvents &&
eventsEnum.map((event, index) => {
if (index !== 0) {
return (
<div key={index}> <div key={index}>
<ControlledCheckbox <ControlledCheckbox
name={event} checked={data.events.includes(event)}
label={event}
checked={events.includes(WebhookEventTypeEnum[event])}
onChange={eventsOnChange}
disabled={disabled} disabled={disabled}
label={event.replace(/\./, "")}
name={event}
onChange={handleEventsChange}
/> />
</div> </div>
))} );
}
})}
</CardContent> </CardContent>
</Card> </Card>
); );

View file

@ -20,12 +20,13 @@ import WebhookStatus from "../WebhookStatus";
export interface FormData { export interface FormData {
id: string; id: string;
events: string[]; events: WebhookEventTypeEnum[];
isActive: boolean; isActive: boolean;
name: string; name: string;
secretKey: string | null; secretKey: string | null;
targetUrl: string; targetUrl: string;
serviceAccount: string; serviceAccount: string;
allEvents: boolean;
} }
export interface WebhooksDetailsPageProps { export interface WebhooksDetailsPageProps {
@ -53,6 +54,16 @@ const WebhooksDetailsPage: React.StatelessComponent<
}) => { }) => {
const intl = useIntl(); const intl = useIntl();
const initialForm: FormData = { const initialForm: FormData = {
allEvents: maybe(
() =>
Object.values(WebhookEventTypeEnum).filter(
perm =>
maybe(() => webhook.events, []).filter(
event => event.eventType === perm
).length === 0
).length === 0,
false
),
events: maybe(() => webhook.events, []).map(event => event.eventType), events: maybe(() => webhook.events, []).map(event => event.eventType),
id: maybe(() => webhook.id, null), id: maybe(() => webhook.id, null),
isActive: maybe(() => webhook.isActive, false), isActive: maybe(() => webhook.isActive, false),
@ -93,8 +104,8 @@ const WebhooksDetailsPage: React.StatelessComponent<
<div> <div>
<WebhookEvents <WebhookEvents
data={data} data={data}
disabled={disabled}
onChange={change} onChange={change}
disabled={disabled}
/> />
<FormSpacer /> <FormSpacer />
<WebhookStatus <WebhookStatus

View file

@ -12,8 +12,8 @@ import TableCell from "@material-ui/core/TableCell";
import TableFooter from "@material-ui/core/TableFooter"; import TableFooter from "@material-ui/core/TableFooter";
import TableHead from "@material-ui/core/TableHead"; import TableHead from "@material-ui/core/TableHead";
import TableRow from "@material-ui/core/TableRow"; import TableRow from "@material-ui/core/TableRow";
import EditIcon from "@material-ui/icons/Edit";
import DeleteIcon from "@material-ui/icons/Delete"; import DeleteIcon from "@material-ui/icons/Delete";
import EditIcon from "@material-ui/icons/Edit";
import React from "react"; import React from "react";
import { useIntl } from "react-intl"; import { useIntl } from "react-intl";
@ -68,6 +68,7 @@ const WebhooksList = withStyles(styles, { name: "PluginList" })(
<Card> <Card>
<Table> <Table>
<TableHead> <TableHead>
<TableRow>
<TableCell className={classes.colName} padding="dense"> <TableCell className={classes.colName} padding="dense">
{intl.formatMessage({ {intl.formatMessage({
defaultMessage: "Name", defaultMessage: "Name",
@ -86,6 +87,7 @@ const WebhooksList = withStyles(styles, { name: "PluginList" })(
description: "user action bar" description: "user action bar"
})} })}
</TableCell> </TableCell>
</TableRow>
</TableHead> </TableHead>
<TableFooter> <TableFooter>
<TableRow> <TableRow>

View file

@ -1,57 +1,47 @@
import { ConfigurationTypeFieldEnum } from "@saleor/types/globalTypes"; import { Webhook_webhook } from "./types/Webhook";
import { Plugin_plugin } from "./types/Plugin"; import { Webhooks_webhooks_edges_node } from "./types/Webhooks";
import { Plugins_plugins_edges_node } from "./types/Plugins";
export const pluginList: Plugins_plugins_edges_node[] = [ export const webhookList: Webhooks_webhooks_edges_node[] = [
{ {
__typename: "Plugin", __typename: "Webhook",
active: true, events: [],
description:
"Lorem ipsum dolor sit amet enim. Etiam ullamcorper. Suspendisse a pellentesque dui, non felis. Maecenas malesuada elit lectus felis, malesuada ultricies. Curabitur et ligula. Ut molestie a, ultricies porta urna. Vestibulum commodo volutpat a, convallis ac, laoreet enim. Phasellus fermentum in, dolor. Pellentesque facilisis. Nulla imperdiet sit amet magna.",
id: "Jzx123sEt==", id: "Jzx123sEt==",
name: "Avalara" isActive: true,
name: "Webhook Test",
secretKey: "dsdasdasd_asdas",
serviceAccount: {
__typename: "ServiceAccount",
id: "Jzx123sEt==",
name: "Test Account"
},
targetUrl: "http://www.getsaleor.com"
}, },
{ {
__typename: "Plugin", __typename: "Webhook",
active: false, events: [],
description:
"Lorem ipsum dolor sit amet enim. Etiam ullamcorper. Suspendisse a pellentesque dui, non felis. Maecenas malesuada elit lectus felis, malesuada ultricies. Curabitur et ligula. Ut molestie a, ultricies porta urna. Vestibulum commodo volutpat a, convallis ac, laoreet enim. Phasellus fermentum in, dolor. Pellentesque facilisis. Nulla imperdiet sit amet magna.",
id: "Jzx123sEt==", id: "Jzx123sEt==",
name: "VatLayer" isActive: true,
name: "Webhook Test 2",
secretKey: "zxczx_asdas",
serviceAccount: {
__typename: "ServiceAccount",
id: "Jzx1ss23sEt==",
name: "Test Account 2"
},
targetUrl: "http://www.getsaleor.com"
} }
]; ];
export const plugin: Plugin_plugin = { export const webhook: Webhook_webhook = {
__typename: "Plugin", __typename: "Webhook",
active: true, events: [],
configuration: [ id: "Jzx123sEt==",
{ isActive: true,
__typename: "ConfigurationItem", name: "Webhook Test 2",
helpText: "Provide user or account details", secretKey: "zxczx_asdas",
label: "Username or account", serviceAccount: {
name: "Username or account", __typename: "ServiceAccount",
type: ConfigurationTypeFieldEnum.STRING, id: "Jzx1ss23sEt==",
value: "" name: "Test Account 2"
}, },
{ targetUrl: "http://www.getsaleor.com"
__typename: "ConfigurationItem",
helpText: "Provide password or license details",
label: "Password or license",
name: "Password or license",
type: ConfigurationTypeFieldEnum.STRING,
value: ""
},
{
__typename: "ConfigurationItem",
helpText: "Determines if Saleor should use Avatax sandbox API.",
label: "Use sandbox",
name: "Use sandbox",
type: ConfigurationTypeFieldEnum.BOOLEAN,
value: "true"
}
],
description:
"Lorem ipsum dolor sit amet enim. Etiam ullamcorper. Suspendisse a pellentesque dui, non felis. Maecenas malesuada elit lectus felis, malesuada ultricies. Curabitur et ligula. Ut molestie a, ultricies porta urna. Vestibulum commodo volutpat a, convallis ac, laoreet enim. Phasellus fermentum in, dolor. Pellentesque facilisis. Nulla imperdiet sit amet magna.",
id: "UGx1Z2luQ29uZmlndXJhdGlvbjoy",
name: "Username or account"
}; };

View file

@ -2,13 +2,15 @@
/* eslint-disable */ /* eslint-disable */
// This file was automatically generated and should not be edited. // This file was automatically generated and should not be edited.
import { WebhookEventTypeEnum } from "./../../types/globalTypes";
// ==================================================== // ====================================================
// GraphQL query operation: Webhook // GraphQL query operation: Webhook
// ==================================================== // ====================================================
export interface Webhook_webhook_events { export interface Webhook_webhook_events {
__typename: "WebhookEvent"; __typename: "WebhookEvent";
eventType: string | null; eventType: WebhookEventTypeEnum | null;
} }
export interface Webhook_webhook_serviceAccount { export interface Webhook_webhook_serviceAccount {

View file

@ -2,7 +2,7 @@
/* eslint-disable */ /* eslint-disable */
// This file was automatically generated and should not be edited. // This file was automatically generated and should not be edited.
import { WebhookCreateInput } from "./../../types/globalTypes"; import { WebhookCreateInput, WebhookEventTypeEnum } from "./../../types/globalTypes";
// ==================================================== // ====================================================
// GraphQL mutation operation: WebhookCreate // GraphQL mutation operation: WebhookCreate
@ -16,7 +16,7 @@ export interface WebhookCreate_webhookCreate_errors {
export interface WebhookCreate_webhookCreate_webhook_events { export interface WebhookCreate_webhookCreate_webhook_events {
__typename: "WebhookEvent"; __typename: "WebhookEvent";
eventType: string | null; eventType: WebhookEventTypeEnum | null;
} }
export interface WebhookCreate_webhookCreate_webhook_serviceAccount { export interface WebhookCreate_webhookCreate_webhook_serviceAccount {

View file

@ -2,13 +2,15 @@
/* eslint-disable */ /* eslint-disable */
// This file was automatically generated and should not be edited. // This file was automatically generated and should not be edited.
import { WebhookEventTypeEnum } from "./../../types/globalTypes";
// ==================================================== // ====================================================
// GraphQL fragment: WebhookFragment // GraphQL fragment: WebhookFragment
// ==================================================== // ====================================================
export interface WebhookFragment_events { export interface WebhookFragment_events {
__typename: "WebhookEvent"; __typename: "WebhookEvent";
eventType: string | null; eventType: WebhookEventTypeEnum | null;
} }
export interface WebhookFragment_serviceAccount { export interface WebhookFragment_serviceAccount {

View file

@ -2,7 +2,7 @@
/* eslint-disable */ /* eslint-disable */
// This file was automatically generated and should not be edited. // This file was automatically generated and should not be edited.
import { WebhookUpdateInput } from "./../../types/globalTypes"; import { WebhookUpdateInput, WebhookEventTypeEnum } from "./../../types/globalTypes";
// ==================================================== // ====================================================
// GraphQL mutation operation: WebhookUpdate // GraphQL mutation operation: WebhookUpdate
@ -16,7 +16,7 @@ export interface WebhookUpdate_webhookUpdate_errors {
export interface WebhookUpdate_webhookUpdate_webhook_events { export interface WebhookUpdate_webhookUpdate_webhook_events {
__typename: "WebhookEvent"; __typename: "WebhookEvent";
eventType: string | null; eventType: WebhookEventTypeEnum | null;
} }
export interface WebhookUpdate_webhookUpdate_webhook_serviceAccount { export interface WebhookUpdate_webhookUpdate_webhook_serviceAccount {

View file

@ -2,13 +2,15 @@
/* eslint-disable */ /* eslint-disable */
// This file was automatically generated and should not be edited. // This file was automatically generated and should not be edited.
import { WebhookEventTypeEnum } from "./../../types/globalTypes";
// ==================================================== // ====================================================
// GraphQL query operation: Webhooks // GraphQL query operation: Webhooks
// ==================================================== // ====================================================
export interface Webhooks_webhooks_edges_node_events { export interface Webhooks_webhooks_edges_node_events {
__typename: "WebhookEvent"; __typename: "WebhookEvent";
eventType: string | null; eventType: WebhookEventTypeEnum | null;
} }
export interface Webhooks_webhooks_edges_node_serviceAccount { export interface Webhooks_webhooks_edges_node_serviceAccount {

View file

@ -2,13 +2,15 @@
/* eslint-disable */ /* eslint-disable */
// This file was automatically generated and should not be edited. // This file was automatically generated and should not be edited.
import { WebhookEventTypeEnum } from "./../../types/globalTypes";
// ==================================================== // ====================================================
// GraphQL fragment: WebhooksDetailsFragment // GraphQL fragment: WebhooksDetailsFragment
// ==================================================== // ====================================================
export interface WebhooksDetailsFragment_events { export interface WebhooksDetailsFragment_events {
__typename: "WebhookEvent"; __typename: "WebhookEvent";
eventType: string | null; eventType: WebhookEventTypeEnum | null;
} }
export interface WebhooksDetailsFragment_serviceAccount { export interface WebhooksDetailsFragment_serviceAccount {

View file

@ -2,8 +2,8 @@ import { WindowTitle } from "@saleor/components/WindowTitle";
import useNavigator from "@saleor/hooks/useNavigator"; import useNavigator from "@saleor/hooks/useNavigator";
import useNotifier from "@saleor/hooks/useNotifier"; import useNotifier from "@saleor/hooks/useNotifier";
import { commonMessages } from "@saleor/intl"; import { commonMessages } from "@saleor/intl";
import { WebhookCreate as WebhookCreateData } from "@saleor/webhooks/types/WebhookCreate";
import { WebhookEventTypeEnum } from "@saleor/types/globalTypes"; import { WebhookEventTypeEnum } from "@saleor/types/globalTypes";
import { WebhookCreate as WebhookCreateData } from "@saleor/webhooks/types/WebhookCreate";
import React from "react"; import React from "react";
import { useIntl } from "react-intl"; import { useIntl } from "react-intl";
@ -47,7 +47,7 @@ export const WebhooksCreate: React.StatelessComponent<
WebhookCreate({ WebhookCreate({
variables: { variables: {
input: { input: {
events: [WebhookEventTypeEnum.ALL_EVENTS], events: [WebhookEventTypeEnum.ORDER_CREATED],
isActive: data.isActive, isActive: data.isActive,
name: data.name, name: data.name,
secretKey: data.secretKey, secretKey: data.secretKey,