From 212a1575d4a91c2e7ae685215b612276c76e7bfa Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Sat, 23 Apr 2022 22:47:00 -0700 Subject: [PATCH] moving group from app context --- net/web/src/Api/getGroups.js | 13 ++++++++ net/web/src/AppContext/useAppContext.hook.js | 29 +---------------- .../src/AppContext/useGroupContext.hook.js | 32 +++++++++++++++++-- 3 files changed, 43 insertions(+), 31 deletions(-) create mode 100644 net/web/src/Api/getGroups.js diff --git a/net/web/src/Api/getGroups.js b/net/web/src/Api/getGroups.js new file mode 100644 index 00000000..a6a64a96 --- /dev/null +++ b/net/web/src/Api/getGroups.js @@ -0,0 +1,13 @@ +import { checkResponse, fetchWithTimeout } from './fetchUtil'; + +export async function getGroups(token, revision) { + let param = "agent=" + token + if (revision != null) { + param += '&revision=' + revision + } + let groups = await fetchWithTimeout(`/alias/groups?${param}`, { method: 'GET' }); + checkResponse(groups) + return await groups.json() +} + + diff --git a/net/web/src/AppContext/useAppContext.hook.js b/net/web/src/AppContext/useAppContext.hook.js index 1d03432f..651fa91a 100644 --- a/net/web/src/AppContext/useAppContext.hook.js +++ b/net/web/src/AppContext/useAppContext.hook.js @@ -1,5 +1,5 @@ import { useEffect, useState, useRef, useContext } from 'react'; -import { getContactProfile, setCardProfile, getCards, getCardImageUrl, getCardProfile, getCardDetail, getListingImageUrl, getListing, getGroups, getAvailable, getUsername, setLogin, createAccount } from './fetchUtil'; +import { getContactProfile, setCardProfile, getCards, getCardImageUrl, getCardProfile, getCardDetail, getListingImageUrl, getListing, getAvailable, getUsername, setLogin, createAccount } from './fetchUtil'; import { getChannels } from '../Api/getChannels'; import { getChannel } from '../Api/getChannel'; import { getContactChannels } from '../Api/getContactChannels'; @@ -12,19 +12,6 @@ import { GroupContext } from './GroupContext'; import { CardContext } from './CardContext'; import { ChannelContext } from './ChannelContext'; -async function updateGroups(token, revision, groupMap, updateData) { - let groups = await getGroups(token, revision); - for (let group of groups) { - if (group.data) { - groupMap.set(group.id, group); - } - else { - groupMap.delete(group.id); - } - } - updateData({ groups: Array.from(groupMap.values()) }); -} - async function updateChannels(token, revision, channelMap, mergeChannels) { let channels = await getChannels(token, revision); for (let channel of channels) { @@ -190,15 +177,11 @@ export function useAppContext() { const [state, setState] = useState(null); const [appRevision, setAppRevision] = useState(); - const groupRevision = useRef(null); - const accountRevision = useRef(null); - const profileRevision = useRef(null); const cardRevision = useRef(null); const channelRevision = useRef(null); const channels = useRef(new Map()); const cards = useRef(new Map()); - const groups = useRef(new Map()); const delay = useRef(2); const ws = useRef(null); @@ -257,13 +240,9 @@ export function useAppContext() { const resetData = () => { revision.current = null; - accountRevision.current = null; - profileRevision.current = null; - groupRevision.current = null; cardRevision.current = null; channels.current = new Map(); cards.current = new Map(); - groups.current = new Map(); setState({}); } @@ -314,12 +293,6 @@ export function useAppContext() { while(revision.current != null) { let rev = revision.current; - // update group if revision changed - if (rev.group != groupRevision.current) { - await updateGroups(token, groupRevision.current, groups.current, updateData); - groupRevision.current = rev.group - } - // update card if revision changed if (rev.card != cardRevision.current) { await updateCards(token, cardRevision.current, cards.current, updateData, mergeChannels); diff --git a/net/web/src/AppContext/useGroupContext.hook.js b/net/web/src/AppContext/useGroupContext.hook.js index 9121d044..91ed93e9 100644 --- a/net/web/src/AppContext/useGroupContext.hook.js +++ b/net/web/src/AppContext/useGroupContext.hook.js @@ -1,10 +1,14 @@ import { useEffect, useState, useRef } from 'react'; +import { getGroups } from '../Api/getGroups'; export function useGroupContext() { const [state, setState] = useState({ token: null, - revision: 0, + revision: null, + groups: new Map(), }); + const next = useRef(null); + const groups = useRef(new Map()); useEffect(() => { }, []); @@ -13,16 +17,38 @@ export function useGroupContext() { setState((s) => ({ ...s, ...value })) } + const setGroups = async (revision) => { + if (next.current == null) { + let delta = await getGroups(state.token, state.revision); + for (let group of delta) { + if (group.data) { + groups.set(group.id, group); + } + else { + groups.delete(group.id); + } + } + updateState({ revision, groups }); + if (next.current != null) { + let rev = next.current; + next.current = null; + setGroups(rev); + } + } + else { + next.current = revision; + } + } + const actions = { setToken: async (token) => { updateState({ token }); }, setRevision: async (revision) => { - updateState({ revision }); + setGroups(revision); }, } return { state, actions } } -