From f1d7d4e866c5f2a96a208f875b65979a46746501 Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Mon, 25 Apr 2022 10:29:22 -0700 Subject: [PATCH] more context refactor --- net/web/src/Api/addContactChannelTopic.js | 8 ++++---- net/web/src/App.js | 4 ++-- net/web/src/AppContext/useCardContext.hook.js | 7 +++++++ .../src/AppContext/useChannelContext.hook.js | 4 ++++ .../Conversation/AddTopic/useAddTopic.hook.js | 16 +++++++-------- .../User/Conversation/useConversation.hook.js | 20 ++++++++++--------- net/web/src/User/useUser.hook.js | 16 --------------- 7 files changed, 36 insertions(+), 39 deletions(-) diff --git a/net/web/src/Api/addContactChannelTopic.js b/net/web/src/Api/addContactChannelTopic.js index 942196bb..646a8c38 100644 --- a/net/web/src/Api/addContactChannelTopic.js +++ b/net/web/src/Api/addContactChannelTopic.js @@ -1,7 +1,7 @@ import { checkResponse, fetchWithTimeout } from './fetchUtil'; -export async function addContactChannelTopic(token, channelId, message, assets ) { - let topic = await fetchWithTimeout(`/content/channels/${channelId}/topics?contact=${token}`, +export async function addContactChannelTopic(server, token, channelId, message, assets ) { + let topic = await fetchWithTimeout(`https://${server}/content/channels/${channelId}/topics?contact=${token}`, { method: 'POST', body: JSON.stringify({}) }); checkResponse(topic); let slot = await topic.json(); @@ -11,11 +11,11 @@ export async function addContactChannelTopic(token, channelId, message, assets ) let subject = { data: JSON.stringify(message, (key, value) => { if (value !== null) return value }), datatype: 'superbasictopic' }; - let unconfirmed = await fetchWithTimeout(`/content/channels/${channelId}/topics/${slot.id}/subject?contact=${token}`, + let unconfirmed = await fetchWithTimeout(`https://${server}/content/channels/${channelId}/topics/${slot.id}/subject?contact=${token}`, { method: 'PUT', body: JSON.stringify(subject) }); checkResponse(unconfirmed); - let confirmed = await fetchWithTimeout(`/content/channels/${channelId}/topics/${slot.id}/confirmed?contact=${token}`, + let confirmed = await fetchWithTimeout(`https://${server}/content/channels/${channelId}/topics/${slot.id}/confirmed?contact=${token}`, { method: 'PUT', body: JSON.stringify('confirmed') }); checkResponse(confirmed); diff --git a/net/web/src/App.js b/net/web/src/App.js index 51e9f162..a7c3187f 100644 --- a/net/web/src/App.js +++ b/net/web/src/App.js @@ -39,12 +39,12 @@ function App() { }> } /> } /> - } /> - diff --git a/net/web/src/AppContext/useCardContext.hook.js b/net/web/src/AppContext/useCardContext.hook.js index d71362a0..31329d71 100644 --- a/net/web/src/AppContext/useCardContext.hook.js +++ b/net/web/src/AppContext/useCardContext.hook.js @@ -7,6 +7,7 @@ import { getCards } from '../Api/getCards'; import { getCardImageUrl } from '../Api/getCardImageUrl'; import { getCardProfile } from '../Api/getCardProfile'; import { getCardDetail } from '../Api/getCardDetail'; +import { addContactChannelTopic } from '../Api/addContactChannelTopic'; export function useCardContext() { const [state, setState] = useState({ @@ -146,6 +147,12 @@ export function useCardContext() { setCards(rev); }, getImageUrl: (cardId, rev) => getCardImageUrl(access.current, cardId, rev), + addChannelTopic: async (cardId, channelId, message, assets) => { + let { cardProfile, cardDetail } = cards.current.get(cardId).data; + let token = cardProfile.guid + '.' + cardDetail.token; + let node = cardProfile.node; + await addContactChannelTopic(node, token, channelId, message, assets); + }, } return { state, actions } diff --git a/net/web/src/AppContext/useChannelContext.hook.js b/net/web/src/AppContext/useChannelContext.hook.js index ec9af55b..d783d657 100644 --- a/net/web/src/AppContext/useChannelContext.hook.js +++ b/net/web/src/AppContext/useChannelContext.hook.js @@ -2,6 +2,7 @@ import { useEffect, useState, useRef } from 'react'; import { getChannels } from '../Api/getChannels'; import { getChannel } from '../Api/getChannel'; import { addChannel } from '../Api/addChannel'; +import { addChannelTopic } from '../Api/addChannelTopic'; export function useChannelContext() { const [state, setState] = useState({ @@ -73,6 +74,9 @@ export function useChannelContext() { addChannel: async (cards, subject, description) => { await addChannel(access.current, cards, subject, description); }, + addChannelTopic: async (channelId, message, assets) => { + await addChannelTopic(access.current, channelId, message, assets); + }, } return { state, actions } diff --git a/net/web/src/User/Conversation/AddTopic/useAddTopic.hook.js b/net/web/src/User/Conversation/AddTopic/useAddTopic.hook.js index 969b1ca0..b363853e 100644 --- a/net/web/src/User/Conversation/AddTopic/useAddTopic.hook.js +++ b/net/web/src/User/Conversation/AddTopic/useAddTopic.hook.js @@ -2,7 +2,8 @@ import { useContext, useState, useEffect } from 'react'; import { useParams } from 'react-router-dom'; import { addChannelTopic } from '../../../Api/addChannelTopic'; import { addContactChannelTopic } from '../../../Api/addContactChannelTopic'; -import { AppContext } from '../../../AppContext/AppContext'; +import { CardContext } from '../../../AppContext/CardContext'; +import { ChannelContext } from '../../../AppContext/ChannelContext'; export function useAddTopic() { @@ -15,8 +16,9 @@ export function useAddTopic() { busy: false, }); - const { card, channel } = useParams(); - const app = useContext(AppContext); + const { cardId, channelId } = useParams(); + const card = useContext(CardContext); + const channel = useContext(ChannelContext); const updateState = (value) => { setState((s) => ({ ...s, ...value })); @@ -56,13 +58,11 @@ export function useAddTopic() { try { let message = { text: state.messageText, textColor: state.messageColor, textSize: state.messageSize, backgroundColor: state.backgroundColor }; - if (card) { - let contact = app.actions.getCard(card); - let token = contact?.data?.cardProfile?.guid + '.' + contact?.data?.cardDetail?.token; - await addContactChannelTopic(token, channel, message, []); + if (cardId) { + await card.actions.addChannelTopic(cardId, channelId, message, []); } else { - await addChannelTopic(app.state.token, channel, message, []); + await channel.actions.addChannelTopic(channelId, message, []); } updateState({ messageText: null, messageColor: null, messageSize: null, backgroundColor: null }); } diff --git a/net/web/src/User/Conversation/useConversation.hook.js b/net/web/src/User/Conversation/useConversation.hook.js index f78984c4..bbbf1f2f 100644 --- a/net/web/src/User/Conversation/useConversation.hook.js +++ b/net/web/src/User/Conversation/useConversation.hook.js @@ -13,12 +13,14 @@ export function useConversation() { topics: [], }); - const { card, channel } = useParams(); + const { cardId, channelId } = useParams(); const navigate = useNavigate(); const app = useContext(AppContext); const conversation = useContext(ConversationContext); const topics = useRef(new Map()); +console.log("PARAMS: ", cardId, channelId); + const updateState = (value) => { setState((s) => ({ ...s, ...value })); } @@ -30,13 +32,13 @@ export function useConversation() { }; const updateConversation = async () => { - if (card) { + if (cardId) { if(app?.actions?.getCard) { - let contact = app.actions.getCard(card); - let conversation = contact.channels.get(channel); + let contact = app.actions.getCard(cardId); + let conversation = contact.channels.get(channelId); if (conversation?.revision != state.revision) { let token = contact.data.cardProfile.guid + "." + contact.data.cardDetail.token; - let slots = await getContactChannelTopics(token, channel, state.revision); + let slots = await getContactChannelTopics(token, channelId, state.revision); for (let topic of slots) { if (topic.data == null) { topics.current.delete(topic.id); @@ -52,7 +54,7 @@ export function useConversation() { cur.data.detailRevision = topic.data.detailRevision; } else { - let slot = await getContactChannelTopic(token, channel, topic.id); + let slot = await getContactChannelTopic(token, channelId, topic.id); cur.data.topicDetail = slot.data.topicDetail; cur.data.detailRevision = slot.data.detailRevision; } @@ -71,9 +73,9 @@ export function useConversation() { } else { if(app?.actions?.getChannel) { - let conversation = app.actions.getChannel(channel); + let conversation = app.actions.getChannel(channelId); if (conversation?.revision != state.revision) { - let slots = await getChannelTopics(app.state.token, channel, state.revision); + let slots = await getChannelTopics(app.state.token, channelId, state.revision); for (let topic of slots) { if (topic.data == null) { @@ -90,7 +92,7 @@ export function useConversation() { cur.data.detailRevision = topic.data.detailRevision; } else { - let slot = await getChannelTopic(app.state.token, channel, topic.id); + let slot = await getChannelTopic(app.state.token, channelId, topic.id); cur.data.topicDetail = slot.data.topicDetail; cur.data.detailRevision = slot.data.detailRevision; } diff --git a/net/web/src/User/useUser.hook.js b/net/web/src/User/useUser.hook.js index 0ab6fc93..6b197511 100644 --- a/net/web/src/User/useUser.hook.js +++ b/net/web/src/User/useUser.hook.js @@ -1,30 +1,14 @@ import { useContext, useState, useEffect } from 'react'; -import { AppContext } from '../AppContext/AppContext'; -import { useNavigate } from "react-router-dom"; export function useUser() { const [state, setState] = useState({}); - const navigate = useNavigate(); - const app = useContext(AppContext); - const actions = { updateState: (value) => { setState((s) => ({ ...s, ...value })); }, }; - useEffect(() => { - if (app) { - if (app.state == null) { - navigate('/') - } - else if (app.state.access === 'admin') { - navigate('/admin') - } - } - }, [app]) - return { state, actions }; }