refactor toasts

This commit is contained in:
Tom
2023-06-07 18:24:02 +02:00
parent a6faed9ada
commit db2e273ea6
14 changed files with 121 additions and 87 deletions
+3 -5
View File
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react'; import React, { useEffect } from 'react';
import { Route, Routes, useLocation } from 'react-router-dom'; import { Route, Routes, useLocation } from 'react-router-dom';
import { Helmet } from 'react-helmet-async'; import { Helmet } from 'react-helmet-async';
import { useAccount, useBufferedFeeds } from '@plebbit/plebbit-react-hooks'; import { useAccount, useBufferedFeeds } from '@plebbit/plebbit-react-hooks';
@@ -40,10 +40,8 @@ export default function App() {
const account = useAccount(); const account = useAccount();
const [errorMessage, setErrorMessage] = useState(null); const setErrorMessage = useError();
const [successMessage, setSuccessMessage] = useState(null); const setSuccessMessage = useSuccess();
useError(errorMessage, [errorMessage]);
useSuccess(successMessage, [successMessage]);
useEffect(() => { useEffect(() => {
+9 -6
View File
@@ -26,10 +26,8 @@ const ModerationModal = ({ isOpen, closeModal, deletePost }) => {
const [reason, setReason] = useState(''); const [reason, setReason] = useState('');
const [triggerPublishCommentEdit, setTriggerPublishCommentEdit] = useState(false); const [triggerPublishCommentEdit, setTriggerPublishCommentEdit] = useState(false);
const [errorMessage, setErrorMessage] = useState(null); const setErrorMessage = useError();
const [successMessage, setSuccessMessage] = useState(null); const setSuccessMessage = useSuccess();
useError(errorMessage, [errorMessage]);
useSuccess(successMessage, [successMessage]);
useEffect(() => { useEffect(() => {
@@ -126,7 +124,7 @@ const ModerationModal = ({ isOpen, closeModal, deletePost }) => {
if (error) { if (error) {
setErrorMessage(error); setErrorMessage(error);
} }
}, [error]); }, [error, setErrorMessage]);
useEffect(() => { useEffect(() => {
@@ -140,11 +138,16 @@ const ModerationModal = ({ isOpen, closeModal, deletePost }) => {
useEffect(() => { useEffect(() => {
if (publishCommentEditOptions && triggerPublishCommentEdit) { if (publishCommentEditOptions && triggerPublishCommentEdit) {
(async () => { (async () => {
try {
await publishCommentEdit(); await publishCommentEdit();
} catch (error) {
setErrorMessage(error);
} finally {
setTriggerPublishCommentEdit(false); setTriggerPublishCommentEdit(false);
}
})(); })();
} }
}, [publishCommentEditOptions, triggerPublishCommentEdit, publishCommentEdit]); }, [publishCommentEditOptions, triggerPublishCommentEdit, publishCommentEdit, setErrorMessage]);
+2 -3
View File
@@ -23,18 +23,17 @@ const ReplyModal = ({ isOpen, closeModal }) => {
const account = useAccount(); const account = useAccount();
const setErrorMessage = useError();
const nodeRef = useRef(null); const nodeRef = useRef(null);
const nameRef = useRef(); const nameRef = useRef();
const commentRef = useRef(); const commentRef = useRef();
const linkRef = useRef(); const linkRef = useRef();
const [errorMessage, setErrorMessage] = useState(null);
const [triggerPublishComment, setTriggerPublishComment] = useState(false); const [triggerPublishComment, setTriggerPublishComment] = useState(false);
const [selectedText, setSelectedText] = useState(''); const [selectedText, setSelectedText] = useState('');
const [isMobile, setIsMobile] = useState(window.innerWidth <= 480); const [isMobile, setIsMobile] = useState(window.innerWidth <= 480);
useError(errorMessage, [errorMessage]);
useEffect(() => { useEffect(() => {
const handleResize = () => setIsMobile(window.innerWidth <= 480); const handleResize = () => setIsMobile(window.innerWidth <= 480);
+3 -5
View File
@@ -20,10 +20,8 @@ const SettingsModal = ({ isOpen, closeModal }) => {
const [expanded, setExpanded] = useState([]); const [expanded, setExpanded] = useState([]);
const [accountJson, setAccountJson] = useState(null); const [accountJson, setAccountJson] = useState(null);
const [errorMessage, setErrorMessage] = useState(null); const setErrorMessage = useError();
const [successMessage, setSuccessMessage] = useState(null); const setSuccessMessage = useSuccess();
useError(errorMessage, [errorMessage]);
useSuccess(successMessage, [successMessage]);
const account = useAccount(); const account = useAccount();
const { accounts } = useAccounts(); const { accounts } = useAccounts();
@@ -149,7 +147,7 @@ const SettingsModal = ({ isOpen, closeModal }) => {
setSuccessMessage("Cache Cleared"); setSuccessMessage("Cache Cleared");
localStorage.removeItem("cacheCleared"); localStorage.removeItem("cacheCleared");
} }
}, []); }, [setSuccessMessage]);
const handleExport = async () => { const handleExport = async () => {
+2 -3
View File
@@ -46,6 +46,7 @@ const All = () => {
const account = useAccount(); const account = useAccount();
const navigate = useNavigate(); const navigate = useNavigate();
const setErrorMessage = useError();
const threadMenuRefs = useRef({}); const threadMenuRefs = useRef({});
const replyMenuRefs = useRef({}); const replyMenuRefs = useRef({});
@@ -57,13 +58,11 @@ const All = () => {
const [prevScrollPos, setPrevScrollPos] = useState(0); const [prevScrollPos, setPrevScrollPos] = useState(0);
const [visible, setVisible] = useState(true); const [visible, setVisible] = useState(true);
const [rotatedStates, setRotatedStates] = useState({}); const [rotatedStates, setRotatedStates] = useState({});
const [errorMessage, setErrorMessage] = useState(null);
const [isImageSearchOpen, setIsImageSearchOpen] = useState(false); const [isImageSearchOpen, setIsImageSearchOpen] = useState(false);
const [outOfViewCid, setOutOfViewCid] = useState(null); const [outOfViewCid, setOutOfViewCid] = useState(null);
const [outOfViewPosition, setOutOfViewPosition] = useState({top: 0, left: 0}); const [outOfViewPosition, setOutOfViewPosition] = useState({top: 0, left: 0});
const [postOnHoverHeight, setPostOnHoverHeight] = useState(0); const [postOnHoverHeight, setPostOnHoverHeight] = useState(0);
useError(errorMessage, [errorMessage]);
const addresses = defaultSubplebbits.map(subplebbit => subplebbit.address); const addresses = defaultSubplebbits.map(subplebbit => subplebbit.address);
const { feed, hasMore, loadMore } = useFeed({subplebbitAddresses: addresses, sortType: 'new'}); const { feed, hasMore, loadMore } = useFeed({subplebbitAddresses: addresses, sortType: 'new'});
@@ -89,7 +88,7 @@ const All = () => {
if (errorString) { if (errorString) {
setErrorMessage(errorString) setErrorMessage(errorString)
} }
}, [errorString]) }, [errorString, setErrorMessage])
useEffect(() => { useEffect(() => {
+1 -4
View File
@@ -14,7 +14,6 @@ import OfflineIndicator from '../OfflineIndicator';
import SettingsModal from '../modals/SettingsModal'; import SettingsModal from '../modals/SettingsModal';
import getCommentMediaInfo from '../../utils/getCommentMediaInfo'; import getCommentMediaInfo from '../../utils/getCommentMediaInfo';
import handleStyleChange from '../../utils/handleStyleChange'; import handleStyleChange from '../../utils/handleStyleChange';
import useError from '../../hooks/useError';
import useFeedStateString from '../../hooks/useFeedStateString'; import useFeedStateString from '../../hooks/useFeedStateString';
import packageJson from '../../../package.json' import packageJson from '../../../package.json'
const {version} = packageJson const {version} = packageJson
@@ -31,6 +30,7 @@ const AllCatalog = () => {
} = useGeneralStore(state => state); } = useGeneralStore(state => state);
const navigate = useNavigate(); const navigate = useNavigate();
const [prevScrollPos, setPrevScrollPos] = useState(0); const [prevScrollPos, setPrevScrollPos] = useState(0);
const [visible, setVisible] = useState(true); const [visible, setVisible] = useState(true);
const addresses = defaultSubplebbits.map(subplebbit => subplebbit.address); const addresses = defaultSubplebbits.map(subplebbit => subplebbit.address);
@@ -40,9 +40,6 @@ const AllCatalog = () => {
const stateString = useFeedStateString(subplebbits); const stateString = useFeedStateString(subplebbits);
const [errorMessage] = useState(null);
useError(errorMessage, [errorMessage]);
// mobile navbar scroll effect // mobile navbar scroll effect
useEffect(() => { useEffect(() => {
const debouncedHandleScroll = debounce(() => { const debouncedHandleScroll = debounce(() => {
+21 -9
View File
@@ -69,6 +69,9 @@ const Board = () => {
const navigate = useNavigate(); const navigate = useNavigate();
const { subplebbitAddress } = useParams(); const { subplebbitAddress } = useParams();
const setErrorMessage = useError();
const setSuccessMessage = useSuccess();
const nameRef = useRef(); const nameRef = useRef();
const subjectRef = useRef(); const subjectRef = useRef();
const commentRef = useRef(); const commentRef = useRef();
@@ -103,11 +106,6 @@ const Board = () => {
const [outOfViewPosition, setOutOfViewPosition] = useState({top: 0, left: 0}); const [outOfViewPosition, setOutOfViewPosition] = useState({top: 0, left: 0});
const [postOnHoverHeight, setPostOnHoverHeight] = useState(0); const [postOnHoverHeight, setPostOnHoverHeight] = useState(0);
const [errorMessage, setErrorMessage] = useState(null);
const [successMessage, setSuccessMessage] = useState(null);
useError(errorMessage, [errorMessage]);
useSuccess(successMessage, [successMessage]);
useEffect(() => { useEffect(() => {
if (subplebbit.roles !== undefined) { if (subplebbit.roles !== undefined) {
@@ -162,7 +160,7 @@ const Board = () => {
if (errorString) { if (errorString) {
setErrorMessage(errorString); setErrorMessage(errorString);
} }
}, [errorString]); }, [errorString, setErrorMessage]);
const flattenedRepliesByThread = useMemo(() => { const flattenedRepliesByThread = useMemo(() => {
@@ -355,11 +353,17 @@ const Board = () => {
useEffect(() => { useEffect(() => {
if (publishCommentOptions && triggerPublishComment) { if (publishCommentOptions && triggerPublishComment) {
(async () => { (async () => {
try {
await publishComment(); await publishComment();
resetFields(); resetFields();
} catch (error) {
setErrorMessage(error);
} finally {
setTriggerPublishComment(false);
}
})(); })();
} }
}, [publishCommentOptions, triggerPublishComment, publishComment, resetFields]); }, [publishCommentOptions, triggerPublishComment, publishComment, resetFields, setErrorMessage]);
const getChallengeAnswersFromUser = async (challenges) => { const getChallengeAnswersFromUser = async (challenges) => {
@@ -415,7 +419,7 @@ const Board = () => {
if (error) { if (error) {
setErrorMessage(error); setErrorMessage(error);
} }
}, [error]); }, [error, setErrorMessage]);
const handleAuthorDeleteClick = (commentCid) => { const handleAuthorDeleteClick = (commentCid) => {
@@ -481,11 +485,19 @@ const Board = () => {
useEffect(() => { useEffect(() => {
if (publishCommentEditOptions && triggerPublishCommentEdit) { if (publishCommentEditOptions && triggerPublishCommentEdit) {
(async () => { (async () => {
try {
await publishCommentEdit(); await publishCommentEdit();
setTriggerPublishCommentEdit(false); setTriggerPublishCommentEdit(false);
} catch (error) {
setErrorMessage(error);
} finally {
setIsAuthorEdit(false);
setIsAuthorDelete(false);
setTriggerPublishCommentEdit(false);
}
})(); })();
} }
}, [publishCommentEditOptions, triggerPublishCommentEdit, publishCommentEdit]); }, [publishCommentEditOptions, triggerPublishCommentEdit, publishCommentEdit, setIsAuthorEdit, setIsAuthorDelete, setTriggerPublishCommentEdit, setErrorMessage]);
// desktop navbar board select functionality // desktop navbar board select functionality
const handleClickTitle = (title, address) => { const handleClickTitle = (title, address) => {
+11 -7
View File
@@ -60,10 +60,8 @@ const Catalog = () => {
const navigate = useNavigate(); const navigate = useNavigate();
const [errorMessage, setErrorMessage] = useState(null); const setErrorMessage = useError();
const [successMessage, setSuccessMessage] = useState(null); const setSuccessMessage = useSuccess();
useError(errorMessage, [errorMessage]);
useSuccess(successMessage, [successMessage]);
const [triggerPublishComment, setTriggerPublishComment] = useState(false); const [triggerPublishComment, setTriggerPublishComment] = useState(false);
const [prevScrollPos, setPrevScrollPos] = useState(0); const [prevScrollPos, setPrevScrollPos] = useState(0);
@@ -145,7 +143,7 @@ const Catalog = () => {
if (errorString) { if (errorString) {
setErrorMessage(errorString); setErrorMessage(errorString);
} }
}, [errorString]); }, [errorString, setErrorMessage]);
const { subscribed, subscribe, unsubscribe } = useSubscribe({subplebbitAddress: selectedAddress}); const { subscribed, subscribe, unsubscribe } = useSubscribe({subplebbitAddress: selectedAddress});
@@ -280,11 +278,17 @@ const Catalog = () => {
useEffect(() => { useEffect(() => {
if (publishCommentOptions && triggerPublishComment) { if (publishCommentOptions && triggerPublishComment) {
(async () => { (async () => {
try {
await publishComment(); await publishComment();
resetFields(); resetFields();
} catch (error) {
setErrorMessage(error);
} finally {
setTriggerPublishComment(false);
}
})(); })();
} }
}, [publishCommentOptions, triggerPublishComment, publishComment, resetFields]); }, [publishCommentOptions, triggerPublishComment, publishComment, resetFields, setErrorMessage]);
const getChallengeAnswersFromUser = async (challenges) => { const getChallengeAnswersFromUser = async (challenges) => {
@@ -340,7 +344,7 @@ const Catalog = () => {
if (error) { if (error) {
setErrorMessage(error); setErrorMessage(error);
} }
}, [error]); }, [error, setErrorMessage]);
const handleAuthorDeleteClick = (commentCid) => { const handleAuthorDeleteClick = (commentCid) => {
+2 -4
View File
@@ -34,6 +34,7 @@ const Pending = () => {
const account = useAccount(); const account = useAccount();
const comment = useAccountComment({commentIndex: index}); const comment = useAccountComment({commentIndex: index});
const setErrorMessage = useError();
useEffect(() => { useEffect(() => {
setSelectedAddress(comment?.subplebbitAddress); setSelectedAddress(comment?.subplebbitAddress);
@@ -55,13 +56,10 @@ const Pending = () => {
if (errorString) { if (errorString) {
setErrorMessage(errorString); setErrorMessage(errorString);
} }
}, [errorString]); }, [errorString, setErrorMessage]);
const [visible] = useState(true); const [visible] = useState(true);
const [errorMessage, setErrorMessage] = useState(null);
useError(errorMessage, [errorMessage]);
const navigate = useNavigate(); const navigate = useNavigate();
const [commentMediaInfo, setCommentMediaInfo] = useState(null); const [commentMediaInfo, setCommentMediaInfo] = useState(null);
const fallbackImgUrl = "assets/filedeleted-res.gif"; const fallbackImgUrl = "assets/filedeleted-res.gif";
+3 -4
View File
@@ -47,6 +47,8 @@ const Subscriptions = () => {
const account = useAccount(); const account = useAccount();
const navigate = useNavigate(); const navigate = useNavigate();
const setErrorMessage = useError();
const threadMenuRefs = useRef({}); const threadMenuRefs = useRef({});
const replyMenuRefs = useRef({}); const replyMenuRefs = useRef({});
const backlinkRefs = useRef({}); const backlinkRefs = useRef({});
@@ -62,9 +64,6 @@ const Subscriptions = () => {
const [outOfViewPosition, setOutOfViewPosition] = useState({top: 0, left: 0}); const [outOfViewPosition, setOutOfViewPosition] = useState({top: 0, left: 0});
const [postOnHoverHeight, setPostOnHoverHeight] = useState(0); const [postOnHoverHeight, setPostOnHoverHeight] = useState(0);
const [errorMessage, setErrorMessage] = useState(null);
useError(errorMessage, [errorMessage]);
const { feed, hasMore, loadMore } = useFeed({subplebbitAddresses: account?.subscriptions, sortType: 'new'}); const { feed, hasMore, loadMore } = useFeed({subplebbitAddresses: account?.subscriptions, sortType: 'new'});
const [selectedFeed, setSelectedFeed] = useState(feed.sort((a, b) => b.timestamp - a.timestamp)); const [selectedFeed, setSelectedFeed] = useState(feed.sort((a, b) => b.timestamp - a.timestamp));
const {subplebbits} = useSubplebbits({subplebbitAddresses: account?.subscriptions, sortType: 'new'}); const {subplebbits} = useSubplebbits({subplebbitAddresses: account?.subscriptions, sortType: 'new'});
@@ -88,7 +87,7 @@ const Subscriptions = () => {
if (errorString) { if (errorString) {
setErrorMessage(errorString) setErrorMessage(errorString)
} }
}, [errorString]) }, [errorString, setErrorMessage])
useEffect(() => { useEffect(() => {
@@ -14,7 +14,6 @@ import OfflineIndicator from '../OfflineIndicator';
import SettingsModal from '../modals/SettingsModal'; import SettingsModal from '../modals/SettingsModal';
import getCommentMediaInfo from '../../utils/getCommentMediaInfo'; import getCommentMediaInfo from '../../utils/getCommentMediaInfo';
import handleStyleChange from '../../utils/handleStyleChange'; import handleStyleChange from '../../utils/handleStyleChange';
import useError from '../../hooks/useError';
import useFeedStateString from '../../hooks/useFeedStateString'; import useFeedStateString from '../../hooks/useFeedStateString';
import packageJson from '../../../package.json' import packageJson from '../../../package.json'
const {version} = packageJson const {version} = packageJson
@@ -31,8 +30,8 @@ const SubscriptionsCatalog = () => {
} = useGeneralStore(state => state); } = useGeneralStore(state => state);
const account = useAccount(); const account = useAccount();
const navigate = useNavigate(); const navigate = useNavigate();
const [prevScrollPos, setPrevScrollPos] = useState(0); const [prevScrollPos, setPrevScrollPos] = useState(0);
const [visible, setVisible] = useState(true); const [visible, setVisible] = useState(true);
const { feed, hasMore, loadMore } = useFeed({subplebbitAddresses: account?.subscriptions, sortType: 'new'}); const { feed, hasMore, loadMore } = useFeed({subplebbitAddresses: account?.subscriptions, sortType: 'new'});
@@ -41,9 +40,6 @@ const SubscriptionsCatalog = () => {
const stateString = useFeedStateString(subplebbits); const stateString = useFeedStateString(subplebbits);
const [errorMessage] = useState(null);
useError(errorMessage, [errorMessage]);
// mobile navbar scroll effect // mobile navbar scroll effect
useEffect(() => { useEffect(() => {
const debouncedHandleScroll = debounce(() => { const debouncedHandleScroll = debounce(() => {
+21 -10
View File
@@ -68,6 +68,9 @@ const Thread = () => {
const navigate = useNavigate(); const navigate = useNavigate();
const handleClickForm = useClickForm(); const handleClickForm = useClickForm();
const setErrorMessage = useError();
const setSuccessMessage = useSuccess();
const nameRef = useRef(); const nameRef = useRef();
const commentRef = useRef(); const commentRef = useRef();
const linkRef = useRef(); const linkRef = useRef();
@@ -82,8 +85,6 @@ const Thread = () => {
const [triggerPublishComment, setTriggerPublishComment] = useState(false); const [triggerPublishComment, setTriggerPublishComment] = useState(false);
const [triggerPublishCommentEdit, setTriggerPublishCommentEdit] = useState(false); const [triggerPublishCommentEdit, setTriggerPublishCommentEdit] = useState(false);
const [deletePost, setDeletePost] = useState(false); const [deletePost, setDeletePost] = useState(false);
const [errorMessage, setErrorMessage] = useState(null);
const [successMessage, setSuccessMessage] = useState(null);
const [isReplyOpen, setIsReplyOpen] = useState(false); const [isReplyOpen, setIsReplyOpen] = useState(false);
const [isEditModalOpen, setIsEditModalOpen] = useState(false); const [isEditModalOpen, setIsEditModalOpen] = useState(false);
const [originalCommentContent, setOriginalCommentContent] = useState(null); const [originalCommentContent, setOriginalCommentContent] = useState(null);
@@ -98,9 +99,6 @@ const Thread = () => {
const [outOfViewPosition, setOutOfViewPosition] = useState({top: 0, left: 0}); const [outOfViewPosition, setOutOfViewPosition] = useState({top: 0, left: 0});
const [postOnHoverHeight, setPostOnHoverHeight] = useState(0); const [postOnHoverHeight, setPostOnHoverHeight] = useState(0);
useError(errorMessage, [errorMessage]);
useSuccess(successMessage, [successMessage]);
const comment = useComment({commentCid: selectedThread}); const comment = useComment({commentCid: selectedThread});
const { subplebbitAddress, threadCid } = useParams(); const { subplebbitAddress, threadCid } = useParams();
const subplebbit = useSubplebbit({subplebbitAddress: comment.subplebbitAddress}); const subplebbit = useSubplebbit({subplebbitAddress: comment.subplebbitAddress});
@@ -175,7 +173,7 @@ const Thread = () => {
if (errorString) { if (errorString) {
setErrorMessage(errorString); setErrorMessage(errorString);
} }
}, [errorString]); }, [errorString, setErrorMessage]);
const flattenedReplies = useMemo(() => const flattenedReplies = useMemo(() =>
@@ -327,12 +325,17 @@ const Thread = () => {
useEffect(() => { useEffect(() => {
if (publishCommentOptions && triggerPublishComment) { if (publishCommentOptions && triggerPublishComment) {
(async () => { (async () => {
try {
await publishComment(); await publishComment();
resetFields(); resetFields();
})(); } catch (error) {
setErrorMessage(error);
} finally {
setTriggerPublishComment(false); setTriggerPublishComment(false);
} }
}, [publishCommentOptions, triggerPublishComment, publishComment, resetFields]); })();
}
}, [publishCommentOptions, triggerPublishComment, publishComment, resetFields, setErrorMessage]);
const getChallengeAnswersFromUser = async (challenges) => { const getChallengeAnswersFromUser = async (challenges) => {
@@ -388,7 +391,7 @@ const Thread = () => {
if (error) { if (error) {
setErrorMessage(error); setErrorMessage(error);
} }
}, [error]); }, [error, setErrorMessage]);
const handleAuthorDeleteClick = (commentCid) => { const handleAuthorDeleteClick = (commentCid) => {
@@ -454,11 +457,19 @@ const Thread = () => {
useEffect(() => { useEffect(() => {
if (publishCommentEditOptions && triggerPublishCommentEdit) { if (publishCommentEditOptions && triggerPublishCommentEdit) {
(async () => { (async () => {
try {
await publishCommentEdit(); await publishCommentEdit();
setTriggerPublishCommentEdit(false); setTriggerPublishCommentEdit(false);
} catch (error) {
setErrorMessage(error);
} finally {
setIsAuthorEdit(false);
setIsAuthorDelete(false);
setTriggerPublishCommentEdit(false);
}
})(); })();
} }
}, [publishCommentEditOptions, triggerPublishCommentEdit, publishCommentEdit]); }, [publishCommentEditOptions, triggerPublishCommentEdit, publishCommentEdit, setIsAuthorEdit, setIsAuthorDelete, setTriggerPublishCommentEdit, setErrorMessage]);
// mobile navbar board select functionality // mobile navbar board select functionality
+15 -5
View File
@@ -1,11 +1,14 @@
import { useEffect } from "react"; import { useEffect, useState } from "react";
import { toast } from "react-toastify"; import { toast } from "react-toastify";
const useError = (message) => { const useError = () => {
const [errorMessage, setErrorMessage] = useState('');
const [renderCount, setRenderCount] = useState(0);
useEffect(() => { useEffect(() => {
if (message && message.length > 0) { if (errorMessage && errorMessage.length > 0) {
const showErrorToast = () => { const showErrorToast = () => {
const toastId = toast.error(message.toString(), { const toastId = toast.error(errorMessage.toString(), {
position: "top-right", position: "top-right",
autoClose: false, autoClose: false,
hideProgressBar: true, hideProgressBar: true,
@@ -27,7 +30,14 @@ const useError = (message) => {
clearTimeout(timeoutId); clearTimeout(timeoutId);
}; };
} }
}, [message]); }, [errorMessage, renderCount]);
const setNewErrorMessage = (message) => {
setErrorMessage(message);
setRenderCount(prevCount => prevCount + 1);
};
return setNewErrorMessage;
}; };
export default useError; export default useError;
+15 -5
View File
@@ -1,11 +1,14 @@
import { useEffect } from "react"; import { useEffect, useState } from "react";
import { toast } from "react-toastify"; import { toast } from "react-toastify";
const useSuccess = (message) => { const useSuccess = () => {
const [successMessage, setSuccessMessage] = useState('');
const [renderCount, setRenderCount] = useState(0);
useEffect(() => { useEffect(() => {
if (message && message.length > 0) { if (successMessage && successMessage.length > 0) {
const showSuccessToast = () => { const showSuccessToast = () => {
const toastId = toast.success(message.toString(), { const toastId = toast.success(successMessage.toString(), {
position: "top-right", position: "top-right",
autoClose: 3000, autoClose: 3000,
hideProgressBar: false, hideProgressBar: false,
@@ -27,7 +30,14 @@ const useSuccess = (message) => {
clearTimeout(timeoutId); clearTimeout(timeoutId);
}; };
} }
}, [message]); }, [successMessage, renderCount]);
const setNewSuccessMessage = (message) => {
setSuccessMessage(message);
setRenderCount(prevCount => prevCount + 1);
};
return setNewSuccessMessage;
}; };
export default useSuccess; export default useSuccess;