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 { 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 { getChannels } from '../Api/getChannels';
|
||||||
import { getChannel } from '../Api/getChannel';
|
import { getChannel } from '../Api/getChannel';
|
||||||
import { getContactChannels } from '../Api/getContactChannels';
|
import { getContactChannels } from '../Api/getContactChannels';
|
||||||
@ -12,19 +12,6 @@ import { GroupContext } from './GroupContext';
|
|||||||
import { CardContext } from './CardContext';
|
import { CardContext } from './CardContext';
|
||||||
import { ChannelContext } from './ChannelContext';
|
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) {
|
async function updateChannels(token, revision, channelMap, mergeChannels) {
|
||||||
let channels = await getChannels(token, revision);
|
let channels = await getChannels(token, revision);
|
||||||
for (let channel of channels) {
|
for (let channel of channels) {
|
||||||
@ -190,15 +177,11 @@ export function useAppContext() {
|
|||||||
const [state, setState] = useState(null);
|
const [state, setState] = useState(null);
|
||||||
const [appRevision, setAppRevision] = useState();
|
const [appRevision, setAppRevision] = useState();
|
||||||
|
|
||||||
const groupRevision = useRef(null);
|
|
||||||
const accountRevision = useRef(null);
|
|
||||||
const profileRevision = useRef(null);
|
|
||||||
const cardRevision = useRef(null);
|
const cardRevision = useRef(null);
|
||||||
const channelRevision = useRef(null);
|
const channelRevision = useRef(null);
|
||||||
|
|
||||||
const channels = useRef(new Map());
|
const channels = useRef(new Map());
|
||||||
const cards = useRef(new Map());
|
const cards = useRef(new Map());
|
||||||
const groups = useRef(new Map());
|
|
||||||
const delay = useRef(2);
|
const delay = useRef(2);
|
||||||
|
|
||||||
const ws = useRef(null);
|
const ws = useRef(null);
|
||||||
@ -257,13 +240,9 @@ export function useAppContext() {
|
|||||||
|
|
||||||
const resetData = () => {
|
const resetData = () => {
|
||||||
revision.current = null;
|
revision.current = null;
|
||||||
accountRevision.current = null;
|
|
||||||
profileRevision.current = null;
|
|
||||||
groupRevision.current = null;
|
|
||||||
cardRevision.current = null;
|
cardRevision.current = null;
|
||||||
channels.current = new Map();
|
channels.current = new Map();
|
||||||
cards.current = new Map();
|
cards.current = new Map();
|
||||||
groups.current = new Map();
|
|
||||||
setState({});
|
setState({});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -314,12 +293,6 @@ export function useAppContext() {
|
|||||||
while(revision.current != null) {
|
while(revision.current != null) {
|
||||||
let rev = revision.current;
|
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
|
// update card if revision changed
|
||||||
if (rev.card != cardRevision.current) {
|
if (rev.card != cardRevision.current) {
|
||||||
await updateCards(token, cardRevision.current, cards.current, updateData, mergeChannels);
|
await updateCards(token, cardRevision.current, cards.current, updateData, mergeChannels);
|
||||||
|
@ -1,10 +1,14 @@
|
|||||||
import { useEffect, useState, useRef } from 'react';
|
import { useEffect, useState, useRef } from 'react';
|
||||||
|
import { getGroups } from '../Api/getGroups';
|
||||||
|
|
||||||
export function useGroupContext() {
|
export function useGroupContext() {
|
||||||
const [state, setState] = useState({
|
const [state, setState] = useState({
|
||||||
token: null,
|
token: null,
|
||||||
revision: 0,
|
revision: null,
|
||||||
|
groups: new Map(),
|
||||||
});
|
});
|
||||||
|
const next = useRef(null);
|
||||||
|
const groups = useRef(new Map());
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
}, []);
|
}, []);
|
||||||
@ -13,16 +17,38 @@ export function useGroupContext() {
|
|||||||
setState((s) => ({ ...s, ...value }))
|
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 = {
|
const actions = {
|
||||||
setToken: async (token) => {
|
setToken: async (token) => {
|
||||||
updateState({ token });
|
updateState({ token });
|
||||||
},
|
},
|
||||||
setRevision: async (revision) => {
|
setRevision: async (revision) => {
|
||||||
updateState({ revision });
|
setGroups(revision);
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return { state, actions }
|
return { state, actions }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user