feat: add 2FA, secure cookies and publishing tools
This commit is contained in:
@@ -2,10 +2,17 @@ import React, { useEffect, useState } from 'react';
|
||||
import { Link } from 'react-router-dom';
|
||||
import { BellIcon, CircleNotchIcon } from '@phosphor-icons/react';
|
||||
import { useToast } from '../context/ToastContext';
|
||||
import { NotificationFeed, NotificationItem, getNotifications, markAllNotificationsRead, markNotificationRead } from '../services/notifications';
|
||||
import {
|
||||
NotificationFeed,
|
||||
NotificationItem,
|
||||
getNotifications,
|
||||
markAllNotificationsRead,
|
||||
markNotificationRead,
|
||||
} from '../services/notifications';
|
||||
import { useIsMobile } from '../hooks/useMediaQuery';
|
||||
|
||||
const formatWhen = (value: string) => new Intl.DateTimeFormat('pt-BR', { day: '2-digit', month: 'short', hour: '2-digit', minute: '2-digit' }).format(new Date(value));
|
||||
const formatWhen = (value: string) =>
|
||||
new Intl.DateTimeFormat('pt-BR', { day: '2-digit', month: 'short', hour: '2-digit', minute: '2-digit' }).format(new Date(value));
|
||||
|
||||
export const NotificationsPage: React.FC = () => {
|
||||
const { showToast } = useToast();
|
||||
@@ -25,12 +32,18 @@ export const NotificationsPage: React.FC = () => {
|
||||
}
|
||||
};
|
||||
|
||||
useEffect(() => { void load(); }, []);
|
||||
useEffect(() => {
|
||||
void load();
|
||||
}, []);
|
||||
|
||||
const markAll = async () => {
|
||||
try {
|
||||
await markAllNotificationsRead();
|
||||
setFeed((current) => current ? { ...current, unread: 0, items: current.items.map((item) => ({ ...item, readAt: item.readAt || new Date().toISOString() })) } : current);
|
||||
setFeed((current) =>
|
||||
current
|
||||
? { ...current, unread: 0, items: current.items.map((item) => ({ ...item, readAt: item.readAt || new Date().toISOString() })) }
|
||||
: current,
|
||||
);
|
||||
window.dispatchEvent(new Event('compor:notifications-updated'));
|
||||
} catch {
|
||||
showToast('Não foi possível marcar as notificações como lidas.', 'error');
|
||||
@@ -39,34 +52,78 @@ export const NotificationsPage: React.FC = () => {
|
||||
|
||||
const open = (item: NotificationItem) => {
|
||||
if (item.readAt) return;
|
||||
void markNotificationRead(item.id).then(() => {
|
||||
setFeed((current) => current ? {
|
||||
...current,
|
||||
unread: Math.max(0, current.unread - 1),
|
||||
items: current.items.map((entry) => entry.id === item.id ? { ...entry, readAt: new Date().toISOString() } : entry),
|
||||
} : current);
|
||||
window.dispatchEvent(new Event('compor:notifications-updated'));
|
||||
}).catch(() => undefined);
|
||||
void markNotificationRead(item.id)
|
||||
.then(() => {
|
||||
setFeed((current) =>
|
||||
current
|
||||
? {
|
||||
...current,
|
||||
unread: Math.max(0, current.unread - 1),
|
||||
items: current.items.map((entry) => (entry.id === item.id ? { ...entry, readAt: new Date().toISOString() } : entry)),
|
||||
}
|
||||
: current,
|
||||
);
|
||||
window.dispatchEvent(new Event('compor:notifications-updated'));
|
||||
})
|
||||
.catch(() => undefined);
|
||||
};
|
||||
|
||||
const list = loading ? <div className="loading"><CircleNotchIcon className="ph spin" /></div> : feed?.items.length ? <div className="notification-list">
|
||||
{feed.items.map((item) => <Link className={`notification${item.readAt ? '' : ' unread'}`} to={item.href} key={item.id} onClick={() => open(item)}>
|
||||
<span className="n-ic"><BellIcon className="ph" /></span>
|
||||
<span className="n-copy"><b>{item.title}</b>{item.body && <p>{item.body}</p>}</span>
|
||||
<small>{formatWhen(item.createdAt)}</small>
|
||||
</Link>)}
|
||||
</div> : <div className="empty notification-empty"><BellIcon className="ph" /><h2>Nenhuma notificação</h2><p>Quando alguém responder seus comentários ou fizer uma pergunta nos seus cursos, você verá aqui.</p></div>;
|
||||
|
||||
if (isMobile) return <main className="m-page m-gut enter">
|
||||
<header className="m-large"><h1>Notificações</h1>{Boolean(feed?.unread) && <button className="link" type="button" onClick={() => void markAll()}>Marcar lidas</button>}</header>
|
||||
{list}
|
||||
</main>;
|
||||
|
||||
return <main className="wrap page enter">
|
||||
<div className="ptitle">
|
||||
<div><h1>Notificações</h1><p>{feed?.unread ? `${feed.unread} não ${feed.unread === 1 ? 'lida' : 'lidas'}` : 'Tudo em dia.'}</p></div>
|
||||
{Boolean(feed?.unread) && <button className="btn sec" type="button" onClick={() => void markAll()}>Marcar todas como lidas</button>}
|
||||
const list = loading ? (
|
||||
<div className="loading">
|
||||
<CircleNotchIcon className="ph spin" />
|
||||
</div>
|
||||
{list}
|
||||
</main>;
|
||||
) : feed?.items.length ? (
|
||||
<div className="notification-list">
|
||||
{feed.items.map((item) => (
|
||||
<Link className={`notification${item.readAt ? '' : ' unread'}`} to={item.href} key={item.id} onClick={() => open(item)}>
|
||||
<span className="n-ic">
|
||||
<BellIcon className="ph" />
|
||||
</span>
|
||||
<span className="n-copy">
|
||||
<b>{item.title}</b>
|
||||
{item.body && <p>{item.body}</p>}
|
||||
</span>
|
||||
<small>{formatWhen(item.createdAt)}</small>
|
||||
</Link>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<div className="empty notification-empty">
|
||||
<BellIcon className="ph" />
|
||||
<h2>Nenhuma notificação</h2>
|
||||
<p>Quando alguém responder seus comentários ou fizer uma pergunta nos seus cursos, você verá aqui.</p>
|
||||
</div>
|
||||
);
|
||||
|
||||
if (isMobile)
|
||||
return (
|
||||
<main className="m-page m-gut enter">
|
||||
<header className="m-large">
|
||||
<h1>Notificações</h1>
|
||||
{Boolean(feed?.unread) && (
|
||||
<button className="link" type="button" onClick={() => void markAll()}>
|
||||
Marcar lidas
|
||||
</button>
|
||||
)}
|
||||
</header>
|
||||
{list}
|
||||
</main>
|
||||
);
|
||||
|
||||
return (
|
||||
<main className="wrap page enter">
|
||||
<div className="ptitle">
|
||||
<div>
|
||||
<h1>Notificações</h1>
|
||||
<p>{feed?.unread ? `${feed.unread} não ${feed.unread === 1 ? 'lida' : 'lidas'}` : 'Tudo em dia.'}</p>
|
||||
</div>
|
||||
{Boolean(feed?.unread) && (
|
||||
<button className="btn sec" type="button" onClick={() => void markAll()}>
|
||||
Marcar todas como lidas
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
{list}
|
||||
</main>
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user