Skip to content

Commit

Permalink
Merge pull request #65 from push-protocol/add-refresh
Browse files Browse the repository at this point in the history
Add loader for refresh click
  • Loading branch information
riyanshu-patro authored Feb 6, 2025
2 parents 9bd4fd9 + 2d766a4 commit 4be018f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 23 deletions.
15 changes: 6 additions & 9 deletions examples/email/src/context/AppContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ interface AppContextType {
account: string | null;
handleSendSignRequestToPushWallet: (data: Uint8Array) => Promise<Uint8Array>;
wallet: Wallet | null;
getEmails: () => Promise<void>;
isLoading: boolean;
getSentEmails: () => Promise<void>;
isSentEmailLoading: boolean;
Expand Down Expand Up @@ -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) => ({
Expand All @@ -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);
Expand All @@ -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()]);
Expand All @@ -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) {
Expand Down Expand Up @@ -158,7 +156,6 @@ export function AppProvider({ children }: { children: ReactNode }) {
account,
handleSendSignRequestToPushWallet,
wallet,
getEmails,
isLoading,
getSentEmails,
isSentEmailLoading,
Expand Down
45 changes: 38 additions & 7 deletions examples/email/src/modules/emailPage/EmailScreen.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -10,6 +17,11 @@ import NewEmail from './components/NewEmail';
import { Header } from './components/Header';

const EmailScreen = () => {
const [isLoading, setIsLoading] = useState<Record<EMAIL_BOX, boolean>>({
inbox: false,
sent: false,
});

const navigate = useNavigate();
const location = useLocation();
const { id } = useParams();
Expand All @@ -22,7 +34,6 @@ const EmailScreen = () => {
setSelectedEmail,
selectedEmail,
replyTo,
getEmails,
getSentEmails,
getReceivedEmails,
} = useAppContext();
Expand All @@ -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);
Expand Down Expand Up @@ -106,10 +129,18 @@ const EmailScreen = () => {
alignItems="center"
width="100%"
>
<Text variant="h3-semibold">Inbox</Text>
<Box cursor="pointer" onClick={getEmails}>
<Refresh size={24} />
</Box>
<Text variant="h3-semibold" textTransform="capitalize">
{currTab}
</Text>
{isLoading[currTab] ? (
<Box>
<Spinner size="medium" variant="primary" />
</Box>
) : (
<Box cursor="pointer" onClick={handleRefreshClick}>
<Refresh size={24} />
</Box>
)}
</Box>
<TextInput
placeholder="Search for a sender address"
Expand Down
10 changes: 5 additions & 5 deletions examples/email/src/modules/emailPage/components/EmailList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ const EmailList: FC<EmailListProps> = ({ type }) => {
return (
<Box height="100%" width="100%" overflow="scroll">
<Box display="flex" flexDirection="column">
{filteredEmails.map((email, index) => (
<EmailCard key={index} {...email} />
))}
{type === EMAIL_BOX.INBOX && <EmailCard {...dummyEmail} />}
{((isLoading && isSentEmailLoading && currTab === EMAIL_BOX.SENT) ||
(isLoading &&
isReceivedEmailLoading &&
currTab === EMAIL_BOX.INBOX)) && (
<Box display="flex" justifyContent="center" margin="spacing-lg">
<Box display="flex" justifyContent="center" padding="spacing-lg">
<Spinner size="medium" variant="primary" />
</Box>
)}
{filteredEmails.map((email, index) => (
<EmailCard key={index} {...email} />
))}
{type === EMAIL_BOX.INBOX && <EmailCard {...dummyEmail} />}
</Box>
</Box>
);
Expand Down
4 changes: 2 additions & 2 deletions examples/email/src/modules/emailPage/components/NewEmail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const NewEmail: React.FC<NewEmailProps> = ({ replyTo }) => {
setReplyTo,
account,
handleSendSignRequestToPushWallet,
getEmails,
getSentEmails,
currTab,
} = useAppContext();
const [sendingMail, setSendingMail] = useState(false);
Expand Down Expand Up @@ -244,7 +244,7 @@ const NewEmail: React.FC<NewEmailProps> = ({ replyTo }) => {
}
console.log('Email sent:', txHash);
setTimeout(() => {
getEmails();
getSentEmails();
}, 5000);
setEmailData({ subject: '', message: '' });
setRecipients([]);
Expand Down

0 comments on commit 4be018f

Please sign in to comment.