From c87a01728d778591441c9885acd509f3006feca2 Mon Sep 17 00:00:00 2001 From: bsourisse Date: Thu, 9 Mar 2023 17:45:32 +0100 Subject: [PATCH] feat: notify multiple Apprise services URLs --- .../AppriseURLs/AppriseURLs.js | 152 ++++++++++++++++++ pages/api/account/updateAppriseServices.js | 85 ++++++++++ 2 files changed, 237 insertions(+) create mode 100644 Containers/UserSettings/AppriseAlertSettings/AppriseURLs/AppriseURLs.js diff --git a/Containers/UserSettings/AppriseAlertSettings/AppriseURLs/AppriseURLs.js b/Containers/UserSettings/AppriseAlertSettings/AppriseURLs/AppriseURLs.js new file mode 100644 index 0000000..b17d00f --- /dev/null +++ b/Containers/UserSettings/AppriseAlertSettings/AppriseURLs/AppriseURLs.js @@ -0,0 +1,152 @@ +//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 + 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); + } + }; + + return ( + <> + {/* APPRISE SERVICES URLS */} +
+
Apprise URLs
+ {error && } +
+ {formIsLoading && ( + + )} + {urlsFormIsSaved && ( +
+ ✅ Apprise configuration has been saved. +
+ )} +
+
+
+