mirror of
https://github.com/balzack/databag.git
synced 2025-02-14 20:49:16 +00:00
more context refactor
This commit is contained in:
parent
4f40c53ed3
commit
f1d7d4e866
@ -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);
|
||||
|
||||
|
@ -39,12 +39,12 @@ function App() {
|
||||
<Route path="/user" element={ <User /> }>
|
||||
<Route path="profile" element={<Profile />} />
|
||||
<Route path="contact/:guid" element={<Contact />} />
|
||||
<Route path="conversation/:card/:channel" element={
|
||||
<Route path="conversation/:cardId/:channelId" element={
|
||||
<ConversationContextProvider>
|
||||
<Conversation />
|
||||
</ConversationContextProvider>
|
||||
} />
|
||||
<Route path="conversation/:channel" element={
|
||||
<Route path="conversation/:channelId" element={
|
||||
<ConversationContextProvider>
|
||||
<Conversation />
|
||||
</ConversationContextProvider>
|
||||
|
@ -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 }
|
||||
|
@ -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 }
|
||||
|
@ -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 });
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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 };
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user