From 2d766a41f7c20e7c313b394b6d2912806023f71a Mon Sep 17 00:00:00 2001 From: Riyanshu Patro Date: Thu, 6 Feb 2025 18:22:02 +0530 Subject: [PATCH] add loader for refresh click --- examples/email/src/context/AppContext.tsx | 15 +++---- .../src/modules/emailPage/EmailScreen.tsx | 45 ++++++++++++++++--- .../emailPage/components/EmailList.tsx | 10 ++--- .../modules/emailPage/components/NewEmail.tsx | 4 +- 4 files changed, 51 insertions(+), 23 deletions(-) diff --git a/examples/email/src/context/AppContext.tsx b/examples/email/src/context/AppContext.tsx index 4f46f33c..a361e2ac 100644 --- a/examples/email/src/context/AppContext.tsx +++ b/examples/email/src/context/AppContext.tsx @@ -34,7 +34,6 @@ interface AppContextType { account: string | null; handleSendSignRequestToPushWallet: (data: Uint8Array) => Promise; wallet: Wallet | null; - getEmails: () => Promise; isLoading: boolean; getSentEmails: () => Promise; isSentEmailLoading: boolean; @@ -66,8 +65,9 @@ export function AppProvider({ children }: { children: ReactNode }) { const { account, handleSendSignRequestToPushWallet } = usePushWalletContext(); const getSentEmails = async () => { - if (!account || !pushEmail || isLoading || isSentEmailLoading) return; + if (!account || !pushEmail || isSentEmailLoading) return; setIsSentEmailLoading(true); + console.log('check'); try { const sent = await pushEmail.getBySender(account); setEmails((prev) => ({ @@ -82,7 +82,7 @@ export function AppProvider({ children }: { children: ReactNode }) { }; const getReceivedEmails = async () => { - if (!account || !pushEmail || isLoading || isReceivedEmailLoading) return; + if (!account || !pushEmail || isReceivedEmailLoading) return; setIsReceivedEmailLoading(true); try { const received = await pushEmail.getByRecipient(account); @@ -98,7 +98,7 @@ export function AppProvider({ children }: { children: ReactNode }) { }; const getEmails = async () => { - if (!account || !pushEmail || isLoading) return; + if (!account || !pushEmail) return; setIsLoading(true); try { await Promise.all([getSentEmails(), getReceivedEmails()]); @@ -108,17 +108,15 @@ export function AppProvider({ children }: { children: ReactNode }) { }; useEffect(() => { + if (!account || !pushEmail) return; getEmails(); - }, [account, pushEmail]); - - useEffect(() => { const interval = setInterval(() => { getSentEmails(); getReceivedEmails(); }, 5 * 60 * 1000); // 5 minutes interval return () => clearInterval(interval); - }); + }, [account, pushEmail]); useEffect(() => { if (account) { @@ -158,7 +156,6 @@ export function AppProvider({ children }: { children: ReactNode }) { account, handleSendSignRequestToPushWallet, wallet, - getEmails, isLoading, getSentEmails, isSentEmailLoading, diff --git a/examples/email/src/modules/emailPage/EmailScreen.tsx b/examples/email/src/modules/emailPage/EmailScreen.tsx index 5c3e5ef4..c11af0bc 100644 --- a/examples/email/src/modules/emailPage/EmailScreen.tsx +++ b/examples/email/src/modules/emailPage/EmailScreen.tsx @@ -1,7 +1,14 @@ -import { Text, Box, TextInput, Tabs, Refresh } from 'shared-components'; +import { + Text, + Box, + TextInput, + Tabs, + Refresh, + Spinner, +} from 'shared-components'; import { useAppContext } from '@/context/AppContext'; import { css } from 'styled-components'; -import { useEffect } from 'react'; +import { useEffect, useState } from 'react'; import { useLocation, useNavigate, useParams } from 'react-router-dom'; import { dummyEmail, EMAIL_BOX } from '@/common'; import EmailLayout from '@/components/EmailLayout'; @@ -10,6 +17,11 @@ import NewEmail from './components/NewEmail'; import { Header } from './components/Header'; const EmailScreen = () => { + const [isLoading, setIsLoading] = useState>({ + inbox: false, + sent: false, + }); + const navigate = useNavigate(); const location = useLocation(); const { id } = useParams(); @@ -22,7 +34,6 @@ const EmailScreen = () => { setSelectedEmail, selectedEmail, replyTo, - getEmails, getSentEmails, getReceivedEmails, } = useAppContext(); @@ -37,6 +48,18 @@ const EmailScreen = () => { // navigate(`/${tab}`); }; + const handleRefreshClick = async () => { + if (currTab === EMAIL_BOX.INBOX) { + setIsLoading((prev) => ({ ...prev, inbox: true })); + await getReceivedEmails(); + setIsLoading((prev) => ({ ...prev, inbox: false })); + } else { + setIsLoading((prev) => ({ ...prev, sent: true })); + await getReceivedEmails(); + setIsLoading((prev) => ({ ...prev, sent: false })); + } + }; + useEffect(() => { if (location.pathname.includes(EMAIL_BOX.INBOX)) { setCurrTab(EMAIL_BOX.INBOX); @@ -106,10 +129,18 @@ const EmailScreen = () => { alignItems="center" width="100%" > - Inbox - - - + + {currTab} + + {isLoading[currTab] ? ( + + + + ) : ( + + + + )} = ({ type }) => { return ( + {filteredEmails.map((email, index) => ( + + ))} + {type === EMAIL_BOX.INBOX && } {((isLoading && isSentEmailLoading && currTab === EMAIL_BOX.SENT) || (isLoading && isReceivedEmailLoading && currTab === EMAIL_BOX.INBOX)) && ( - + )} - {filteredEmails.map((email, index) => ( - - ))} - {type === EMAIL_BOX.INBOX && } ); diff --git a/examples/email/src/modules/emailPage/components/NewEmail.tsx b/examples/email/src/modules/emailPage/components/NewEmail.tsx index 1bfb6093..0ca462a0 100644 --- a/examples/email/src/modules/emailPage/components/NewEmail.tsx +++ b/examples/email/src/modules/emailPage/components/NewEmail.tsx @@ -62,7 +62,7 @@ const NewEmail: React.FC = ({ replyTo }) => { setReplyTo, account, handleSendSignRequestToPushWallet, - getEmails, + getSentEmails, currTab, } = useAppContext(); const [sendingMail, setSendingMail] = useState(false); @@ -244,7 +244,7 @@ const NewEmail: React.FC = ({ replyTo }) => { } console.log('Email sent:', txHash); setTimeout(() => { - getEmails(); + getSentEmails(); }, 5000); setEmailData({ subject: '', message: '' }); setRecipients([]);