'use client'; import { useFetch } from '@gitroom/helpers/utils/custom.fetch'; import useSWR from 'swr'; import React, { FC, useCallback, useState } from 'react'; import { Button } from '@gitroom/react/form/button'; import { useRouter } from 'next/navigation'; import { useModals } from '@gitroom/frontend/components/layout/new-modal'; import { FieldValues, FormProvider, useForm } from 'react-hook-form'; import { useT } from '@gitroom/react/translation/get.transation.service.client'; import { Input } from '@gitroom/react/form/input'; import { useToaster } from '@gitroom/react/toaster/toaster'; import { ModalWrapperComponent } from '@gitroom/frontend/components/new-launch/modal.wrapper.component'; export const ApiModal: FC<{ identifier: string; title: string; update: () => void; }> = (props) => { const { title, identifier, update } = props; const fetch = useFetch(); const router = useRouter(); const modal = useModals(); const toaster = useToaster(); const [loading, setLoading] = useState(false); const closePopup = useCallback(() => { modal.closeAll(); }, []); const methods = useForm({ mode: 'onChange', }); const close = useCallback(() => { if (closePopup) { return closePopup(); } modal.closeAll(); }, []); const submit = useCallback( async (data: FieldValues) => { setLoading(true); const add = await fetch(`/third-party/${identifier}`, { method: 'POST', body: JSON.stringify({ api: data.api, }), }); if (add.ok) { toaster.show('Integration added successfully', 'success'); if (closePopup) { closePopup(); } else { modal.closeAll(); } router.refresh(); if (update) update(); return; } const { message } = await add.json(); methods.setError('api', { message, }); setLoading(false); }, [props] ); const t = useT(); return (