Extract recents to component

This commit is contained in:
Ben Grant 2021-05-18 21:45:12 +10:00
parent 2534ff289e
commit 32c45d04f3
6 changed files with 73 additions and 69 deletions

View file

@ -0,0 +1,30 @@
import { useTranslation } from 'react-i18next';
import { useRecentsStore } from 'stores';
import dayjs from 'dayjs';
import relativeTime from 'dayjs/plugin/relativeTime';
import { AboutSection, StyledMain } from '../../pages/Home/homeStyle';
import { Recent } from './recentsStyle';
dayjs.extend(relativeTime);
const Recents = () => {
const recents = useRecentsStore(state => state.recents);
const { t } = useTranslation(['home', 'common']);
return !!recents.length && (
<AboutSection id="recents">
<StyledMain>
<h2>{t('home:recently_visited')}</h2>
{recents.map(event => (
<Recent href={`/${event.id}`} key={event.id}>
<span className="name">{event.name}</span>
<span className="date" title={dayjs.unix(event.created).format('D MMMM, YYYY')}>{t('common:created')} {dayjs.unix(event.created).fromNow()}</span>
</Recent>
))}
</StyledMain>
</AboutSection>
);
};
export default Recents;

View file

@ -0,0 +1,36 @@
import styled from '@emotion/styled';
export const Recent = styled.a`
text-decoration: none;
color: inherit;
display: flex;
align-items: center;
justify-content: space-between;
padding: 5px 0;
flex-wrap: wrap;
& .name {
font-weight: 700;
font-size: 1.1em;
color: ${props => props.theme.primaryDark};
flex: 1;
display: block;
}
& .date {
font-weight: 400;
opacity: .8;
white-space: nowrap;
}
&:hover .name {
text-decoration: underline;
}
@media (max-width: 500px) {
display: block;
& .date {
white-space: normal;
}
}
`;

View file

@ -17,3 +17,4 @@ export { default as Donate } from './Donate/Donate';
export { default as Settings } from './Settings/Settings'; export { default as Settings } from './Settings/Settings';
export { default as Egg } from './Egg/Egg'; export { default as Egg } from './Egg/Egg';
export { default as Footer } from './Footer/Footer'; export { default as Footer } from './Footer/Footer';
export { default as Recents } from './Recents/Recents';

View file

@ -15,6 +15,7 @@ import {
Button, Button,
Donate, Donate,
Error, Error,
Recents,
} from 'components'; } from 'components';
import { import {
@ -28,9 +29,6 @@ import {
Footer, Footer,
AboutSection, AboutSection,
} from './createStyle'; } from './createStyle';
import {
Recent,
} from '../Home/homeStyle';
import api from 'services'; import api from 'services';
import { useRecentsStore } from 'stores'; import { useRecentsStore } from 'stores';
@ -54,7 +52,7 @@ const Create = ({ offline }) => {
const { push } = useHistory(); const { push } = useHistory();
const recentsStore = useRecentsStore(); const addRecent = useRecentsStore(state => state.addRecent);
useEffect(() => { useEffect(() => {
if (window.self === window.top) { if (window.self === window.top) {
@ -123,7 +121,7 @@ const Create = ({ offline }) => {
}, },
}); });
setCreatedEvent(response.data); setCreatedEvent(response.data);
recentsStore.addRecent({ addRecent({
id: response.data.id, id: response.data.id,
created: response.data.created, created: response.data.created,
name: response.data.name, name: response.data.name,
@ -175,19 +173,7 @@ const Create = ({ offline }) => {
</StyledMain> </StyledMain>
) : ( ) : (
<> <>
{!!recentsStore.recents.length && ( <Recents />
<AboutSection id="recents">
<StyledMain>
<h2>Recently visited</h2>
{recentsStore.recents.map(event => (
<Recent href={`/${event.id}`} target="_blank" key={event.id}>
<span className="name">{event.name}</span>
<span className="date">Created {dayjs.unix(event.created).format('D MMMM, YYYY')}</span>
</Recent>
))}
</StyledMain>
</AboutSection>
)}
<StyledMain> <StyledMain>
{offline ? ( {offline ? (

View file

@ -17,6 +17,7 @@ import {
Center, Center,
Error, Error,
Footer, Footer,
Recents,
} from 'components'; } from 'components';
import { import {
@ -33,11 +34,9 @@ import {
StatNumber, StatNumber,
StatLabel, StatLabel,
OfflineMessage, OfflineMessage,
Recent,
} from './homeStyle'; } from './homeStyle';
import api from 'services'; import api from 'services';
import { useRecentsStore } from 'stores';
import logo from 'res/logo.svg'; import logo from 'res/logo.svg';
import timezones from 'res/timezones.json'; import timezones from 'res/timezones.json';
@ -60,7 +59,6 @@ const Home = ({ offline }) => {
version: 'loading...', version: 'loading...',
}); });
const { push } = useHistory(); const { push } = useHistory();
const recentsStore = useRecentsStore();
const { t } = useTranslation(['common', 'home']); const { t } = useTranslation(['common', 'home']);
useEffect(() => { useEffect(() => {
@ -161,19 +159,7 @@ const Home = ({ offline }) => {
</Links> </Links>
</StyledMain> </StyledMain>
{!!recentsStore.recents.length && ( <Recents />
<AboutSection id="recents">
<StyledMain>
<h2>{t('home:recently_visited')}</h2>
{recentsStore.recents.map(event => (
<Recent href={`/${event.id}`} key={event.id}>
<span className="name">{event.name}</span>
<span className="date">Created {dayjs.unix(event.created).format('D MMMM, YYYY')}</span>
</Recent>
))}
</StyledMain>
</AboutSection>
)}
<StyledMain> <StyledMain>
{offline ? ( {offline ? (

View file

@ -87,38 +87,3 @@ export const OfflineMessage = styled.div`
text-align: center; text-align: center;
margin: 50px 0 20px; margin: 50px 0 20px;
`; `;
export const Recent = styled.a`
text-decoration: none;
color: inherit;
display: flex;
align-items: center;
justify-content: space-between;
padding: 5px 0;
flex-wrap: wrap;
& .name {
font-weight: 700;
font-size: 1.1em;
color: ${props => props.theme.primaryDark};
flex: 1;
display: block;
}
& .date {
font-weight: 400;
opacity: .8;
white-space: nowrap;
}
&:hover .name {
text-decoration: underline;
}
@media (max-width: 500px) {
display: block;
& .date {
white-space: normal;
}
}
`;