more context refactor

This commit is contained in:
Roland Osborne 2022-04-25 10:29:22 -07:00
parent 4f40c53ed3
commit f1d7d4e866
7 changed files with 36 additions and 39 deletions

View File

@ -1,7 +1,7 @@
import { checkResponse, fetchWithTimeout } from './fetchUtil'; import { checkResponse, fetchWithTimeout } from './fetchUtil';
export async function addContactChannelTopic(token, channelId, message, assets ) { export async function addContactChannelTopic(server, token, channelId, message, assets ) {
let topic = await fetchWithTimeout(`/content/channels/${channelId}/topics?contact=${token}`, let topic = await fetchWithTimeout(`https://${server}/content/channels/${channelId}/topics?contact=${token}`,
{ method: 'POST', body: JSON.stringify({}) }); { method: 'POST', body: JSON.stringify({}) });
checkResponse(topic); checkResponse(topic);
let slot = await topic.json(); 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) => { let subject = { data: JSON.stringify(message, (key, value) => {
if (value !== null) return value if (value !== null) return value
}), datatype: 'superbasictopic' }; }), 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) }); { method: 'PUT', body: JSON.stringify(subject) });
checkResponse(unconfirmed); 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') }); { method: 'PUT', body: JSON.stringify('confirmed') });
checkResponse(confirmed); checkResponse(confirmed);

View File

