import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { Button, Select, showToast } from "@calcom/ui"; export interface IAppConfigurationProps { credentialIds: number[]; } const saveSettings = async ({ parameter, sleepValue, }: { parameter: { label: string; value: string }; sleepValue: number; }) => { try { const response = await fetch("/api/integrations/vital/save", { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ sleepValue, parameter: parameter.value, }), }); if (response.ok && response.status === 200) { return true; } } catch (error) { if (error instanceof Error) { console.error(error.message); } } }; const AppConfiguration = (props: IAppConfigurationProps) => { const { t } = useTranslation(); const [credentialId] = props.credentialIds; const options = [ { label: t("vital_app_total_label", { ns: "vital" }), value: "total", }, { label: t("vital_app_duration_label", { ns: "vital" }), value: "duration", }, ]; const [selectedParam, setSelectedParam] = useState<{ label: string; value: string }>(options[0]); const [touchedForm, setTouchedForm] = useState(false); const defaultSleepValue = 0; const [sleepValue, setSleepValue] = useState(defaultSleepValue); const [connected, setConnected] = useState(false); const [saveLoading, setSaveLoading] = useState(false); useEffect(() => { async function getVitalsConfig() { const response = await fetch("/api/integrations/vital/settings", { method: "GET", headers: { "Content-Type": "application/json", }, }); if (response.status === 200) { const vitalSettings: { connected: boolean; parameter: string; sleepValue: number; } = await response.json(); if (vitalSettings && vitalSettings.connected) { setConnected(vitalSettings.connected); } if (vitalSettings.sleepValue && vitalSettings.parameter) { const selectedParam = options.find((item) => item.value === vitalSettings.parameter); if (selectedParam) { setSelectedParam(selectedParam); } setSleepValue(vitalSettings.sleepValue); } } } getVitalsConfig(); }, []); if (!credentialId) { return <>; } const disabledSaveButton = !touchedForm || sleepValue === 0; return (

{t("connected_vital_app", { ns: "vital" })} Vital App: {connected ? "Yes" : "No"}


{t("vital_app_sleep_automation", { ns: "vital" })}

{t("vital_app_automation_description", { ns: "vital" })}

{ setSleepValue(Number(e.currentTarget.value)); setTouchedForm(true); }} className="pr-12shadow-sm mt-1 block w-full rounded-sm border border-gray-300 py-2 pl-6 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm" />

{t("vital_app_hours", { ns: "vital" })}

); }; export default AppConfiguration;