From 57470485df4cdb5a2ef87b7991fb24bdb47c9b8d Mon Sep 17 00:00:00 2001 From: Gwynn DP Date: Fri, 11 Mar 2022 18:10:33 -0800 Subject: [PATCH] fix: cleanup comments etc --- src/components/Messaging/AnnounceMessage.js | 18 ----- src/components/Messaging/MessageBody.js | 2 - src/components/Messaging/NewMessage.js | 80 ++++++++++----------- src/components/Messaging/Survey.js | 21 ------ src/context/AppContext.js | 3 - src/context/MessagingContext.js | 2 +- 6 files changed, 39 insertions(+), 87 deletions(-) diff --git a/src/components/Messaging/AnnounceMessage.js b/src/components/Messaging/AnnounceMessage.js index 8a32c6504..d715076ed 100644 --- a/src/components/Messaging/AnnounceMessage.js +++ b/src/components/Messaging/AnnounceMessage.js @@ -61,7 +61,6 @@ const AnnounceMessageForm = ({ setToggleAnnounceMessage }) => { user, regions, postBulkMessageSend, - threads, setThreads, } = useContext(MessagingContext); const { form, sendButton } = useStyles(); @@ -117,26 +116,10 @@ const AnnounceMessageForm = ({ setToggleAnnounceMessage }) => { (payload.body && payload.region_id) ) { const res = await postBulkMessageSend(payload); - log.debug('Announce submit', threads, res); if (res.error) { setErrorMessage(res.message); - // handleModalOpen(); } else { - // id: null; - // type: 'announce'; - // parent_message_id: null; - // from: 'admin'; - // to: null; - // recipient_organization_id: '8b2628b3-733b-4962-943d-95ebea918c9d'; - // recipient_region_id: null; - // subject: 'asdf2'; - // body: 'asdf'; - // composed_at: '2022-03-11T21:55:37.782Z'; - // video_link: null; - // survey_response: null; - // survey: null; - const newAnnouncement = { id: null, type: 'announce', @@ -164,7 +147,6 @@ const AnnounceMessageForm = ({ setToggleAnnounceMessage }) => { }, ...prev, ]; - log.debug('updated threads', updated); return updated; }); } diff --git a/src/components/Messaging/MessageBody.js b/src/components/Messaging/MessageBody.js index 68454cdc3..d22004035 100644 --- a/src/components/Messaging/MessageBody.js +++ b/src/components/Messaging/MessageBody.js @@ -497,7 +497,6 @@ const MessageBody = ({ messages, messageRecipient, avatar }) => { const res = await postMessageSend(messagePayload); if (res.error) { setErrorMessage(res.message); - // handleModalOpen(); } else { const newMessage = { parent_message_id: lastMessage.id ? lastMessage.id : null, @@ -531,7 +530,6 @@ const MessageBody = ({ messages, messageRecipient, avatar }) => { new Date(b?.messages?.at(-1).composed_at) - new Date(a?.messages?.at(-1).composed_at) ); - // log.debug('updated threads', updated); return updated; }); } diff --git a/src/components/Messaging/NewMessage.js b/src/components/Messaging/NewMessage.js index 3941c7300..e1fc02e83 100644 --- a/src/components/Messaging/NewMessage.js +++ b/src/components/Messaging/NewMessage.js @@ -1,4 +1,5 @@ import React, { useState, useContext, useEffect } from 'react'; +import uuid from 'uuid/v4'; import { MessagingContext } from 'context/MessagingContext'; import { FormControl, @@ -13,6 +14,7 @@ import { makeStyles } from '@material-ui/styles'; import GSInputLabel from 'components/common/InputLabel'; const log = require('loglevel'); + const useStyles = makeStyles((theme) => ({ box: { position: 'absolute', @@ -63,8 +65,9 @@ const NewMessage = ({ openModal, handleClose }) => { setErrorMessage, user, authors, - postMessageSend, threads, + setThreads, + postMessageSend, } = useContext(MessagingContext); const [messageContent, setMessageContent] = useState(''); const [recipient, setRecipient] = useState(''); @@ -104,48 +107,41 @@ const NewMessage = ({ openModal, handleClose }) => { if (res.error) { setErrorMessage(res.message); - // handleModalOpen(); } else { - // author_handle: 'admin'; - // body: 'Do your thang\nDo what you gotta do...'; - // organization_id: '8b2628b3-733b-4962-943d-95ebea918c9d'; - // subject: 'I have something to say'; - // type: 'announce'; - // const newAnnouncement = { - // parent_message_id: lastMessage.id ? lastMessage.id : null, - // body: messageContent, - // composed_at: new Date().toISOString(), - // from: user.userName, - // id: uuid(), - // recipient_organization_id: null, - // recipient_region_id: null, - // subject: 'Message', - // survey: null, - // to: messageRecipient, - // type: 'message', - // video_link: null, - // }; - // log.debug('...update threads w/ new announcement'); - // // update the full set of threads - // setThreads((prev) => { - // const updated = prev - // .reduce( - // (threads, thread) => { - // if (thread.userName === messageRecipient) { - // thread.messages.push(newMessage); - // } - // return threads; - // }, - // [...prev] - // ) - // .sort( - // (a, b) => - // new Date(b?.messages?.at(-1).composed_at) - - // new Date(a?.messages?.at(-1).composed_at) - // ); - // // log.debug('updated threads', updated); - // return updated; - // }); + const newMessage = { + parent_message_id: null, + body: messageContent, + composed_at: new Date().toISOString(), + from: user.userName, + id: uuid(), + recipient_organization_id: null, + recipient_region_id: null, + survey: null, + to: recipient, + type: 'message', + video_link: null, + }; + + log.debug('...update threads after postMessageSend'); + // update the full set of threads + setThreads((prev) => { + const updated = prev + .reduce( + (threads, thread) => { + if (thread.userName === recipient) { + thread.messages.push(newMessage); + } + return threads; + }, + [...prev] + ) + .sort( + (a, b) => + new Date(b?.messages?.at(-1).composed_at) - + new Date(a?.messages?.at(-1).composed_at) + ); + return updated; + }); } } handleClose(); diff --git a/src/components/Messaging/Survey.js b/src/components/Messaging/Survey.js index 6bc2279d3..52ab5134a 100644 --- a/src/components/Messaging/Survey.js +++ b/src/components/Messaging/Survey.js @@ -172,27 +172,7 @@ const SurveyForm = ({ setToggleSurvey }) => { if (res.error) { setErrorMessage(res.message); - // handleModalOpen(); } else { - // id: null; - // type: 'survey'; - // parent_message_id: null; - // from: 'admin'; - // to: null; - // recipient_organization_id: null; - // recipient_region_id: null; - // subject: 'Number of trees planted today'; - // body: null; - // composed_at: '2022-01-22T00:00:00.000Z'; - // video_link: null; - // survey_response: null; - - // survey - // id:"99e61995-666c-48c8-9d4e-ca3cff1bfbeb" - // title:"Number of trees planted today" - // questions: [{…}] - // response: false - const newSurvey = { id: uuid(), type: 'survey', @@ -219,7 +199,6 @@ const SurveyForm = ({ setToggleSurvey }) => { }, ...prev, ]; - log.debug('updated threads', updated); return updated; }); } diff --git a/src/context/AppContext.js b/src/context/AppContext.js index 1c2112b71..1b6f40662 100644 --- a/src/context/AppContext.js +++ b/src/context/AppContext.js @@ -32,7 +32,6 @@ import CreditCardIcon from '@material-ui/icons/CreditCard'; import InboxRounded from '@material-ui/icons/InboxRounded'; import { session, hasPermission, POLICIES } from '../models/auth'; import api from '../api/treeTrackerApi'; -import log from 'loglevel'; // no initial context here because we want login values to be 'undefined' until they are confirmed export const AppContext = createContext({}); @@ -196,8 +195,6 @@ export const AppProvider = (props) => { const [userHasOrg, setUserHasOrg] = useState(false); const [orgList, setOrgList] = useState([]); - log.debug('AppProvider', !!user, !!token, !!routes, !!userHasOrg, !!orgList); - // TODO: The below `selectedFilters` state would be better placed under a // separate FilterContext in the future iterations when the need to share // filter state across components increases. For now this is used only in diff --git a/src/context/MessagingContext.js b/src/context/MessagingContext.js index 5bc3772f2..af5ae6a7f 100644 --- a/src/context/MessagingContext.js +++ b/src/context/MessagingContext.js @@ -99,7 +99,7 @@ export const MessagingProvider = (props) => { }; const loadAuthors = async () => { - log.debug('...load authors', user); + log.debug('...load authors'); const res = await api.getAuthors(); if (res.authors) {