mirror of
https://github.com/balzack/databag.git
synced 2025-02-12 03:29:16 +00:00
moving group from app context
This commit is contained in:
parent
79373f26a6
commit
212a1575d4
13
net/web/src/Api/getGroups.js
Normal file
13
net/web/src/Api/getGroups.js
Normal file
@ -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()
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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 }
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user