import { useEffect, useState, useMemo } 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 = useMemo( () => [ { label: t("vital_app_total_label", { ns: "vital" }), value: "total" }, { label: t("vital_app_duration_label", { ns: "vital" }), value: "duration" }, ], [t] ); 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: { value: string }) => item.value === vitalSettings.parameter ); if (selectedParam) { setSelectedParam(selectedParam); } setSleepValue(vitalSettings.sleepValue); } } } getVitalsConfig(); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); 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" })}
{t("vital_app_hours", { ns: "vital" })}