This commit is contained in:
J Francisco Rader committed 2021-03-30 02:07:04 -03:00
1 parent ff4381fa01
commit a97d858f99
21 files changed
+659 -161

No files matched your search

+7 -7
View File
@@ -2,13 +2,13 @@ import React from 'react';
import { AppWrapper } from '../../../ui/components';
import { AppContent } from '../../../ui/components/AppContent';
import { EmailThemeProvider } from '../providers/EmailThemeProvider';
import EmailNavBar from './EmailNavBar';
import { EmailAppNav } from './EmailAppNav';
import { Route, Switch } from 'react-router-dom';
import { InboxPage } from './InboxPage';
import { InboxPage } from './pages/InboxPage';
import { EmailAppHeader } from './EmailAppHeader';
import { NewEmailPage } from './NewEmailPage';
import { SentPage } from './SentPage';
import { EmailDetails } from './EmailDetails';
import { NewEmailPage } from './pages/NewEmailPage';
import { SentPage } from './pages/SentPage';
import { EmailDetailsPage } from './pages/EmailDetailsPage';
export const EmailApp = () => {
return (
@@ -27,11 +27,11 @@ export const EmailApp = () => {
<NewEmailPage />
</Route>
<Route path="/email/details/:id">
<EmailDetails />
<EmailDetailsPage />
</Route>
</Switch>
</AppContent>
<EmailNavBar />
<EmailAppNav />
</AppWrapper>
</EmailThemeProvider>
);
@@ -19,7 +19,7 @@ const useStyles = makeStyles((theme: Theme) => ({
},
}));
const EmailNavBar = () => {
export const EmailAppNav = () => {
const classes = useStyles();
const { pathname } = useLocation();
const [page, setPage] = useState(pathname);
@@ -58,5 +58,3 @@ const EmailNavBar = () => {
</BottomNavigation>
);
};
export default EmailNavBar;
@@ -1,43 +0,0 @@
import { Box, Card, CardContent, CardHeader, Container } from '@material-ui/core';
import React from 'react';
import { useTranslation } from 'react-i18next';
import { useParams } from 'react-router';
import { useEmail } from '../hooks/useEmail';
export const EmailDetails = () => {
const { t } = useTranslation();
const { id } = useParams<{ id: string }>();
const { getEmailById } = useEmail();
const email = getEmailById(Number(id));
if (!email) {
return (
<Box width="100%" textAlign="center">
404
</Box>
);
}
return (
<Container>
<Card>
<CardContent>
{t('GENERIC_FROM')}: <i>{email.sender}</i>
</CardContent>
<CardContent>
{t('GENERIC_TO')}: <i>{email.receivers.join(', ')}</i>
</CardContent>
<CardContent>
{t('APPS_EMAIL_SUBJECT')}: <i>{email.subject || '( - )'}</i>
</CardContent>
</Card>
<Box mt={2}>
<Card>
<CardContent>{email.body}</CardContent>
</Card>
</Box>
</Container>
);
};
@@ -0,0 +1,78 @@
import { Box, Button, CircularProgress, makeStyles } from '@material-ui/core';
import LeftIcon from '@material-ui/icons/ChevronLeft';
import RightIcon from '@material-ui/icons/ChevronRight';
import React, { useEffect, useState } from 'react';
import { useParams } from 'react-router';
import { IEmailMessage } from '../../../../common/typings/email';
import { useEmail } from '../../hooks/useEmail';
import { EmailMessage } from '../shared/EmailMessage';
const useStyles = makeStyles((theme) => ({
search: {
zIndex: 5,
position: 'absolute',
backgroundColor: theme.palette.primary.light,
top: 0,
},
}));
export const EmailDetailsPage = () => {
const classes = useStyles();
const { id } = useParams<{ id: string }>();
const { getEmailById, head } = useEmail();
const email = getEmailById(Number(id));
const [currMessage, setCurrMessage] = useState<number>(0);
const [message, setMessage] = useState<IEmailMessage>(null);
useEffect(() => {
if (!message && email) {
if (head(email.messages)) {
return;
}
setCurrMessage(0);
}
}, [email, head, message]);
useEffect(() => {
if (email) {
setMessage(email.messages[currMessage]);
}
}, [currMessage, email, message]);
const disableNextPage = currMessage === email?.messages.length - 1;
const disablePrevPage = currMessage === 0;
return (
<Box height="100%" pt="80px" position="relative">
<Box width="100%" p={2} className={classes.search}>
<Box width="100%" display="flex" justifyContent="space-between">
<Button
fullWidth
startIcon={<LeftIcon />}
color="primary"
onClick={() => setCurrMessage((curr) => curr + 1)}
disabled={disableNextPage}
>
Previous
</Button>
<Button
fullWidth
endIcon={<RightIcon />}
color="primary"
onClick={() => setCurrMessage((curr) => curr - 1)}
disabled={disablePrevPage}
>
Newest
</Button>
</Box>
</Box>
{!message || !email ? (
<Box width="100%" height="100%" display="flex" alignItems="center" justifyContent="center">
<CircularProgress color="primary" />
</Box>
) : (
<EmailMessage message={message} />
)}
</Box>
);
};
@@ -1,8 +1,8 @@
import React, { useState } from 'react';
import { Box, makeStyles, TextField } from '@material-ui/core';
import { useQueryParams } from '../../../common/hooks/useQueryParams';
import { useEmail } from '../hooks/useEmail';
import { EmailList } from './EmailList';
import React, { useEffect, useState } from 'react';
import { Box, CircularProgress, makeStyles, TextField } from '@material-ui/core';
import { useQueryParams } from '../../../../common/hooks/useQueryParams';
import { useEmail } from '../../hooks/useEmail';
import { EmailList } from '../shared/EmailList';
const useStyles = makeStyles((theme) => ({
search: {
@@ -16,14 +16,19 @@ const useStyles = makeStyles((theme) => ({
export const InboxPage = () => {
const classes = useStyles();
const { inbox } = useEmail();
const { inbox, fetchInbox, loading } = useEmail();
const { autoSearch } = useQueryParams<{ autoSearch }>();
const [search, setSearch] = useState<string>('');
useEffect(() => {
fetchInbox();
}, [fetchInbox]);
const handleSearch = (e) => {
setSearch(e.target.value);
};
return (
<Box height="100%" pt="80px" position="relative">
<Box width="100%" p={2} className={classes.search}>
@@ -36,9 +41,15 @@ export const InboxPage = () => {
value={search}
/>
</Box>
<Box overflow="auto" maxHeight="100%">
<EmailList emails={inbox} />
</Box>
{!inbox && loading ? (
<Box width="100%" height="100%" display="flex" alignItems="center" justifyContent="center">
<CircularProgress color="primary" />
</Box>
) : (
<Box overflow="auto" maxHeight="100%">
<EmailList emails={inbox} />
</Box>
)}
</Box>
);
};
@@ -1,7 +1,7 @@
import React from 'react';
import { Box } from '@material-ui/core';
import { useEmail } from '../hooks/useEmail';
import { EmailList } from './EmailList';
import { useEmail } from '../../hooks/useEmail';
import { EmailList } from '../shared/EmailList';
export const SentPage = () => {
const { sent } = useEmail();
@@ -1,5 +1,5 @@
import React from 'react';
import { IEmail } from '../../../common/typings/email';
import { IEmail } from '../../../../common/typings/email';
import { EmailListItem } from './EmailListItem';
import { List } from '@material-ui/core';
@@ -8,5 +8,7 @@ interface IProps {
}
export const EmailList = ({ emails }: IProps) => {
return <List>{emails && emails.map((email) => <EmailListItem email={email} />)}</List>;
return (
<List>{emails && emails.map((email) => <EmailListItem key={email.id} email={email} />)}</List>
);
};
@@ -11,32 +11,37 @@ import dayjs from 'dayjs';
import { useTranslation } from 'react-i18next';
import MarkRead from '@material-ui/icons/Markunread';
import MarkUnread from '@material-ui/icons/MarkunreadMailbox';
import { useEmail } from '../hooks/useEmail';
import { IEmail } from '../../../common/typings/email';
import { IEmail } from '../../../../common/typings/email';
import { useHistory } from 'react-router';
import { useEmail } from '../../hooks/useEmail';
export const EmailListItem = ({ email }: { email: IEmail }) => {
const { t } = useTranslation();
const { isMine } = useEmail();
const { head } = useEmail();
const history = useHistory();
const sendDate = dayjs.unix(email.sendDate).format(t('DATE_FORMAT'));
const read = email.isRead === true;
const mine = isMine(email);
const highlight = !mine && !read;
const latestMessage = head(email.messages);
const sendDate = dayjs.unix(latestMessage.sendDate).format(t('DATE_FORMAT'));
const read = latestMessage.isRead === true;
const mine = latestMessage.isMine === true;
const highlight = !mine && !read;
return (
<ListItem
button
divider
key={email.id}
key={latestMessage.id}
onClick={() => history.push(`/email/details/${email.id}`)}
>
<ListItemAvatar>
<Avatar>{email.sender.charAt(0).toUpperCase()}</Avatar>
<Avatar>{latestMessage.sender.charAt(0).toUpperCase()}</Avatar>
</ListItemAvatar>
<ListItemText secondary={highlight ? <strong>{sendDate}</strong> : <span>{sendDate}</span>}>
{highlight ? <strong>{email.subject}</strong> : <span>{email.subject}</span>}
{highlight ? (
<strong>{latestMessage.subject}</strong>
) : (
<span>{latestMessage.subject}</span>
)}
</ListItemText>
<ListItemSecondaryAction>
{!mine && (
@@ -0,0 +1,29 @@
import { Box, Card, CardContent } from '@material-ui/core';
import React from 'react';
import { useTranslation } from 'react-i18next';
import { IEmailMessage } from '../../../../common/typings/email';
export const EmailMessage = ({ message }: { message: IEmailMessage }) => {
const { t } = useTranslation();
return (
<Box px={2}>
<Card>
<CardContent>
{t('GENERIC_FROM')}: <i>{message.sender}</i>
</CardContent>
<CardContent>
{t('GENERIC_TO')}: <i>{message.receivers.join(', ')}</i>
</CardContent>
<CardContent>
{t('APPS_EMAIL_SUBJECT')}: <i>{message.subject || '( - )'}</i>
</CardContent>
</Card>
<Box mt={2}>
<Card>
<CardContent>{message.body}</CardContent>
</Card>
</Box>
</Box>
);
};
+8
View File
@@ -2,6 +2,14 @@ import { atom } from 'recoil';
import { IEmail } from '../../../common/typings/email';
export const emailState = {
search: atom<IEmail[]>({
default: null,
key: 'EMAIL:search',
}),
searchTerm: atom<string>({
default: null,
key: 'EMAIL:searchTerm',
}),
inbox: atom<IEmail[]>({
default: null,
key: 'EMAIL:inbox',
+23 -9
View File
@@ -1,18 +1,32 @@
import { useMemo } from 'react';
import { useRecoilValue } from 'recoil';
import { IEmail } from '../../../common/typings/email';
import { useCallback, useMemo } from 'react';
import { useRecoilState, useRecoilValue } from 'recoil';
import { IEmailMessage } from '../../../common/typings/email';
import { useNuiEventCallback } from '../../../os/nui-events/hooks/useNuiEventCallback';
import { emailState } from './state';
export const useEmail = () => {
const inbox = useRecoilValue(emailState.inbox);
const sent = useRecoilValue(emailState.sent);
const [inbox, setInbox] = useRecoilState(emailState.inbox);
const [sent, setSent] = useRecoilState(emailState.sent);
const myEmail = useRecoilValue(emailState.myEmail);
const [fetchInbox, { loading: inboxLoading }] = useNuiEventCallback(
'EMAIL',
'phone:fetchEmails',
setInbox,
);
const [fetchSent, { loading: sentLoading }] = useNuiEventCallback(
'EMAIL',
'phone:fetchSentEmails',
setSent,
);
const loading = inboxLoading || sentLoading;
const allEmails = useMemo(() => [...(sent || []), ...(inbox || [])], [inbox, sent]);
const isMine = (email: IEmail) => email.sender === myEmail;
const head = useCallback((arr: IEmailMessage[]): IEmailMessage => arr && arr[0], []);
const getEmailById = useCallback((id: number) => allEmails.find((e) => e.id === id) || null, [
allEmails,
]);
const getEmailById = (id: number) => allEmails.find((e) => e.id === id) || null;
return { inbox, sent, myEmail, isMine, getEmailById };
return { inbox, sent, myEmail, getEmailById, head, fetchSent, fetchInbox, loading };
};
+153 -71
View File
@@ -5,13 +5,9 @@ import { useNuiEvent } from '../../../os/nui-events/hooks/useNuiEvent';
import { emailState } from './state';
export const useEmailService = () => {
const setInbox = useSetRecoilState(emailState.inbox);
const setSent = useSetRecoilState(emailState.sent);
const setMyEmail = useSetRecoilState(emailState.myEmail);
useNuiEvent('EMAIL', 'phone:setMyEmail', setMyEmail);
useNuiEvent('EMAIL', 'phone:fetchAllSentEmailsSuccess', setSent);
useNuiEvent('EMAIL', 'phone:fetchAllEmailsSuccess', setInbox);
};
InjectDebugData<string>([
@@ -26,104 +22,190 @@ InjectDebugData<IEmail[]>(
[
{
app: 'EMAIL',
method: 'phone:fetchAllSentEmailsSuccess',
method: 'phone:fetchSentEmailsSuccess',
data: [
{
id: 1,
subject: 'Hey!',
body: 'Hey did you try this email app? Looking good bruh',
receivers: ['[email protected]'],
sender: '[email protected]',
sendDate: Date.now() / 1000,
messages: [
{
id: 1,
emailId: 1,
subject: 'Hey!',
body: 'Hey did you try this email app? Looking good bruh',
receivers: ['[email protected]'],
sender: '[email protected]',
sendDate: Date.now() / 1000,
},
],
},
{
id: 2,
subject: 'What is up?',
body: 'Hey did you try this email app? Looking good bruh',
receivers: ['[email protected]'],
sender: '[email protected]',
sendDate: Date.now() / 1000,
messages: [
{
id: 2,
emailId: 2,
subject: 'What is up?',
body: 'Hey did you try this email app? Looking good bruh',
receivers: ['[email protected]'],
sender: '[email protected]',
sendDate: Date.now() / 1000,
},
],
},
],
},
{
app: 'EMAIL',
method: 'phone:fetchAllEmailsSuccess',
method: 'phone:fetchEmailsSuccess',
data: [
{
id: 1,
subject: 'Try this great app!',
body: 'Hey did you try this email app? Looking good bruh',
receivers: ['[email protected]'],
sender: '[email protected]',
sendDate: Date.now() / 1000,
isRead: false,
},
{
id: 2,
subject: 'Try this great app!',
body: 'Hey did you try this email app? Looking good bruh',
receivers: ['[email protected]'],
sender: '[email protected]',
sendDate: Date.now() / 1000,
isRead: false,
},
{
id: 3,
subject: 'Try this great app!',
body: 'Hey did you try this email app? Looking good bruh',
receivers: ['[email protected]'],
sender: '[email protected]',
sendDate: Date.now() / 1000,
isRead: true,
messages: [
{
id: 11,
parentId: 12,
emailId: 3,
subject: 'Try this great app! 1',
body: 'Hey did you try this email app? Looking good bruh',
receivers: ['[email protected]'],
sender: '[email protected]',
sendDate: Date.now() / 1000,
isRead: false,
isMine: true,
},
{
id: 12,
parentId: 3,
emailId: 3,
subject: 'Try this great app! 2',
body: 'Hey did you try this email app? Looking good bruh',
receivers: ['[email protected]'],
sender: '[email protected]',
sendDate: Date.now() / 1000,
isRead: false,
isMine: false,
},
{
id: 3,
parentId: null,
emailId: 3,
subject: 'Try this great app! 3',
body: 'Hey did you try this email app? Looking good bruh',
receivers: ['[email protected]'],
sender: '[email protected]',
sendDate: Date.now() / 1000,
isRead: false,
isMine: true,
},
],
},
{
id: 4,
subject: 'Try this great app!',
body: 'Hey did you try this email app? Looking good bruh',
receivers: ['[email protected]'],
sender: '[email protected]',
sendDate: Date.now() / 1000,
isRead: true,
messages: [
{
id: 4,
emailId: 1,
subject: 'Try this great app!',
body: 'Hey did you try this email app? Looking good bruh',
receivers: ['[email protected]'],
sender: '[email protected]',
sendDate: Date.now() / 1000,
isRead: false,
},
],
},
{
id: 5,
subject: 'Try this great app!',
body: 'Hey did you try this email app? Looking good bruh',
receivers: ['[email protected]'],
sender: '[email protected]',
sendDate: Date.now() / 1000,
isRead: true,
messages: [
{
id: 5,
emailId: 5,
subject: 'Try this great app!',
body: 'Hey did you try this email app? Looking good bruh',
receivers: ['[email protected]'],
sender: '[email protected]',
sendDate: Date.now() / 1000,
isRead: true,
},
],
},
{
id: 6,
subject: 'Try this great app!',
body: 'Hey did you try this email app? Looking good bruh',
receivers: ['[email protected]'],
sender: '[email protected]',
sendDate: Date.now() / 1000,
isRead: false,
messages: [
{
id: 6,
emailId: 6,
subject: 'Try this great app!',
body: 'Hey did you try this email app? Looking good bruh',
receivers: ['[email protected]'],
sender: '[email protected]',
sendDate: Date.now() / 1000,
isRead: true,
},
],
},
{
id: 7,
subject: 'Try this great app!',
body: 'Hey did you try this email app? Looking good bruh',
receivers: ['[email protected]'],
sender: '[email protected]',
sendDate: Date.now() / 1000,
isRead: false,
messages: [
{
id: 7,
emailId: 7,
subject: 'Try this great app!',
body: 'Hey did you try this email app? Looking good bruh',
receivers: ['[email protected]'],
sender: '[email protected]',
sendDate: Date.now() / 1000,
isRead: true,
},
],
},
{
id: 8,
subject: 'Try this great app!',
body: 'Hey did you try this email app? Looking good bruh',
receivers: ['[email protected]'],
sender: '[email protected]',
sendDate: Date.now() / 1000,
isRead: false,
messages: [
{
id: 8,
emailId: 8,
subject: 'Try this great app!',
body: 'Hey did you try this email app? Looking good bruh',
receivers: ['[email protected]'],
sender: '[email protected]',
sendDate: Date.now() / 1000,
isRead: false,
},
],
},
{
id: 9,
messages: [
{
id: 9,
emailId: 9,
subject: 'Try this great app!',
body: 'Hey did you try this email app? Looking good bruh',
receivers: ['[email protected]'],
sender: '[email protected]',
sendDate: Date.now() / 1000,
isRead: false,
},
],
},
{
id: 10,
messages: [
{
id: 10,
emailId: 10,
subject: 'Try this great app!',
body: 'Hey did you try this email app? Looking good bruh',
receivers: ['[email protected]'],
sender: '[email protected]',
sendDate: Date.now() / 1000,
isRead: false,
},
],
},
],
},
],
2500,
4000,
);