From 88b12d04c4631126ba6e9d28d50821eed5f26265 Mon Sep 17 00:00:00 2001 From: bsourisse Date: Thu, 9 Mar 2023 12:45:42 +0100 Subject: [PATCH] feat: select Apprise Mode --- .../AppriseMode/AppriseMode.js | 167 ++++++++++++++++++ styles/default.css | 1 + 2 files changed, 168 insertions(+) create mode 100644 Containers/UserSettings/AppriseAlertSettings/AppriseMode/AppriseMode.js diff --git a/Containers/UserSettings/AppriseAlertSettings/AppriseMode/AppriseMode.js b/Containers/UserSettings/AppriseAlertSettings/AppriseMode/AppriseMode.js new file mode 100644 index 0000000..132b68f --- /dev/null +++ b/Containers/UserSettings/AppriseAlertSettings/AppriseMode/AppriseMode.js @@ -0,0 +1,167 @@ +//Lib +import { useEffect } from 'react'; +import { toast } from 'react-toastify'; +import 'react-toastify/dist/ReactToastify.css'; +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 AppriseMode() { + //Var + const { + register, + handleSubmit, + formState: { errors }, + } = useForm({ mode: 'onBlur' }); + + ////State + const [formIsLoading, setFormIsLoading] = useState(false); + const [modeFormIsSaved, setModeFormIsSaved] = useState(false); + const [error, setError] = useState(false); + const [displayStatelessURL, setDisplayStatelessURL] = useState(false); + const [appriseMode, setAppriseMode] = useState(''); + const [appriseStatelessURL, setAppriseStatelessURL] = useState(); + + ////LifeCycle + //Component did mount + useEffect(() => { + //Initial fetch to get Apprise Mode enabled + const getAppriseMode = async () => { + try { + const response = await fetch('/api/account/getAppriseMode', { + method: 'GET', + headers: { + 'Content-type': 'application/json', + }, + }); + const { appriseStatelessURL, appriseMode } = + await response.json(); + setAppriseMode(appriseMode); + if (appriseMode == 'stateless') { + setAppriseStatelessURL(appriseStatelessURL); + setDisplayStatelessURL(true); + } + } catch (error) { + console.log('Fetching Apprise Mode failed.'); + } + }; + getAppriseMode(); + }, []); + + ////Functions + //Form submit handler to modify Apprise Mode + const modeFormSubmitHandler = async (data) => { + //Remove old error + setError(); + //Loading button on submit to avoid multiple send. + setFormIsLoading(true); + //POST API to update Apprise Mode + const response = await fetch('/api/account/updateAppriseMode', { + 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); + setModeFormIsSaved(true); + setTimeout(() => setModeFormIsSaved(false), 3000); + } + }; + + return ( + <> + {/* APPRISE MODE SELECTION */} +
+
Apprise mode
+
+ {formIsLoading && ( + + )} + {modeFormIsSaved && ( +
+ ✅ Apprise mode has been saved. +
+ )} +
+
+ {error && } +
+
+ + +
+ {displayStatelessURL && ( + + )} + {errors.appriseStatelessURL && ( + + {errors.appriseStatelessURL.message} + + )} +
+ + ); +} diff --git a/styles/default.css b/styles/default.css index a57d6a7..a48897e 100644 --- a/styles/default.css +++ b/styles/default.css @@ -204,6 +204,7 @@ code { .radio-group input[type='radio'] { opacity: 0; + width: 0; } .radio-group span {