2021-12-13 14:43:30 +00:00
|
|
|
import { MultiAutocompleteChoiceType } from "@saleor/components/MultiAutocompleteSelectField";
|
2020-07-07 10:14:12 +00:00
|
|
|
import { WebhookFragment } from "@saleor/fragments/types/WebhookFragment";
|
2021-12-13 14:43:30 +00:00
|
|
|
import {
|
|
|
|
WebhookEventTypeAsync,
|
|
|
|
WebhookEventTypeSync
|
|
|
|
} from "@saleor/types/globalTypes";
|
2020-03-18 11:03:20 +00:00
|
|
|
|
|
|
|
export function isUnnamed(webhook: WebhookFragment): boolean {
|
|
|
|
return ["", null].includes(webhook?.name);
|
|
|
|
}
|
2021-12-13 14:43:30 +00:00
|
|
|
|
|
|
|
export function mapSyncEventsToChoices(
|
|
|
|
events: WebhookEventTypeSync[]
|
|
|
|
): MultiAutocompleteChoiceType[] {
|
|
|
|
return events.map(event => ({
|
|
|
|
label: event,
|
|
|
|
value: event
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function mapAsyncEventsToChoices(
|
|
|
|
events: WebhookEventTypeAsync[],
|
|
|
|
selectedEvents: WebhookEventTypeAsync[]
|
|
|
|
): MultiAutocompleteChoiceType[] {
|
|
|
|
const isAnyAsyncEventSelected = selectedEvents.includes(
|
|
|
|
WebhookEventTypeAsync.ANY_EVENTS
|
|
|
|
);
|
|
|
|
|
|
|
|
return events.map(event => ({
|
|
|
|
label: event,
|
|
|
|
value: event,
|
|
|
|
disabled:
|
|
|
|
event !== WebhookEventTypeAsync.ANY_EVENTS && isAnyAsyncEventSelected
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
export const filterSelectedAsyncEvents = (
|
|
|
|
asyncEvents: WebhookEventTypeAsync[]
|
|
|
|
) => {
|
|
|
|
const anyEvent = asyncEvents.find(
|
|
|
|
event => event === WebhookEventTypeAsync.ANY_EVENTS
|
|
|
|
);
|
|
|
|
if (anyEvent) {
|
|
|
|
return [anyEvent];
|
|
|
|
}
|
|
|
|
return asyncEvents;
|
|
|
|
};
|