From 204f7820d481b058ecc0f856e0798bc699bc3f8c Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Tue, 7 Feb 2023 14:59:09 -0800 Subject: [PATCH] testing refactored card context in mobile app --- app/mobile/src/context/useCardContext.hook.js | 34 ++++++++++--------- .../src/context/useChannelContext.hook.js | 1 + app/mobile/test/useTestStoreContext.hook.js | 13 ++++++- 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/app/mobile/src/context/useCardContext.hook.js b/app/mobile/src/context/useCardContext.hook.js index ab1b03b8..88c224f2 100644 --- a/app/mobile/src/context/useCardContext.hook.js +++ b/app/mobile/src/context/useCardContext.hook.js @@ -78,8 +78,8 @@ export function useCardContext() { revision: cardChannel.revision, detailRevision: cardChannel.data.detailRevision, topicRevision: cardChannel.data.topicRevision, - detail = cardChannel.data.detail, - summary = cardChannel.data.summary, + detail: cardChannel.data.detail, + summary: cardChannel.data.summary, }; }; @@ -110,7 +110,7 @@ export function useCardContext() { syncing.current = true; try { - const { server, token, guid } = session.current; + const { server, token, guid } = access.current; const entry = cards.current.get(cardId); if (entry?.card?.detail === 'connected') { const card = await getCard(server, token, cardId); @@ -137,7 +137,7 @@ export function useCardContext() { force.current = false; try { - const { server, token, guid } = session.current; + const { server, token, guid } = access.current; const revision = curRevision.current; const delta = await getCards(server, token, setRevision.current); for (let card of delta) { @@ -153,7 +153,7 @@ export function useCardContext() { if (item.detailRevision !== detailRevision) { entry.card.detailRevision = item.detailRevision; entry.card.detail = await getCardDetail(server, token, card.id); - await store.actions.setCardItemDetail(guid, card.id, item.detailRevision, item.detail); + await store.actions.setCardItemDetail(guid, card.id, entry.card.detailRevision, entry.card.detail); } if (entry.card.detail?.state === 'connected' && !entry.card.offsync) { try { @@ -170,9 +170,13 @@ export function useCardContext() { } else { const entry = cards.current.get(card.id) || { card: {}, channels: new Map() }; + const ids = []; entry.channels.forEach((value, key) => { - await store.actions.clearCardChannelTopicItems(guid, card.id, key); + ids.push(key); }); + for (let i = 0; i < ids.length; i++) { + await store.actions.clearCardChannelTopicItems(guid, card.id, ids[i]); + } await store.actions.clearCardChannelItems(guid, card.id); await store.actions.clearCardItem(guid, card.id); cards.current.delete(card.id); @@ -221,12 +225,12 @@ export function useCardContext() { if (item.detailRevision !== detailRevision) { channelEntry.detail = await getContactChannelDetail(cardServer, cardToken, channel.id); channelEntry.detailRevision = detailRevision; - await store.actions.setCardChannelItemDetail(guid, cardId, channel.id, detailRevision, channelEntry.detail); + await store.actions.setCardChannelItemDetail(guid, cardId, channel.id, channelEntry.detailRevision, channelEntry.detail); } if (item.topicRevision !== topicRevision) { channelEntry.summary = await getContactChannelSummary(cardServer, cardToken, channel.id); channelEntry.topicRevision = topicRevision; - await store.actions.setCardChannelItemSummary(guid, cardId, channel.id, topicRevision, channelEntry.summary); + await store.actions.setCardChannelItemSummary(guid, cardId, channel.id, channelEntry.topicRevision, channelEntry.summary); } entry.card.notifiedChannel = cardRevision.channel; await store.actions.setCardItemNotifiedChannel(guid, cardId, channelRevision.channel); @@ -244,8 +248,7 @@ export function useCardContext() { }; const actions = { - - setSession: (session) => { + setSession: async (session) => { if (access.current || syncing.current) { throw new Error('invalid card state'); } @@ -255,19 +258,18 @@ export function useCardContext() { for(card of cardItems) { cards.current.set(card.cardId, { card, channels: new Map() }); } - const cardChannelItems = await store.actions.getCardChannelItems(guid); + const cardChannelItems = await store.actions.getCardChannelItems(session.guid); for (cardChannel of cardChannelItems) { const card = cards.current.get(cardChannel.cardId); if (card) { card.channels.set(card.channelId, cardChannel); } } - const status = await store.actions.getCardRequestStatus(guid); - + const status = await store.actions.getCardRequestStatus(session.guid); const revision = await store.actions.getCardRevision(session.guid); curRevision.current = revision; setRevision.current = revision; - setState({ offsync: false, viewRevision: status?.revision, channels: channels.current }); + setState({ offsync: false, viewRevision: status?.revision, cards: cards.current }); }, clearSession: () => { access.current = null; @@ -321,7 +323,7 @@ export function useCardContext() { const cardToken = `${profile?.guid}.${detail?.token}`; return await removeContactChannel(profile?.node, cardToken, channelId); }, - addTopic: async (cardId, channelId, type, type, message, files) => { + addTopic: async (cardId, channelId, type, message, files) => { const { detail, profile } = cards.current.get(cardId) || {}; const cardToken = `${profile?.guid}.${detail?.token}`; const node = profile?.node; @@ -459,7 +461,7 @@ export function useCardContext() { await store.actions.setCardChannelItemUnsealedDetail(guid, cardId, channelId, revision, unsealed); }, setUnsealedChannelSummary: async (cardId, channelId, revision, unsealed) => { - const { guid } = session.current; + const { guid } = access.current; await store.actions.setCardChannelItemUnsealedSummary(guid, cardId, channelId, revision, unsealed); }, setUnsealedTopicSubject: async (cardId, channelId, topicId, revision, unsealed) => { diff --git a/app/mobile/src/context/useChannelContext.hook.js b/app/mobile/src/context/useChannelContext.hook.js index 468cd217..3b370013 100644 --- a/app/mobile/src/context/useChannelContext.hook.js +++ b/app/mobile/src/context/useChannelContext.hook.js @@ -132,6 +132,7 @@ export function useChannelContext() { access.current = session; channels.current = new Map(); const items = await store.actions.getChannelItems(session.guid); + for(item of items) { channels.current.set(item.channelId, item); } diff --git a/app/mobile/test/useTestStoreContext.hook.js b/app/mobile/test/useTestStoreContext.hook.js index 95dedb45..6f8f4fb1 100644 --- a/app/mobile/test/useTestStoreContext.hook.js +++ b/app/mobile/test/useTestStoreContext.hook.js @@ -11,6 +11,10 @@ export function useTestStoreContext() { const channelRevision = useRef(0); const channels = useRef(new Map()); + const cardRevision = useRef(0); + const cards = useRef(new Map()); + const cardChannels = useRef(new Map()); + const initSession = async (guid) => { } @@ -59,10 +63,13 @@ export function useTestStoreContext() { }, getCardRevision: async (guid) => { + return cardRevision.current; }, setCardRevision: async (guid, revision) => { + cardRevision.current = revision; }, setCardItem: async (guid, card) => { + cards.current.set(card?.cardId, card) }, clearCardItem: async (guid, cardId) => { }, @@ -91,8 +98,10 @@ export function useTestStoreContext() { getCardItemStatus: async (guid, cardId) => { }, getCardItemView: async (guid, cardId) => { + return cards.current.get(cardId); }, getCardItems: async (guid) => { + return Array.from(cards.current.values()); }, getChannelRevision: async (guid) => { @@ -131,7 +140,7 @@ export function useTestStoreContext() { return channels.current.get(channelId); }, getChannelItems: async (guid) => { - return Array.from(channels.current.values()); + return Array.from(channels.current.values()) }, getChannelTopicItems: async (guid, channelId) => { @@ -150,6 +159,7 @@ export function useTestStoreContext() { }, setCardChannelItem: async (guid, cardId, channel) => { + cardChannelItems.current.set(`${cardId}:${channel.channelId}`, channel); }, clearCardChannelItem: async (guid, cardId, channelId) => { }, @@ -172,6 +182,7 @@ export function useTestStoreContext() { getCardChannelItemView: async (guid, cardId, channelId) => { }, getCardChannelItems: async (guid) => { + return Array.from(cardChannels.current.values()); }, clearCardChannelItems: async (guid, cardId) => { },