import React, { useState, useEffect } from "react"; import { syntaxHighlight } from "utilities/helpers"; import Button from "components/buttons/Button"; import Checkbox from "components/forms/fields/Checkbox"; // @ts-ignore import Dropdown from "components/forms/fields/Dropdown"; // @ts-ignore import InputField from "components/forms/fields/InputField"; import Modal from "components/Modal"; import { IAppConfigFormProps, IFormField, IAppConfigFormErrors, percentageOfHosts, numberOfDays, hostStatusPreview, } from "../constants"; const baseClass = "app-config-form"; const HostStatusWebhook = ({ appConfig, handleSubmit, isUpdatingSettings, }: IAppConfigFormProps): JSX.Element => { const [ showHostStatusWebhookPreviewModal, setShowHostStatusWebhookPreviewModal, ] = useState(false); const [formData, setFormData] = useState({ enableHostStatusWebhook: appConfig.webhook_settings.host_status_webhook .enable_host_status_webhook || false, hostStatusWebhookDestinationURL: appConfig.webhook_settings.host_status_webhook.destination_url || "", hostStatusWebhookHostPercentage: appConfig.webhook_settings.host_status_webhook.host_percentage || undefined, hostStatusWebhookDaysCount: appConfig.webhook_settings.host_status_webhook.days_count || undefined, }); const { enableHostStatusWebhook, hostStatusWebhookDestinationURL, hostStatusWebhookHostPercentage, hostStatusWebhookDaysCount, } = formData; const [formErrors, setFormErrors] = useState({}); const handleInputChange = ({ name, value }: IFormField) => { setFormData({ ...formData, [name]: value }); setFormErrors({}); }; const validateForm = () => { const errors: IAppConfigFormErrors = {}; if (enableHostStatusWebhook) { if (!hostStatusWebhookDestinationURL) { errors.destination_url = "Destination URL must be present"; } if (!hostStatusWebhookDaysCount) { errors.days_count = "Number of days must be present"; } if (!hostStatusWebhookDaysCount) { errors.host_percentage = "Percentage of hosts must be present"; } } setFormErrors(errors); }; useEffect(() => { validateForm(); }, [enableHostStatusWebhook]); const toggleHostStatusWebhookPreviewModal = () => { setShowHostStatusWebhookPreviewModal(!showHostStatusWebhookPreviewModal); return false; }; const onFormSubmit = (evt: React.MouseEvent) => { evt.preventDefault(); // Formatting of API not UI const formDataToSubmit = { webhook_settings: { host_status_webhook: { enable_host_status_webhook: enableHostStatusWebhook, destination_url: hostStatusWebhookDestinationURL, host_percentage: hostStatusWebhookHostPercentage, days_count: hostStatusWebhookDaysCount, }, }, }; handleSubmit(formDataToSubmit); }; const renderHostStatusWebhookPreviewModal = () => { if (!showHostStatusWebhookPreviewModal) { return null; } return ( <>

An example request sent to your configured Destination URL.

          
); }; return ( <>

Host status webhook

Send an alert if a portion of your hosts go offline.

Enable host status webhook

A request will be sent to your configured Destination URL{" "} if the configured Percentage of hosts have not checked into Fleet for the configured Number of days.

Provide a URL to deliver
the webhook request to.

\ " } />
Select the minimum percentage of hosts that
must fail to check into Fleet in order to trigger
the webhook request.

\ " } />
Select the minimum number of days that the
configured Percentage of hosts must fail to
check into Fleet in order to trigger the
webhook request.

\ " } />
{showHostStatusWebhookPreviewModal && renderHostStatusWebhookPreviewModal()} ); }; export default HostStatusWebhook;