Skip to content

Commit 4be018f

Browse files
Merge pull request #65 from push-protocol/add-refresh
Add loader for refresh click
2 parents 9bd4fd9 + 2d766a4 commit 4be018f

File tree

4 files changed

+51
-23
lines changed

4 files changed

+51
-23
lines changed

examples/email/src/context/AppContext.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ interface AppContextType {
3434
account: string | null;
3535
handleSendSignRequestToPushWallet: (data: Uint8Array) => Promise<Uint8Array>;
3636
wallet: Wallet | null;
37-
getEmails: () => Promise<void>;
3837
isLoading: boolean;
3938
getSentEmails: () => Promise<void>;
4039
isSentEmailLoading: boolean;
@@ -66,8 +65,9 @@ export function AppProvider({ children }: { children: ReactNode }) {
6665
const { account, handleSendSignRequestToPushWallet } = usePushWalletContext();
6766

6867
const getSentEmails = async () => {
69-
if (!account || !pushEmail || isLoading || isSentEmailLoading) return;
68+
if (!account || !pushEmail || isSentEmailLoading) return;
7069
setIsSentEmailLoading(true);
70+
console.log('check');
7171
try {
7272
const sent = await pushEmail.getBySender(account);
7373
setEmails((prev) => ({
@@ -82,7 +82,7 @@ export function AppProvider({ children }: { children: ReactNode }) {
8282
};
8383

8484
const getReceivedEmails = async () => {
85-
if (!account || !pushEmail || isLoading || isReceivedEmailLoading) return;
85+
if (!account || !pushEmail || isReceivedEmailLoading) return;
8686
setIsReceivedEmailLoading(true);
8787
try {
8888
const received = await pushEmail.getByRecipient(account);
@@ -98,7 +98,7 @@ export function AppProvider({ children }: { children: ReactNode }) {
9898
};
9999

100100
const getEmails = async () => {
101-
if (!account || !pushEmail || isLoading) return;
101+
if (!account || !pushEmail) return;
102102
setIsLoading(true);
103103
try {
104104
await Promise.all([getSentEmails(), getReceivedEmails()]);
@@ -108,17 +108,15 @@ export function AppProvider({ children }: { children: ReactNode }) {
108108
};
109109

110110
useEffect(() => {
111+
if (!account || !pushEmail) return;
111112
getEmails();
112-
}, [account, pushEmail]);
113-
114-
useEffect(() => {
115113
const interval = setInterval(() => {
116114
getSentEmails();
117115
getReceivedEmails();
118116
}, 5 * 60 * 1000); // 5 minutes interval
119117

120118
return () => clearInterval(interval);
121-
});
119+
}, [account, pushEmail]);
122120

123121
useEffect(() => {
124122
if (account) {
@@ -158,7 +156,6 @@ export function AppProvider({ children }: { children: ReactNode }) {
158156
account,
159157
handleSendSignRequestToPushWallet,
160158
wallet,
161-
getEmails,
162159
isLoading,
163160
getSentEmails,
164161
isSentEmailLoading,

examples/email/src/modules/emailPage/EmailScreen.tsx

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
import { Text, Box, TextInput, Tabs, Refresh } from 'shared-components';
1+
import {
2+
Text,
3+
Box,
4+
TextInput,
5+
Tabs,
6+
Refresh,
7+
Spinner,
8+
} from 'shared-components';
29
import { useAppContext } from '@/context/AppContext';
310
import { css } from 'styled-components';
4-
import { useEffect } from 'react';
11+
import { useEffect, useState } from 'react';
512
import { useLocation, useNavigate, useParams } from 'react-router-dom';
613
import { dummyEmail, EMAIL_BOX } from '@/common';
714
import EmailLayout from '@/components/EmailLayout';
@@ -10,6 +17,11 @@ import NewEmail from './components/NewEmail';
1017
import { Header } from './components/Header';
1118

1219
const EmailScreen = () => {
20+
const [isLoading, setIsLoading] = useState<Record<EMAIL_BOX, boolean>>({
21+
inbox: false,
22+
sent: false,
23+
});
24+
1325
const navigate = useNavigate();
1426
const location = useLocation();
1527
const { id } = useParams();
@@ -22,7 +34,6 @@ const EmailScreen = () => {
2234
setSelectedEmail,
2335
selectedEmail,
2436
replyTo,
25-
getEmails,
2637
getSentEmails,
2738
getReceivedEmails,
2839
} = useAppContext();
@@ -37,6 +48,18 @@ const EmailScreen = () => {
3748
// navigate(`/${tab}`);
3849
};
3950

51+
const handleRefreshClick = async () => {
52+
if (currTab === EMAIL_BOX.INBOX) {
53+
setIsLoading((prev) => ({ ...prev, inbox: true }));
54+
await getReceivedEmails();
55+
setIsLoading((prev) => ({ ...prev, inbox: false }));
56+
} else {
57+
setIsLoading((prev) => ({ ...prev, sent: true }));
58+
await getReceivedEmails();
59+
setIsLoading((prev) => ({ ...prev, sent: false }));
60+
}
61+
};
62+
4063
useEffect(() => {
4164
if (location.pathname.includes(EMAIL_BOX.INBOX)) {
4265
setCurrTab(EMAIL_BOX.INBOX);
@@ -106,10 +129,18 @@ const EmailScreen = () => {
106129
alignItems="center"
107130
width="100%"
108131
>
109-
<Text variant="h3-semibold">Inbox</Text>
110-
<Box cursor="pointer" onClick={getEmails}>
111-
<Refresh size={24} />
112-
</Box>
132+
<Text variant="h3-semibold" textTransform="capitalize">
133+
{currTab}
134+
</Text>
135+
{isLoading[currTab] ? (
136+
<Box>
137+
<Spinner size="medium" variant="primary" />
138+
</Box>
139+
) : (
140+
<Box cursor="pointer" onClick={handleRefreshClick}>
141+
<Refresh size={24} />
142+
</Box>
143+
)}
113144
</Box>
114145
<TextInput
115146
placeholder="Search for a sender address"

examples/email/src/modules/emailPage/components/EmailList.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,18 @@ const EmailList: FC<EmailListProps> = ({ type }) => {
3737
return (
3838
<Box height="100%" width="100%" overflow="scroll">
3939
<Box display="flex" flexDirection="column">
40+
{filteredEmails.map((email, index) => (
41+
<EmailCard key={index} {...email} />
42+
))}
43+
{type === EMAIL_BOX.INBOX && <EmailCard {...dummyEmail} />}
4044
{((isLoading && isSentEmailLoading && currTab === EMAIL_BOX.SENT) ||
4145
(isLoading &&
4246
isReceivedEmailLoading &&
4347
currTab === EMAIL_BOX.INBOX)) && (
44-
<Box display="flex" justifyContent="center" margin="spacing-lg">
48+
<Box display="flex" justifyContent="center" padding="spacing-lg">
4549
<Spinner size="medium" variant="primary" />
4650
</Box>
4751
)}
48-
{filteredEmails.map((email, index) => (
49-
<EmailCard key={index} {...email} />
50-
))}
51-
{type === EMAIL_BOX.INBOX && <EmailCard {...dummyEmail} />}
5252
</Box>
5353
</Box>
5454
);

examples/email/src/modules/emailPage/components/NewEmail.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ const NewEmail: React.FC<NewEmailProps> = ({ replyTo }) => {
6262
setReplyTo,
6363
account,
6464
handleSendSignRequestToPushWallet,
65-
getEmails,
65+
getSentEmails,
6666
currTab,
6767
} = useAppContext();
6868
const [sendingMail, setSendingMail] = useState(false);
@@ -244,7 +244,7 @@ const NewEmail: React.FC<NewEmailProps> = ({ replyTo }) => {
244244
}
245245
console.log('Email sent:', txHash);
246246
setTimeout(() => {
247-
getEmails();
247+
getSentEmails();
248248
}, 5000);
249249
setEmailData({ subject: '', message: '' });
250250
setRecipients([]);

0 commit comments

Comments
 (0)