Creating a share modal and button which allows a user to send a notification event to all users.

This commit is contained in:
Roland 2023-04-10 13:15:34 -06:00
parent 4e85e2f3d7
commit ac363ae0e2
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
9 changed files with 139 additions and 8 deletions

View file

@ -5,15 +5,15 @@ import {toast} from "react-semantic-toasts-2";
const apiEventName = 'apiEvent';
function eventToast(title, description, type = 'success', time = 5000) {
toast({type: type, title: title, description: description, time: time});
function eventToast(title, description, type = 'success', time = 5000, onClick = null) {
toast({type: type, title: title, description: description, time: time, onClick: onClick});
}
function handleEvents(events) {
const newestEvents = {};
events.forEach(e => {
const {event, message, subject, dt} = e;
const {event, message, subject, dt, url} = e;
document.dispatchEvent(new CustomEvent(apiEventName, {detail: e}));
if (subject && newestEvents[subject] && newestEvents[subject] > dt) {
@ -27,6 +27,16 @@ function handleEvents(events) {
eventToast('Downloads Disabled', message, 'info');
}
if (event === 'user_notify_message') {
console.log(message, url);
eventToast(
message,
'Click here to view the shared page',
'info',
10_000,
() => window.open(url, '_self'));
}
if (subject) {
newestEvents[subject] = dt;
}

View file

@ -782,4 +782,15 @@ export async function fetchFile(path) {
} else {
console.error('Unable to fetch file dict! See client logs.');
}
}
}
export async function sendNotification(message, url) {
const body = {message, url};
const response = await apiPost(`${API_URI}/notify`, body);
if (response.status === 201) {
toast({type: 'success', title: 'Shared', description: 'Your share was sent', time: 2000});
} else {
toast({type: 'error', title: 'Error', description: 'Your share failed to send!', time: 5000});
console.error('Failed to share');
}
}

View file

@ -4,6 +4,7 @@ import {Dropdown, Menu} from "semantic-ui-react";
import {Media, StatusContext, ThemeContext} from "../contexts/contexts";
import Icon from "semantic-ui-react/dist/commonjs/elements/Icon";
import {DarkModeToggle, HotspotStatusIcon, NAME} from "./Common";
import {ShareButton} from "./Share";
const links = [
{text: 'Videos', to: '/videos', key: 'videos'},
@ -64,6 +65,7 @@ export function NavBar() {
{topNavText}
</NavLink>;
const icons = <React.Fragment>
<NavIcon><ShareButton/></NavIcon>
<NavIcon><HotspotStatusIcon/></NavIcon>
<NavIcon><DarkModeToggle/></NavIcon>
</React.Fragment>;

View file

@ -0,0 +1,62 @@
import React from "react";
import {Icon} from "./Theme";
import {Button as SButton, Header, IconGroup, Modal} from "semantic-ui-react";
import QRCode from "react-qr-code";
import {sendNotification} from "../api";
export const ShareEveryoneButton = ({url, size, onClick}) => {
const handleShare = async () => {
await sendNotification('Look at this page', url);
if (onClick) {
await onClick();
}
}
return <SButton
color='violet'
size={size}
onClick={handleShare}
>
Share with everyone
</SButton>
}
export const ShareButton = () => {
const [open, setOpen] = React.useState(false);
const handleOpen = (e) => {
if (e) {
e.preventDefault();
}
setOpen(true);
}
const handleClose = (e) => {
if (e) {
e.preventDefault();
}
setOpen(false);
}
return <>
<Modal closeIcon
open={open}
onClose={handleClose}
>
<Modal.Header>Share this page</Modal.Header>
<Modal.Content>
<Header as='h4'>Another user can scan this QR code to view this page</Header>
<QRCode value={window.location.href}/>
</Modal.Content>
<Modal.Actions>
<ShareEveryoneButton url={window.location.href} onClick={handleClose}/>
<SButton onClick={handleClose}>Close</SButton>
</Modal.Actions>
</Modal>
<a href='#' onClick={handleOpen}>
<IconGroup size='large'>
<Icon name='share'/>
</IconGroup>
</a>
</>
}