Skip to content

Commit

Permalink
adding functionality for notifications on doc view
Browse files Browse the repository at this point in the history
  • Loading branch information
mbogo-mit committed Dec 27, 2023
1 parent ae62a24 commit 10ef992
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 66 deletions.
51 changes: 45 additions & 6 deletions src/components/AnnotationCard/AnnotationCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ import {
DocumentAnnotationsContext,
DocumentFiltersContext,
DocumentActiveAnnotationsContext,
WebsocketContext,
} from '../../contexts/DocumentContext';
import { copyToClipboard } from '../../utils/docUIUtils';
import { DeepCopyObj, copyToClipboard } from '../../utils/docUIUtils';
import { FirstNameLastInitial } from '../../utils/nameUtil';
import { fixIframes } from '../../utils/parseUtil';
import AnnotationShareableLinkIcon from '../AnnotationShareableLinkIcon';
Expand Down Expand Up @@ -76,6 +77,9 @@ function AnnotationCard({
membersIntersection,
setAlerts,
}) {

const [messageHistory, setMessageHistory, handleSendJsonMessage, lastJsonMessage, readyState, connectionStatus, getWebSocket, websocketID] = useContext(WebsocketContext);

const refCardClickedAlready = useRef(false);
const [activeAnnotations] = useContext(DocumentActiveAnnotationsContext);
const [,,,
Expand Down Expand Up @@ -192,10 +196,45 @@ function AnnotationCard({
$(`.annotation-highlighted-text[annotation-id='${id}']`).removeClass('active');
}

function SetAndSaveAnnotationData(anno) {
function SendNewAnnotationWebsocketNotification(anno, new_anno) {

const { permissions: { private: priv, sharedTo } } = anno;

if (priv || sharedTo) {
// don't send out notification
return;
}

const rm = `[doc-view]-${anno.target.document.slug}`;
const json = {
"action": 'request',
"rm": rm,
"_notification": {
"annotation": anno,
"new_annotation": new_anno,
"user": { "id": `${anno.creator.id}`, "name": `${anno.creator.name}`, "email": `${anno.creator.email}`, "websocket_id": `${websocketID}`, date: new Date(), },
"withGroupId": anno.creator.withGroupId,
},
};

handleSendJsonMessage(DeepCopyObj(json));

}

function SetAndSaveAnnotationData(args) {
const {
anno,
new: new_anno,
sendNotification,
} = args || {};
setAnnotationData(anno);
saveAnnotationChanges(anno, side);
setDocumentFilters({ ...documentFilters, filterOnInit: true });

// send websocket notification that user made an annotation
if (sendNotification) {
SendNewAnnotationWebsocketNotification(anno, new_anno)
}
}

function SaveAnnotation() {
Expand Down Expand Up @@ -277,7 +316,7 @@ function AnnotationCard({
setExpanded(true);
AddClassActive(newAnnotationData._id);
// update the annotation data
SetAndSaveAnnotationData(newAnnotationData);
SetAndSaveAnnotationData({ anno: newAnnotationData, new: true, sendNotification: true });
// focus annotation so that things get shifted to their correct spots
setUpdateFocusOfAnnotation(true);
}).catch((err) => {
Expand All @@ -295,7 +334,7 @@ function AnnotationCard({
setSavingAnnotation(false);
// once the new annotation data saves properly
// on the database then we can update the annotation data
SetAndSaveAnnotationData(newAnnotationData);
SetAndSaveAnnotationData({ anno: newAnnotationData, new: false, sendNotification: true });

// after setting the annotation data we need
// to reset the "new" data back to null
Expand Down Expand Up @@ -335,7 +374,7 @@ function AnnotationCard({
// if the annotation is not new then canceling
// should just return it to its previous state
annotationData.editing = false;
SetAndSaveAnnotationData(annotationData);
SetAndSaveAnnotationData({ anno: annotationData, new: false, sendNotification: false });
setUpdateFocusOfAnnotation(true);
}
}
Expand Down Expand Up @@ -707,7 +746,7 @@ function AnnotationCard({
setShowUnsavedChangesToast(true);
} else {
annotationData.editing = true;
SetAndSaveAnnotationData(annotationData);
SetAndSaveAnnotationData({ anno: annotationData, new: false, sendNotification: false });
}
}}
>
Expand Down
8 changes: 7 additions & 1 deletion src/pages/_app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'semantic-ui-css/components/label.min.css';
import 'semantic-ui-css/components/icon.min.css';
import 'react-bootstrap-typeahead/css/Typeahead.css';
import '../style/custom.scss';
import { RID } from '../utils/docUIUtils';

Router.events.on('routeChangeStart', () => NProgress.start());
Router.events.on('routeChangeComplete', () => NProgress.done());
Expand All @@ -24,6 +25,7 @@ export default function AnnotationStudio({ Component, pageProps }) {

const [socketUrl, setSocketUrl] = useState('wss://wq5pn518mf.execute-api.us-east-2.amazonaws.com/dev/');
const [messageHistory, setMessageHistory] = useState([]);
const [websocketID, setWebsocketID] = useState();

const {
sendMessage,
Expand Down Expand Up @@ -57,9 +59,13 @@ export default function AnnotationStudio({ Component, pageProps }) {
}
}, [lastJsonMessage, setMessageHistory]);

useEffect(() => {
setWebsocketID(RID());
}, [])

return (
<SessionProvider session={pageProps.session}>
<WebsocketContext.Provider value={[messageHistory, setMessageHistory, handleSendJsonMessage, lastJsonMessage, readyState, connectionStatus, getWebSocket]}>
<WebsocketContext.Provider value={[messageHistory, setMessageHistory, handleSendJsonMessage, lastJsonMessage, readyState, connectionStatus, getWebSocket, websocketID]}>
<Component {...pageProps} statefulSession={session} updateSession={setSession} />
</WebsocketContext.Provider>
</SessionProvider>
Expand Down
Loading

0 comments on commit 10ef992

Please sign in to comment.