//Lib import { useEffect } from 'react'; import classes from '../../UserSettings.module.css'; import { useState } from 'react'; import { SpinnerCircularFixed } from 'spinners-react'; import { useForm } from 'react-hook-form'; //Components import Error from '../../../../Components/UI/Error/Error'; export default function AppriseURLs() { //Var const { register, handleSubmit, formState: { errors }, } = useForm({ mode: 'onBlur' }); ////State const [formIsLoading, setFormIsLoading] = useState(false); const [urlsFormIsSaved, setUrlsFormIsSaved] = useState(false); const [appriseServicesList, setAppriseServicesList] = useState(); const [error, setError] = useState(); ////LifeCycle //Component did mount useEffect(() => { //Initial fetch to build the list of Apprise Services enabled const getAppriseServices = async () => { try { const response = await fetch( '/api/account/getAppriseServices', { method: 'GET', headers: { 'Content-type': 'application/json', }, } ); let servicesArray = (await response.json()).appriseServices; const AppriseServicesListToText = () => { let list = ''; for (let service of servicesArray) { list += service + '\n'; } return list; }; setAppriseServicesList(AppriseServicesListToText()); } catch (error) { console.log('Fetching Apprise services list failed.'); } }; getAppriseServices(); }, []); ////Functions //Form submit handler to modify Apprise services const urlsFormSubmitHandler = async (data) => { //Remove old error setError(); //Loading button on submit to avoid multiple send. setFormIsLoading(true); //POST API to update Apprise Services try { const response = await fetch('/api/account/updateAppriseServices', { method: 'PUT', headers: { 'Content-type': 'application/json', }, body: JSON.stringify(data), }); const result = await response.json(); if (!response.ok) { setFormIsLoading(false); setError(result.message); setTimeout(() => setError(), 4000); } else { setFormIsLoading(false); setUrlsFormIsSaved(true); setTimeout(() => setUrlsFormIsSaved(false), 3000); } } catch (error) { setFormIsLoading(false); setError( 'Failed to update your services. Contact your administrator.' ); setTimeout(() => { setError(); }, 4000); } }; return ( <> {/* APPRISE SERVICES URLS */}
Apprise URLs
{error && }
{formIsLoading && ( )} {urlsFormIsSaved && (
✅ Apprise configuration has been saved.
)}