@ -39,12 +39,12 @@ function App() {
<Route path="/user" element={ <User /> }> <Route path="/user" element={ <User /> }>
<Route path="profile" element={<Profile />} /> <Route path="profile" element={<Profile />} />
<Route path="contact/:guid" element={<Contact />} /> <Route path="contact/:guid" element={<Contact />} />
<Route path="conversation/:card/:channel" element={ <Route path="conversation/:cardId/:channelId" element={
<ConversationContextProvider> <ConversationContextProvider>
<Conversation /> <Conversation />
</ConversationContextProvider> </ConversationContextProvider>
} /> } />
<Route path="conversation/:channel" element={ <Route path="conversation/:channelId" element={
<ConversationContextProvider> <ConversationContextProvider>
<Conversation /> <Conversation />
</ConversationContextProvider> </ConversationContextProvider>

View File

@ -7,6 +7,7 @@ import { getCards } from '../Api/getCards';
import { getCardImageUrl } from '../Api/getCardImageUrl'; import { getCardImageUrl } from '../Api/getCardImageUrl';
import { getCardProfile } from '../Api/getCardProfile'; import { getCardProfile } from '../Api/getCardProfile';
import { getCardDetail } from '../Api/getCardDetail'; import { getCardDetail } from '../Api/getCardDetail';
import { addContactChannelTopic } from '../Api/addContactChannelTopic';
export function useCardContext() { export function useCardContext() {
const [state, setState] = useState({ const [state, setState] = useState({
@ -146,6 +147,12 @@ export function useCardContext() {
setCards(rev); setCards(rev);
}, },
getImageUrl: (cardId, rev) => getCardImageUrl(access.current, cardId, 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 } return { state, actions }

View File

@ -2,6 +2,7 @@ import { useEffect, useState, useRef } from 'react';
import { getChannels } from '../Api/getChannels'; import { getChannels } from '../Api/getChannels';
import { getChannel } from '../Api/getChannel'; import { getChannel } from '../Api/getChannel';
import { addChannel } from '../Api/addChannel'; import { addChannel } from '../Api/addChannel';
import { addChannelTopic } from '../Api/addChannelTopic';
export function useChannelContext() { export function useChannelContext() {
const [state, setState] = useState({ const [state, setState] = useState({
@ -73,6 +74,9 @@ export function useChannelContext() {
addChannel: async (cards, subject, description) => { addChannel: async (cards, subject, description) => {
await addChannel(access.current, 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 } return { state, actions }

View File

@ -2,7 +2,8 @@ import { useContext, useState, useEffect } from 'react';
import { useParams } from 'react-router-dom'; import { useParams } from 'react-router-dom';
import { addChannelTopic } from '../../../Api/addChannelTopic'; import { addChannelTopic } from '../../../Api/addChannelTopic';
import { addContactChannelTopic } from '../../../Api/addContactChannelTopic'; import { addContactChannelTopic } from '../../../Api/addContactChannelTopic';
import { AppContext } from '../../../AppContext/AppContext'; import { CardContext } from '../../../AppContext/CardContext';
import { ChannelContext } from '../../../AppContext/ChannelContext';
export function useAddTopic() { export function useAddTopic() {
@ -15,8 +16,9 @@ export function useAddTopic() {
busy: false, busy: false,
}); });
const { card, channel } = useParams(); const { cardId, channelId } = useParams();
const app = useContext(AppContext); const card = useContext(CardContext);
const channel = useContext(ChannelContext);
const updateState = (value) => { const updateState = (value) => {
setState((s) => ({ ...s, ...value })); setState((s) => ({ ...s, ...value }));
@ -56,13 +58,11 @@ export function useAddTopic() {
try { try {
let message = { text: state.messageText, textColor: state.messageColor, let message = { text: state.messageText, textColor: state.messageColor,
textSize: state.messageSize, backgroundColor: state.backgroundColor }; textSize: state.messageSize, backgroundColor: state.backgroundColor };
if (card) { if (cardId) {
let contact = app.actions.getCard(card); await card.actions.addChannelTopic(cardId, channelId, message, []);
let token = contact?.data?.cardProfile?.guid + '.' + contact?.data?.cardDetail?.token;
await addContactChannelTopic(token, channel, message, []);
} }
else { else {
await addChannelTopic(app.state.token, channel, message, []); await channel.actions.addChannelTopic(channelId, message, []);
} }
updateState({ messageText: null, messageColor: null, messageSize: null, backgroundColor: null }); updateState({ messageText: null, messageColor: null, messageSize: null, backgroundColor: null });
} }

View File

@ -13,12 +13,14 @@ export function useConversation() {
topics: [], topics: [],
}); });
const { card, channel } = useParams(); const { cardId, channelId } = useParams();
const navigate = useNavigate(); const navigate = useNavigate();
const app = useContext(AppContext); const app = useContext(AppContext);
const conversation = useContext(ConversationContext); const conversation = useContext(ConversationContext);
const topics = useRef(new Map()); const topics = useRef(new Map());
console.log("PARAMS: ", cardId, channelId);
const updateState = (value) => { const updateState = (value) => {
setState((s) => ({ ...s, ...value })); setState((s) => ({ ...s, ...value }));
} }
@ -30,13 +32,13 @@ export function useConversation() {
}; };
const updateConversation = async () => { const updateConversation = async () => {
if (card) { if (cardId) {
if(app?.actions?.getCard) { if(app?.actions?.getCard) {
let contact = app.actions.getCard(card); let contact = app.actions.getCard(cardId);
let conversation = contact.channels.get(channel); let conversation = contact.channels.get(channelId);
if (conversation?.revision != state.revision) { if (conversation?.revision != state.revision) {
let token = contact.data.cardProfile.guid + "." + contact.data.cardDetail.token; 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) { for (let topic of slots) {
if (topic.data == null) { if (topic.data == null) {
topics.current.delete(topic.id); topics.current.delete(topic.id);
@ -52,7 +54,7 @@ export function useConversation() {
cur.data.detailRevision = topic.data.detailRevision; cur.data.detailRevision = topic.data.detailRevision;
} }
else { 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.topicDetail = slot.data.topicDetail;
cur.data.detailRevision = slot.data.detailRevision; cur.data.detailRevision = slot.data.detailRevision;
} }
@ -71,9 +73,9 @@ export function useConversation() {
} }
else { else {
if(app?.actions?.getChannel) { if(app?.actions?.getChannel) {
let conversation = app.actions.getChannel(channel); let conversation = app.actions.getChannel(channelId);
if (conversation?.revision != state.revision) { 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) { for (let topic of slots) {
if (topic.data == null) { if (topic.data == null) {
@ -90,7 +92,7 @@ export function useConversation() {
cur.data.detailRevision = topic.data.detailRevision; cur.data.detailRevision = topic.data.detailRevision;
} }
else { 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.topicDetail = slot.data.topicDetail;
cur.data.detailRevision = slot.data.detailRevision; cur.data.detailRevision = slot.data.detailRevision;
} }

View File

@ -1,30 +1,14 @@
import { useContext, useState, useEffect } from 'react'; import { useContext, useState, useEffect } from 'react';
import { AppContext } from '../AppContext/AppContext';
import { useNavigate } from "react-router-dom";
export function useUser() { export function useUser() {
const [state, setState] = useState({}); const [state, setState] = useState({});
const navigate = useNavigate();
const app = useContext(AppContext);
const actions = { const actions = {
updateState: (value) => { updateState: (value) => {
setState((s) => ({ ...s, ...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 }; return { state, actions };
} }