diff --git a/net/web/src/Api/getListing.js b/net/web/src/Api/getListing.js new file mode 100644 index 00000000..e6a31f8e --- /dev/null +++ b/net/web/src/Api/getListing.js @@ -0,0 +1,8 @@ +import { checkResponse, fetchWithTimeout } from './fetchUtil'; + +export async function getListing(server) { + let listing = await fetchWithTimeout(`https://${server}/account/listing`, { method: 'GET' }); + checkResponse(listing); + return await listing.json(); +} + diff --git a/net/web/src/Api/getListingImageUrl.js b/net/web/src/Api/getListingImageUrl.js new file mode 100644 index 00000000..61dbf3e1 --- /dev/null +++ b/net/web/src/Api/getListingImageUrl.js @@ -0,0 +1,5 @@ +export function getListingImageUrl(server, guid, revision) { + return `https://${server}/account/listing/${guid}/image?revision=${revision}` +} + + diff --git a/net/web/src/AppContext/useAccountContext.hook.js b/net/web/src/AppContext/useAccountContext.hook.js index eb8a5030..e03371dd 100644 --- a/net/web/src/AppContext/useAccountContext.hook.js +++ b/net/web/src/AppContext/useAccountContext.hook.js @@ -40,7 +40,7 @@ export function useAccountContext() { setStatus(rev); }, setSearchable: async (flag) => { - await setAccountSearchable(state.token, flag); + await setAccountSearchable(access.current, flag); }, } diff --git a/net/web/src/AppContext/useCardContext.hook.js b/net/web/src/AppContext/useCardContext.hook.js index 31329d71..d3f288b9 100644 --- a/net/web/src/AppContext/useCardContext.hook.js +++ b/net/web/src/AppContext/useCardContext.hook.js @@ -8,6 +8,13 @@ import { getCardImageUrl } from '../Api/getCardImageUrl'; import { getCardProfile } from '../Api/getCardProfile'; import { getCardDetail } from '../Api/getCardDetail'; import { addContactChannelTopic } from '../Api/addContactChannelTopic'; +import { setCardConnecting, setCardConnected, setCardConfirmed } from '../Api/setCardStatus'; +import { getCardOpenMessage } from '../Api/getCardOpenMessage'; +import { setCardOpenMessage } from '../Api/setCardOpenMessage'; +import { getCardCloseMessage } from '../Api/getCardCloseMessage'; +import { setCardCloseMessage } from '../Api/setCardCloseMessage'; +import { addCard } from '../Api/addCard'; +import { removeCard } from '../Api/removeCard'; export function useCardContext() { const [state, setState] = useState({ @@ -139,6 +146,16 @@ export function useCardContext() { } } + const getCardByGuid = (guid) => { + let card = null; + cards.current.forEach((value, key, map) => { + if(value?.data?.cardProfile?.guid == guid) { + card = value; + } + }); + return card; + } + const actions = { setToken: async (token) => { access.current = token; @@ -146,6 +163,7 @@ export function useCardContext() { setRevision: async (rev) => { setCards(rev); }, + getCardByGuid: getCardByGuid, getImageUrl: (cardId, rev) => getCardImageUrl(access.current, cardId, rev), addChannelTopic: async (cardId, channelId, message, assets) => { let { cardProfile, cardDetail } = cards.current.get(cardId).data; @@ -153,6 +171,33 @@ export function useCardContext() { let node = cardProfile.node; await addContactChannelTopic(node, token, channelId, message, assets); }, + addCard: async (message) => { + return await addCard(access.current, message); + }, + removeCard: async (cardId) => { + return await removeCard(access.current, cardId); + }, + setCardConnecting: async (cardId) => { + return await setCardConnecting(access.current, cardId); + }, + setCardConnected: async (cardId, token, view, article, channel, profile) => { + return await setCardConnected(access.current, cardId, token, view, article, channel, profile); + }, + setCardConfirmed: async (cardId) => { + return await setCardConfirmed(access.current, cardId); + }, + getCardOpenMessage: async (cardId) => { + return await getCardOpenMessage(access.current, cardId); + }, + setCardOpenMessage: async (server, message) => { + return await setCardOpenMessage(server, message); + }, + getCardCloseMessage: async (cardId) => { + return await getCardCloseMessage(access.current, cardId); + }, + setCardCloseMessage: async (server, message) => { + return await setCardCloseMessage(server, message); + }, } return { state, actions } diff --git a/net/web/src/AppContext/useProfileContext.hook.js b/net/web/src/AppContext/useProfileContext.hook.js index 23f22abf..1c529dcd 100644 --- a/net/web/src/AppContext/useProfileContext.hook.js +++ b/net/web/src/AppContext/useProfileContext.hook.js @@ -42,10 +42,10 @@ export function useProfileContext() { setProfile(rev); }, setProfileData: async (name, location, description) => { - await setProfileData(state.token, name, location, description); + await setProfileData(access.current, name, location, description); }, setProfileImage: async (image) => { - await setProfileImage(state.token, image); + await setProfileImage(access.current, image); }, profileImageUrl: () => getProfileImageUrl(access.current, revision.current), } diff --git a/net/web/src/User/Contact/useContact.hook.js b/net/web/src/User/Contact/useContact.hook.js index 37bd2eb6..bd091904 100644 --- a/net/web/src/User/Contact/useContact.hook.js +++ b/net/web/src/User/Contact/useContact.hook.js @@ -1,14 +1,8 @@ import { useContext, useState, useEffect } from 'react'; -import { AppContext } from '../../AppContext/AppContext'; +import { CardContext } from '../../AppContext/CardContext'; import { useNavigate, useLocation, useParams } from "react-router-dom"; import { getListingMessage } from '../../Api/getListingMessage'; -import { addCard } from '../../Api/addCard'; -import { removeCard } from '../../Api/removeCard'; -import { setCardConnecting, setCardConnected, setCardConfirmed } from '../../Api/setCardStatus'; -import { getCardOpenMessage } from '../../Api/getCardOpenMessage'; -import { setCardOpenMessage } from '../../Api/setCardOpenMessage'; -import { getCardCloseMessage } from '../../Api/getCardCloseMessage'; -import { setCardCloseMessage } from '../../Api/setCardCloseMessage'; +import { getListingImageUrl } from '../../Api/getListingImageUrl'; export function useContact() { @@ -28,7 +22,7 @@ export function useContact() { const data = useLocation(); const { guid } = useParams(); const navigate = useNavigate(); - const app = useContext(AppContext); + const card = useContext(CardContext); const updateState = (value) => { setState((s) => ({ ...s, ...value })); @@ -43,7 +37,7 @@ export function useContact() { updateState({ busy: true }); try { let message = await getListingMessage(state.node, guid); - let card = await addCard(app.state.token, message); + await card.actions.addCard(message); } catch (err) { window.alert(err); @@ -55,7 +49,7 @@ export function useContact() { if (!state.busy) { updateState({ busy: true }); try { - await setCardConfirmed(app.state.token, state.cardId); + await card.actions.setCardConfirmed(state.cardId); } catch (err) { window.alert(err); @@ -67,11 +61,11 @@ export function useContact() { if (!state.busy) { updateState({ busy: true }); try { - await setCardConnecting(app.state.token, state.cardId); - let message = await getCardOpenMessage(app.state.token, state.cardId); - let contact = await setCardOpenMessage(state.node, message); + await card.actions.setCardConnecting(state.cardId); + let message = await card.actions.getCardOpenMessage(state.cardId); + let contact = await card.actions.setCardOpenMessage(state.node, message); if (contact.status === 'connected') { - await setCardConnected(app.state.token, state.cardId, contact.token, contact.viewRevision, contact.articleRevision, contact.channelRevision, contact.profileRevision); + await card.actions.setCardConnected(state.cardId, contact.token, contact.viewRevision, contact.articleRevision, contact.channelRevision, contact.profileRevision); } } catch (err) { @@ -84,10 +78,10 @@ export function useContact() { if (!state.busy) { updateState({ busy: true }); try { - await setCardConfirmed(app.state.token, state.cardId); + await card.actions.setCardConfirmed(state.cardId); try { - let message = await getCardCloseMessage(app.state.token, state.cardId); - await setCardCloseMessage(state.node, message); + let message = await card.actions.getCardCloseMessage(state.cardId); + await card.actions.setCardCloseMessage(state.node, message); } catch (err) { console.log(err); @@ -103,7 +97,7 @@ export function useContact() { if (!state.busy) { updateState({ busy: true }); try { - await removeCard(app.state.token, state.cardId); + await card.actions.removeCard(state.cardId); navigate('/user'); } catch (err) { @@ -116,11 +110,11 @@ export function useContact() { if (!state.busy) { updateState({ busy: true }); try { - await setCardConfirmed(app.state.token, state.cardId); + await card.actions.setCardConfirmed(state.cardId); try { - let message = await getCardCloseMessage(app.state.token, state.cardId); - await setCardCloseMessage(state.node, message); - await removeCard(app.state.token, state.cardId); + let message = await card.actions.getCardCloseMessage(state.cardId); + await card.actions.setCardCloseMessage(state.node, message); + await card.actions.removeCard(state.cardId); navigate('/user'); } catch (err) { @@ -138,12 +132,12 @@ export function useContact() { updateState({ busy: true }); try { let profile = await getListingMessage(state.node, guid); - let card = await addCard(app.state.token, profile); - await setCardConnecting(app.state.token, card.id); - let open = await getCardOpenMessage(app.state.token, card.id); - let contact = await setCardOpenMessage(state.node, open); + let added = await card.actions.addCard(profile); + await card.actions.setCardConnecting(added.id); + let open = await card.actions.getCardOpenMessage(added.id); + let contact = await card.actions.setCardOpenMessage(state.node, open); if (contact.status === 'connected') { - await setCardConnected(app.state.token, card.id, contact.token, contact.viewRevision, contact.articleRevision, contact.channelRevision, contact.profileRevision); + await card.actions.setCardConnected(added.id, contact.token, contact.viewRevision, contact.articleRevision, contact.channelRevision, contact.profileRevision); } } catch (err) { @@ -155,61 +149,59 @@ export function useContact() { }; useEffect(() => { - if (app?.state?.access === 'user') { - let card = app.actions.getCardByGuid(guid); - if (card) { - let profile = card.data.cardProfile; - updateState({ cardId: card.id }); - updateState({ handle: profile.handle }); - updateState({ name: profile.name }); - updateState({ description: profile.description }); - updateState({ location: profile.location }); - updateState({ node: profile.node }); - if (card.data.cardProfile.imageSet) { - updateState({ imageUrl: app.actions.getCardImageUrl(card.id, card.revision) }); - } - else { - updateState({ imageUrl: '' }); - } - let status = card.data.cardDetail.status; - if (status === 'connected') { - updateState({ status: 'connected' }); - updateState({ showButtons: { disconnect: true, disconnectRemove: true }}); - } - if (status === 'connecting') { - updateState({ status: 'connecting' }); - updateState({ showButtons: { cancel: true, disconnectRemove: true }}); - } - if (status === 'pending') { - updateState({ status: 'pending' }); - updateState({ showButtons: { ignore: true, confirm: true, confirmConnect: true }}); - } - if (status === 'confirmed') { - updateState({ status: 'confirmed' }); - updateState({ showButtons: { remove: true, connect: true }}); - } - if (status === 'requested') { - updateState({ status: 'requested' }); - updateState({ showButtons: { deny: true, accept: true }}); - } + let contact = card.actions.getCardByGuid(guid); + if (contact) { + let profile = contact.data.cardProfile; + updateState({ cardId: contact.id }); + updateState({ handle: profile.handle }); + updateState({ name: profile.name }); + updateState({ description: profile.description }); + updateState({ location: profile.location }); + updateState({ node: profile.node }); + if (contact.data.cardProfile.imageSet) { + updateState({ imageUrl: card.actions.getImageUrl(contact.id, contact.revision) }); } - else if (data.state) { - updateState({ handle: data.state.handle }); - updateState({ name: data.state.name }); - updateState({ description: data.state.description }); - updateState({ location: data.state.location }); - updateState({ node: data.state.node }); - if (data.state.imageSet) { - updateState({ imageUrl: app.actions.getRegistryImageUrl(data.state.node, guid, data.state.revision) }); - } - else { - updateState({ imageUrl: '' }); - } - updateState({ status: 'unsaved' }); - updateState({ showButtons: { save: true, saveRequest: true }}); + else { + updateState({ imageUrl: '' }); + } + let status = contact.data.cardDetail.status; + if (status === 'connected') { + updateState({ status: 'connected' }); + updateState({ showButtons: { disconnect: true, disconnectRemove: true }}); + } + if (status === 'connecting') { + updateState({ status: 'connecting' }); + updateState({ showButtons: { cancel: true, disconnectRemove: true }}); + } + if (status === 'pending') { + updateState({ status: 'pending' }); + updateState({ showButtons: { ignore: true, confirm: true, confirmConnect: true }}); + } + if (status === 'confirmed') { + updateState({ status: 'confirmed' }); + updateState({ showButtons: { remove: true, connect: true }}); + } + if (status === 'requested') { + updateState({ status: 'requested' }); + updateState({ showButtons: { deny: true, accept: true }}); } } - }, [app, guid]) + else if (data.state) { + updateState({ handle: data.state.handle }); + updateState({ name: data.state.name }); + updateState({ description: data.state.description }); + updateState({ location: data.state.location }); + updateState({ node: data.state.node }); + if (data.state.imageSet) { + updateState({ imageUrl: getListingImageUrl(data.state.node, guid, data.state.revision) }); + } + else { + updateState({ imageUrl: '' }); + } + updateState({ status: 'unsaved' }); + updateState({ showButtons: { save: true, saveRequest: true }}); + } + }, [card, guid]) return { state, actions }; } diff --git a/net/web/src/User/SideBar/Contacts/Cards/Registry/useRegistry.hook.js b/net/web/src/User/SideBar/Contacts/Cards/Registry/useRegistry.hook.js index cbaeae4d..8d150483 100644 --- a/net/web/src/User/SideBar/Contacts/Cards/Registry/useRegistry.hook.js +++ b/net/web/src/User/SideBar/Contacts/Cards/Registry/useRegistry.hook.js @@ -1,6 +1,8 @@ import { useContext, useState, useEffect } from 'react'; -import { AppContext } from '../../../../../AppContext/AppContext'; +import { ProfileContext } from '../../../../../AppContext/ProfileContext'; import { useNavigate } from "react-router-dom"; +import { getListing } from '../../../../../Api/getListing'; +import { getListingImageUrl } from '../../../../../Api/getListingImageUrl'; export function useRegistry() { @@ -11,18 +13,18 @@ export function useRegistry() { }); const navigate = useNavigate(); - const app = useContext(AppContext); + const profile = useContext(ProfileContext); const updateState = (value) => { setState((s) => ({ ...s, ...value })); } useEffect(() => { - if (app?.state?.Data?.profile) { - let profile = app.state.Data.profile; - updateState({ server: profile.node }); + if (profile?.state?.profile) { + let identity = profile.state.profile; + updateState({ server: identity.node }); } - }, [app]); + }, [profile]); const actions = { setServer: (server) => { @@ -32,7 +34,7 @@ export function useRegistry() { if (!state.busy && state.server != '') { updateState({ busy: true }); try { - let profiles = await app.actions.getRegistry(state.server) + let profiles = await getListing(state.server) updateState({ profiles: profiles }); } catch (err) { @@ -42,10 +44,7 @@ export function useRegistry() { } }, getRegistryImageUrl: (guid, revision) => { - if (app?.actions?.getRegistryImageUrl) { - return app.actions.getRegistryImageUrl(state.server, guid, revision); - } - return null; + return getListingImageUrl(state.server, guid, revision); }, select: (contact) => { navigate(`/user/contact/${contact.guid}`, { state: contact });